From e81b68ce8b2cfc6062918409224c856303bc914f Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 21 Sep 2015 13:59:01 -0600 Subject: [PATCH 1/4] API method for simplifying geometries --- src/ol/geom/geometry.js | 14 ++++++++++++++ src/ol/geom/simplegeometry.js | 1 + test/spec/ol/geom/linestring.test.js | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index 0a15563150..bf252fbb44 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -169,6 +169,20 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) { }; +/** + * Create a simplified version of this geometry using the {@link + * http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm + * Douglas Peucker} algorithm. + * @function + * @param {number} tolerance The tolerance distance for simplification. + * @return {ol.geom.Geometry} A new, simplified version of the original + * geometry. + */ +ol.geom.Geometry.prototype.simplify = function(tolerance) { + return this.getSimplifiedGeometry(tolerance * tolerance); +}; + + /** * Create a simplified version of this geometry using the Douglas Peucker * algorithm. diff --git a/src/ol/geom/simplegeometry.js b/src/ol/geom/simplegeometry.js index e99e5479b9..fd3f3d62fb 100644 --- a/src/ol/geom/simplegeometry.js +++ b/src/ol/geom/simplegeometry.js @@ -144,6 +144,7 @@ ol.geom.SimpleGeometry.prototype.getLayout = function() { /** * @inheritDoc + * @api */ ol.geom.SimpleGeometry.prototype.getSimplifiedGeometry = function(squaredTolerance) { diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index f4dd6cbd11..023510b090 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -263,6 +263,29 @@ describe('ol.geom.LineString', function() { }); + describe('#simplify', function() { + + it('returns a simplified geometry', function() { + var simplified = lineString.simplify(1); + expect(simplified).to.be.an(ol.geom.LineString); + expect(simplified.getCoordinates()).to.eql( + [[0, 0], [3, 3], [5, 1], [7, 5]]); + }); + + it('does not modify the original', function() { + lineString.simplify(1); + expect(lineString.getCoordinates()).to.eql( + [[0, 0], [1.5, 1], [3, 3], [5, 1], [6, 3.5], [7, 5]]); + }); + + it('delegates to the internal method', function() { + var simplified = lineString.simplify(2); + var internal = lineString.getSimplifiedGeometry(4); + expect(simplified.getCoordinates()).to.eql(internal.getCoordinates()); + }); + + }); + describe('#getSimplifiedGeometry', function() { it('returns the expectedResult', function() { From 250e86e5f233175b50c58247e867789a230edd92 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 22 Sep 2015 09:49:42 -0600 Subject: [PATCH 2/4] Update simplification docs --- src/ol/geom/geometry.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index bf252fbb44..c505551935 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -170,9 +170,11 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) { /** - * Create a simplified version of this geometry using the {@link + * Create a simplified version of this geometry. For linestrings, this uses + * the the {@link * http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm - * Douglas Peucker} algorithm. + * Douglas Peucker} algorithm. For polygons, a quantization-based + * simplification is used to preserve topology. * @function * @param {number} tolerance The tolerance distance for simplification. * @return {ol.geom.Geometry} A new, simplified version of the original From 436561d9dd8fa58813e1e1276c05672224ae59c3 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 22 Sep 2015 10:34:43 -0600 Subject: [PATCH 3/4] Make geom.simplify() exportable --- src/ol/geom/geometry.js | 1 + src/ol/geom/simplegeometry.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index c505551935..f74a083a92 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -179,6 +179,7 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) { * @param {number} tolerance The tolerance distance for simplification. * @return {ol.geom.Geometry} A new, simplified version of the original * geometry. + * @api */ ol.geom.Geometry.prototype.simplify = function(tolerance) { return this.getSimplifiedGeometry(tolerance * tolerance); diff --git a/src/ol/geom/simplegeometry.js b/src/ol/geom/simplegeometry.js index fd3f3d62fb..e99e5479b9 100644 --- a/src/ol/geom/simplegeometry.js +++ b/src/ol/geom/simplegeometry.js @@ -144,7 +144,6 @@ ol.geom.SimpleGeometry.prototype.getLayout = function() { /** * @inheritDoc - * @api */ ol.geom.SimpleGeometry.prototype.getSimplifiedGeometry = function(squaredTolerance) { From 483f567d26af48557c27868a32f36b3b6bab89ec Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 22 Sep 2015 10:39:19 -0600 Subject: [PATCH 4/4] Working Wikipedia links --- src/ol/geom/geometry.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index f74a083a92..c75da4f1eb 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -172,7 +172,7 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) { /** * Create a simplified version of this geometry. For linestrings, this uses * the the {@link - * http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm + * https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm * Douglas Peucker} algorithm. For polygons, a quantization-based * simplification is used to preserve topology. * @function @@ -189,7 +189,7 @@ ol.geom.Geometry.prototype.simplify = function(tolerance) { /** * Create a simplified version of this geometry using the Douglas Peucker * algorithm. - * @see http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm + * @see https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm * @function * @param {number} squaredTolerance Squared tolerance. * @return {ol.geom.Geometry} Simplified geometry.