Mapping messy addresses part 2: insights from Folium
By: Mark Ryan
Tags: Geocoding, Mapping, Toronto, Data Visualization, Folium

Toronto streetcar delay heat map

In the first article of this series I described the Toronto streetcar delay dataset and went through the steps required to get from the original dataset with imprecise, messy location values:

To a dataframe with latitude and longitude values for all the locations:

In this article I’ll review the steps I took to get map visualizations of the delay data using the latitude and longitude values from the first article.

Mapping Take 1: Pixiedust

For my first attempt at visualizing the dataset including latitude and longitude I decided to use Pixiedust.

After installing and importing Pixiedust, display the dataframe:

With the keys latitude and longitude and values Min Delay:

Selecting Simple Cluster style I get the following map:

Some streetcar delay locations are obviously not correct

Some observations from the Pixiedust visualization:

The values in the Atlantic Ocean south of Ghana are at the intersection of the prime meridian and the equator (latitude / longitude = 0.0/0.0). These are locations where the Geocoding API returned an empty list and the get_geocode_result function described in the previous article returned a placeholder value.

function described in the previous article returned a placeholder value. Of the 10 k locations mapped, over 9.7 k of them are in the Toronto area and are potentially valid sites for a streetcar delay.

In addition to the 9.7 k locations in the Toronto area and the 238 values at [0.0,0.0] , there are 43 locations in other parts of the world which obviously do not correspond with valid sites for streetcar delays.

This “Toronto” streetcar is in San Francisco, but the dataset only covers Toronto (photo by author)

With that I had basically exhausted what I could get with Pixiedust. While it didn’t give me everything I wanted, Pixiedust did provide some very rapid insights that helped save time later.

Mapping Take 2: Folium

After exhausting what I could get from Pixiedust, I turned to Folium, a library that makes it easy to take advantage of the facilities of the leaflet.js library in the context of Python.

The Pixiedust visualization showed that there were values outside of the legitimate delay locations on the streetcar network. To prepare for using Folium I removed records with locations outside of the boundaries of the streetcar network. This reduced the size of the dataset by 6.5% to 65 k records.

Bounding box for valid streetcar delays

You may wonder why the street grid isn’t aligned with bounding box. The Toronto street grid is roughly perpendicular to the Lake Ontario shoreline in the city centre, so the grid is twisted to the left relative to the bounding box defined by latitude and longitude.

Back to the code — now we’re ready to install and import Folium:

Then we can create a simple map that displays clusters of delays for the first 2.5 k records in the dataset:

And here is the resulting map, which gives us an idea of the relative concentration of delays in different parts of the city:

We can zoom into the western downtown area. Note the hotspot on Queen Street West between Bathurst and Ossington, indicated by the count just south of Trinity Bellwoods Park.

Zooming in to take a closer look at the hotspot, it’s clear that Queen and Strachan, just south of the park, is a magnet for delays. In this sample, 7% of all delays citywide occur at this one intersection.

Now that we’ve looked at clusters, let’s adapt the code for the cluster view to show a heat map view of delay durations:

Here’s what the whole system looks like:

And if we zoom into the central part of downtown, concentrations of delays are evident, including three distinct hotspots near the lake where Bathurst, Spadina and Bay meet Queen’s Quay.

Conclusion

Through the preceding article and this one, I’ve described the journey from messy free-format addresses, to latitude and longitude, to useful map visualizations with Folium. These visualizations provide insights into the data, such as the locations of streetcar delay hotspots, that would be extremely difficult to extract from the original dataset.

If you would like explore the code described in this article: