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
sphinx_gallery_thumbnail_path = ‘../../examples/editing_models/simplification.gif’
original_model = Path("/tmp/Grid")
target = original_model.parent / "simplified_grid"
shutil.copytree(original_model, target, dirs_exist_ok=True)
PosixPath('/tmp/simplified_grid')
A little function to plot the network, which we will use a few times
def make_map(network):
network.data_tables.refresh_cache()
links = network.data_tables.get_geo_layer("Link")
nodes = network.data_tables.get_geo_layer("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)
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)
/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.")
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)
/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.")
Now we can close the network
network.close()
Total running time of the script: (0 minutes 7.998 seconds)