For people (like me) coming here via search engine and just looking for a solution which works out of the box, I recommend installing mpu. Install it via pip install mpu --user and use it like this to get the haversine distance:
import mpu # Point one lat1 = 52.2296756 lon1 = 21.0122287 # Point two lat2 = 52.406374 lon2 = 16.9251681 # What you were looking for dist = mpu.haversine_distance((lat1, lon1), (lat2, lon2)) print(dist) # gives 278.45817507541943.An alternative package is gpxpy.
If you don't want dependencies, you can use:
import math def distance(origin, destination): """ Calculate the Haversine distance. Parameters ---------- origin : tuple of float (lat, long) destination : tuple of float (lat, long) Returns ------- distance_in_km : float Examples -------- >>> origin = (48.1372, 11.5756) # Munich >>> destination = (52.5186, 13.4083) # Berlin >>> round(distance(origin, destination), 1) 504.2 """ lat1, lon1 = origin lat2, lon2 = destination radius = 6371 # km dlat = math.radians(lat2 - lat1) dlon = math.radians(lon2 - lon1) a = (math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon / 2) * math.sin(dlon / 2)) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) d = radius * c return d if __name__ == '__main__': import doctest doctest.testmod()The other alternative package is haversine
from haversine import haversine, Unit lyon = (45.7597, 4.8422) # (lat, lon) paris = (48.8567, 2.3508) haversine(lyon, paris) >> 392.2172595594006 # in kilometers haversine(lyon, paris, unit=Unit.MILES) >> 243.71201856934454 # in miles # you can also use the string abbreviation for units: haversine(lyon, paris, unit='mi') >> 243.71201856934454 # in miles haversine(lyon, paris, unit=Unit.NAUTICAL_MILES) >> 211.78037755311516 # in nautical milesThey claim to have performance optimization for distances between all points in two vectors
from haversine import haversine_vector, Unit lyon = (45.7597, 4.8422) # (lat, lon) paris = (48.8567, 2.3508) new_york = (40.7033962, -74.2351462) haversine_vector([lyon, lyon], [paris, new_york], Unit.KILOMETERS) >> array([ 392.21725956, 6163.43638211]) Photo by Capturing the human heart. on UnsplashA few months back I was working on a freelance project of visualizing geo-location data(i.e. latitude and longitude) in which I have to visualize central facilities and customers location on a map. As per one of the client’s requirements, I have to find all the customers locations that are within the range of 3 km from different facilities. To do this I have to calculate the distance between all the locations. It was the first time I was working with raw coordinates, so I tried a naive attempt to calculate distance using Euclidean distance, but sooner realized that this approach was wrong.
Euclidean Distance works for the flat surface like a Cartesian plain however, Earth is not flat. So we have to use a special type of formula known as Haversine Distance.
Haversine Distancecan be defined asthe angular distance between two locations on the Earth’s surface.
Haversine distance can be calculated as:
Looks Daunting, yes it would be daunting if you have to apply it using raw python code, but thanks to the python’s vibrant developers community that we have a dedicated library to calculate Haversine distance called haversine(one of the perks of using python).
That’s it with the introduction lets get started with its implementation:
Step 1: Installing “haversine”
To install haversine type following command in jupyter notebook.
!pip install haversineIf you are installing through anaconda prompt remove the “!” mark from the above command.
Step 2: Importing library
After installing the library import it
import haversine as hsStep 3: Calculating distance between two locations
loc1=(28.426846,77.088834)loc2=(28.394231,77.050308)
hs.haversine(loc1,loc2)
Output: 5.229712941541709
By default the haversine function returns distance in km. If you want to change the unit of distance to miles or meters you can use unit parameter of haversine function as shown below:
from haversine import Unit#To calculate distance in meters
hs.haversine(loc1,loc2,unit=Unit.METERS)
Output: 5229.7129415417085
#To calculate distance in mileshs.haversine(loc1,loc2,unit=Unit.MILES)
Output: 3.2495929643035977
Similarly you can calculate distance in inches also.
Calculating distance between two locations is a basic requirement if you are working with raw location data. It not only helps you to visualize better but it also provides an edge to your Machine learning algorithm. It might give an edge to your model and improves its overall efficiency by adding a new dimension “distance”.
This is the output of the project that i mentioned earlier in this article:
If you want to see the whole code you can visit my github page: //github.com/ashutoshb418/Foodies-Visualization