Created
May 26, 2019 23:40
-
-
Save sierra073/3f64e239d6ba05584560a440fe50d27f to your computer and use it in GitHub Desktop.
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
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