From aface2f43e0073af44399ecb683cce415f1a158f Mon Sep 17 00:00:00 2001 From: Thomas Chandelle Date: Thu, 8 Jun 2017 11:55:46 +0200 Subject: [PATCH 1/3] Clone the geometry before applying the decimals transform We may only do it if the geometry has not been previously transformed. --- src/ol/format/feature.js | 3 +++ test/spec/ol/format/geojson.test.js | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/ol/format/feature.js b/src/ol/format/feature.js index d4d88c131f..79ae29a4d4 100644 --- a/src/ol/format/feature.js +++ b/src/ol/format/feature.js @@ -206,6 +206,9 @@ ol.format.Feature.transformWithOptions = function( if (Array.isArray(transformed)) { transform(transformed); } else { + if (transformed === geometry) { + transformed = transformed.clone(); + } transformed.applyTransform(transform); } } diff --git a/test/spec/ol/format/geojson.test.js b/test/spec/ol/format/geojson.test.js index f291c2ef5f..95b4271b92 100644 --- a/test/spec/ol/format/geojson.test.js +++ b/test/spec/ol/format/geojson.test.js @@ -826,6 +826,8 @@ describe('ol.format.GeoJSON', function() { }); expect(format.readGeometry(geojson).getCoordinates()).to.eql( [[42.123457, 38.987654], [43, 39]]); + expect(linestring.getCoordinates()).to.eql( + [[42.123456789, 38.987654321], [43, 39]]); }); }); From b8117b39313b2920dc1530a5bdd8959a892c05a4 Mon Sep 17 00:00:00 2001 From: Thomas Chandelle Date: Thu, 8 Jun 2017 12:00:56 +0200 Subject: [PATCH 2/3] This method will never be used with an extent and write = true --- src/ol/format/feature.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/ol/format/feature.js b/src/ol/format/feature.js index 79ae29a4d4..0a5e53b751 100644 --- a/src/ol/format/feature.js +++ b/src/ol/format/feature.js @@ -183,9 +183,9 @@ ol.format.Feature.transformWithOptions = function( // FIXME this is necessary because ol.format.GML treats extents // as geometries transformed = ol.proj.transformExtent( - write ? geometry.slice() : geometry, - write ? featureProjection : dataProjection, - write ? dataProjection : featureProjection); + geometry, + dataProjection, + featureProjection); } } else { transformed = geometry; @@ -203,14 +203,10 @@ ol.format.Feature.transformWithOptions = function( } return coordinates; }; - if (Array.isArray(transformed)) { - transform(transformed); - } else { - if (transformed === geometry) { - transformed = transformed.clone(); - } - transformed.applyTransform(transform); + if (transformed === geometry) { + transformed = transformed.clone(); } + transformed.applyTransform(transform); } return transformed; }; From d8dba61b0a989bfd8584dab5c49d0a67aa3440ee Mon Sep 17 00:00:00 2001 From: Thomas Chandelle Date: Thu, 8 Jun 2017 12:06:40 +0200 Subject: [PATCH 3/3] Rounds if decimals options = 0 --- src/ol/format/feature.js | 2 +- test/spec/ol/format/geojson.test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ol/format/feature.js b/src/ol/format/feature.js index 0a5e53b751..5887dcc106 100644 --- a/src/ol/format/feature.js +++ b/src/ol/format/feature.js @@ -190,7 +190,7 @@ ol.format.Feature.transformWithOptions = function( } else { transformed = geometry; } - if (write && opt_options && opt_options.decimals) { + if (write && opt_options && opt_options.decimals !== undefined) { var power = Math.pow(10, opt_options.decimals); // if decimals option on write, round each coordinate appropriately /** diff --git a/test/spec/ol/format/geojson.test.js b/test/spec/ol/format/geojson.test.js index 95b4271b92..a6b20bc652 100644 --- a/test/spec/ol/format/geojson.test.js +++ b/test/spec/ol/format/geojson.test.js @@ -829,6 +829,18 @@ describe('ol.format.GeoJSON', function() { expect(linestring.getCoordinates()).to.eql( [[42.123456789, 38.987654321], [43, 39]]); }); + + it('rounds a linestring with decimals option = 0', function() { + var linestring = new ol.geom.LineString([[42.123456789, 38.987654321], + [43, 39]]); + var geojson = format.writeGeometry(linestring, { + decimals: 0 + }); + expect(format.readGeometry(geojson).getCoordinates()).to.eql( + [[42, 39], [43, 39]]); + expect(linestring.getCoordinates()).to.eql( + [[42.123456789, 38.987654321], [43, 39]]); + }); }); });