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?

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

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

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 ?
    – adev
    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. Dec 12, 2011 at 16:46

Your Answer

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

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