Note
Go to the end to download the full example code.
Simplifying networks#
In this example we show how to perform topological simplification on a network.
This process changes the data in place, so we recommend working on a copy of your model, as done below.
import shutil
from pathlib import Path
import folium
from polaris import Polaris
from polaris.utils.testing.temp_model import TempModel
sphinx_gallery_thumbnail_path = ‘../../examples/editing_models/simplification.gif’
original_model = TempModel("Grid")
target = original_model.parent / "simplified_grid"
shutil.copytree(original_model, target, dirs_exist_ok=True)
PosixPath('/tmp/polaris_studio_testing/simplified_grid')
A little function to plot the network, which we will use a few times
def make_map(network):
network.tables.refresh_cache()
links = network.tables.get("Link")
nodes = network.tables.get("Node")
map = links.explore(color="black", style_kwds={"weight": 1}, tool_tip="link_type", name="links")
map = nodes.explore(m=map, color="red", style_kwds={"radius": 3, "fillOpacity": 1.0}, name="nodes")
folium.LayerControl().add_to(map) # Add a layer control button to our map
return map
Let’s plot the network we have to begin with
network = Polaris.from_dir(target).network
make_map(network)
2025-10-30 02:28:22 UTC+0000 - No pre-existing parameter file exists for this project. Will use default
2025-10-30 02:28:23 UTC+0000 - Working with file on /tmp/polaris_studio_testing/simplified_grid/Grid-Supply.sqlite
Now let’s simplify the network
# Let's allow links to be a maximum of 2000 meters long
# And we will merge links if they have up to 5% difference in speed
# The recommended maximum length is 1,000, but we are using 100,000 here for illustration purposes
network.tools.simplify_network(maximum_allowable_link_length=100_000, max_speed_ratio=1.05, rebuild_network=False)
# What happened to the network after this simplification?
make_map(network)
/home/gitlab-runner/builds/polaris/code/polarislib/polaris/network/tools/network_simplifier.py:28: UserWarning: This will alter your database in place. Make sure you have a backup.
warnings.warn("This will alter your database in place. Make sure you have a backup.")
2025-10-30 02:28:24 UTC+0000 - Field(s) a_node_network, source has(ve) at least one NaN value. Check your computations
: 0%| | 0/15 [00:00<?, ?it/s]
2025-10-30 02:28:24 UTC+0000 - Field(s) a_node_network, source has(ve) at least one NaN value. Check your computations
2025-10-30 02:28:26 UTC+0000 - 459 links will be removed
2025-10-30 02:28:26 UTC+0000 - 149 links will be added
2025-10-30 02:28:27 UTC+0000 - Old distance: 427442.36285424477, new distance: 427442.36285424477. Difference: 0.0
2025-10-30 02:28:27 UTC+0000 - Network has been rebuilt. You should run this tool's rebuild network method
We can also delete links and collapse them into nodes For this operation, we just choose an arbitrary link to delete here
network.tools.collapse_links_into_nodes(links=[838], rebuild_network=False)
# What happened to the network after this deletion?
make_map(network)
/home/gitlab-runner/builds/polaris/code/polarislib/polaris/network/tools/network_simplifier.py:28: UserWarning: This will alter your database in place. Make sure you have a backup.
warnings.warn("This will alter your database in place. Make sure you have a backup.")
2025-10-30 02:28:28 UTC+0000 - 1 links collapsed into nodes
Now we can close the network
network.close()
Total running time of the script: (0 minutes 7.619 seconds)