Simplifying networks

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
⚠️ Friendly exception handler is disabled via FRIENDLY_ERRORS_DISABLED

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)
2026-06-07 10:07:15 UTC+0000 - Working with file on /tmp/polaris_studio_testing/simplified_grid/Grid-Supply.sqlite
Make this Notebook Trusted to load map: File -> Trust Notebook


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:31: 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.")
/venv-py312/lib/python3.12/site-packages/geopandas/array.py:1770: UserWarning: CRS not set for some of the concatenation inputs. Setting output's CRS as NAD83 / UTM zone 16N (the single non-null crs provided).
  return GeometryArray(data, crs=_get_common_crs(to_concat))
2026-06-07 10:07:17 UTC+0000 - Field(s) a_node_network, source has(ve) at least one NaN value. Check your computations
2026-06-07 10:07:17 UTC+0000 - Field(s) a_node_network, source has(ve) at least one NaN value. Check your computations
2026-06-07 10:07:19 UTC+0000 - 459 links will be removed
2026-06-07 10:07:19 UTC+0000 - 149 links will be added
2026-06-07 10:07:20 UTC+0000 - Old distance: 427442.36285424477, new distance: 427442.36285424477. Difference: 0.0
2026-06-07 10:07:20 UTC+0000 - Network has been rebuilt. You should run this tool's rebuild network method
Make this Notebook Trusted to load map: File -> Trust Notebook


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:31: 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.")
2026-06-07 10:07:20 UTC+0000 - 1 links collapsed into nodes
Make this Notebook Trusted to load map: File -> Trust Notebook


Now we can close the network

network.close()

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

Gallery generated by Sphinx-Gallery