Accessor for polygon rings
This commit is contained in:
+19
-8
@@ -35,8 +35,9 @@ ol.geom.Polygon = function(coordinates) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Array.<ol.geom.LinearRing>}
|
* @type {Array.<ol.geom.LinearRing>}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
this.rings = new Array(numRings);
|
this.rings_ = new Array(numRings);
|
||||||
var ringCoords;
|
var ringCoords;
|
||||||
for (var i = 0; i < numRings; ++i) {
|
for (var i = 0; i < numRings; ++i) {
|
||||||
ringCoords = coordinates[i];
|
ringCoords = coordinates[i];
|
||||||
@@ -51,7 +52,7 @@ ol.geom.Polygon = function(coordinates) {
|
|||||||
ringCoords.reverse();
|
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
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.geom.Polygon.prototype.getBounds = function() {
|
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.
|
* @return {Array.<ol.CoordinateArray>} Coordinates array.
|
||||||
*/
|
*/
|
||||||
ol.geom.Polygon.prototype.getCoordinates = function() {
|
ol.geom.Polygon.prototype.getCoordinates = function() {
|
||||||
var count = this.rings.length;
|
var count = this.rings_.length;
|
||||||
var coordinates = new Array(count);
|
var coordinates = new Array(count);
|
||||||
for (var i = 0; i < count; ++i) {
|
for (var i = 0; i < count; ++i) {
|
||||||
coordinates[i] = this.rings[i].getCoordinates();
|
coordinates[i] = this.rings_[i].getCoordinates();
|
||||||
}
|
}
|
||||||
return coordinates;
|
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
|
* 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
|
* 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.
|
* @return {boolean} Whether the coordinate is inside the polygon.
|
||||||
*/
|
*/
|
||||||
ol.geom.Polygon.prototype.containsCoordinate = function(coordinate) {
|
ol.geom.Polygon.prototype.containsCoordinate = function(coordinate) {
|
||||||
var rings = this.rings;
|
var rings = this.rings_;
|
||||||
/** @type {boolean} */
|
/** @type {boolean} */
|
||||||
var containsCoordinate;
|
var containsCoordinate;
|
||||||
for (var i = 0, ii = rings.length; i < ii; ++i) {
|
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_)) {
|
if (goog.isNull(this.labelPoint_)) {
|
||||||
var center = ol.extent.getCenter(this.getBounds()),
|
var center = ol.extent.getCenter(this.getBounds()),
|
||||||
resultY = center[1],
|
resultY = center[1],
|
||||||
vertices = this.rings[0].getCoordinates(),
|
vertices = this.rings_[0].getCoordinates(),
|
||||||
intersections = [],
|
intersections = [],
|
||||||
maxLength = 0,
|
maxLength = 0,
|
||||||
i, vertex1, vertex2, x, segmentLength, resultX;
|
i, vertex1, vertex2, x, segmentLength, resultX;
|
||||||
@@ -163,7 +174,7 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() {
|
|||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.geom.Polygon.prototype.transform = function(transform) {
|
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) {
|
for (var i = 0, ii = rings.length; i < ii; ++i) {
|
||||||
rings[i].transform(transform);
|
rings[i].transform(transform);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ ol.parser.GPX = function(opt_options) {
|
|||||||
var desc = attributes['description'] || this.defaultDesc;
|
var desc = attributes['description'] || this.defaultDesc;
|
||||||
this.writeNode('desc', desc, undefined, node);
|
this.writeNode('desc', desc, undefined, node);
|
||||||
var geom = feature.getGeometry();
|
var geom = feature.getGeometry();
|
||||||
var i, ii;
|
var i, ii, rings;
|
||||||
if (geom instanceof ol.geom.LineString) {
|
if (geom instanceof ol.geom.LineString) {
|
||||||
this.writeNode('trkseg', feature.getGeometry(), undefined, node);
|
this.writeNode('trkseg', feature.getGeometry(), undefined, node);
|
||||||
} else if (geom instanceof ol.geom.MultiLineString) {
|
} 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);
|
this.writeNode('trkseg', geom.components[i], undefined, node);
|
||||||
}
|
}
|
||||||
} else if (geom instanceof ol.geom.Polygon) {
|
} else if (geom instanceof ol.geom.Polygon) {
|
||||||
for (i = 0, ii = geom.rings.length; i < ii; ++i) {
|
rings = geom.getRings();
|
||||||
this.writeNode('trkseg', geom.rings[i], undefined, node);
|
for (i = 0, ii = rings.length; i < ii; ++i) {
|
||||||
|
this.writeNode('trkseg', rings[i], undefined, node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
|
|||||||
@@ -229,9 +229,10 @@ ol.parser.WKT.prototype.encodeMultiLineString_ = function(geom) {
|
|||||||
*/
|
*/
|
||||||
ol.parser.WKT.prototype.encodePolygon_ = function(geom) {
|
ol.parser.WKT.prototype.encodePolygon_ = function(geom) {
|
||||||
var array = [];
|
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,
|
array.push('(' + this.encodeLineString_.apply(this,
|
||||||
[geom.rings[i]]) + ')');
|
[rings[i]]) + ')');
|
||||||
}
|
}
|
||||||
return array.join(',');
|
return array.join(',');
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ ol.renderer.canvas.Vector.prototype.renderPolygonFeatures_ =
|
|||||||
}
|
}
|
||||||
for (j = 0, jj = components.length; j < jj; ++j) {
|
for (j = 0, jj = components.length; j < jj; ++j) {
|
||||||
poly = components[j];
|
poly = components[j];
|
||||||
rings = poly.rings;
|
rings = poly.getRings();
|
||||||
numRings = rings.length;
|
numRings = rings.length;
|
||||||
if (numRings > 0) {
|
if (numRings > 0) {
|
||||||
// TODO: scenario 4
|
// TODO: scenario 4
|
||||||
|
|||||||
@@ -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]);
|
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
|
||||||
|
var rings = poly.getRings();
|
||||||
expect(poly.rings.length).to.be(3);
|
expect(rings.length).to.be(3);
|
||||||
expect(poly.rings[0]).to.be.a(ol.geom.LinearRing);
|
expect(rings[0]).to.be.a(ol.geom.LinearRing);
|
||||||
expect(poly.rings[1]).to.be.a(ol.geom.LinearRing);
|
expect(rings[1]).to.be.a(ol.geom.LinearRing);
|
||||||
expect(poly.rings[2]).to.be.a(ol.geom.LinearRing);
|
expect(rings[2]).to.be.a(ol.geom.LinearRing);
|
||||||
});
|
});
|
||||||
|
|
||||||
var isClockwise = ol.geom.LinearRing.isClockwise;
|
var isClockwise = ol.geom.LinearRing.isClockwise;
|
||||||
@@ -34,7 +34,7 @@ describe('ol.geom.Polygon', function() {
|
|||||||
expect(isClockwise(outer)).to.be(false);
|
expect(isClockwise(outer)).to.be(false);
|
||||||
|
|
||||||
var poly = new ol.geom.Polygon([outer]);
|
var poly = new ol.geom.Polygon([outer]);
|
||||||
var ring = poly.rings[0];
|
var ring = poly.getRings()[0];
|
||||||
expect(isClockwise(ring.getCoordinates())).to.be(true);
|
expect(isClockwise(ring.getCoordinates())).to.be(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ describe('ol.geom.Polygon', function() {
|
|||||||
expect(isClockwise(inner)).to.be(true);
|
expect(isClockwise(inner)).to.be(true);
|
||||||
|
|
||||||
var poly = new ol.geom.Polygon([outer, inner]);
|
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);
|
expect(isClockwise(ring.getCoordinates())).to.be(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -150,10 +150,11 @@ describe('ol.parser.GeoJSON', function() {
|
|||||||
|
|
||||||
var obj = parser.read(str);
|
var obj = parser.read(str);
|
||||||
expect(obj).to.be.a(ol.geom.Polygon);
|
expect(obj).to.be.a(ol.geom.Polygon);
|
||||||
expect(obj.rings.length).to.be(3);
|
var rings = obj.getRings();
|
||||||
expect(obj.rings[0]).to.be.a(ol.geom.LinearRing);
|
expect(rings.length).to.be(3);
|
||||||
expect(obj.rings[1]).to.be.a(ol.geom.LinearRing);
|
expect(rings[0]).to.be.a(ol.geom.LinearRing);
|
||||||
expect(obj.rings[2]).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() {
|
it('parses geometry collection', function() {
|
||||||
|
|||||||
@@ -75,9 +75,10 @@ describe('ol.parser.WKT', function() {
|
|||||||
var wkt = 'POLYGON((30 10,10 20,20 40,40 40,30 10))';
|
var wkt = 'POLYGON((30 10,10 20,20 40,40 40,30 10))';
|
||||||
var geom = parser.read(wkt);
|
var geom = parser.read(wkt);
|
||||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||||
expect(geom.rings.length).to.eql(1);
|
var rings = geom.getRings();
|
||||||
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
|
expect(rings.length).to.eql(1);
|
||||||
expect(geom.rings[0].getCoordinates()).to.eql(
|
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]]);
|
[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]);
|
||||||
expect(parser.write(geom)).to.eql(wkt);
|
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))';
|
wkt = 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,30 20,35 35,20 30))';
|
||||||
geom = parser.read(wkt);
|
geom = parser.read(wkt);
|
||||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||||
expect(geom.rings.length).to.eql(2);
|
var rings = geom.getRings();
|
||||||
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
|
expect(rings.length).to.eql(2);
|
||||||
expect(geom.rings[1].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
|
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
|
||||||
expect(geom.rings[0].getCoordinates()).to.eql(
|
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]]);
|
[[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]]);
|
[[20, 30], [30, 20], [35, 35], [20, 30]]);
|
||||||
expect(parser.write(geom)).to.eql(wkt);
|
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) )';
|
wkt = 'POLYGON ( (30 10, 10 20, 20 40, 40 40, 30 10) )';
|
||||||
geom = parser.read(wkt);
|
geom = parser.read(wkt);
|
||||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||||
expect(geom.rings.length).to.eql(1);
|
var rings = geom.getRings();
|
||||||
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
|
expect(rings.length).to.eql(1);
|
||||||
expect(geom.rings[0].getCoordinates()).to.eql(
|
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]]);
|
[[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.length).to.eql(2);
|
||||||
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
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[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||||
expect(geom.components[0].rings.length).to.eql(1);
|
expect(geom.components[0].getRings().length).to.eql(1);
|
||||||
expect(geom.components[1].rings.length).to.eql(2);
|
expect(geom.components[1].getRings().length).to.eql(2);
|
||||||
expect(geom.components[0].rings[0].getCoordinates()).to.eql(
|
expect(geom.components[0].getRings()[0].getCoordinates()).to.eql(
|
||||||
[[40, 40], [45, 30], [20, 45], [40, 40]]);
|
[[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]]);
|
[[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]]);
|
[[30, 20], [20, 25], [20, 15], [30, 20]]);
|
||||||
expect(parser.write(geom)).to.eql(wkt);
|
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.length).to.eql(2);
|
||||||
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
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[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||||
expect(geom.components[0].rings.length).to.eql(1);
|
expect(geom.components[0].getRings().length).to.eql(1);
|
||||||
expect(geom.components[1].rings.length).to.eql(2);
|
expect(geom.components[1].getRings().length).to.eql(2);
|
||||||
expect(geom.components[0].rings[0].getCoordinates()).to.eql(
|
expect(geom.components[0].getRings()[0].getCoordinates()).to.eql(
|
||||||
[[40, 40], [45, 30], [20, 45], [40, 40]]);
|
[[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]]);
|
[[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]]);
|
[[30, 20], [20, 25], [20, 15], [30, 20]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user