Extracting street information from OpenStreetMap with Python

We spend a lot of time working with maps as part of data projects we’re working on, which is why I was excited to discover a new map tool yesterday. OSMnx is a fantastic new Python package that makes it really easy to extract boundary shapes and street data from OpenStreetMap. OSMnx is being developed by Geoff Boeing as part of his PhD at UC Berkley and it really does live up to the promise of making extracting this data as simple as possible, so long as you know a little Python.

Geoff has written up an excellent introduction to OSMnx is worth reading to get a quick insight into OSMnx’s potential.

OSMnx can extract boundary data (such as countries, provinces/states, and cities) as well as street data. In the two examples below I pulled the Gauteng province boundary data and the combined data for South Africa and Botswana. The code for the Gauteng boundary data is just three lines long:

import osmnx as ox
place = ox.gdf_from_place('Gauteng')

These are PNG versions of the results but OSMnx can also save the results to SVG, shapefiles and GraphML.

Extracting boundary data is pretty cool but OSMnx can do a whole lot more that just this. In particular OSMnx can extract street data. The area that you want to get data for can be specified in one of many different ways. For example you could specify the area you want by giving OSMnx bounding latitudes and longitudes. You could also specify a place by LatLon or by its address and request an area around that to be returned. In the image below I gave OSMnx a latitude-longitude point in Johannesburg and then requested the area 5km around that, which makes for a pretty cool image. This one I did as an SVG and did some basic adjustments in Sketch.



The shapefile versions of the maps you create using OSMnx include basic data like road names, LatLon properties as well as information on one-way roads which opens up a whole lot of potential. The street networks returned can also be limited to driving, cycling and walking routes among others.

One of the more fun things todo with OSMnx is to construct 1-mile maps for cities/areas. These are 1 square mile road maps that can often give interesting insights into how different cities are built. Geoff has some great examples on his site, but for fun I made a few for places of interest around South Africa:

Alexandra, Gauteng
Bisho, Eastern Cape
Cape Town, Western Cape
Diepsloot, Gauteng
Durban KwaZulu-Natal
Johannesburg, Gauteng
Khayelitsha, Western Cape
Kimberley, Northern Cape
Polokwane, Limpopo
Port Elizabeth, Eastern Cape
Pretoria, Gauteng
Sandton, Gauteng





















































If you’re interested in the more technical aspects of OSMnx you can download Geoff’s paper on the subject. Also read Geoff’s blog post introducing OSMnx which gives some idea of the potential for this tool beyond what I’ve briefly touched on here.

Leave a Reply

Your email address will not be published. Required fields are marked *