Driving Distance to Office Locations

For my first post on my blog, I wanted to come up with a practical project that has real world applications.  My initial goal is to improve my coding skills in both Python as I am relatively new to the language. 

 

I am from Maryland, moved recently to Virginia, and was recently looking at apartments in the DMV area. My company has four offices in the DMV and was curious how far of a drive it was to them as I can go to any office location for my role.  If I am going to be moving, I want an easy and convenient commute. I also thought about it from my companies perspective, on how our employee's office locations should be structured based on their location and job.  So that's how I got to my initial question: 

 

How can you find the distance between a given zip code and a given office location? 

 

I wanted to take a list of locations where I was am considering moving and turn it into a list of zip codes with added columns for distances to all four offices. With one zip code, it's easy. I can just put them into google maps and find what office is easiest to commute to. If you have hundreds of zip codes, though, it might be more useful to do this with code. I thought it was an interesting question so I decided to give it a try.

 

 

After doing some research on how to accomplish this task, everything seemed to come back to Google.  They have many different APIs for accomplishing different tasks with Maps. 

 

In order to use these different APIs, Google makes you to create an API key. It is free unless used thousands of times. 

I decided to use the Distance Matrix API, which finds the distances between addresses. Since google supports using zip codes in the API, all I had to do was create a csv file with all desired zip codes. However, some of the zip code Data I found was not fully supported by google, so I had to improvise and use LAT/LONG in some cases.

I installed the google maps package using "pip install -U google maps", and then it was time to write the code. 

 

I started by Importing the google maps API package and linking my API key. 

 

Next I needed to find a way to calculate the distance from each zip code to the four office locations in the area.  I plugged in the data with the distance metric API, but it returned a weird dict object.  To find the actual distance just required a little hunting through this object and retrieving rows, elements, distance, and value. Then I divided by 1609.34 to get the distance in Miles instead of Meters. 

Finally, with my csv file of zip codes, i created a nested list called zips and appended the distances calculated above to the file. 

Now I have an ouput that give me the driving distance in miles to each office location from the center of the zip code

 

This was a good straight forward first project for me to get back into Python and the Distance Matrix API. I have taken some computer science classes in college and know the basic of Python, but I am still very much learning. I think this project is useful as I can easily tell what the shortest driving distance is to each office from each zip code.  For my next blog post, I will be building a visual in python to display the data I calculated here. 

Add comment

Comments

There are no comments yet.