From 45da80f8adef5a7c8c5793f839921cda937978cc Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 31 Mar 2007 16:21:14 +0000 Subject: [PATCH] read/write for Well-Known Text representation of vector geometries - support for simple features: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, and GeometryCollection - examples/wkt.html for a demonstration git-svn-id: http://svn.openlayers.org/trunk/openlayers@2942 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/wkt.html | 146 ++++++++++++++++++ lib/OpenLayers.js | 1 + lib/OpenLayers/Format/WKT.js | 286 +++++++++++++++++++++++++++++++++++ tests/Format/test_WKT.html | 174 +++++++++++++++++++++ tests/list-tests.html | 2 + 5 files changed, 609 insertions(+) create mode 100644 examples/wkt.html create mode 100644 lib/OpenLayers/Format/WKT.js create mode 100644 tests/Format/test_WKT.html diff --git a/examples/wkt.html b/examples/wkt.html new file mode 100644 index 0000000000..8c569c5bbb --- /dev/null +++ b/examples/wkt.html @@ -0,0 +1,146 @@ + + + WKT + + + + + +

OpenLayers WKT Example

+
+
+
+

See Wikipedia + for a description and examples of WKT.

+
+ +
+ +
+ +
+ + diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index dbdf3ccf75..89cc92f102 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -143,6 +143,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") { "OpenLayers/Format/KML.js", "OpenLayers/Format/GeoRSS.js", "OpenLayers/Format/WFS.js", + "OpenLayers/Format/WKT.js", "OpenLayers/Layer/WFS.js", "OpenLayers/Control/MouseToolbar.js", "OpenLayers/Control/NavToolbar.js", diff --git a/lib/OpenLayers/Format/WKT.js b/lib/OpenLayers/Format/WKT.js new file mode 100644 index 0000000000..14ea6bf593 --- /dev/null +++ b/lib/OpenLayers/Format/WKT.js @@ -0,0 +1,286 @@ +/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. + * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt + * for the full text of the license. */ + +/** + * Read and write WKT. + * @requires OpenLayers/Format/JSON.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(); + 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 + + + + + + + \ No newline at end of file diff --git a/tests/list-tests.html b/tests/list-tests.html index 72dd1221d8..3f8bb3fc58 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -16,6 +16,8 @@
  • Geometry/test_MultiPolygon.html
  • Geometry/test_Rectangle.html
  • Geometry/test_Surface.html
  • +
  • test_Format.html
  • +
  • Format/test_WKT.html
  • test_Icon.html
  • test_Marker.html
  • test_Popup.html