1

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?

1
  • Are you looking for actual formulas or do you just need working code?
    – whuber
    Commented Dec 9, 2011 at 14:33

2 Answers 2

2

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
  • 1
    whoops, cross post. Sorry
    – Stev_k
    Commented Dec 9, 2011 at 13:37
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.

2
  • 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 ?
    – adev
    Commented 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.