## A guide on how to make a customized commuter’s map of your home city using data science and Google Maps API

Last year I got a job in Massachusetts at the Center for Astrophysics in Cambridge. When looking for nearby apartments, one of my key requirements was that it shouldn’t take too long to commute to work. Since Boston has a fairly built up public transportation system (MBTA) with several subway lines, it was not obvious in which neighborhoods I should even be looking for apartments. So I decided to make a map of the Greater Boston area showing how long it takes to commute to my workplace during morning rush hour. This post details how I made the map and how you can do one for yourself. You can find the source on GitHub.

**Warning**: If you want to do a similar project (i.e., a map for a different destination/city) you have to get your own API key. Google Maps allows you to do a moderate number of calls for free per month, so it is possible to do a project like this for free. However, you must be **very careful**, it is easy to go over the limit and then be charged hundreds of dollars.

## Calculating Commute Times

The first step was to get a map of the Boston area and create a mapping between it and GPS coordinates (i.e., latitude and longitude for each pixel). On this map we can define a grid of points where we will calculate the travel time. To reduce the number of API calls I chose to make my grid points more dense near the center and less dense on the outskirts. Note that Google Maps automatically “snaps” to nearby roads, so we don’t need to worry about whether our grid points are actually on a road.

Once we have the grid, we just need to call Google Maps and use its Distance Matrix API to calculate the travel times to our destination. However, there are a few subtleties to keep in mind when calculating travel times:

- We need to specify the time of the day. Since I would be commuting to my workplace during the morning rush hour I set an arrival time of 9 AM.
- Google Maps can provide travel times for driving, biking and taking public transportation. For my project I picked public transportation only.
- Most forms of public transportation are fairly infrequent in the US, even during rush hour (e.g., buses coming only every 15 minutes). This can introduce an error in our travel time calculation, so to reduce its effects I decided to calculate the travel times for 2 more arrival times (8:45, 8:52) and take the minimum of the three values. This essentially means that I would be willing to come into work a bit sooner if that means not waiting at the bus stop for 20 minutes.

## Commute Time overlaid on City Map

Once we have the commute times for each grid point, we can visualize them on the city map with a filled contour plot.

As expected, commute time increases with distance, but we can also notice anomalies, points physically further being closer by commute time. There are even embedded islands of lower commute times. This is due to the structure of the MBTA network. For example, living near the Kendall/MIT subway station we can get to our destination within 30 min, but if we lived several streets closer to our destination we would need to take a bus and would get there later. There are also small islands from where it is not possible to reach our destination (e.g., train repair center in East Cambridge).

## Commute Distance of Boston Neighborhoods

While this map is helpful, it would be better to have something we could use to filter results from apartment listing websites. Most of these sites list which neighborhood each apartment belongs to, allowing us to filter for it. So, it would make sense for us to translate our commute time map into a map of neighborhoods. First, let’s just make a map of the Boston neighborhoods.

We could try to draw a map of the Boston neighborhoods where the distance of any point to my workplace (the new origin) is proportional to the commute time (instead of the physical distance). We can accomplish this by changing the distance of each pixel relative of the origin to be proportional to the commute time, while keeping their relative direction the same.

`phi = np.arctan2(y_image,x_image)`

x_new = commute_time * np.cos(phi)

y_new = commute_time * np.sin(phi)

This will distort the image, and lead to uneven pixel sizes (i.e., some pixels will be crowding each other, some will have gaps between them). We can correct for that by doing a Voronoi diagram and coloring the resulting cells according the the color of the corresponding pixel.

`from scipy.spatial import Voronoi, voronoi_plot_2d`

vor = Voronoi(np.vstack((x_new,y_new)).T)

voronoi_plot_2d(vor,ax=ax,show_points=False,show_vertices=False,line_width=0.0)

...

#Colorize the Voronoi plot

for i,region in enumerate(vor.regions):

color = ...

polygon = [vor.vertices[k] for k in region]

plt.fill(*zip(*polygon),c=color)

With this map we can quickly see which neighborhoods are close enough for us to commute from. Note that some areas in adjacent neighborhoods get mixed (e.g., Downtown area). This is due to the presence of high speed public transportation (e.g., subway), which makes it faster to commute from a station one stop further than from an apartment that is a 5 minute walk from the current station.

This project began with me looking for an apartment from which my commute wouldn’t be too long(< 45 min). This means I should mainly be looking for apartments in Cambridge, Somerville, Belmont, Arlington, Allston, Watertown and the Downtown area. Of course there are other considerations when buying an apartment (rent, noise etc.). In the end I rented an apartment near the boundary of Cambridge and Arlington.

If you want to try to do the same for your city, check out the source on GitHub.