When we think of maps, one word which will hit us first is ‘location’. You may wonder how our exact location is being fetched by some of the apps like Google Maps.
One more thing is, how the exact distance is being measured between two locations. For all these questions, we got the one-word answer – ‘Geocodes’. The geocodes, in simple words are the geographical coordinates concerning a particular location.
Well, today, we will be focusing on how we can measure the distance between two locations using Geocodes in python!
What are Geocodes and Geocoding
- Geocodes are nothing but a set of latitude and longitude co-ordinates. And the word geocoding is a process of transforming those co-ordinates into a physical location.
- They are basically 4 types of geocoding –
- ZIP + 4
- Parcel centroid
- You may ask, how accurate is this process of accessing a location based on the geocodes. It is basically depends on the type of geocoding. The Roof top level geocodes are the ones which has high price and high precision as well.
- The geocoding is not a one way process. You can also transform a physical location into geocodes (lat and long coordinates).
I hope now you got some understanding about the geocodes and the geocoding process. Now, let’s see how we can extract distance between two locations.
- The haversine distance is also called as Great circle distance.
- It is used to calculate the distance between two points in a sphere using their geocodes (set of latitudes and longitudes).
- Python offers
"haversine"package to calculate the distance between two locations based on their geocodes.
- Using this package you can pass the coordinates of two locations to get the distance between them.
- You can even set the units as km, miles, meter and more.
- But, by default the haversine function will set the units as km (kilometer).
Install Haversine in Python
To install the haversine module in python, run the below code.
# Install haversine module pip install haversine
#Load the library import haversine as hs
Well, we have installed and loaded the haversine packages in python. I hope you are following me on this.
Measuring Haversine Distance Using Geocodes in Python
Now, we have our package ready and also we are aware of how haversine / great circle distance works. If you are excited about the formula which helps in measuring the haversine distance, you can find it below –
Now, we can get out hands dirty with some code to calculate haversine. In this example, we are measuring the distance between Bangalore and Delhi.
First, you need to collect the coordinates of these two locations. You can search for coordinates on google. You will get it easily.
For your reference –
Latitude and Longitude of Delhi – (28.7041 , 77.1025)
Latitude and Longitude of Bangalore – (12.9716, 77.5946)
#Example 1 #import the library import haversine as hs #set the coordinates / geocodes location_Delhi = (28.7041, 77.1025) location_Bangalore = (12.9716, 77.5946) #calculate the distance h_distance = hs.haversine(location_Delhi, location_Bangalore) #Print the result with a message print('The distance between Delhi and Bangalore is -', round(h_distance,2),'km')
- In the above code, we have imported the haversine library.
- Set the coordinates (geocodes)
- Using haversine function, we have calculated the distance.
- Used round function to limit the decimal numbers.
- Finally, printed the distance with a message.
As I said earlier, by default the haversine function keeps the result in kilometer unit. But, you can change it by passing the unit argument to the function. You can get the result in miles, meters, and even inches as well.
For this, you have to import the unit module from the haversine package as shown below.
#Import units from haversine from haversine import Unit #output in miles distance_in_miles = hs.haversine(location_Delhi, location_Bangalore, unit = Unit.MILES) #Print the output with 2 decimal points print('The distance between Delhi and Bangalore is -', round(distance_in_miles,2),'Miles')
- We have imported unit module from the haversine package as unit.
- The process will be same for distance measuring.
- Passed the unit argument as unit.MILES to get the distance in miles.
- Printed the output distance with two decimal points.
All smiles 🙂
That’s it. We could have even tried by passing inches as an argument but I don’t think it makes sense :P. But based on the use case, you can use various units as shown.
Note: You can cross-check the result using Google Maps. The bus route/by road route can show different results as we cannot have straight roads. but flight distance will be almost 95% accurate with this output.
Geocodes in Python and Haversine Distance – CONCLUSION
Well, python has plenty to offer and it is up to us to explore and dig deep to witness cool stuff. The mission of this tutorial is to introduce some amazing concepts which you might lose or never came across in your journey of becoming a Pythonista!
As I explained, it is very easy to measure the distance between two locations using the haversine and geocodes in python. I hope you love this as much as I do. That’s all for now!
See you next time, Happy Python!!!
More read: Haversine distance / Great circle distance