[OpenLayers-Users] Polygon centroid oddity. Is something missing?

plen peteralen at earthlink.net
Fri Oct 17 16:32:53 EDT 2008


Hello,

I have various polygons (drawn and regular OpenLayers polygons - no
irregular ones that cross lines) which I need to find the centroid.  I found
the following Javascript method for this from one of the forum items:

-----------------------------------------
function findCentroid(vertices, area) {
   // this actual calculates the center of mass.
   var cx = 0;
   var cy = 0;
   var i, j, n = vertices.length;
   var factor = 0;
   if (vertices[0].y == vertices[vertices.length - 1].y &&
       vertices[0].x == vertices[vertices.length - 1].x) {
       // do nothing.
   }else {
      // We must close the polygon by adding the first vertices to
      // also be the last.
      vertices[vertices.length] = vertices[0];
   }
   for (i=0; i<n; i++) {
      j = (i + 1) % n;
      factor = (vertices[i].y * vertices[j].x -
                vertices[j].y * vertices[i].x);
           
      cx += (vertices[i].y + vertices[j].y) * factor;
      cy += (vertices[i].x + vertices[j].x) * factor;
   }
   area *= 6;
   factor = 1 / area;
   cx *= factor;
   cy *= factor;
 
   if (cx < 0) {
      return new OpenLayers.LonLat(-cy, -cx);
   }else {
      return new OpenLayers.LonLat(cy, cx);
   }
}
---------------------------------------------

Now for the problem, which deals with the latlong that gets returned at the
end.  Way back, when I found this function, the example's return was:
 
return new OpenLayers.LonLat(cy, cx);

I then saw something that indicated that a check should be made of the lat
coord and do this:
 
if (cx < 0) {
      return new OpenLayers.LonLat(-cy, -cx);
}else {
      return new OpenLayers.LonLat(cy, cx);
}

I thought all was well, but I started seeing some problems while running
through some OpenLayers polygon examples.  Typically, when I created a
polygon (OL polygon draw tool), I made my points in a clockwise order.  With
the OpenLayers "regular" polygons, the order of the coordinates are listed
in counter-clockwise order.  This is when I started to see that the centroid
function was not working properly on those types of polygons.  After
numerous tests, this is what I found out:
 
If the return was "return new OpenLayers.LonLat(cy, cx);" then all clockwise
polygons were fine, but counter-clockwise polygons got a centroid on the
opposite side of the globe.

If the return was "return new OpenLayers.LonLat(-cy, -cx);" then all
counter-clockwise polygons were fine, but clockwise polygons got a centroid
on the opposite side of the globe.

if the return depended on the "if (cx < 0) {" then all clockwise and
counter-clockwise polygons ABOVE the equator were fine, but not BELOW the
equator.

Has anyone noticed this?  There is no doubt that there is a problem with the
function's returned coordinate depending on the order of the vertices sent
in to the function.  Am I missing something else to do?  Or is there some
other way to identify the centroid?

Any thoughts would be greatly appreciated - Peter


-- 
View this message in context: http://www.nabble.com/Polygon-centroid-oddity.--Is-something-missing--tp20040567p20040567.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.




More information about the Users mailing list