From 619fd4965b90f41650f5ae0f156206aa6ade6586 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 12 Nov 2020 09:28:28 +0100 Subject: [PATCH] Write the correct SRS code in EsriJSON --- src/ol/format/EsriJSON.js | 12 +++++------- test/spec/ol/format/esrijson.test.js | 29 +++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/ol/format/EsriJSON.js b/src/ol/format/EsriJSON.js index 77973fdf2c..066647957a 100644 --- a/src/ol/format/EsriJSON.js +++ b/src/ol/format/EsriJSON.js @@ -207,16 +207,14 @@ class EsriJSON extends JSONFeature { const geometry = feature.getGeometry(); if (geometry) { object['geometry'] = writeGeometry(geometry, opt_options); - if (opt_options && opt_options.featureProjection) { + const projection = + opt_options && + (opt_options.dataProjection || opt_options.featureProjection); + if (projection) { object['geometry'][ 'spatialReference' ] = /** @type {EsriJSONSpatialReferenceWkid} */ ({ - wkid: Number( - getProjection(opt_options.featureProjection) - .getCode() - .split(':') - .pop() - ), + wkid: Number(getProjection(projection).getCode().split(':').pop()), }); } delete properties[feature.getGeometryName()]; diff --git a/test/spec/ol/format/esrijson.test.js b/test/spec/ol/format/esrijson.test.js index 44c8e1c188..76eb1db46c 100644 --- a/test/spec/ol/format/esrijson.test.js +++ b/test/spec/ol/format/esrijson.test.js @@ -1763,15 +1763,38 @@ describe('ol.format.EsriJSON', function () { expect(esrijson.attributes).to.eql({}); }); - it('adds the projection inside the geometry correctly', function () { + it('adds the projection inside the geometry correctly when featureProjection is set', function () { const str = JSON.stringify(data); const array = format.readFeatures(str); const esrijson = format.writeFeaturesObject(array, { - featureProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857', }); - esrijson.features.forEach(function (feature) { + esrijson.features.forEach(function (feature, i) { + const spatialReference = feature.geometry.spatialReference; + expect(Number(spatialReference.wkid)).to.equal(3857); + expect(feature.geometry.paths[0]).to.eql( + array[i].getGeometry().getCoordinates() + ); + }); + }); + + it('adds the projection inside the geometry correctly when dataProjection is set', function () { + const str = JSON.stringify(data); + const array = format.readFeatures(str); + const esrijson = format.writeFeaturesObject(array, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857', + }); + esrijson.features.forEach(function (feature, i) { const spatialReference = feature.geometry.spatialReference; expect(Number(spatialReference.wkid)).to.equal(4326); + expect(feature.geometry.paths[0]).to.eql( + array[i] + .getGeometry() + .clone() + .transform('EPSG:3857', 'EPSG:4326') + .getCoordinates() + ); }); }); });