From 2633f9ba88f32cb3de6d60c5d2be7dd882de2a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 5 Oct 2009 09:21:07 +0000 Subject: [PATCH] GeoJSON input should accept 3D coords, p=sgillies, r=me (closes #2070) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9700 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Format/GeoJSON.js | 12 ++++++++++-- tests/Format/GeoJSON.html | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/Format/GeoJSON.js b/lib/OpenLayers/Format/GeoJSON.js index 5a0bd39575..15ea61b1de 100644 --- a/lib/OpenLayers/Format/GeoJSON.js +++ b/lib/OpenLayers/Format/GeoJSON.js @@ -24,6 +24,13 @@ */ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { + /** + * APIProperty: ignoreExtraDims + * {Boolean} Ignore dimensions higher than 2 when reading geometry + * coordinates. + */ + ignoreExtraDims: false, + /** * Constructor: OpenLayers.Format.GeoJSON * Create a new parser for GeoJSON. @@ -269,8 +276,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { * {} A geometry. */ "point": function(array) { - if(array.length != 2) { - throw "Only 2D points are supported: " + array; + if (this.ignoreExtraDims == false && + array.length != 2) { + throw "Only 2D points are supported: " + array; } return new OpenLayers.Geometry.Point(array[0], array[1]); }, diff --git a/tests/Format/GeoJSON.html b/tests/Format/GeoJSON.html index ee76b9bf4c..db3dd7c278 100644 --- a/tests/Format/GeoJSON.html +++ b/tests/Format/GeoJSON.html @@ -409,6 +409,18 @@ t.eq(data[0].bounds.top, 74.0, "read top left bound is correct"); } + function test_Format_GeoJSON_point_extradims(t) { + t.plan(3); + var point_feature_3d = '{"geometry": {"type": "Point", "coordinates": [94.21875, 72.94921875, 0.0]}, "type": "Feature", "id": 573, "properties": {"strokeColor": "blue", "title": "Feature 5", "author": "Your Name Here"}}'; + p = new OpenLayers.Format.GeoJSON({"ignoreExtraDims": true}); + data = p.read(point_feature_3d); + t.eq(data[0].fid, 573, "Fid is correct on point feature"); + t.eq(data[0].geometry.x, 94.21875, 'Reading point feature gives correct x'); + data = p.read(point_feature_3d, "Feature"); + t.eq(data.fid, 573, 'Reading point feature with type gives feature instead of array of features '); + } + +