Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# czpython/center_geo.py

Forked from amites/center_geo.py
Last active Feb 7, 2018
calculate center coordinate from a list of latitude, longitude pairs
 # -*- coding: utf-8 -*- import math 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) """ cos = math.cos radians = math.radians sin = math.sin x = 0 y = 0 z = 0 for lat, lon in geolocations: lat, lon = radians(lat), radians(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)) latitude = math.atan2(z, math.sqrt(x * x + y * y)) * (180/math.pi) longitude = math.atan2(y, x) * (180/math.pi) return latitude, longitude
to join this conversation on GitHub. Already have an account? Sign in to comment