diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index b4c8d28709..7655c81d78 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -162,12 +162,30 @@ ol.geom.MultiPolygon.prototype.getArea = function() { /** + * Get the coordinate array for this geometry. This array has the structure + * of a GeoJSON coordinate array for multi-polygons. + * + * @param {boolean=} opt_right Orient coordinates according to the right-hand + * rule (counter-clockwise for exterior and clockwise for interior rings). + * If `false`, coordinates will be oriented according to the left-hand rule + * (clockwise for exterior and counter-clockwise for interior rings). + * By default, coordinate orientation will depend on how the geometry was + * constructed. * @return {Array.>>} Coordinates. * @api stable */ -ol.geom.MultiPolygon.prototype.getCoordinates = function() { +ol.geom.MultiPolygon.prototype.getCoordinates = function(opt_right) { + var flatCoordinates; + if (goog.isDef(opt_right)) { + flatCoordinates = this.getOrientedFlatCoordinates().slice(); + ol.geom.flat.orient.orientLinearRingss( + flatCoordinates, 0, this.endss_, this.stride, opt_right); + } else { + flatCoordinates = this.flatCoordinates; + } + return ol.geom.flat.inflate.coordinatesss( - this.flatCoordinates, 0, this.endss_, this.stride); + flatCoordinates, 0, this.endss_, this.stride); }; diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js index e04f59962f..64ca5e494b 100644 --- a/test/spec/ol/geom/multipolygon.test.js +++ b/test/spec/ol/geom/multipolygon.test.js @@ -89,6 +89,32 @@ describe('ol.geom.MultiPolygon', function() { [[[3, 0], [4, 1], [5, 2], [5, 0]]]); }); + describe('#getCoordinates()', function() { + + var cw = [[-180, -90], [-180, 90], [180, 90], [180, -90], [-180, -90]]; + var cw2 = [[-140, -60], [-140, 60], [140, 60], [140, -60], [-140, -60]]; + var ccw = [[-180, -90], [180, -90], [180, 90], [-180, 90], [-180, -90]]; + var ccw2 = [[-140, -60], [140, -60], [140, 60], [-140, 60], [-140, -60]]; + var right = new ol.geom.MultiPolygon([[ccw, cw], [ccw2, cw2]]); + var left = new ol.geom.MultiPolygon([[cw, ccw], [cw2, ccw2]]); + + it('returns coordinates as they were constructed', function() { + expect(right.getCoordinates()).to.eql([[ccw, cw], [ccw2, cw2]]); + expect(left.getCoordinates()).to.eql([[cw, ccw], [cw2, ccw2]]); + }); + + it('can return coordinates with right-hand orientation', function() { + expect(right.getCoordinates(true)).to.eql([[ccw, cw], [ccw2, cw2]]); + expect(left.getCoordinates(true)).to.eql([[ccw, cw], [ccw2, cw2]]); + }); + + it('can return coordinates with left-hand orientation', function() { + expect(right.getCoordinates(false)).to.eql([[cw, ccw], [cw2, ccw2]]); + expect(left.getCoordinates(false)).to.eql([[cw, ccw], [cw2, ccw2]]); + }); + + }); + describe('#getSimplifiedGeometry', function() { it('returns the expected result', function() {