getCentroid should just return the first point if a linear ring only has less than three components

This commit is contained in:
Bart van den Eijnden
2012-03-22 12:59:55 +01:00
parent 6b8c7a8ca5
commit 955c83baa9
2 changed files with 33 additions and 14 deletions

View File

@@ -191,21 +191,26 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
* {<OpenLayers.Geometry.Point>} The centroid of the collection
*/
getCentroid: function() {
if (this.components && (this.components.length > 2)) {
var sumX = 0.0;
var sumY = 0.0;
for (var i = 0; i < this.components.length - 1; i++) {
var b = this.components[i];
var c = this.components[i+1];
sumX += (b.x + c.x) * (b.x * c.y - c.x * b.y);
sumY += (b.y + c.y) * (b.x * c.y - c.x * b.y);
if (this.components) {
var len = this.components.length;
if (len > 0 && len <= 2) {
return this.components[0].clone();
} else if (len > 2) {
var sumX = 0.0;
var sumY = 0.0;
for (var i = 0; i < this.components.length - 1; i++) {
var b = this.components[i];
var c = this.components[i+1];
sumX += (b.x + c.x) * (b.x * c.y - c.x * b.y);
sumY += (b.y + c.y) * (b.x * c.y - c.x * b.y);
}
var area = -1 * this.getArea();
var x = sumX / (6 * area);
var y = sumY / (6 * area);
return new OpenLayers.Geometry.Point(x, y);
} else {
return null;
}
var area = -1 * this.getArea();
var x = sumX / (6 * area);
var y = sumY / (6 * area);
return new OpenLayers.Geometry.Point(x, y);
} else {
return null;
}
},