/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. * See http://svn.openlayers.org/trunk/openlayers/release-license.txt * for the full text of the license. */ /** * Read and write WKT. * @requires OpenLayers/Format.js */ OpenLayers.Format.WKT = OpenLayers.Class.create(); OpenLayers.Format.WKT.prototype = OpenLayers.Class.inherit(OpenLayers.Format, { /** * */ 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]); }, /** * Deserialize a WKT string and return an OpenLayers.Geometry or an array * of OpenLayers.Geometry. Supports WKT for POINT, MULTIPOINT, LINESTRING, * MULTILINESTRING, POLYGON, MULTIPOLYGON, and GEOMETRYCOLLECTION. * @param {String} wkt A WKT string * @returns {OpenLayers.Geometry|Array} A geometry or array of geometries * for GEOMETRYCOLLECTION WKT. */ read: function(wkt) { var geometry, type, str; var matches = this.regExes.typeStr.exec(wkt); if(matches) { type = matches[1].toLowerCase(); str = matches[2]; if(this.parse[type]) { geometry = this.parse[type].apply(this, [str]); } } return geometry; }, /** * Serialize a geometry or array of geometries into a WKT string. * @param {OpenLayers.Geometry|Array} geom A geometry or array of geometries * @returns {String} The WKT string representation of the input geometries */ write: function(geom) { var collection, geometry, type, data, isCollection; if(geom.constructor == Array) { collection = geom; isCollection = true; } else { collection = [geom]; isCollection = false; } var pieces = []; if(isCollection) { pieces.push('GEOMETRYCOLLECTION('); } for(var i=0; i0) { pieces.push(','); } geometry = collection[i]; type = geometry.CLASS_NAME.split('.')[2].toLowerCase(); if(!this.extract[type]) { return null; } data = this.extract[type].apply(this, [geometry]); pieces.push(type.toUpperCase() + '(' + data + ')'); } if(isCollection) { pieces.push(')'); } return pieces.join(''); }, /** * 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; i