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

您所在的位置:网站首页 polyline的json (转) Arcgis for js之WKT和GEOMETRY的相互转换

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

2023-08-07 14:02| 来源: 网络整理| 查看: 265

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 EMPTYMULTIPOLYGON EMPTY

2、geometry

geometry为Arcgis中的几何对象,包括Extent、Multipoint、Point 、Polygon 、Polyline。

3、相互转换

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

WKTUtil.js

 

[javascript] view plain copy    print? var WKTUtil = function(options){       this.initialize(options);   }      WKTUtil.prototype = {       /**       * Constructor: OpenLayers.Format.WKT       * Create a new parser for WKT       *       * Parameters:       * options - {Object} An optional object whose properties will be set on       *           this instance       *       * Returns:       * {} 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 vector features.  Supports WKT for POINT, MULTIPOINT,       * LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, and       * GEOMETRYCOLLECTION.       *       * Parameters:       * wkt - {String} A WKT string       *       * Returns:       * {|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 - {}       *       * Returns:       * {String} A WKT string 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 are functions that do 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.spaces);               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 multipoint feature           * @private           */           'multipoint': function(str) {               var point;               var points = this.trim(str).split(',');               var components = [];               for(var i=0, len=points.length; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3