Difference between revisions of "Network Analysis in Python"

From CUOSGwiki
Jump to navigationJump to search
Line 1: Line 1:
 
=Introduction=
 
=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.
   
 
=Outline=
 
=Outline=

Revision as of 04:04, 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.

Outline

Setup

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