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

Last yr I got a job in Massachusetts on the Center for Astrophysics in Cambridge. When searching for nearby apartments, one in all my key requirements was that it shouldn’t take too long to commute to work. Since Boston has a reasonably built up public transportation system (MBTA) with several subway lines, it was not obvious by which neighborhoods I should even be searching for apartments. So I made a decision 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 the way you’ll be able to do one for yourself. You could find the source on GitHub.

**Warning**: If you wish to do an analogous project (i.e., a map for a distinct destination/city) you’ve got to get your personal API key. Google Maps permits you to do a moderate variety of calls without spending a dime per thirty days, so it is feasible to do a project like this without spending a dime. Nevertheless, you have to be **very careful**, it is simple to go over the limit after which be charged lots of of dollars.

## Calculating Commute Times

Step one was to get a map of the Boston area and create a mapping between it and GPS coordinates (i.e., latitude and longitude for every pixel). On this map we will define a grid of points where we’ll calculate the travel time. To cut back the variety of API calls I selected to make my grid points more dense near the middle and fewer dense on the outskirts. Note that Google Maps robotically “snaps” to nearby roads, so we don’t must worry about whether our grid points are literally on a road.

Once we now have the grid, we just must call Google Maps and use its Distance Matrix API to calculate the travel times to our destination. Nevertheless, there are just a few subtleties to take note when calculating travel times:

- We want to specify the time of the day. Since I could be commuting to my workplace through 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 types of public transportation are fairly infrequent within the US, even during rush hour (e.g., buses coming only every quarter-hour). This may introduce an error in our travel time calculation, so to scale back its effects I made a decision to calculate the travel times for two more arrival times (8:45, 8:52) and take the minimum of the three values. This essentially implies that I could be willing to return into work a bit sooner if which means not waiting on the bus stop for 20 minutes.

## Commute Time overlaid on City Map

Once we now have the commute times for every grid point, we will visualize them on town map with a filled contour plot.

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

## Commute Distance of Boston Neighborhoods

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

We could try to attract a map of the Boston neighborhoods where the space of any point to my workplace (the brand new origin) is proportional to the commute time (as an alternative of the physical distance). We will accomplish this by changing the space of every pixel relative of the origin to be proportional to the commute time, while keeping their relative direction the identical.

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

x_new = commute_time * np.cos(phi)

y_new = commute_time * np.sin(phi)

It will distort the image, and result in uneven pixel sizes (i.e., some pixels shall be crowding one another, some could have gaps between them). We will correct for that by doing a Voronoi diagram and coloring the resulting cells according the the colour 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 will quickly see which neighborhoods are close enough for us to commute from. Note that some areas in adjoining neighborhoods get mixed (e.g., Downtown area). That is on account of 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 could be a 5 minute walk from the present station.

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

If you wish to attempt to do the identical in your city, take a look at the source on GitHub.