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:
@@ -190,7 +190,7 @@ 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 && (this.components.length > 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++) {
|
||||||
@@ -202,8 +202,10 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
|
|||||||
var area = -1 * this.getArea();
|
var area = -1 * this.getArea();
|
||||||
var x = sumX / (6 * area);
|
var x = sumX / (6 * area);
|
||||||
var y = sumY / (6 * area);
|
var y = sumY / (6 * area);
|
||||||
|
return new OpenLayers.Geometry.Point(x, y);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return new OpenLayers.Geometry.Point(x, y);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -112,7 +112,24 @@
|
|||||||
var ring = new OpenLayers.Geometry.LinearRing(components);
|
var ring = new OpenLayers.Geometry.LinearRing(components);
|
||||||
t.eq(ring.getLength(), 40, "getLength returns the correct perimiter");
|
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) {
|
function test_LinearRing_move(t) {
|
||||||
|
|
||||||
var nvert = 4,
|
var nvert = 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user