.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/network_manipulation/plot_network_simplification.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_network_manipulation_plot_network_simplification.py: .. _plot_network_simplifier: 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 theses changes into the project. We use Folium to visualize the resulting network. .. GENERATED FROM PYTHON SOURCE LINES 15-19 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.project.tools.network_simplifier.NetworkSimplifier` .. GENERATED FROM PYTHON SOURCE LINES 21-33 .. code-block:: Python # Imports import branca import folium from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae.utils.create_example import create_example from aequilibrae.project.tools.network_simplifier import NetworkSimplifier .. GENERATED FROM PYTHON SOURCE LINES 35-36 Let's use the Nauru example project for display .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "nauru") .. GENERATED FROM PYTHON SOURCE LINES 42-44 To simplify the network, we need to create a graph. As Nauru doesn't have any centroid in its network we have to create a centroid from an arbitrary node, otherwise we cannot create a graph. .. GENERATED FROM PYTHON SOURCE LINES 44-54 .. code-block:: Python nodes = project.network.nodes centroid_count = nodes.data.query("is_centroid == 1").shape[0] if centroid_count == 0: arbitrary_node = nodes.data["node_id"][0] nd = nodes.get(arbitrary_node) nd.is_centroid = 1 nd.save() .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: Python # Let's analyze the mode car or 'c' in our model mode = "c" .. GENERATED FROM PYTHON SOURCE LINES 60-69 .. code-block:: Python # Let's set the graph for computation network = project.network network.build_graphs(modes=[mode]) graph = network.graphs[mode] graph.set_graph("distance") graph.set_skimming("distance") graph.set_blocked_centroid_flows(False) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:248: UserWarning: Found centroids not present in the graph! [1] warnings.warn("Found centroids not present in the graph!\n" + str(centroids[~present_centroids])) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:248: UserWarning: Found centroids not present in the graph! [1] warnings.warn("Found centroids not present in the graph!\n" + str(centroids[~present_centroids])) .. GENERATED FROM PYTHON SOURCE LINES 70-76 .. code-block:: Python # Let's revert to setting up that node as centroid in case we had to do it if centroid_count == 0: nd.is_centroid = 0 nd.save() .. GENERATED FROM PYTHON SOURCE LINES 77-78 We check the number of links and nodes our project has initially. .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: Python links_before = project.network.links.data nodes_before = project.network.nodes.data print("This project initially has {} links and {} nodes".format(links_before.shape[0], nodes_before.shape[0])) .. rst-class:: sphx-glr-script-out .. code-block:: none This project initially has 1389 links and 1239 nodes .. GENERATED FROM PYTHON SOURCE LINES 85-87 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. .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python net = NetworkSimplifier() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/project/tools/network_simplifier.py:29: 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.") .. GENERATED FROM PYTHON SOURCE LINES 90-92 When we choose to simplify the network, we pass a graph object to the function, and the output of this operation is .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. code-block:: Python net.simplify(graph) net.rebuild_network() .. rst-class:: sphx-glr-script-out .. code-block:: none Simplifying links : 0%| | 0/152 [00:00
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 123-125 Differently we can simplify the network by collapsing links into nodes. Notice that this operation modifies the network in the neighborhood. .. GENERATED FROM PYTHON SOURCE LINES 125-129 .. code-block:: Python net.collapse_links_into_nodes([903]) net.rebuild_network() .. GENERATED FROM PYTHON SOURCE LINES 130-131 Let's plot the network once again and check the modifications! .. GENERATED FROM PYTHON SOURCE LINES 131-135 .. code-block:: Python links_after = net.network.links.data nodes_after = net.network.nodes.data .. GENERATED FROM PYTHON SOURCE LINES 136-155 .. code-block:: Python 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 .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 156-157 .. code-block:: Python project.close() .. rst-class:: sphx-glr-script-out .. code-block:: none This project at /tmp/d170e8771b264c0f817a9341beb20eed is already closed .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.852 seconds) .. _sphx_glr_download__auto_examples_network_manipulation_plot_network_simplification.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_network_simplification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_network_simplification.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_network_simplification.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_