diff --git a/src/ol/geom/Collection.js b/src/ol/geom/Collection.js index bf57b31506..28e4cd356e 100644 --- a/src/ol/geom/Collection.js +++ b/src/ol/geom/Collection.js @@ -180,8 +180,12 @@ ol.geom.Collection.prototype.getCentroid = function() { if (len > 0) { goog.array.forEach(components, function(component){ var singleCentroid = component.getCentroid(); - sum_x += singleCentroid.getX(); - sum_y += singleCentroid.getX(); + if (goog.isDefAndNotNull(singleCentroid)) { + sum_x += singleCentroid.getX(); + sum_y += singleCentroid.getX(); + } else { + len--; + } }); centroid = new ol.geom.Point(sum_x / len, sum_y / len); } diff --git a/test/spec/api/geom/collection.test.js b/test/spec/api/geom/collection.test.js index eaff90f748..60a925ca73 100644 --- a/test/spec/api/geom/collection.test.js +++ b/test/spec/api/geom/collection.test.js @@ -178,6 +178,40 @@ describe("ol.geom.collection", function() { expect(c.components().length).toBe(0); }); }); + + describe("the centroid method is functional", function(){ + it("returns an instance of ol.geom.Point", function(){ + expect(c.centroid()).toBeA(ol.geom.Point); + }); + + it("does not choke when components returns a null centroid", function(){ + var centroid; + expect( + function(){ + c.add(new ol.geom.linestring([])); + centroid = c.centroid(); + } + ).not.toThrow(); + + expect(centroid).toBeA(ol.geom.Point); + }); + + it("has the expected coordinates", function(){ + c = ol.geom.collection([ + ol.geom.point([10,10]), + ol.geom.point([30,30]), + ol.geom.linestring([ + ol.geom.point([10,10]), + ol.geom.point([10,30]), + ol.geom.point([30,30]), + ol.geom.point([30,10]) + ]) + ]); + debugger; + var centroid = c.centroid(); + expect(centroid.x() + ',' + centroid.y()).toBe('20,20'); + }); + }); }); diff --git a/test/spec/ol/geom/Collection.test.js b/test/spec/ol/geom/Collection.test.js index fa5df0ed6c..fd5e63ec93 100644 --- a/test/spec/ol/geom/Collection.test.js +++ b/test/spec/ol/geom/Collection.test.js @@ -132,4 +132,37 @@ describe("ol.geom.Collection", function() { expect( components[1].getX() + ',' + components[1].getY()).toBe( '10,20' ); expect( components[2].getX() + ',' + components[2].getY()).toBe( '30,40' ); }); + + describe("the getCentroid method is functional", function(){ + it("returns an instance of ol.geom.Point", function(){ + expect(c.getCentroid()).toBeA(ol.geom.Point); + }); + + it("does not choke when components returns a null centroid", function(){ + var centroid; + expect( + function(){ + c.addComponent(new ol.geom.LineString([])); + centroid = c.getCentroid(); + } + ).not.toThrow(); + + expect(centroid).toBeA(ol.geom.Point); + }); + + it("has the expected coordinates", function(){ + c = new ol.geom.Collection([ + new ol.geom.Point(10,10), + new ol.geom.Point(30,30), + new ol.geom.LineString([ + new ol.geom.Point(10,10), + new ol.geom.Point(10,30), + new ol.geom.Point(30,30), + new ol.geom.Point(30,10) + ]) + ]); + var centroid = c.getCentroid(); + expect(centroid.getX() + ',' + centroid.getY()).toBe('20,20'); + }); + }); });