diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 4eea4150e3..9c8d8ca020 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -151,12 +151,30 @@ ol.geom.Polygon.prototype.getArea = function() { /** + * Get the coordinate array for this geometry. This array has the structure + * of a GeoJSON coordinate array for 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.Polygon.prototype.getCoordinates = function() { +ol.geom.Polygon.prototype.getCoordinates = function(opt_right) { + var flatCoordinates; + if (goog.isDef(opt_right)) { + flatCoordinates = this.getOrientedFlatCoordinates().slice(); + ol.geom.flat.orient.orientLinearRings( + flatCoordinates, 0, this.ends_, this.stride, opt_right); + } else { + flatCoordinates = this.flatCoordinates; + } + return ol.geom.flat.inflate.coordinatess( - this.flatCoordinates, 0, this.ends_, this.stride); + flatCoordinates, 0, this.ends_, this.stride); }; diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js index 163d36ce87..5ca233f260 100644 --- a/test/spec/ol/geom/polygon.test.js +++ b/test/spec/ol/geom/polygon.test.js @@ -120,6 +120,30 @@ describe('ol.geom.Polygon', function() { expect(polygon.containsCoordinate(insideInner)).to.be(false); }); + describe('#getCoordinates()', function() { + + var cw = [[-180, -90], [-180, 90], [180, 90], [180, -90], [-180, -90]]; + var ccw = [[-180, -90], [180, -90], [180, 90], [-180, 90], [-180, -90]]; + var right = new ol.geom.Polygon([ccw, cw]); + var left = new ol.geom.Polygon([cw, ccw]); + + it('returns coordinates as they were constructed', function() { + expect(right.getCoordinates()).to.eql([ccw, cw]); + expect(left.getCoordinates()).to.eql([cw, ccw]); + }); + + it('can return coordinates with right-hand orientation', function() { + expect(right.getCoordinates(true)).to.eql([ccw, cw]); + expect(left.getCoordinates(true)).to.eql([ccw, cw]); + }); + + it('can return coordinates with left-hand orientation', function() { + expect(right.getCoordinates(false)).to.eql([cw, ccw]); + expect(left.getCoordinates(false)).to.eql([cw, ccw]); + }); + + }); + describe('#getOrientedFlatCoordinates', function() { it('reverses the outer ring if necessary', function() {