From 6275d8528eb30cd3e055495033d71ea18bc727bd Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 6 Dec 2013 15:01:24 +0100 Subject: [PATCH] Add ol.geom.LineString#getSimplifiedGeometryInternal --- src/ol/geom/linestring.js | 17 ++++++++++++++ test/spec/ol/geom/linestring.test.js | 33 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 40b231674f..7ae4bc0edf 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -2,6 +2,7 @@ goog.provide('ol.geom.LineString'); goog.require('ol.geom.Geometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.simplify'); @@ -36,6 +37,22 @@ ol.geom.LineString.prototype.getLength = function() { }; +/** + * @inheritDoc + */ +ol.geom.LineString.prototype.getSimplifiedGeometryInternal = + function(squaredTolerance) { + var simplifiedFlatCoordinates = []; + simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, + squaredTolerance, simplifiedFlatCoordinates, 0); + var simplifiedLineString = new ol.geom.LineString(null); + simplifiedLineString.setFlatCoordinates( + ol.geom.GeometryLayout.XY, simplifiedFlatCoordinates); + return simplifiedLineString; +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index 78c67739cc..1a45d345b8 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -156,6 +156,39 @@ describe('ol.geom.LineString', function() { }); + describe('with a simple line string', function() { + + var lineString; + beforeEach(function() { + lineString = new ol.geom.LineString( + [[0, 0], [1, 1], [3, 3], [5, 1], [6, 3], [7, 5]]); + }); + + describe('#getSimplifiedGeometry', function() { + + it('returns the expectedResult', function() { + var simplifiedGeometry = lineString.getSimplifiedGeometry(1); + expect(simplifiedGeometry).to.be.an(ol.geom.LineString); + expect(simplifiedGeometry.getCoordinates()).to.eql( + [[0, 0], [3, 3], [5, 1], [7, 5]]); + }); + + it('caches by resolution', function() { + var simplifiedGeometry1 = lineString.getSimplifiedGeometry(1); + var simplifiedGeometry2 = lineString.getSimplifiedGeometry(1); + expect(simplifiedGeometry1).to.be(simplifiedGeometry2); + }); + + it('invalidates the cache when the geometry changes', function() { + var simplifiedGeometry1 = lineString.getSimplifiedGeometry(1); + lineString.setCoordinates(lineString.getCoordinates()); + var simplifiedGeometry2 = lineString.getSimplifiedGeometry(1); + expect(simplifiedGeometry1).not.to.be(simplifiedGeometry2); + }); + + }); + }); + });