Haversine Formula in Python (Bearing and Distance between two GPS points) Haversine Formula in Python (Bearing and Distance between two GPS points) python python

Haversine Formula in Python (Bearing and Distance between two GPS points)


Here's a Python version:

from math import radians, cos, sin, asin, sqrtdef haversine(lon1, lat1, lon2, lat2):    """    Calculate the great circle distance in kilometers 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))     r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.    return c * r


Most of these answers are "rounding" the radius of the earth. If you check these against other distance calculators (such as geopy), these functions will be off.

This works well:

from math import radians, cos, sin, asin, sqrtdef haversine(lat1, lon1, lat2, lon2):      R = 3959.87433 # this is in miles.  For Earth radius in kilometers use 6372.8 km      dLat = radians(lat2 - lat1)      dLon = radians(lon2 - lon1)      lat1 = radians(lat1)      lat2 = radians(lat2)      a = sin(dLat/2)**2 + cos(lat1)*cos(lat2)*sin(dLon/2)**2      c = 2*asin(sqrt(a))      return R * c# Usagelon1 = -103.548851lat1 = 32.0004311lon2 = -103.6041946lat2 = 33.374939print(haversine(lat1, lon1, lat2, lon2))


There is also a vectorized implementation, which allows to use 4 numpy arrays instead of scalar values for coordinates:

def distance(s_lat, s_lng, e_lat, e_lng):   # approximate radius of earth in km   R = 6373.0   s_lat = s_lat*np.pi/180.0                         s_lng = np.deg2rad(s_lng)        e_lat = np.deg2rad(e_lat)                          e_lng = np.deg2rad(e_lng)     d = np.sin((e_lat - s_lat)/2)**2 + np.cos(s_lat)*np.cos(e_lat) * np.sin((e_lng - s_lng)/2)**2   return 2 * R * np.arcsin(np.sqrt(d))