Created
March 15, 2018 18:39
-
-
Save osin-vladimir/6007ce334915cabeb0cb75af1b445506 to your computer and use it in GitHub Desktop.
Distance between two geographical points
This file contains 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, asin, sqrt | |
def distance(lat1, lon1, lat2, lon2): | |
# https://en.wikipedia.org/wiki/Haversine_formula | |
p = 0.017453292519943295 | |
a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2 | |
return 12742 * asin(sqrt(a)) | |
def closest(data, v): | |
return min(data, key=lambda p: distance(v['lat'],v['lon'],p['lat'],p['lon'])) | |
tempDataList = [{'lat': 39.7612992, 'lon': -86.1519681}, | |
{'lat': 39.762241, 'lon': -86.158436 }, | |
{'lat': 39.7622292, 'lon': -86.1578917}] | |
v = {'lat': 39.7622290, 'lon': -86.1519750} | |
print(closest(tempDataList, v)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment