getCentroid should just return the first point if a linear ring only has less than three components
This commit is contained in:
@@ -191,7 +191,11 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
|
|||||||
* {<OpenLayers.Geometry.Point>} The centroid of the collection
|
* {<OpenLayers.Geometry.Point>} The centroid of the collection
|
||||||
*/
|
*/
|
||||||
getCentroid: function() {
|
getCentroid: function() {
|
||||||
if (this.components && (this.components.length > 2)) {
|
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 sumX = 0.0;
|
||||||
var sumY = 0.0;
|
var sumY = 0.0;
|
||||||
for (var i = 0; i < this.components.length - 1; i++) {
|
for (var i = 0; i < this.components.length - 1; i++) {
|
||||||
@@ -207,6 +211,7 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
|
|||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -386,6 +386,20 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testGetCentroid(t) {
|
||||||
|
t.plan(4);
|
||||||
|
var bounds = new OpenLayers.Bounds(5, 10, 5, 10);
|
||||||
|
var geometry = bounds.toGeometry();
|
||||||
|
var centroid = geometry.getCentroid();
|
||||||
|
t.eq(geometry.components[0].components.length, 2, "only two vertices since the box has left=right and bottom=top");
|
||||||
|
t.ok(centroid && centroid.x === 5 && centroid.y === 10, "getCentroid returns a point geometry even if the ring of the polygon has only 2 vertices");
|
||||||
|
bounds = new OpenLayers.Bounds(0, 0, 10, 10);
|
||||||
|
geometry = bounds.toGeometry();
|
||||||
|
centroid = geometry.getCentroid();
|
||||||
|
t.eq(geometry.components[0].components.length, 5, "five vertices expected");
|
||||||
|
t.ok(centroid && centroid.x === 5 && centroid.y === 5, "getCentroid returns the correct point geometry");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Reference in New Issue
Block a user