From 7c22d8ffd48ea6abd302599f2c614a41c6b14b1a Mon Sep 17 00:00:00 2001 From: tsauerwein Date: Thu, 10 Jul 2014 15:58:27 +0200 Subject: [PATCH] Also accept empty array for ol.geom.MultiPolygon --- src/ol/geom/multipolygon.js | 10 +++++++--- test/spec/ol/geom/multipolygon.test.js | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 6bffbe3db6..931c22f7a7 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -330,9 +330,13 @@ ol.geom.MultiPolygon.prototype.setCoordinates = } var endss = ol.geom.flat.deflate.coordinatesss( this.flatCoordinates, 0, coordinates, this.stride, this.endss_); - var lastEnds = endss[endss.length - 1]; - this.flatCoordinates.length = lastEnds.length === 0 ? - 0 : lastEnds[lastEnds.length - 1]; + if (endss.length === 0) { + this.flatCoordinates.length = 0; + } else { + var lastEnds = endss[endss.length - 1]; + this.flatCoordinates.length = lastEnds.length === 0 ? + 0 : lastEnds[lastEnds.length - 1]; + } this.dispatchChangeEvent(); } }; diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js index 03dad6ab5b..e04f59962f 100644 --- a/test/spec/ol/geom/multipolygon.test.js +++ b/test/spec/ol/geom/multipolygon.test.js @@ -10,7 +10,7 @@ describe('ol.geom.MultiPolygon', function() { }).not.to.throwException(); }); - describe('with an empty MultiPolygon', function() { + describe('with a null MultiPolygon', function() { var multiPolygon; beforeEach(function() { @@ -28,6 +28,30 @@ describe('ol.geom.MultiPolygon', function() { [[[0, 0], [0, 2], [1, 1], [2, 0]]], [[[3, 0], [4, 1], [5, 2], [5, 0]]] ]); + expect(multiPolygon.getPolygons().length).to.eql(2); + }); + + }); + + describe('with an empty MultiPolygon', function() { + + var multiPolygon; + beforeEach(function() { + multiPolygon = new ol.geom.MultiPolygon([]); + }); + + it('can append polygons', function() { + multiPolygon.appendPolygon( + new ol.geom.Polygon([[[0, 0], [0, 2], [1, 1], [2, 0]]])); + expect(multiPolygon.getCoordinates()).to.eql( + [[[[0, 0], [0, 2], [1, 1], [2, 0]]]]); + multiPolygon.appendPolygon( + new ol.geom.Polygon([[[3, 0], [4, 1], [5, 2], [5, 0]]])); + expect(multiPolygon.getCoordinates()).to.eql([ + [[[0, 0], [0, 2], [1, 1], [2, 0]]], + [[[3, 0], [4, 1], [5, 2], [5, 0]]] + ]); + expect(multiPolygon.getPolygons().length).to.eql(2); }); });