Calculate the distance like the Google Maps in longitude and latitude origin and longitude and latitude final

Question:

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?

Asked By: Vivian

||

Answers:

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
Answered By: Rahul K P
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.