Last active
October 15, 2021 23:14
-
-
Save amites/3718961 to your computer and use it in GitHub Desktop.
Center Geolocations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(z, sqrt(x * x + y * y)), atan2(y, x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@AdamEyreWalker @Firzen7 You have to convert to radians first, this funcion assumes input is in radians:
lat_rad = latPi/180
lon_rad = lonPi/180
just convert it back to degrees at the end