From Road Network to Graph

This section contains a non-exhaustive list of operations on geospatial data that you should familiarize yourself with. More information can be found by consulting the Tools and Python Libraries page or the respective libary’s API documentation.

Creating a Graph from a named place

Mathematically speaking, a graph can be represented by \(G\), where


For a graph \(G\), vertices are represented by \(V\), and edges by \(E\).

Each edge is a tuple \((v,w)\), where

\(w\), \(v \in V\)

Weight can be added as a third component to the edge tuple.

In other words, graphs consist of 3 sets:

  • vertices/nodes

  • edges

  • a set representing relations between vertices and edges

The nodes represent intersections, and the edges represent the roads themselves. A route is a sequence of edges connecting the origin node to the destination node.

osmnx can convert a text descriptor of a place into a networkx graph. Let’s use the University of Toronto as an example:

import osmnx

place_name = "University of Toronto"

# networkx graph of the named place
graph = osmnx.graph_from_address(place_name)

# Plot the graphs
(<Figure size 720x720 with 1 Axes>, <AxesSubplot:>)

The graph shows edges and nodes of the road network surrouding the University of Toronto’s St. George (downtown Toronto) campus. While it may look visually interesting, it extends a bit too far off campus, and lacks the context of the street names and other geographic features. Let’s restrict the scope of the network to 500 meters around the university, and use a folium map as a baselayer. We will discuss more about folium later in this section.

graph = osmnx.graph_from_address(place_name, dist=300)
Make this Notebook Trusted to load map: File -> Trust Notebook