views:

1724

answers:

7

Hello,

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.

+1  A: 

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.

Bill the Lizard
A: 

What do you mean by "centroid" if you simply mean the point in the middle of the smallest area that contains all the points, then just average the latitude of the farthest north and south points, and the longitudes of the farthest East and West points...

if you want a "weighted" centroid, then average all the latitudes, and all the longitudes...

Charles Bretana
+2  A: 

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)?
pdemarest
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.
Paul Tomblin
A: 

Average them. First sum all points using p1+p2 = Point(p1.x+p2.x, p1.y+p2.y). Then divide the x and y by the number of points.

Jules
+5  A: 

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
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 Jeness Enterprises for making the information available. Note: I am in no way affiliated with the author of this material.

e.James
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
Bill the Lizard
Thank you. Let's just hope it is all worthwile for chews.
e.James
That PDF is missing. Check this instead [24MB!] http://www.jennessent.com/downloads/graphics_shapes_poster_full.pdf
mixdev
@mixdev: Thank you for the link. I have updated my answer.
e.James
+1  A: 

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)

Martin Beckett
A: 

Separately average the latitudes and longitudes.

joel.neely