Accessor for polygon rings

This commit is contained in:
Tim Schaub
2013-09-27 15:43:54 +02:00
parent 42115baabb
commit 626a319222
7 changed files with 65 additions and 48 deletions

View File

@@ -35,8 +35,9 @@ ol.geom.Polygon = function(coordinates) {
/**
* @type {Array.<ol.geom.LinearRing>}
* @private
*/
this.rings = new Array(numRings);
this.rings_ = new Array(numRings);
var ringCoords;
for (var i = 0; i < numRings; ++i) {
ringCoords = coordinates[i];
@@ -51,7 +52,7 @@ ol.geom.Polygon = function(coordinates) {
ringCoords.reverse();
}
}
this.rings[i] = new ol.geom.LinearRing(ringCoords);
this.rings_[i] = new ol.geom.LinearRing(ringCoords);
}
};
@@ -62,7 +63,7 @@ goog.inherits(ol.geom.Polygon, ol.geom.Geometry);
* @inheritDoc
*/
ol.geom.Polygon.prototype.getBounds = function() {
return this.rings[0].getBounds();
return this.rings_[0].getBounds();
};
@@ -70,10 +71,10 @@ ol.geom.Polygon.prototype.getBounds = function() {
* @return {Array.<ol.CoordinateArray>} Coordinates array.
*/
ol.geom.Polygon.prototype.getCoordinates = function() {
var count = this.rings.length;
var count = this.rings_.length;
var coordinates = new Array(count);
for (var i = 0; i < count; ++i) {
coordinates[i] = this.rings[i].getCoordinates();
coordinates[i] = this.rings_[i].getCoordinates();
}
return coordinates;
};
@@ -87,6 +88,16 @@ ol.geom.Polygon.prototype.getType = function() {
};
/**
* Get polygon rings.
* @return {Array.<ol.geom.LinearRing>} Array of rings. The first ring is the
* exterior and any additional rings are interior.
*/
ol.geom.Polygon.prototype.getRings = function() {
return this.rings_;
};
/**
* Check whether a given coordinate is inside this polygon. Note that this is a
* fast and simple check - points on an edge or vertex of the polygon or one of
@@ -96,7 +107,7 @@ ol.geom.Polygon.prototype.getType = function() {
* @return {boolean} Whether the coordinate is inside the polygon.
*/
ol.geom.Polygon.prototype.containsCoordinate = function(coordinate) {
var rings = this.rings;
var rings = this.rings_;
/** @type {boolean} */
var containsCoordinate;
for (var i = 0, ii = rings.length; i < ii; ++i) {
@@ -122,7 +133,7 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() {
if (goog.isNull(this.labelPoint_)) {
var center = ol.extent.getCenter(this.getBounds()),
resultY = center[1],
vertices = this.rings[0].getCoordinates(),
vertices = this.rings_[0].getCoordinates(),
intersections = [],
maxLength = 0,
i, vertex1, vertex2, x, segmentLength, resultX;
@@ -163,7 +174,7 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() {
* @inheritDoc
*/
ol.geom.Polygon.prototype.transform = function(transform) {
var rings = this.rings;
var rings = this.rings_;
for (var i = 0, ii = rings.length; i < ii; ++i) {
rings[i].transform(transform);
}

View File

@@ -155,7 +155,7 @@ ol.parser.GPX = function(opt_options) {
var desc = attributes['description'] || this.defaultDesc;
this.writeNode('desc', desc, undefined, node);
var geom = feature.getGeometry();
var i, ii;
var i, ii, rings;
if (geom instanceof ol.geom.LineString) {
this.writeNode('trkseg', feature.getGeometry(), undefined, node);
} else if (geom instanceof ol.geom.MultiLineString) {
@@ -163,8 +163,9 @@ ol.parser.GPX = function(opt_options) {
this.writeNode('trkseg', geom.components[i], undefined, node);
}
} else if (geom instanceof ol.geom.Polygon) {
for (i = 0, ii = geom.rings.length; i < ii; ++i) {
this.writeNode('trkseg', geom.rings[i], undefined, node);
rings = geom.getRings();
for (i = 0, ii = rings.length; i < ii; ++i) {
this.writeNode('trkseg', rings[i], undefined, node);
}
}
return node;

View File

@@ -229,9 +229,10 @@ ol.parser.WKT.prototype.encodeMultiLineString_ = function(geom) {
*/
ol.parser.WKT.prototype.encodePolygon_ = function(geom) {
var array = [];
for (var i = 0, ii = geom.rings.length; i < ii; ++i) {
var rings = geom.getRings();
for (var i = 0, ii = rings.length; i < ii; ++i) {
array.push('(' + this.encodeLineString_.apply(this,
[geom.rings[i]]) + ')');
[rings[i]]) + ')');
}
return array.join(',');
};

View File

@@ -362,7 +362,7 @@ ol.renderer.canvas.Vector.prototype.renderPolygonFeatures_ =
}
for (j = 0, jj = components.length; j < jj; ++j) {
poly = components[j];
rings = poly.rings;
rings = poly.getRings();
numRings = rings.length;
if (numRings > 0) {
// TODO: scenario 4

View File

@@ -16,15 +16,15 @@ describe('ol.geom.Polygon', function() {
});
describe('#rings', function() {
describe('#getRings()', function() {
it('is an array of LinearRing', function() {
it('returns an array of LinearRing', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly.rings.length).to.be(3);
expect(poly.rings[0]).to.be.a(ol.geom.LinearRing);
expect(poly.rings[1]).to.be.a(ol.geom.LinearRing);
expect(poly.rings[2]).to.be.a(ol.geom.LinearRing);
var rings = poly.getRings();
expect(rings.length).to.be(3);
expect(rings[0]).to.be.a(ol.geom.LinearRing);
expect(rings[1]).to.be.a(ol.geom.LinearRing);
expect(rings[2]).to.be.a(ol.geom.LinearRing);
});
var isClockwise = ol.geom.LinearRing.isClockwise;
@@ -34,7 +34,7 @@ describe('ol.geom.Polygon', function() {
expect(isClockwise(outer)).to.be(false);
var poly = new ol.geom.Polygon([outer]);
var ring = poly.rings[0];
var ring = poly.getRings()[0];
expect(isClockwise(ring.getCoordinates())).to.be(true);
});
@@ -44,7 +44,7 @@ describe('ol.geom.Polygon', function() {
expect(isClockwise(inner)).to.be(true);
var poly = new ol.geom.Polygon([outer, inner]);
var ring = poly.rings[1];
var ring = poly.getRings()[1];
expect(isClockwise(ring.getCoordinates())).to.be(false);
});

View File

@@ -150,10 +150,11 @@ describe('ol.parser.GeoJSON', function() {
var obj = parser.read(str);
expect(obj).to.be.a(ol.geom.Polygon);
expect(obj.rings.length).to.be(3);
expect(obj.rings[0]).to.be.a(ol.geom.LinearRing);
expect(obj.rings[1]).to.be.a(ol.geom.LinearRing);
expect(obj.rings[2]).to.be.a(ol.geom.LinearRing);
var rings = obj.getRings();
expect(rings.length).to.be(3);
expect(rings[0]).to.be.a(ol.geom.LinearRing);
expect(rings[1]).to.be.a(ol.geom.LinearRing);
expect(rings[2]).to.be.a(ol.geom.LinearRing);
});
it('parses geometry collection', function() {

View File

@@ -75,9 +75,10 @@ describe('ol.parser.WKT', function() {
var wkt = 'POLYGON((30 10,10 20,20 40,40 40,30 10))';
var geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.rings.length).to.eql(1);
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[0].getCoordinates()).to.eql(
var rings = geom.getRings();
expect(rings.length).to.eql(1);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[0].getCoordinates()).to.eql(
[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -85,12 +86,13 @@ describe('ol.parser.WKT', function() {
wkt = 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,30 20,35 35,20 30))';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.rings.length).to.eql(2);
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[1].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[0].getCoordinates()).to.eql(
var rings = geom.getRings();
expect(rings.length).to.eql(2);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[1].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[0].getCoordinates()).to.eql(
[[35, 10], [10, 20], [15, 40], [45, 45], [35, 10]]);
expect(geom.rings[1].getCoordinates()).to.eql(
expect(rings[1].getCoordinates()).to.eql(
[[20, 30], [30, 20], [35, 35], [20, 30]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -98,9 +100,10 @@ describe('ol.parser.WKT', function() {
wkt = 'POLYGON ( (30 10, 10 20, 20 40, 40 40, 30 10) )';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.rings.length).to.eql(1);
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[0].getCoordinates()).to.eql(
var rings = geom.getRings();
expect(rings.length).to.eql(1);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[0].getCoordinates()).to.eql(
[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]);
});
@@ -113,13 +116,13 @@ describe('ol.parser.WKT', function() {
expect(geom.components.length).to.eql(2);
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.components[0].rings.length).to.eql(1);
expect(geom.components[1].rings.length).to.eql(2);
expect(geom.components[0].rings[0].getCoordinates()).to.eql(
expect(geom.components[0].getRings().length).to.eql(1);
expect(geom.components[1].getRings().length).to.eql(2);
expect(geom.components[0].getRings()[0].getCoordinates()).to.eql(
[[40, 40], [45, 30], [20, 45], [40, 40]]);
expect(geom.components[1].rings[0].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[0].getCoordinates()).to.eql(
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
expect(geom.components[1].rings[1].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[1].getCoordinates()).to.eql(
[[30, 20], [20, 25], [20, 15], [30, 20]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -132,13 +135,13 @@ describe('ol.parser.WKT', function() {
expect(geom.components.length).to.eql(2);
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.components[0].rings.length).to.eql(1);
expect(geom.components[1].rings.length).to.eql(2);
expect(geom.components[0].rings[0].getCoordinates()).to.eql(
expect(geom.components[0].getRings().length).to.eql(1);
expect(geom.components[1].getRings().length).to.eql(2);
expect(geom.components[0].getRings()[0].getCoordinates()).to.eql(
[[40, 40], [45, 30], [20, 45], [40, 40]]);
expect(geom.components[1].rings[0].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[0].getCoordinates()).to.eql(
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
expect(geom.components[1].rings[1].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[1].getCoordinates()).to.eql(
[[30, 20], [20, 25], [20, 15], [30, 20]]);
});