Determine the centroid of multiple points

15,758

Solution 1

Have a look at the pdf document linked below. It explains how to apply the plane figure algorithm that Bill the Lizard mentions, but on the surface of a sphere.

poster thumbnail and some details http://img51.imageshack.us/img51/4093/centroidspostersummary.jpg
Source: http://www.jennessent.com/arcgis/shapes_poster.htm
There is also a 25 MB full-size PDF available for download.
Credit goes to mixdev for finding the link to the original source, and of course to Jenness Enterprises for making the information available. Note: I am in no way affiliated with the author of this material.

Solution 2

Adding to Andrew Rollings' answer.

You will also need to make sure that if you have points on either side of the 0/360 longitude line that you are measuring in the "right direction"

Is the center of (0,359) and (0, 1) at (0,0) or (0,180)?

Solution 3

If you are averaging angles and have to deal with them crossing the 0/360 then it is safer to sum the sin and cos of each value and then Average = atan2(sum of sines,sum of cosines)
(be careful of the argument order in your atan2 function)

Solution 4

The math is pretty simple if the points form a plane figure. There's no guarantee, however, that a set of latitudes and longitudes are that simple, so it may first be necessary to find the convex hull of the points.

EDIT: As eJames points out, you have to make corrections for the surface of a sphere. My fault for assuming (without thinking) that this was understood. +1 to him.

Share:
15,758
ben
Author by

ben

Updated on June 20, 2022

Comments

  • ben
    ben almost 2 years

    I'm writing a mapping application that I am writing in python and I need to get the lat/lon centroid of N points. Say I have two locations

    a.lat = 101
    a.lon = 230
    
    b.lat = 146
    b.lon = 200
    

    Getting the center of two points is fairly easy using a euclidean formula. I would like to be able to do it for more then two points.

    Fundamentally I'm looking to do something like http://a.placebetween.us/ where one can enter multiple addresses and find a the spot that is equidistant for everyone.

  • Paul Tomblin
    Paul Tomblin over 15 years
    This is the main reason why my app treats the east hemisphere completely separately from the west hemisphere. For instance, there isn't a "extents of the United States", there is a "US-E" and "US-W" and I combine them as needed.
  • Bill the Lizard
    Bill the Lizard over 15 years
    This is a definite improvement. I've been doing a lot of work with GIS lately, so I just assumed that this was understood. That's a terrible assumption in a general forum. +1
  • Mathias
    Mathias over 15 years
    Thank you. Let's just hope it is all worthwile for chews.
  • mixdev
    mixdev about 14 years
    That PDF is missing. Check this instead [24MB!] jennessent.com/downloads/graphics_shapes_poster_full.pdf
  • CrazyCoder
    CrazyCoder over 13 years
    I have gone through the link given by you guys.For calculating cartesian coordinates & centroid of triangle we need radius right. But how to calculate the radius of the sphere?
  • Mathias
    Mathias over 13 years
    @Windbros: If all you have are latitude/longitude pairs, there is no way to calculate the radius of the sphere. It is possible to calculate a radius if you have at least three unique 3D points that are known to be on the surface of that sphere, but if you are talking about the Earth (as in this case) the nominal radius is simply 6378.1 kilometers according to Google.
  • CrazyCoder
    CrazyCoder over 13 years
    @e.James: All I have is only longitude & latitude pair. So What shall I do? My aim is to calculate the centroid of polygon from n longitude and latitude pairs. From the link, I understand that I need to calculate the centroid of triangles for finding centroid of polygon. Is my understanding right?
  • Mathias
    Mathias over 13 years
    @Winbros: Do your lat/lon pairs describe points on Earth? If not, you can always use the 2D version of this algorithm and ignore the radius entirely. Bill the Lizard provides a link to the 2D version in his answer
  • Mathias
    Mathias over 13 years
    @Winbros: Please forgive me for spelling your name wrong in my previous comment.
  • CrazyCoder
    CrazyCoder over 13 years
    @e.james: My lat/lon pairs describe points on Earth. Do you have sample implementation of above mentioned method. If not, can you give me the pseudocode for using above method to calculate the centroid of the polygon?
  • Mathias
    Mathias over 13 years
    Sorry, @Winbros. I do not have a sample implementation or pseudocode. The Wikipedia atricle and the poster both provide the background math. It should not be too difficult to translate into code. Just a lot of loops and floating point operations :)
  • redcalx
    redcalx over 12 years
    Very useful PDF. However there's an error in how they project the averaged cartesian coord back onto the surface of the sphere. Basically you need to use the unit sphere (radius 1.0) when calculating the centroid cartesian coords and the value of L, and then rescale the final X,Y,Z by multiplying by the radius as a final step.