'How to calculate haversine distance between 4 columns correctly
I try to calculate haversine distance between 4 columns
cgi longitude_bts latitude_bts longitude_poi latitude_poi
0 510-11-32111-7131 95.335142 5.565253 95.337588 5.563713
1 510-11-32111-7135 95.335142 5.565253 95.337588 5.563713
Here's my code
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
import numpy as np
import math
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
# Radius of earth in kilometers is 6371
km = 6371008.799485213* c
return km
ref_location_airport_hospital['radius'] = ref_location_airport_hospital.apply(lambda x: haversine(x['latitude_bts'], x['longitude_bts'], x['latitude_poi'], x['longitude_poi']), axis=1)
Here's the result
cgi longitude_bts latitude_bts longitude_poi latitude_poi radius
0 510-11-32111-7131 95.335142 5.565253 95.337588 5.563713 272.441676
1 510-11-32111-7135 95.335142 5.565253 95.337588 5.563713 272.441676
The result is not rational, the two points distasance is less than 0.004, so the radius should less than 1 km
Note: 1 longitude/latitide is arroun 111 km
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
