1, Wkt Introduction
WKT (well-known text) is a text markup language used to represent transformations between vector geometric objects, spatial reference systems, and spatial reference systems. Its binary representation, WKB (well-known-binary), is better than the same information stored in the transmission and in the database. The format is developed by the Open Geospatial Consortium (OGC). The geometric objects that wkt can represent are: points, lines, polygons, tins (triangulated irregular networks), and multi-faceted bodies. The following is a sample of the geometry wkt string:
Point (6 10)
LINESTRING (3 4,10 50,20 25)
POLYGON ((1 1,5 1,5 5,1 5,1 1), (2 2,2 3,3 3,3 2,2 2))
MULTIPOINT (3.5 5.6, 4.8 10.5)
Multilinestring (3 4,10 50,20 25), ( -5-8,-10-8,-15-4))
Multipolygon (((1 1,5 1,5 5,1 5,1 1), (2 2,2 3,3 3,3 2,2 2)), ((6 3,9 2,9 4,6 3))
GeometryCollection (Point (4 6), LINESTRING (4 6,7 10))
Point ZM (1 1 5 60)
Point M (1 1 80)
Point EMPTY
Multipolygon EMPTY
2, Geometry
Geometry is a geometric object in ArcGIS, including extent, Multipoint, point, Polygon, Polyline.
3, Mutual conversion
To achieve mutual conversion, encapsulated into two JS files, the contents are as follows:
Wktutil.js
var wktutil = function (options) {this.initialize (options);} Wktutil.prototype = {/** * Constructor:OpenLayers.Format.WKT * Create A new parser for WKT * * Parame Ters: * Options-{object} an optional Object whose properties'll be set in * this instance * * Returns: * {<openlayers.format.wkt>} A new WKT parser. */initialize:function (options) {this.regexes = {' Typestr ':/^\s* (\w+) \s*\ (\s* (. *) \s*\) \s*$/, ' Spaces ':/\s+/, ' Parencomma ':/\) \s*,\s*\ (/, ' Doubleparencomma ':/\) \s*\) \s*,\s*\ (\s*\ (/,/ /Can ' t use {2} here ' Trimparens ':/^\s*\ (?. *?) \)? \s*$/}; for (var i in options) {this[i] = options[i]; }},/** * Apimethod:read * Deserialize a WKT string and return a vector feature or an * array of vecto R features. Supports WKT for Point, MULTIPOINT, * LINESTRING, multilinestring, POLYGON, Multipolygon, and *GeometryCollection. * * Parameters: * wkt-{string} A wkt String * * Returns: * {<openlayers.feature.vector>| Array} A feature or array of features for * geometrycollection WKT. */Read:function (wkt) {var features, type, str; wkt = Wkt.replace (/[\n\r]/g, ""); var matches = this.regExes.typeStr.exec (wkt); if (matches) {type = Matches[1].tolowercase (); str = matches[2]; if (This.parse[type]) {features = This.parse[type].apply (this, [str]); Console.log (features); }} return features; },/** * method:extractgeometry * Entry point to construct the WKT for a single Geometry object. * * Parameters: * geometry-{<openlayers.geometry.geometry>} * * Returns: * {String} A WKT Stri Ng of representing the geometry */extractgeometry:function (geometry) { var type = geometry. Class_name.split ('. ') [2].tolowercase (); if (!this.extract[type]) {return null; } if (This.internalprojection && this.externalprojection) {geometry = Geometry.clone (); Geometry.transform (This.internalprojection, this.externalprojection); } var wkttype = type = = ' collection '? ' GeometryCollection ': type.touppercase (); var data = Wkttype + ' (' + this.extract[type].apply (this, [geometry]) + ') '; return data; }, Trim:function (str) {return str.replace (/^\s\s*/, "). Replace (/\s\s*$/,"); },/** * Object with properties corresponding to the geometry types. * Property values is functions that does the actual parsing. */Parse: {/** * Return point feature given a point WKT fragment. * @param {String} str A WKT fragment representing the point * @returns {OpenLayers.Feature.Vector} A point Feature * @private */' point ': function (str) {var coords = This.trim (str). Split (THIS.REGEXES.S paces); Return coords;//new Esri.geometry.Point (Coords[0], coords[1]); },/** * Return a multipoint feature given a multipoint WKT fragment. * @param {String} str a WKT fragment representing the multipoint * @returns {OpenLayers.Feature.Vector} A Multipoi NT Feature * @private */' multipoint ': function (str) {var point; var points = This.trim (str). Split (', '); var components = []; For (var i=0, len=points.length; i<len; ++i) {point = Points[i].replace (This.regExes.trimParens, ' $ '); Components.push (This.parse.point.apply (this, [Point]). geometry); } return new OpenLayers.Feature.Vector (new OpenLayers.Geometry.MultiPoint (components) ); },/** * REturn a linestring feature given a linestring WKT fragment. * @param {String} str a WKT fragment representing the linestring * @returns {OpenLayers.Feature.Vector} A Linestri NG feature * @private * * * ' linestring ': function (str) {var points = This.trim (str). Split (','); var components = []; For (var i=0, len=points.length; i<len; ++i) {Components.push (the this.parse.point.apply (this, [Points[i]])) ; } return components//new Esri.geometry.Polyline (components); },/** * Return a multilinestring feature given a multilinestring WKT fragment. * @param {String} str a WKT fragment representing the multilinestring * @returns {OpenLayers.Feature.Vector} A mul Tilinestring feature * @private */' multilinestring ': function (str) {var line; var lines = OpenLayers.String.trim (str). Split (This.regExes.parEncomma); var components = []; For (var i=0, len=lines.length; i<len; ++i) {line = Lines[i].replace (This.regExes.trimParens, ' $ '); Components.push (This.parse.linestring.apply (this, [line]). geometry); } return new OpenLayers.Feature.Vector (new OpenLayers.Geometry.MultiLineString (components) ); },/** * Return A polygon feature given a polygon WKT fragment. * @param {String} str a WKT fragment representing the polygon * @returns {OpenLayers.Feature.Vector} A polygon FEA ture * @private */' polygon ': function (str) {var ring, linestring, linearring; var rings = This.trim (str). Split (This.regExes.parenComma); var components = []; For (var i=0, len=rings.length; i<len; ++i) {ring = Rings[i].replace (This.regExes.trimParens, ' $ '); LineString = This.parse.linestring.apply (this, [ring]); Components.push (linestring); } return components; } }}
Maptran.js
/** *wkt converted to ArcGIS Point object * @param wkt * @returns {Polyline} * @constructor */function wkttopoint (wkt,spatialreference) { var wktutil = new Wktutil (); var pt = wktutil.read (wkt); var json = {X:pt[0], y:pt[1], spatialreference:spatialreference} var point = new Esri.geomet Ry. Point (JSON); return point;} /** *wkt converted to ArcGIS Polyline object * @param wkt * @returns {Polyline} * @constructor */function wkttopolyline (wkt, Spatialrefere NCE) {var wktutil = new Wktutil (); var points = Wktutil.read (wkt); var json = {paths: [points], spatialreference:spatialreference} var polyline = new Esri.geometry.Po Lyline (JSON); return polyline;} /** * Wkt converted to ArcGIS Polygon object * @param wkt * @returns {Polygon} * @constructor */function Wkttopolygon (wkt, Spatialreferenc e) {var wktutil = new Wktutil (); var points = Wktutil.read (wkt); var json = {rings:points,spatialreference: {"Wkid": 4326} }var polygon = new Esri.geometry.Polygon (JSON); Return POlygon;} /** * @param geometry */function pointtowkt (geometry) {console.log (geometry); Return "point (" +geometry.x+ "" +geometry.y+ ")";} /** * @param geometry*/function polygontowkt (geometry) {var wkt = [];var rings = Geometry.rings;for (var i in rings) {var rin g = Rings[i];for (Var j in ring) {var p = Ring[j];wkt.push (P.join (""));}} Return "POLYGON ((" +wkt.join (",") + "))"; /** * @param geometry*/function linetowkt (geometry) {var wkt = [];var paths = Geometry.paths;for (var i in paths) {var path = Paths[i];for (var j in Path) {var p = Path[j];wkt.push (P.join (""));}} Return "LINESTRING (" +wkt.join (",") + ")";
When used, call the corresponding function directly.
If you have any questions, please contact:
qq:1004740957
Email:[email protected]
Arcgis for JS wkt and Geometry conversions