Add ol.geom.Polygon#getSimplifiedGeometryInternal

This commit is contained in:
Tom Payne
2013-12-06 15:01:43 +01:00
parent 956bff0fe2
commit e560192c0f
2 changed files with 47 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ goog.provide('ol.geom.Polygon');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.flat');
goog.require('ol.geom.simplify');
@@ -104,6 +105,23 @@ ol.geom.Polygon.prototype.getLinearRings = function() {
};
/**
* @inheritDoc
*/
ol.geom.Polygon.prototype.getSimplifiedGeometryInternal =
function(squaredTolerance) {
var simplifiedFlatCoordinates = [];
var simplifiedEnds = [];
simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeuckers(
this.flatCoordinates, 0, this.ends_, this.stride, squaredTolerance,
simplifiedFlatCoordinates, 0, simplifiedEnds);
var simplifiedPolygon = new ol.geom.Polygon(null);
simplifiedPolygon.setFlatCoordinates(
ol.geom.GeometryLayout.XY, simplifiedFlatCoordinates, simplifiedEnds);
return simplifiedPolygon;
};
/**
* @inheritDoc
*/

View File

@@ -310,6 +310,35 @@ describe('ol.geom.Polygon', function() {
});
describe('with a simple polygon', function() {
var polygon;
beforeEach(function() {
polygon = new ol.geom.Polygon(
[[[3, 0], [1, 3], [0, 6], [2, 6], [3, 7], [4, 6], [6, 6], [4, 3]]]);
});
describe('#getSimplifiedGeometry', function() {
it('returns the expected result', function() {
var simplifiedGeometry = polygon.getSimplifiedGeometry(1);
expect(simplifiedGeometry).to.be.an(ol.geom.Polygon);
expect(simplifiedGeometry.getCoordinates()).to.eql(
[[[3, 0], [0, 6], [6, 6], [4, 3]]]);
});
it('caches multiple simplified geometries', function() {
var simplifiedGeometry1 = polygon.getSimplifiedGeometry(1);
var simplifiedGeometry2 = polygon.getSimplifiedGeometry(2);
var simplifiedGeometry3 = polygon.getSimplifiedGeometry(1);
var simplifiedGeometry4 = polygon.getSimplifiedGeometry(2);
expect(simplifiedGeometry1).to.be(simplifiedGeometry3);
expect(simplifiedGeometry2).to.be(simplifiedGeometry4);
});
});
});
});