Skip to content

Instantly share code, notes, and snippets.

@neuralvis
Forked from amites/center_geo.py
Created April 25, 2017 12:11
Show Gist options
  • Save neuralvis/e92f97d32d6b58ff05f7b370564386c2 to your computer and use it in GitHub Desktop.
Save neuralvis/e92f97d32d6b58ff05f7b370564386c2 to your computer and use it in GitHub Desktop.
Center Geolocations
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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment