Difference between revisions of "Network Analysis in Python"

From CUOSGwiki
Jump to navigationJump to search
m
Line 38: Line 38:
   
 
''NetworkX, Geopandas, Pandas, and Numpy are all dependencies of OSMnx and will also be installed. Enter "y" for yes if prompted''
 
''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:
  +
<pre>
  +
!pip install osmnx
  +
</pre>
  +
''NetworkX, Geopandas, Pandas, and Numpy are all dependencies of OSMnx and will also be installed.''
   
 
=Review of Graphs as Data Structures=
 
=Review of Graphs as Data Structures=

Revision as of 04:16, 18 December 2025

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.

Review of Graphs as Data Structures

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