Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Fixing your road network graph

Note: This notebook includes processes covered in the Step 2 Tutorial Notebook.

The following notebook showcases some common practices to inspect the quality of your road-network graph, and possibilities to improve it.

Import Libraries and Load Data

We will start by importing the necessary libraries and loading the data.

Import Libraries

We will use the following libraries:

  • os to set the working directory and handle file paths

  • networkx to handle the graph data structure

  • osmnx to plot the network graph

  • pickle to load the network data

  • matplotlib to help plot and format the graph

  • GOSTnets to access custom GOSTnets functionality

import os
import networkx as nx
import osmnx as ox
import pickle as pkl
import matplotlib.pyplot as plt

# import the GOSTnets library
import GOSTnets as gn

Load Data

Define the path to the tutorial data and then load the network.

pth = "./"  # change this path to your working folder
data_pth = os.path.join(pth, "tutorial_outputs")

# read back your graph from step 2 from you saved pickle
G = pkl.load(open(os.path.join(data_pth, "iceland_network_clean.pickle"), "rb"))

Graph processing

In this notebook the first processing step we take is to load the largest subgraph.

Identify the largest subgraph

We will start by identifying the largest subgraph in the network.

# note the use of sorted to sort by number of edges
list_of_subgraphs = [
    G.subgraph(c).copy()
    for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)
]
G_largest = list_of_subgraphs[0]
print(G_largest)

Plot the largest subgraph

We will then plot the largest subgraph to visualize the network.

# plotting functions only work if the graphs have a name and a crs attribute
G_largest.graph["crs"] = "epsg:32646"
G_largest.graph["name"] = "Iceland"

fig, ax = plt.subplots(figsize=(10, 14))
ax.set_facecolor("k")
ax.set_title("Iceland - The Largest Network")
# largest connected subgraph
fig, ax = ox.plot_graph(G_largest, ax=ax, edge_linewidth=1, node_size=7)
plt.show()
edges_largest = gn.edge_gdf_from_graph(G_largest)
edges_largest.to_csv(os.path.join(data_pth, "edges_largest.csv"))

The next step would be to inspect changes in QGIS, add new paths to improve connectivity, and re-run the graph import notebooks to produce a better connected graph.