OSMnx Part 1. Loading data from Open Street Maps and finding shortest route between two points

Maksym Kozlenko 🇺🇦
4 min readOct 16, 2021

Open Street Map is a collaborative project to create a free editable geographic database of the world. It contains information about roads, houses, train stations or any other feature you would expect to see on map. Most exciting is that this data can be loaded and processed.

On Open street map page with “Query features tool” you can select road get features located at this location. Upon clicking on feature, you can see which properties are defined (see screenshot below). Road has attributes as as number of lanes, maximum speed, directions, name, etc.

This information can be accessed using Python and OSMnx library.
First install required packages:

pip install geopandas tqdm osmnx matplotlib==3.1.3

Loading data from OSM

Since loading large map areas will take time, I will load map data for a single area in Sydney. You can see its boundary in OpenStreetMap.

Loading road network and displaying. Network type “drive” will load road accessible to cars. Other types available: “all_private”, “all”, “bike”, “drive”, “drive_service”, “walk”. G object is an instance of MultiDiGraph which is a directed graph type from NetworkX package.

Loaded road network for geographical area

Exploring network graph

This network contains nodes (road intersections) shown in red and edges connecting it to other nodes shown in grey. Direction graph is used since some streets can be one way streets.

How many nodes and edges we have?

print("node count:", len(G.nodes()))
print("edge count:", len(G.edges()))
# --> node count: 5510
# --> edge count: 12308

We can explore data by picking nearest node to coordinates 34.06714 S, 151.014898 E which is an intersection of Station Street and Princes Highway (a.k.a Railway Parade):

Point with coordinates (-34.06714, 151.014898)
Singe node of directional graph and its edges connecting it to other nodes

This node is connected to two other nodes. One goes to roundabout on Station Street (to node 268185063) and another is left turn to Railway Parade (ID 268185063). It is impossible to turn right into highway from this intersection.

To get information about neighbouring nodes, edges and its attributes:

Edge attributes contain street name, maximum speed, directions, length and other parameters:

Finding shortest route

With maximum speed and length of edge in meters we can calculate how long it would take to drive along this edge (road segment) at maximum allowed speed in seconds.

# impute edge (driving) speeds and calculate edge traversal times
G = ox.add_edge_speeds(G)
G = ox.add_edge_travel_times(G)

Now new attribute “travel_time” was added:

travel_time attribute shows time in seconds

Now we can calculate shortest route between two points by driving and walking. Calculated routes is shown in red below code.

As you can see, while two points located nearby, driving route is much longer compared to walking route, since there is no direct road open to traffic.

Shortest route, network_type=’driving’
Shortest route, network_type=’walk’

To calculate routes lengths and travel times:

# by driving
Total route length in km: 16.786728
Travel time in minutes: 16.895000000000003
# by walking
Total route length in km: 3.168510000000001
Travel time in minutes: 4.194999999999999

Summary

In this part we loaded road network data from Open Street Map and explored its nodes and edges, calculated shortest route between two nodes got route length and travel time.

OSMnx Part 2. Creating driving time heatmap.

Complete code example to run in Google Colab
(please restart runtime after packages installed using pip)

Complete source code in GitHub

--

--