Interactive Python Map

After my first blog post where I calculated the driving distance to four office locations using Python, I decided it would be a good idea to extend the project and build an interactive visual to display the data.  I settled on creating a map in Python.  The idea was to create a map that contained data points of every zip code in the first blog, the four office locations, and some way to display the driving distance I previously calculated. 

 

My first step was creating a csv file that could display the information I wanted. I had the file with zip code and driving distances, but I needed the latitude/longitude to plot points on the map. The distance.matrix API allowed me to simply plug in the Zip code and get a driving distance, but when plotting points on a map I need the corresponding lat/long. Thankfully this was relatively simple, as there is public data of each zip codes lat/long.  This gave me a csv file looking like this: 

I started writing code in Python by using Pandas to create a data frame and decided on using Folium for the map. I wanted this map to be interactive, so I decided a search feature would be useful.  This way I could plug in a zip code and see the driving distance I calculated to each office without having to manually look around the map.  Thankfully, a Folium plug in exists called search: 

Next, I used pd.read_csv to read my file into a data frame 

Now that i had my data frame with my column labels, I could go about creating my map.  I zoomed the map to the average location of my lat/longs, and then manually placed my four office locations on the map with home icons to differentiate between zip codes. 

Now that I had my base map with my office locations, I needed to plot each zip code.  My first instinct was to use itertuples to iterate over my data frame rows as tuples, so i could use my lat/long columns together. 

There were problems with this, however.  First, all of my zip codes came up on my packed very closely together, creating a cluttered view.  Second, I needed my pop up on each zip code to display not only my zip, but the driving distance to each office. 

My solution to the popup problem was to use range(len.  To display the pop-up in tabular form at each location, a loop process is performed on each row of the data frame, converting one row from a series to a data frame, and then transposing it. I also adjusted the width of the data frame. This took me a bit of time to figure out, but I finally got it working how I wanted. 

To solve the clutter problem, Imported the marker cluster plugin and added my data points to the marker_cluster layer, grouping my data points together. Finally, I added the search box to filter on the marker_cluster layer.  

My final map looked like this: 

When I type in a zip code, it circles that location like this 

And when I zoom in and hover over that data point, I can see the driving distance in miles to all four office locations

Add comment

Comments

There are no comments yet.