LinearRing.getCentroid: return null if the geometry has no components. r=pgiraud (closes #2530)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10116 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Frédéric Junod
2010-03-19 09:05:12 +00:00
parent c54dc76d15
commit 37e0c0b4f5
2 changed files with 22 additions and 3 deletions

View File

@@ -190,7 +190,7 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
* {<OpenLayers.Geometry.Point>} The centroid of the collection
*/
getCentroid: function() {
if ( this.components && (this.components.length > 2)) {
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++) {
@@ -202,8 +202,10 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
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;
}
return new OpenLayers.Geometry.Point(x, y);
},
/**

View File

@@ -112,7 +112,24 @@
var ring = new OpenLayers.Geometry.LinearRing(components);
t.eq(ring.getLength(), 40, "getLength returns the correct perimiter");
}
function test_LinearRing_getCentroid(t) {
t.plan(2);
var components = [
new OpenLayers.Geometry.Point(0,0),
new OpenLayers.Geometry.Point(0,10),
new OpenLayers.Geometry.Point(10,10),
new OpenLayers.Geometry.Point(10,0)
];
var ring = new OpenLayers.Geometry.LinearRing(components);
var centroid = ring.getCentroid();
t.ok(centroid.x === 5 && centroid.y === 5, "getCentroid returns the correct centroid");
ring.destroy();
ring = new OpenLayers.Geometry.LinearRing();
t.eq(ring.getCentroid(), null, "getCentroid returns null if no components");
}
function test_LinearRing_move(t) {
var nvert = 4,