/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for * full list of contributors). Published under the 2-clause BSD license. * See license.txt in the OpenLayers distribution or repository for the * full text of the license. */ /** * @requires OpenLayers/Format.js * @requires OpenLayers/Feature/Vector.js * @requires OpenLayers/Geometry/Point.js * @requires OpenLayers/Geometry/MultiPoint.js * @requires OpenLayers/Geometry/LineString.js * @requires OpenLayers/Geometry/MultiLineString.js * @requires OpenLayers/Geometry/Polygon.js * @requires OpenLayers/Geometry/MultiPolygon.js */ /** * Class: OpenLayers.Format.WKT * Class for reading and writing Well-Known Text. Create a new instance * with the constructor. * * Inherits from: * - */ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, { /** * 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*$/ }; OpenLayers.Format.prototype.initialize.apply(this, [options]); }, /** * 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]); } if (this.internalProjection && this.externalProjection) { if (features && features.CLASS_NAME == "OpenLayers.Feature.Vector") { features.geometry.transform(this.externalProjection, this.internalProjection); } else if (features && type != "geometrycollection" && typeof features == "object") { for (var i=0, len=features.length; i|Array} A feature or array of * features * * Returns: * {String} The WKT string representation of the input geometries */ write: function(features) { var collection, geometry, isCollection; if (features.constructor == Array) { collection = features; isCollection = true; } else { collection = [features]; isCollection = false; } var pieces = []; if (isCollection) { pieces.push('GEOMETRYCOLLECTION('); } for (var i=0, len=collection.length; i0) { pieces.push(','); } geometry = collection[i].geometry; pieces.push(this.extractGeometry(geometry)); } if (isCollection) { pieces.push(')'); } return pieces.join(''); }, /** * 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; }, /** * Object with properties corresponding to the geometry types. * Property values are functions that do the actual data extraction. */ extract: { /** * Return a space delimited string of point coordinates. * @param {OpenLayers.Geometry.Point} point * @returns {String} A string of coordinates representing the point */ 'point': function(point) { return point.x + ' ' + point.y; }, /** * Return a comma delimited string of point coordinates from a multipoint. * @param {OpenLayers.Geometry.MultiPoint} multipoint * @returns {String} A string of point coordinate strings representing * the multipoint */ 'multipoint': function(multipoint) { var array = []; for(var i=0, len=multipoint.components.length; i * @param {OpenLayers.Geometry.Collection} collection * @returns {String} internal WKT representation of the collection */ 'collection': function(collection) { var array = []; for(var i=0, len=collection.components.length; i