2

I have a pandas df of origin and destination latitude and longitude.

df = pd.DataFrame({'orig_lat': [32.8111, 34.3424], 'orig_long': [-122.2221,-132.2133],
          'dest_lat': [33.2231, 35.3394], 'dest_long':[-123.2211,-125.1133]})

Now, I want to create a new column called Euclidean_dist to find the Euclidean distance between origin and destination values. Could anyone please help me how to find Euclidean distance?

Jos Butler
  • 23
  • 1
  • 8

1 Answers1

0

I think you need this:

from math import radians, cos, sin, asin, sqrt
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 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    # Radius of earth in kilometers is 6371
    km = 6371* c
    return km

df['Eucl'] = [haversine(df.orig_long[i],df.orig_lat[i],df.dest_long[i],df.dest_lat[i]) for i in range(len(df))]
  • Not sure but isn't it `c = 2 * asin2(sqrt(a), sqrt(1-a))`? I am not a python whiz so maybe the one you wrote works, just curious now. – WolfyD Oct 28 '18 at 18:28
  • @WolfyD So far as I saw, it's `c = 2 * atan2(sqrt(a), sqrt(1-a))`, which is the same as `c = 2 * asin(sqrt(a))` – Partha D. Apr 19 '20 at 13:14
  • This answer would benefit a bit from elaborating why the Euclidean distance does not really make sense for latitude-longitude coordinates and why you are proposing the Haversine distance instead. – ngmir Jan 12 '23 at 17:18