All The Places We'll Go

When I first started learning to code, one of the hardest aspects was figuring out how to apply the syntax I was learning to real world problems. For example, understanding the uses of JavaScript was pretty much impossible for me until I downloaded Sublime Text, created an html file within it, and could thereby see the code I'd written live in my Chrome browser. Unfortunately, my journey with JavaScript stopped there. But, I have higher hopes for my understanding of Python.

The easiest way to start playing around with Python is to download Anaconda. Within Anaconda, there is a program called Jupyter Notebook that will allow you to execute any Python code you so choose and save your results. Every time you want to experience the results of your Python code within Jupyter Notebook, simply press shift+Enter when you're done writing.

Diving In

Seeing as none of us is going very far, I often like to live vicariously through the geocoding capabilities of my favorite programming languages. Fortunately, these simple lines of code provide a wonderful segue into familiarizing yourself with the languages and accompanying software.

Installing the Necessary Packages

In order to calculate the distance from one place to another in Python, you will need to install a package called Geopy.  Unfortunately, you can't do this simply as a command in Jupyter Notebook.  You must open Anaconda Prompt (included when you download Anaconda) and do it there using the code: 

pip install geopy 

Once geopy is downloaded, you will be able to access its libraries within Jupyter Notebook. 

Retrieving X and Y Coordinates

The rest of this code is pretty simple, but you will need to know the x,y coordinates for the addresses between which you are measuring the distance. 

from geopy.geocoders import Nominatim 
geolocator = Nominatim()
location = geolocator.geocode("345 Chambers Street New York") 
print((location.latitude, location.longitude)) 

This code will return the coordinates (40.7179876, -71.013842044) for this address.  You can repeat the same code for the second address as follows: 

from geopy.geocoders import Nominatim 
geolocator = Nominatim()
location = geolocator.geocode("3014 Dauphine Street New Orleans") 
print((location.latitude, location.longitude)) 

The code will return the coordinates (29.9646279, -74.013842044) for the second address. 

If you want to double check the generated x,y coordinates, you can plug them into the following code: 

location = geolocator.reverse("29.9646279, -90.0465397") 
print(location.address) 

Finding the Distance

Now that you have your coordinates, you can find the distance between these two places using the following code:

from geopy.distance import geodesic
Stuyvesant_High = (40.7179876, -74.013842044)
The_Warehouse = (29.9646279, -90.0465397)
print(geodesic(Stuyvesant_High, The_Warehouse).miles)

I now know that the distance between the high school I attended in New York and the co-working space at which I current work is 1167 miles. 

Thirsty for more content about Python?

Support my content’s expansion into more languages by becoming a patron of DiKayo Data on Patreon!

Danielle Oberdier