Network Analysis in Python

From CUOSGwiki
Jump to navigationJump to search

Introduction

Summary

This tutorial explores some basic network analysis in Python using the OSMnx and NetworkX packages.

First, a road network for Ottawa, Ontario is imported from OpenStreetMap (OSM) with OSMnx. The Nominatim API is used for geocoding - no external data download required. Then, shortest path examples are shown, first distance-based, then based on travel time. The incomplete nature of OSM datasets is explored in this section. Additional features such as creating *n* number of shortest paths, comparing routes, plotting with OSMnx, and exporting a route to a shapefile are shown. Finally, a basic example of the Travelling Salesman Problem (TSP) is demonstrated.

This tutorial expects that the reader has at least a basic knowledge of Python.


Setup

Instructions for setting up a Jupyter Notebook with Anaconda and Google Colab are shown below. The only differences between the methods are found when installing the packages, exporting data, and the fact that geocoding seems to take somewhat longer with Google Colab.

Anaconda

A Jupyter Notebook of this tutorial is planned to be provided in the future.

Download Anaconda here

Setup your Conda environment with the required packages by entering the following commands into Anaconda Prompt:

Create an environment

conda create -n network_analysis

Activate the environment

conda activate network_analysis

Install required packages

conda install -c conda-forge jupyter notebook osmnx

NetworkX, Geopandas, Pandas, and Numpy are all dependencies of OSMnx and will also be installed. Enter "y" for yes if prompted


Google Colab

Add the following to the start of your notebook:

!pip install osmnx

NetworkX, Geopandas, Pandas, and Numpy are all dependencies of OSMnx and will also be installed.

Quick Review of Networks and Graphs

Networks are abstract structures that represent relationships between objects. Their applications vary widely, from social network analysis to neural networks in the brain, transportation planning, and stream network analysis. Graphs are used to represent and analyze networks mathematically.

Graphs are composed of two parts:

Nodes -> things or objects

road intersections

Edges -> links or connections between nodes

road segments

Edges can be directed or undirected (single or bi-directional).

Directed edges are useful for analysing stream networks, or in this tutorial for highways and one-way streets.

The study of the arrangement of nodes and edges in relation to each other, as well as in physical space, is known as Topology.

For more information, check out the Open Geomatics Textbook here.

Data Structure

Graphs, DiGraphs, MultiGraphs, and MultiDiGraphs are the data structures used by packages like OSMnx and Networkx.

Graphs and DiGraphs represent simple undirected and directed graphs respectively. "Multi-" means that multiple parallel edges are allowed, like multiple lanes on a highway.
Note: "parallel" in graph theory means that two edges connect Node A to Node B. This does not apply to bi-directional streets, since one connects A to B, and the other connects B to A, even though we would consider those lanes to be geometrically parallel.

The data imported in this tutorial is by default a MultiDiGraph, which suits the purposes of this tutorial.

For more information on the different graph data types, see the NetworkX documentation here.

Importing OSM Data

To explore some of the network analysis options in Python, this tutorial will use the road network of Ottawa, Ontario. This can easily be acquired using the OSMnx package, which uses Overpass API. The graph_from_place() function geocodes the area of interest (AOI) query and filters the network to the City of Ottawa's boundaries, while graph_from_polygon works with a custom polygon.


For additional information, check out the OSMnx documentation:

https://osmnx.readthedocs.io/en/stable/user-reference.html


and the example gallery:

https://github.com/gboeing/osmnx-examples/blob/main/notebooks/00-osmnx-features-demo.ipynb


# create query for Smiths Falls
AOI = "Ottawa, Ontario, Canada"

# retrieve road network -> this may take some time
graph = osmnx.graph_from_place(
    AOI,
    network_type="drive" # this selects roads only. Other network options include "bike" and "walk".
)

# visualise the road network
figure, ax = osmnx.plot_graph(graph)

Simple Routing: Shortest Path

1. Distance-Based 2. Time-Based 3. Route Comparison

Generating Simple Directions for a Route

Multiple Shortest Routes

Exporting Routes

Complex Routing: Travelling Salesman Problem