Network simplifier#

In this example we use Nauru network to show how one can simplify the network, merging short links into longer ones or turning links into nodes, and saving these changes into the project.

We use Folium to visualize the resulting network.

See also

Several functions, methods, classes and modules are used in this example:

  • aequilibrae.project.tools.network_simplifier()

# Imports
from os.path import join
from tempfile import gettempdir
from uuid import uuid4

import branca
import folium

from aequilibrae.project import NetworkSimplifier
from aequilibrae.examples import create_example

Let’s use the Nauru example project for display

fldr = join(gettempdir(), uuid4().hex)

project = create_example(fldr, "nauru")

To simplify the network, we need to create a compact graph, which requires centroids. As Nauru doesn’t have any centroids, we use an arbitrary node as a graph-only centroid.

network = project.road.build_network()
centroids = network.centroids
if centroids is None:
    centroids = network.mapping.node_ids[:1]
# Let's analyse the mode car or 'c' in our model
mode = "c"
# Let's set the graph for computation
graph = network.build_graph(
    mode, "distance", centroids=centroids, skimming=["distance"], compact=True, block_centroid_flows=False
)
# No centroid changes need reverting: the graph-only centroid was not written to the project.

We check the number of links and nodes our project has initially.

links_before = project.tables.links.data
nodes_before = project.tables.nodes.data

print("This project initially has {} links and {} nodes".format(links_before.shape[0], nodes_before.shape[0]))
This project initially has 1389 links and 1239 nodes

Let’s call the NetworkSimplifier class. Any changes made to the database using this class are permanent. Make sure you have a backup if necessary.

When we choose to simplify the network, we pass a graph object to the function, and the output of this operation is

Simplifying links                                 :   0%|          | 0/133 [00:00<?, ?it/s]
Simplifying links                                 :  12%|█▏        | 16/133 [00:00<00:00, 145.92it/s]
Simplifying links                                 :  23%|██▎       | 31/133 [00:00<00:00, 127.37it/s]
Simplifying links                                 :  33%|███▎      | 44/133 [00:00<00:00, 120.14it/s]
Simplifying links                                 :  43%|████▎     | 57/133 [00:00<00:00, 122.79it/s]
Simplifying links                                 :  56%|█████▌    | 74/133 [00:00<00:00, 136.62it/s]
Simplifying links                                 :  68%|██████▊   | 91/133 [00:00<00:00, 146.00it/s]
Simplifying links                                 :  82%|████████▏ | 109/133 [00:00<00:00, 154.59it/s]
Simplifying links                                 :  95%|█████████▌| 127/133 [00:00<00:00, 162.09it/s]

Let’s plot the previous and actual networks!

links_after = net.tables.links.data
nodes_after = net.tables.nodes.data
fig = branca.element.Figure()

subplot1 = fig.add_subplot(1, 2, 1)
subplot2 = fig.add_subplot(1, 2, 2)

map1 = folium.Map(location=[-0.508371, 166.931142], zoom_start=17)
map1 = links_before.explore(m=map1, color="black", style_kwds={"weight": 2}, name="links_before")
map1 = nodes_before.explore(m=map1, color="red", style_kwds={"radius": 3, "fillOpacity": 1.0}, name="nodes_before")
folium.LayerControl().add_to(map1)

map2 = folium.Map(location=[-0.508371, 166.931142], zoom_start=17)
map2 = links_after.explore(m=map2, color="black", style_kwds={"weight": 2}, name="links_after")
map2 = nodes_after.explore(m=map2, color="blue", style_kwds={"radius": 3, "fillOpacity": 1.0}, name="nodes_after")
folium.LayerControl().add_to(map2)

subplot1.add_child(map1)
subplot2.add_child(map2)

fig
Make this Notebook Trusted to load map: File -> Trust Notebook


Differently we can simplify the network by collapsing links into nodes. Notice that this operation modifies the network in the neighborhood.

Let’s plot the network once again and check the modifications!

links_after = net.tables.links.data
nodes_after = net.tables.nodes.data
fig = branca.element.Figure()

subplot1 = fig.add_subplot(1, 2, 1)
subplot2 = fig.add_subplot(1, 2, 2)

map1 = folium.Map(location=[-0.509363, 166.928563], zoom_start=18)
map1 = links_before.explore(m=map1, color="black", style_kwds={"weight": 2}, name="links_before")
map1 = nodes_before.explore(m=map1, color="red", style_kwds={"radius": 3, "fillOpacity": 1.0}, name="nodes_before")
folium.LayerControl().add_to(map1)

map2 = folium.Map(location=[-0.509363, 166.928563], zoom_start=18)
map2 = links_after.explore(m=map2, color="black", style_kwds={"weight": 2}, name="links_after")
map2 = nodes_after.explore(m=map2, color="blue", style_kwds={"radius": 3, "fillOpacity": 1.0}, name="nodes_after")
folium.LayerControl().add_to(map2)

subplot1.add_child(map1)
subplot2.add_child(map2)

fig
Make this Notebook Trusted to load map: File -> Trust Notebook


Total running time of the script: (0 minutes 2.258 seconds)

Gallery generated by Sphinx-Gallery