Create Your Own City Street Map in Python

Max van Haastrecht
4 min readSep 19, 2021

--

London street map. Source: https://www.amazon.com/England-Cityscape-UNFRAMED-Artwork-Entryway/dp/B081Y4JFMR

Ever see a street map like this one of London for sale? And ever think: “Cool, but I want one of my own city”? I’m here to tell you there’s no need to wait until these creators pick your city for one of their projects. You can just make one on your own!

Prerequisites

What do you need to get started? Well, not much actually. You will need to be somewhat familiar with Python (notebooks) so you can edit the code to make it work for you. Additionally, you will need to install the following packages:

… and that’s it. Well, that’s not completely fair. If you want to make some nice custom plots, you may also want to use some plotting library such as Matplotlib. However, Geopandas facilitates plotting, so it’s not necessary.

Additionally, you’ll need to have files describing the roads you’re interested in. To be able to plot roads on a map, we first need to have some data representing these roads. But what type of data?

That’s where GIS comes in. GIS stands for Geographic Information System, which is a framework for geographic data capture and analysis. When we are looking to describe roads, we are specifically interested in ‘.shp’ (shape) and the corresponding ‘.shx’ files. These files provide a spatial description of roads, and can be easily parsed using Geopandas.

So where can you find these files? Personally, I used MapCruzin, but there are many other sites that make shape files accessible.

Creating a City Street Map

Before we can get started, we need to download the shape files we are interested in. I live in Utrecht in The Netherlands, so I wanted to make a city street map of Utrecht (very egocentric, I know). To get started, I downloaded the shape files of the Dutch road network from MapCruzin. I stored the required files as ‘roads.shp’ and ‘roads.shx’ in the directory of my Python notebook (for simplicity).

The code below shows how you can import the shape file. You also need to have the .shx file in the same directory, you just don’t load it directly using Geopandas.

Then, you need to find the coordinates of the city you’re interested. This can be done quite easily using, for example, Google Maps. You can see the coordinates for the city I was interested in: Utrecht.

The code then plots the selection of roads. You might want to play around with the colouring scheme a bit. For Matplotlib, you can find some of the options here. I ended up using ‘Wistia’ for Utrecht, the result of which you can see below. Pretty cool right?

Result of plotting Utrecht streets. Pretty cool right?

Grid of Cities

Grid of city street maps. Source: https://www.mentalfloss.com/article/92200/what-square-mile-citys-grid-looks-around-world

Maybe just one city is not enough for you. Maybe you have lived in several cities, or you just want to plot some cities with nice street layouts. If you, like me, downloaded the shape files for a complete country, it’s pretty easy to change around the coordinates within the country to produce some new city images.

For The Netherlands, let’s try Amsterdam, Rotterdam, and The Hague as additions to Utrecht. Following the same process as before, we can create a few new images. For the details on coordinates and colouring schemes, check out the detailed code on my Github page. When you have your new images, the code below will help you to create a 2 by 2 grid of city street map images.

You can see the final result below. Pretty impressive for a few lines of code, right? Nevertheless, I’m certainly not the most creative person, so I’m sure you’ll be able to come up with much more beautiful colour and city combinations. So go out and explore!

The final result showing Amsterdam, The Hague, Rotterdam, and Utrecht.

--

--

Max van Haastrecht

I'm a PhD candidate in Cybersecurity at Leiden University. I'm a bit of a statistics geek and love seeing the cool things people can do with code.