# How to calculate center point of geo coordinates?

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.

-
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

2
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 Narain Dec 12 '12 at 0:45
– Pablo Dec 12 '12 at 0:51

1
@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

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.

-

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.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 11, "mathjax_display_tex": 4, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9323098063468933, "perplexity_flag": "middle"}