(转) Arcgis for js之WKT和GEOMETRY的相互转换

http://blog.csdn.net/gisshixisheng/article/details/44057453

1、wkt简介

WKT(Well-known text)是一种文本标记语言,用于表示矢量几何对象、空间参照系统及空间参照系统之间的转换。它的二进制表示方式,亦即WKB(well-known-binary)则胜于在传输和在数据库中存储相同的信息。该格式由开放地理空间联盟(OGC)制定。WKT可以表示的几何对象包括:点,线,多边形,TIN(不规则三角网)及多面体。以下为几何WKT字串样例:
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为Arcgis中的几何对象,包括Extent、Multipoint、Point 、Polygon 、Polyline。

3、相互转换

实现相互转换,封装成了两个js文件,内容如下:

WKTUtil.js

 

[javascript] view plain copy

 

 print?

  1. var WKTUtil = function(options){  
  2.     this.initialize(options);  
  3. }  
  4.   
  5. WKTUtil.prototype = {  
  6.     /** 
  7.      * Constructor: OpenLayers.Format.WKT 
  8.      * Create a new parser for WKT 
  9.      * 
  10.      * Parameters: 
  11.      * options – {Object} An optional object whose properties will be set on 
  12.      *           this instance 
  13.      * 
  14.      * Returns: 
  15.      * {<OpenLayers.Format.WKT>} A new WKT parser. 
  16.      */  
  17.     initialize: function(options) {  
  18.         this.regExes = {  
  19.             ‘typeStr’: /^\s*(\w+)\s*\s(.)\s\s*$/,  
  20.             ‘spaces’: /\s+/,  
  21.             ‘parenComma’: /\)\s*,\s*\(/,  
  22.             ‘doubleParenComma’: /\)\s*\)\s*,\s*\(\s*\(/,  // can’t use {2} here  
  23.             ‘trimParens’: /^\s*?(.?)?\s*$/  
  24.         };  
  25.         for(var i in options){  
  26.             this[i] = options[i];  
  27.         }  
  28.     },  
  29.   
  30.     /** 
  31.      * APIMethod: read 
  32.      * Deserialize a WKT string and return a vector feature or an 
  33.      * array of vector features.  Supports WKT for POINT, MULTIPOINT, 
  34.      * LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, and 
  35.      * GEOMETRYCOLLECTION. 
  36.      * 
  37.      * Parameters: 
  38.      * wkt – {String} A WKT string 
  39.      * 
  40.      * Returns: 
  41.      * {<OpenLayers.Feature.Vector>|Array} A feature or array of features for 
  42.      * GEOMETRYCOLLECTION WKT. 
  43.      */  
  44.     read: function(wkt) {  
  45.         var features, type, str;  
  46.         wkt = wkt.replace(/[\n\r]/g, ” “);  
  47.         var matches = this.regExes.typeStr.exec(wkt);  
  48.         if(matches) {  
  49.             type = matches[1].toLowerCase();  
  50.             str = matches[2];  
  51.             if(this.parse[type]) {  
  52.                 features = this.parse[type].apply(this, [str]);  
  53.                 //console.log(features);  
  54.             }  
  55.               
  56.               
  57.         }      
  58.         return features;  
  59.     },  
  60.   
  61.     /** 
  62.      * Method: extractGeometry 
  63.      * Entry point to construct the WKT for a single Geometry object. 
  64.      * 
  65.      * Parameters: 
  66.      * geometry – {<OpenLayers.Geometry.Geometry>} 
  67.      * 
  68.      * Returns: 
  69.      * {String} A WKT string of representing the geometry 
  70.      */  
  71.     extractGeometry: function(geometry) {  
  72.         var type = geometry.CLASS_NAME.split(‘.’)[2].toLowerCase();  
  73.         if (!this.extract[type]) {  
  74.             return null;  
  75.         }  
  76.         if (this.internalProjection && this.externalProjection) {  
  77.             geometry = geometry.clone();  
  78.             geometry.transform(this.internalProjection, this.externalProjection);  
  79.         }                         
  80.         var wktType = type == ‘collection’ ? ‘GEOMETRYCOLLECTION’ : type.toUpperCase();  
  81.         var data = wktType + ‘(‘ + this.extract[type].apply(this, [geometry]) + ‘)’;  
  82.         return data;  
  83.     },  
  84.       
  85.     trim: function(str){  
  86.         return str.replace(/^\s\s*/, ”).replace(/\s\s*$/, ”);  
  87.     },  
  88.     /** 
  89.      * Object with properties corresponding to the geometry types. 
  90.      * Property values are functions that do the actual parsing. 
  91.      */  
  92.     parse: {  
  93.         /** 
  94.          * Return point feature given a point WKT fragment. 
  95.          * @param {String} str A WKT fragment representing the point 
  96.          * @returns {OpenLayers.Feature.Vector} A point feature 
  97.          * @private 
  98.          */  
  99.         ‘point’: function(str) {  
  100.             var coords = this.trim(str).split(this.regExes.spaces);  
  101.             return coords;//new esri.geometry.Point(coords[0], coords[1]);  
  102.         },  
  103.   
  104.         /** 
  105.          * Return a multipoint feature given a multipoint WKT fragment. 
  106.          * @param {String} str A WKT fragment representing the multipoint 
  107.          * @returns {OpenLayers.Feature.Vector} A multipoint feature 
  108.          * @private 
  109.          */  
  110.         ‘multipoint’: function(str) {  
  111.             var point;  
  112.             var points = this.trim(str).split(‘,’);  
  113.             var components = [];  
  114.             for(var i=0, len=points.length; i<len; ++i) {  
  115.                 point = points[i].replace(this.regExes.trimParens, ‘$1’);  
  116.                 components.push(this.parse.point.apply(this, [point]).geometry);  
  117.             }  
  118.             return new OpenLayers.Feature.Vector(  
  119.                 new OpenLayers.Geometry.MultiPoint(components)  
  120.             );  
  121.         },  
  122.           
  123.         /** 
  124.          * Return a linestring feature given a linestring WKT fragment. 
  125.          * @param {String} str A WKT fragment representing the linestring 
  126.          * @returns {OpenLayers.Feature.Vector} A linestring feature 
  127.          * @private 
  128.          */  
  129.         ‘linestring’: function(str) {  
  130.             var points = this.trim(str).split(‘,’);  
  131.               
  132.             var components = [];  
  133.             for(var i=0, len=points.length; i<len; ++i) {  
  134.                 components.push(this.parse.point.apply(this, [points[i]]));  
  135.             }  
  136.             return components//new esri.geometry.Polyline(components);  
  137.         },  
  138.   
  139.         /** 
  140.          * Return a multilinestring feature given a multilinestring WKT fragment. 
  141.          * @param {String} str A WKT fragment representing the multilinestring 
  142.          * @returns {OpenLayers.Feature.Vector} A multilinestring feature 
  143.          * @private 
  144.          */  
  145.         ‘multilinestring’: function(str) {  
  146.             var line;  
  147.             var lines = OpenLayers.String.trim(str).split(this.regExes.parenComma);  
  148.             var components = [];  
  149.             for(var i=0, len=lines.length; i<len; ++i) {  
  150.                 line = lines[i].replace(this.regExes.trimParens, ‘$1’);  
  151.                 components.push(this.parse.linestring.apply(this, [line]).geometry);  
  152.             }  
  153.             return new OpenLayers.Feature.Vector(  
  154.                 new OpenLayers.Geometry.MultiLineString(components)  
  155.             );  
  156.         },  
  157.           
  158.         /** 
  159.          * Return a polygon feature given a polygon WKT fragment. 
  160.          * @param {String} str A WKT fragment representing the polygon 
  161.          * @returns {OpenLayers.Feature.Vector} A polygon feature 
  162.          * @private 
  163.          */  
  164.         ‘polygon’: function(str) {  
  165.             var ring, linestring, linearring;  
  166.             var rings = this.trim(str).split(this.regExes.parenComma);  
  167.               
  168.             var components = [];  
  169.             for(var i=0, len=rings.length; i<len; ++i) {  
  170.                 ring = rings[i].replace(this.regExes.trimParens, ‘$1’);  
  171.                 linestring = this.parse.linestring.apply(this, [ring]);  
  172.                 components.push(linestring);  
  173.             }  
  174.             return components;  
  175.         }  
  176.     }  
  177. }  

mapTran.js

 

 

[javascript] view plain copy

 

 print?

  1. /** 
  2.  *wkt转化成arcgis的Point对象 
  3.  * @param wkt 
  4.  * @returns {Polyline} 
  5.  * @constructor 
  6.  */  
  7. function WktToPoint(wkt,spatialreference){  
  8.     var wktUtil = new WKTUtil();  
  9.     var pt = wktUtil.read(wkt);  
  10.     var json = {  
  11.         x:pt[0],  
  12.         y:pt[1],  
  13.         spatialReference: spatialreference  
  14.     }  
  15.     var point = new esri.geometry.Point(json);  
  16.     return point;  
  17. }  
  18. /** 
  19.  *wkt转化成arcgis的Polyline对象 
  20.  * @param wkt 
  21.  * @returns {Polyline} 
  22.  * @constructor 
  23.  */  
  24. function WktToPolyline(wkt, spatialreference){  
  25.     var wktUtil = new WKTUtil();  
  26.     var points = wktUtil.read(wkt);  
  27.     var json = {  
  28.         paths: [points],  
  29.         spatialReference: spatialreference  
  30.     }  
  31.     var polyline = new esri.geometry.Polyline(json);  
  32.     return polyline;  
  33. }  
  34. /** 
  35.  * wkt转化成arcgis的Polygon对象 
  36.  * @param wkt 
  37.  * @returns {Polygon} 
  38.  * @constructor 
  39.  */  
  40. function WktToPolygon(wkt, spatialreference){  
  41.     var wktUtil = new WKTUtil();  
  42.     var points = wktUtil.read(wkt);  
  43.     var json = {  
  44.             rings: points,  
  45.             spatialReference: {“wkid”:4326}  
  46.     }  
  47.     var polygon = new esri.geometry.Polygon(json);  
  48.     return polygon;  
  49. }  
  50. /** 
  51.  * @param geometry 
  52.  */  
  53. function PointToWKT(geometry){  
  54.     console.log(geometry);  
  55.     return “POINT (“+geometry.x+” “+geometry.y+“)”;  
  56. }  
  57. /** 
  58.  * @param geometry 
  59. */  
  60. function PolygonToWKT(geometry){  
  61.     var wkt = [];  
  62.     var rings = geometry.rings;  
  63.     for(var i in rings){  
  64.         var ring = rings[i];  
  65.         for(var j in ring){  
  66.             var p = ring[j];  
  67.             wkt.push(p.join(” “));  
  68.         }  
  69.     }     
  70.     return “POLYGON ((“+wkt.join(“,”)+“))”;  
  71. }  
  72.   
  73. /**  
  74.  * @param geometry 
  75. */  
  76. function LineToWKT(geometry){  
  77.     var wkt = [];  
  78.     var paths = geometry.paths;  
  79.     for(var i in paths){  
  80.         var path = paths[i];  
  81.         for(var j in path){  
  82.             var p = path[j];  
  83.             wkt.push(p.join(” “));  
  84.         }  
  85.     }     
  86.     return “LINESTRING (“+wkt.join(“,”)+“)”;  
  87. }  

使用的时候,直接调用对应的函数即可。

转载自:https://blog.csdn.net/weixin_34056162/article/details/85901146

You may also like...