(转) 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
- 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:
- * {<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 vector 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 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<len; ++i) {
- point = points[i].replace(this.regExes.trimParens, '$1');
- 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 linestring 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(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 multilinestring 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, '$1');
- 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 feature
- * @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, '$1');
- linestring = this.parse.linestring.apply(this, [ring]);
- components.push(linestring);
- }
- return components;
- }
- }
- }
mapTran.js
- /**
- *wkt转化成arcgis的Point对象
- * @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.geometry.Point(json);
- return point;
- }
- /**
- *wkt转化成arcgis的Polyline对象
- * @param wkt
- * @returns {Polyline}
- * @constructor
- */
- function WktToPolyline(wkt, spatialreference){
- var wktUtil = new WKTUtil();
- var points = wktUtil.read(wkt);
- var json = {
- paths: [points],
- spatialReference: spatialreference
- }
- var polyline = new esri.geometry.Polyline(json);
- return polyline;
- }
- /**
- * wkt转化成arcgis的Polygon对象
- * @param wkt
- * @returns {Polygon}
- * @constructor
- */
- function WktToPolygon(wkt, spatialreference){
- 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 ring = 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(",")+")";
- }
使用的时候,直接调用对应的函数即可。
(转) Arcgis for js之WKT和GEOMETRY的相互转换的更多相关文章
- (转) 基于Arcgis for Js的web GIS数据在线采集简介
http://blog.csdn.net/gisshixisheng/article/details/44310765 在前一篇博文“Arcgis for js之WKT和geometry转换”中实现了 ...
- (转)Arcgis for JS之对象捕捉
http://blog.csdn.net/gisshixisheng/article/details/44098615 在web操作,如绘制或者测量的时候,为了精确,需要捕捉到某一图层的对象,在此,讲 ...
- Arcgis for JS之Cluster聚类分析的实现(基于区域范围的)
原文:Arcgis for JS之Cluster聚类分析的实现(基于区域范围的) 咱们书接上文,在上文,实现了基于距离的空间聚类的算法实现,在本文,将继续介绍空间聚类之基于区域范围的实现方式,好了,闲 ...
- Arcgis for JS之Cluster聚类分析的实现
原文:Arcgis for JS之Cluster聚类分析的实现 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来 的,包含XY坐标信息的,通过graphic和graphicla ...
- Arcgis for js载入天地图
综述:本节讲述的是用Arcgis for js载入天地图的切片资源. 天地图的切片地图能够通过esri.layers.TiledMapServiceLayer来载入.在此将之进行了一定的封装,例如以下 ...
- arcgis for js开发之路径分析
arcgis for js开发之路径分析 //方法封装 function routeplan(x1, x2, y1, y2, barrierPathArray, isDraw, callback) { ...
- Arcgis for js开发之直线、圆、箭头、多边形、集结地等绘制方法
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- arcgis for js学习之Draw类
arcgis for js学习之Draw类 <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- arcgis for js学习之Graphic类
arcgis for js学习之Graphic类 <title>Graphic类</title> <meta charset="utf-8" /> ...
随机推荐
- RMAN RECOVERY
Data Recovery Advisor The health monitor and the ADR The capabilities and limitations of DRA using t ...
- 1.求整数最大的连续0的个数 BinaryGap Find longest sequence of zeros in binary representation of an integer.
求整数最大的连续0的个数 A binary gap within a positive integer N is any maximal sequence of consecutive zeros t ...
- Java - this的使用方法
this在内部获得当前对象的引用时调用: (1) return返回当前对象; (2) 构造器调用还有一个构造器, 带參数; (3) 參数的名称和数据成员的名称同样; 注意: this构造器在方法中仅仅 ...
- VC 获取任务栏窗体的句柄
本文将介绍一个未公开的Win32 API函数:GetTaskmanWindow.利用它对Windows的任务栏进行操作. 这个函数返回拥有任务栏button的窗体句柄. 在微软的MSDN文档中. ...
- Cache-control no-transform Robots Exclusion Protocol
蜘蛛协议 <今日头条>支持蜘蛛协议(Robots Exclusion Protocol)"ToutiaoSpider",同时,我们尊重所有的网络媒体,如媒体不希望内容被 ...
- 又发现2个高人写的Delphi图文并茂的消息研究
http://ymg97526.blog.163.com/blog/static/173658160201139101120862/http://ymg97526.blog.163.com/blog/ ...
- bzoj 5090 组题
题目大意: 一个数列,求一段长度不少于k的数 使平均值最大 思路: 把所有数列里的数,转换为(i,sum i)的点 然后求一个下凸包,在这个过程中对于长度特殊处理一下,使栈内至少有一段长度大于等于k ...
- cas4.2的安装
cas4.2使用的是gradle来构建项目的,项目代码在https://github.com/Jasig/cas下载. 然后之后进入项目的根目录,然后执行gradle来编译项目,如下: gradle ...
- [POI 2018] Prawnicy
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5102 [算法] 首先,n条线段的交集一定是[Lmax,Rmin] , 其中,Lmax ...
- 洛谷P2059 [JLOI2013]卡牌游戏
题目描述 N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先把 ...