Skip to content

Instantly share code, notes, and snippets.

@R3DDY97
Last active April 29, 2018 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save R3DDY97/9719594c7a34b651326de5dfa62a66a7 to your computer and use it in GitHub Desktop.
Save R3DDY97/9719594c7a34b651326de5dfa62a66a7 to your computer and use it in GitHub Desktop.
Calculates distance between two points on earth using their latitudes and longitudes
#!/usr/bin/env python3
'''Calculates Haversian distance between two points on earth'''
from math import (sqrt, radians, sin, cos, atan2)
def haversian_dist(loc1, loc2):
'''Calculates Haversian distance between two points on earth
loc1 and loc2 are (lat, long) of two points'''
radius = 6373.0 # approx earth radius in km
lat1, lon1, lat2, lon2 = map(radians, loc1 + loc2)
lon_diff, lat_diff = lon2 - lon1, lat2 - lat1
a = sin(lat_diff / 2)**2 + cos(lat1) * cos(lat2) * sin(lon_diff / 2)**2
distance = radius * 2 * atan2(sqrt(a), sqrt(1 - a))
return distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment