Difference between revisions of "Network Analysis in Python"

From CUOSGwiki
Jump to navigationJump to search
Line 8: Line 8:
   
 
=Importing OSM Data=
 
=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
  +
  +
  +
<pre>
  +
# 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)
  +
<pre>
   
 
=Simple Routing: Shortest Path=
 
=Simple Routing: Shortest Path=

Revision as of 11:31, 17 December 2025

Introduction

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