Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
Are you looking for actual formulas or do you just need working code? –  whuber Dec 9 '11 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/

share|improve this answer
1  
whoops, cross post. Sorry –  Stev_k Dec 9 '11 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.

share|improve this answer
    
Ok thank you men, –  adev Dec 9 '11 at 13:53
    
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 '11 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. –  OptimizePrime Dec 12 '11 at 16:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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