Previous articles in this series:
- 1. Motivations and Methods
- 2. Obtaining OpenStreetMap data
- 3. Manipulating geodata with GeoPandas
- 4. Cleaning text data with fuzzywuzzy
- 5. Building a street name classifier with scikit-learn
- 6. Adding features with Pipelines and Feature Unions
A web map in two lines of Python
Here’s how to make a map from a GeoPandas GeoDataFrame in one step:
where classification
was the name of the column with our new
Malay/Chinese/British/Indian/Generic/Other labels on each road (row).
What if we want to make this nice and interactive, like a Leaflet map? So we can pan and zoom and actually see street names? There’s a library called mplleaflet, by Jake Wasserman, that can do this for you:
(If you don’t see colours on that map, just reload the page.)
To export it to an HTML page, you can do this:
We don’t have much control over colours here, but it would be nice to theme them, associating Chinese with its traditional red, Malay with its traditional green, etc. Here’s a hacky way to do it:
Alternatives
mplleaflet
is awesome for exploratory data analysis, but you might want to have more control over how your map looks. For this, I recommend using one of the following:
- QGIS (C++ but has Python bindings)
- Mapnik (C++ but has Python bindings)
- Tilemill (GUI built on top of Mapnik)
- Folium (maybe, haven’t investigated fully)
A nice feature of Tilemill is that it allows you to define your map styling using CartoCSS. For example, here’s how we would define the colours:
You can also control the line width at various zoom levels:
If these are too fiddly, some web mapping solutions also use CartoCSS. I really like CartoDB, which is how I made my main map:
We can browse this map to look at clusters of street names, which are now conveniently colour-coded for our analysis!
Conclusion
It’s remarkably easy to make maps with GeoPandas and ancillary libraries like mplleaflet
, thanks to the developers of these libraries :)
That’s all the technical stuff in this series. Next time, I’ll round everything off and talk about what I learned about Singapore street names from doing this project.