From 28558c171cb015993009d4e6e12ce3d4937adf6b Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 20 Aug 2010 20:28:48 +0000 Subject: [PATCH] Giving the getCentroid method on geometry collections a 'weighted' argument. If true, the centroid will be calculated based on the weighted average of all component centroids. Thanks for the great patch dzwarg. r=me (closes #2469) git-svn-id: http://svn.openlayers.org/trunk/openlayers@10652 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Geometry/Collection.js | 69 ++++++++++--- tests/Geometry/Collection.html | 142 ++++++++++++++++++++++++++ 2 files changed, 199 insertions(+), 12 deletions(-) diff --git a/lib/OpenLayers/Geometry/Collection.js b/lib/OpenLayers/Geometry/Collection.js index 3780dcb05e..1fe7ad93c2 100644 --- a/lib/OpenLayers/Geometry/Collection.js +++ b/lib/OpenLayers/Geometry/Collection.js @@ -261,22 +261,67 @@ OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, { /** * APIMethod: getCentroid * + * Compute the centroid for this geometry collection. + * + * Parameters: + * weighted - {Boolean} Perform the getCentroid computation recursively, + * returning an area weighted average of all geometries in this collection. + * * Returns: * {} The centroid of the collection */ - getCentroid: function() { - return this.components.length && this.components[0].getCentroid(); - /* - var centroid; - for (var i=0, len=this.components.length; i 0) ? area : minArea; + centroids.push(centroid); + } + len = areas.length; + if (areaSum === 0) { + // all the components in this collection have 0 area + // probably a collection of points -- weight all the points the same + for (var i=0; i