Add ol.geom.MultiPolygon#getPolygon

This commit is contained in:
Tom Payne
2014-03-10 16:40:19 +01:00
parent 719384ad14
commit c8bbeca06e
3 changed files with 43 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
@exportProperty ol.geom.MultiPolygon.prototype.getArea
@exportProperty ol.geom.MultiPolygon.prototype.getCoordinates
@exportProperty ol.geom.MultiPolygon.prototype.getInteriorPoints
@exportProperty ol.geom.MultiPolygon.prototype.getPolygon
@exportProperty ol.geom.MultiPolygon.prototype.getPolygons
@exportProperty ol.geom.MultiPolygon.prototype.getType
@exportProperty ol.geom.MultiPolygon.prototype.setCoordinates

View File

@@ -205,6 +205,37 @@ ol.geom.MultiPolygon.prototype.getSimplifiedGeometryInternal =
};
/**
* @param {number} index Index.
* @return {ol.geom.Polygon} Polygon.
*/
ol.geom.MultiPolygon.prototype.getPolygon = function(index) {
goog.asserts.assert(0 <= index && index < this.endss_.length);
if (index < 0 || this.endss_.length <= index) {
return null;
}
var offset;
if (index === 0) {
offset = 0;
} else {
var prevEnds = this.endss_[index - 1];
offset = prevEnds[prevEnds.length - 1];
}
var ends = this.endss_[index].slice();
var end = ends[ends.length - 1];
if (offset !== 0) {
var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) {
ends[i] -= offset;
}
}
var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(offset, end), ends);
return polygon;
};
/**
* @return {Array.<ol.geom.Polygon>} Polygons.
* @todo stability experimental

View File

@@ -20,6 +20,17 @@ describe('ol.geom.MultiPolygon', function() {
]);
});
it('can return individual polygons', function() {
var polygon0 = multiPolygon.getPolygon(0);
expect(polygon0).to.be.an(ol.geom.Polygon);
expect(polygon0.getCoordinates()).to.eql(
[[[0, 0], [0, 2], [1, 1], [2, 0]]]);
var polygon1 = multiPolygon.getPolygon(1);
expect(polygon1).to.be.an(ol.geom.Polygon);
expect(polygon1.getCoordinates()).to.eql(
[[[3, 0], [4, 1], [5, 2], [5, 0]]]);
});
it('can return all polygons', function() {
var polygons = multiPolygon.getPolygons();
expect(polygons).to.be.an(Array);