I have the follow df with the columns:
lati_origin longi_oringin lati_final longi_final
-19.864315 -44.047180 -3.026643 -59.955860
I used the follow function, but don't return the same distance in km of Google Maps.
def haversine_np(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
All args must be of equal length.
"""
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2.0)**2 np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
km = 6371 * c
return km
Can someone help me calculate the distance similar the Google Maps from my df?
CodePudding user response:
You can use geopy
to find the distance
import geopy.distance
def calculte_distance(lon1, lat1, lon2, lat2):
coords_1 = (lat1, lon1)
coords_2 = (lat2, lon2)
return geopy.distance.geodesic(coords_1, coords_2).km
Installation
pip install geopy
Execution:
In [1]: calculte_distance(-44.047180,-19.864315,-59.955860,-3.026643)
Out[1]: 2541.261913716022