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:
osto set the working directory and handle file pathsnetworkxto handle the graph data structureosmnxto plot the network graphpickleto load the network datamatplotlibto help plot and format the graphGOSTnetsto 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
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 5
3 import osmnx as ox
4 import pickle as pkl
----> 5 import matplotlib.pyplot as plt
7 # import the GOSTnets library
8 import GOSTnets as gn
ModuleNotFoundError: No module named 'matplotlib'
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)
MultiDiGraph with 14740 nodes and 37322 edges
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()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 5
2 G_largest.graph["crs"] = "epsg:32646"
3 G_largest.graph["name"] = "Iceland"
----> 5 fig, ax = plt.subplots(figsize=(10, 14))
6 ax.set_facecolor("k")
7 ax.set_title("Iceland - The Largest Network")
NameError: name 'plt' is not defined
edges_largest = gn.edge_gdf_from_graph(G_largest)
edges_largest.to_csv(os.path.join(data_pth, "edges_largest.csv"))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[7], line 1
----> 1 edges_largest = gn.edge_gdf_from_graph(G_largest)
2 edges_largest.to_csv(os.path.join(data_pth, "edges_largest.csv"))
NameError: name 'gn' is not defined
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.