From e65c522cb4c537b3ad98b67c34dcf560a0336601 Mon Sep 17 00:00:00 2001 From: Peter Robins Date: Sat, 19 Mar 2016 16:30:48 +0000 Subject: [PATCH] Fix geojson write GeometryCollection --- src/ol/format/geojsonformat.js | 4 +++- test/spec/ol/format/geojsonformat.test.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ol/format/geojsonformat.js b/src/ol/format/geojsonformat.js index dcb6881cca..594665d0de 100644 --- a/src/ol/format/geojsonformat.js +++ b/src/ol/format/geojsonformat.js @@ -211,7 +211,9 @@ ol.format.GeoJSON.writeGeometryCollectionGeometry_ = function( goog.asserts.assertInstanceof(geometry, ol.geom.GeometryCollection, 'geometry should be an ol.geom.GeometryCollection'); var geometries = geometry.getGeometriesArray().map(function(geometry) { - return ol.format.GeoJSON.writeGeometry_(geometry, opt_options); + var options = ol.object.assign({}, opt_options); + delete options.featureProjection; + return ol.format.GeoJSON.writeGeometry_(geometry, options); }); return /** @type {GeoJSONGeometryCollection} */ ({ type: 'GeometryCollection', diff --git a/test/spec/ol/format/geojsonformat.test.js b/test/spec/ol/format/geojsonformat.test.js index 95358095df..7371a064e0 100644 --- a/test/spec/ol/format/geojsonformat.test.js +++ b/test/spec/ol/format/geojsonformat.test.js @@ -725,6 +725,28 @@ describe('ol.format.GeoJSON', function() { .to.be.lessThan(0.0000001); }); + it('transforms and encodes geometry collection', function() { + var collection = new ol.geom.GeometryCollection([ + new ol.geom.Point([2, 3]), + new ol.geom.LineString([[3, 2], [2, 1]]) + ]); + var geojson = format.writeGeometry(collection, { + featureProjection: 'EPSG:3857' + }); + var got = format.readGeometry(geojson, { + featureProjection: 'EPSG:3857' + }); + var gotGeometries = got.getGeometries(); + var geometries = collection.getGeometries(); + expect(geometries[0].getCoordinates()[0]).to.roughlyEqual( + gotGeometries[0].getCoordinates()[0], 1e-8); + expect(geometries[0].getCoordinates()[1]).to.roughlyEqual( + gotGeometries[0].getCoordinates()[1], 1e-8); + expect(geometries[1].getCoordinates()[0][0]).to.roughlyEqual( + gotGeometries[1].getCoordinates()[0][0], 1e-8); + expect(geometries[1].getCoordinates()[0][1]).to.roughlyEqual( + gotGeometries[1].getCoordinates()[0][1], 1e-8); + }); }); });