Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

How can I calculate center point of N lat/lon pairs, if the distance is at most 30m? Assuming altitude is same for all points.

share|improve this question
First you need to define what you mean by "center". There are many different such definitions in use, especially ones invented by cities that want to have the center of such-and-such country or continent X inside their territory. –  Henning Makholm Dec 12 '12 at 0:22
I will try. I have to get single coordinate from GPS. Unfortunately it does not have built in averaging function. So it gives me coordinates with certain error. I can however collect dozens of coordinates for that exact position. But i don't know how to average these coordinates into single which can more or less smoothen gps error. –  Pablo Dec 12 '12 at 0:26
If all the points are within 30 meters of each other, just averaging the latitudes and longitudes will be very close to the "true" geodesic center for any reasonable definition of "center". At least as long as (i) you are not near the poles, and (ii) your points don't span across the 180° meridian. –  Rahul Dec 12 '12 at 0:45
Someone used the following method gist.github.com/3718961 and I wonder if this will work better than just averaging –  Pablo Dec 12 '12 at 0:51
@Rahul Narain you can answer so I can accept it, if you would like. –  Pablo Dec 12 '12 at 10:14
show 2 more comments

2 Answers

up vote 2 down vote accepted

For completeness (I know this is pretty late; no need to change your accepted answer):

You have $n$ points on the globe, given as latitude $\phi_i$ and longitude $\lambda_i$ for $i=1,\ldots,n$ (adopting Wikipedia's notation). Consider a Cartesian coordinate system in which the Earth is a sphere centered at the origin, with $z$ pointing to the North pole and $x$ crossing the Equator at the $\lambda=0$ meridian. The 3D coordinates of the given points are $$\begin{align} x_i &= r\cos\phi_i\cos\lambda_i,\\ y_i &= r\cos\phi_i\sin\lambda_i,\\ z_i &= r\sin\phi_i, \end{align}$$ (compare spherical coordinates, which uses $\theta=90^\circ-\phi$ and $\varphi=\lambda$). The centroid of these points is of course $$(\bar x,\bar y,\bar z) = \frac1n \sum (x_i, y_i, z_i).$$ This will not in general lie on the unit sphere, but we don't need to actually project it to the unit sphere to determine the geographic coordinates its projection would have. We can simply observe that $$\begin{align} \sin\phi &= z/r, \\ \cos\phi &= \sqrt{x^2+y^2}/r, \\ \sin\lambda &= y/(r\cos\phi), \\ \cos\lambda &= x/(r\cos\phi), \end{align}$$ which implies, since $r$ and $r\cos\phi$ are nonnegative, that $$\begin{align} \bar\phi &= \operatorname{atan2}\left(\bar z, \sqrt{\bar x^2+\bar y^2}\right), \\ \bar\lambda &= \operatorname{atan2}(\bar y, \bar x). \end{align}$$

So yes, the code you linked to does appear to switch the latitude and longitude in the output. You should submit a patch to the author.

share|improve this answer
add comment

If your points are no more than 30m apart, then doing any trigonometric computations will likely introduce more errors than it avoids. So I'd say simply treat the coordinates as coordinates on a plane, and average them to get the centroid.

In order to avoid issues with the 180° meridian, you might want to pick one point as reference and ensure that all the others don't differ in longitude by more than 180°. If they do, then add or subtract 360° to remedy that. The end result might be outside the [-180°, 180°] range but can be adjusted by another 360° if desired.

Near the poles, the compted longitude will likely have a great deal of uncertainty due to input distributed over a wide range of values. But this only corresponds to the fact that at the poles, large differences in longiude correspond to small differences in distance, so nothing wrong there. If you are even closer to the pole, there might be situations where the geodesics between your data points would be badly approximated by the planar interpretation. Roughly speaking, the computation would connect them along a parallel while the most direct connection might be a lot closer to the pole. But I'd expect such effects to only matter once you are within 100m or so of the pole, so probably they are not worth the effort, as even the badly computed result isn't completely off, like it would be for the 180° meridian case.

share|improve this answer
add comment

Your Answer


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.