From 99eca3037d7b5f7b5dd756cbdf585f86b1722e89 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 13 Jan 2014 12:45:45 +0100 Subject: [PATCH] Encode ol.geom.Circles as empty geometry collections in GeoJSON GeoJSON does not support circles, nor null geometries. Empty geometry collections seem to be the way to represent at null geometry in GeoJSON. --- src/ol/format/geojsonformat.js | 16 +++++++++++++++- test/spec/ol/format/geojsonformat.test.js | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ol/format/geojsonformat.js b/src/ol/format/geojsonformat.js index d703970609..b9f3b59886 100644 --- a/src/ol/format/geojsonformat.js +++ b/src/ol/format/geojsonformat.js @@ -153,6 +153,19 @@ ol.format.GeoJSON.writeGeometry_ = function(geometry) { }; +/** + * @param {ol.geom.Geometry} geometry Geometry. + * @private + * @return {GeoJSONGeometryCollection} Empty GeoJSON geometry collection. + */ +ol.format.GeoJSON.writeEmptyGeometryCollectionGeometry_ = function(geometry) { + return /** @type {GeoJSONGeometryCollection} */ ({ + 'type': 'GeometryCollection', + 'geometries': [] + }); +}; + + /** * @param {ol.geom.Geometry} geometry Geometry. * @private @@ -283,7 +296,8 @@ ol.format.GeoJSON.GEOMETRY_WRITERS_ = { 'MultiPoint': ol.format.GeoJSON.writeMultiPointGeometry_, 'MultiLineString': ol.format.GeoJSON.writeMultiLineStringGeometry_, 'MultiPolygon': ol.format.GeoJSON.writeMultiPolygonGeometry_, - 'GeometryCollection': ol.format.GeoJSON.writeGeometryCollectionGeometry_ + 'GeometryCollection': ol.format.GeoJSON.writeGeometryCollectionGeometry_, + 'Circle': ol.format.GeoJSON.writeEmptyGeometryCollectionGeometry_ }; diff --git a/test/spec/ol/format/geojsonformat.test.js b/test/spec/ol/format/geojsonformat.test.js index a6792d834f..d9248b6661 100644 --- a/test/spec/ol/format/geojsonformat.test.js +++ b/test/spec/ol/format/geojsonformat.test.js @@ -458,6 +458,16 @@ describe('ol.format.GeoJSON', function() { expect(geometries[i].getCoordinates()). to.eql(gotGeometries[i].getCoordinates()); } + + }); + + it('encodes a circle as an empty geometry collection', function() { + var circle = new ol.geom.Circle([0, 0], 1); + var geojson = format.writeGeometry(circle); + expect(geojson).to.eql({ + 'type': 'GeometryCollection', + 'geometries': [] + }); }); }); @@ -468,6 +478,7 @@ describe('ol.format.GeoJSON', function() { goog.require('ol.Feature'); goog.require('ol.extent'); goog.require('ol.format.GeoJSON'); +goog.require('ol.geom.Circle'); goog.require('ol.geom.GeometryCollection'); goog.require('ol.geom.LineString'); goog.require('ol.geom.LinearRing');