From 44a6b9b176363bec33c8ef243b2f332951d0266c Mon Sep 17 00:00:00 2001 From: crschmidt Date: Sat, 15 Dec 2007 21:45:51 +0000 Subject: [PATCH] GeoJSON should parse and serialize GeometryCollections as a Geometry.Collection object. (Closes #1067) git-svn-id: http://svn.openlayers.org/trunk/openlayers@5435 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/geojson.html | 66 ++++++++++++ lib/OpenLayers/Format/GeoJSON.js | 169 ++++++++++++++++--------------- tests/Format/test_GeoJSON.html | 86 +++++++++++++--- 3 files changed, 227 insertions(+), 94 deletions(-) create mode 100644 examples/geojson.html diff --git a/examples/geojson.html b/examples/geojson.html new file mode 100644 index 0000000000..36458b1cbd --- /dev/null +++ b/examples/geojson.html @@ -0,0 +1,66 @@ + + + + + + + +
+ + diff --git a/lib/OpenLayers/Format/GeoJSON.js b/lib/OpenLayers/Format/GeoJSON.js index a894c9d4c2..90d5ce347c 100644 --- a/lib/OpenLayers/Format/GeoJSON.js +++ b/lib/OpenLayers/Format/GeoJSON.js @@ -40,9 +40,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { * Parameters: * json - {String} A GeoJSON string * type - {String} Optional string that determines the structure of - * the output. Supported values are "Geometry", "Feature", - * "GeometryCollection", and "FeatureCollection". If absent or null, - * a default of "FeatureCollection" is assumed. + * the output. Supported values are "Geometry", "Feature", and + * "FeatureCollection". If absent or null, a default of + * "FeatureCollection" is assumed. * filter - {Function} A function which will be called for every key and * value at every level of the final result. Each value will be * replaced by the result of the filter function. This can be used to @@ -56,9 +56,7 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { * must represent a single geometry, and the return will be an * . If type is "Feature", the input json must * represent a single feature, and the return will be an - * . If type is "GeometryCollection", the - * input json must represent a geometry collection, and the return will - * be an array of . + * . */ read: function(json, type, filter) { type = (type) ? type : "FeatureCollection"; @@ -91,17 +89,6 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { OpenLayers.Console.error(err); } break; - case "GeometryCollection": - results = []; - for(var i=0; i, , - * or an array of either features or geometries. + * or an array of features. * pretty - {Boolean} Structure the output with newlines and indentation. * Default is false. * * Returns: * {String} The GeoJSON string representation of the input geometry, - * features, array of geometries, or array of features. + * features, or array of features. */ write: function(obj, pretty) { var geojson = { "type": null }; if(obj instanceof Array) { - if(obj[0] instanceof OpenLayers.Feature.Vector) { - geojson.features = []; - } else if (obj[0].CLASS_NAME.search("OpenLayers.Geometry") == 0) { - geojson.geometries = []; - } - for(var i=0; i} + * + * Returns: + * {Array} An array of geometry objects representing the geometry + * collection. + */ + 'collection': function(collection) { + var len = collection.components.length; + var array = new Array(len); + for(var i=0; i