public
Last active

Center Geolocations

  • Download Gist
center_geo.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
from math import cos, sin, atan2, sqrt
 
def center_geolocation(geolocations):
"""
Provide a relatively accurate center lat, lon returned as a list pair, given
a list of list pairs.
ex: in: geolocations = ((lat1,lon1), (lat2,lon2),)
out: (center_lat, center_lon)
"""
x = 0
y = 0
z = 0
 
for lat, lon in geolocations:
lat = float(lat)
lon = float(lon)
x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)
 
x = float(x / len(geolocations))
y = float(y / len(geolocations))
z = float(z / len(geolocations))
 
return (atan2(y, x), atan2(z, sqrt(x * x + y * y)))

assuming you want your output center location to be in the form (lat, lon), shouldn't you switch the ordering of your return statements?

i.e: return (atan2(z, sqrt(x * x + y * y)), atan2(y, x))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.