I need to compute the centroid position of an irregular polygon with lat and lon coordinates in Java. Which formula should I use to correctly compute a centroid?
2 Answers
You could do a lot worse than use the JTS library for this, Look at http://jts-topo-suite.svn.sourceforge.net/viewvc/jts-topo-suite/trunk/jts/java/src/com/vividsolutions/jts/algorithm/CentroidArea.java?revision=488&view=markup for details. From the JavaDocs you can find the algorithm used at http://www.faqs.org/faqs/graphics/algorithms-faq/
-
1
Have you looked at the Java Topology Suite (http://www.vividsolutions.com/jts/JTSHome.htm)? If you're not worried about executable size, then this would seem to fit the bill with a minimum of additional code, and a fairly battle tested library. It's under LGPL licence, so hopefully that's suitable.
-
Ok thank you guys, but I would like to know what's the formula best fit the centroid coputing for geographics positioning. For example what's wrong if I use a simple mean like: lat = Sum(lat_1..lat_n)/n , lon=Sum(lon_1, lon_n)/n ?– adevCommented Dec 9, 2011 at 14:00
-
A very simple method for finding the centroid (centre of mass) for a Geometry is to find the average for the longitude and latitude, though if you require a point that is always inside the feature you'll have issues with concave features. In which case you'll need to look at label point algorithms. Label point algorithms are essentially a centroid algorithm with a shift of the resultant point into the bounds of the feature. Commented Dec 12, 2011 at 16:46