Skip to content

Instantly share code, notes, and snippets.

@sierra073
Created May 26, 2019 23:40
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 sierra073/3f64e239d6ba05584560a440fe50d27f to your computer and use it in GitHub Desktop.
Save sierra073/3f64e239d6ba05584560a440fe50d27f to your computer and use it in GitHub Desktop.
def distance(origin, destination):
'''Haversine distance function to compute distance between 2 lat/long pairs. source: https://gist.github.com/rochacbruno/2883505'''
lat1, lon1 = origin
lat2, lon2 = destination
radius = 3959 # miles
dlat = math.radians(lat2 - lat1)
dlon = math.radians(lon2 - lon1)
a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon / 2) * math.sin(dlon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = radius * c
return d
data['lat_lon_distance_miles'] = data.apply(lambda row: distance((row['latitude1'], row['longitude1']), (row['latitude2'], row['longitude2'])), axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment