.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/network_manipulation/plot_centroid_connection.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_centroid_connection.py: .. _plot_centroid_connection: Create centroid connectors ========================== We use Coquimbo example to show how we can create centroids and connect them to the existing network efficiently. We use Folium to visualize the resulting network. .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.project.network.zones` .. GENERATED FROM PYTHON SOURCE LINES 20-30 .. code-block:: Python # Imports import folium from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae.examples import create_example .. GENERATED FROM PYTHON SOURCE LINES 32-39 .. code-block:: Python # Let's create an arbitrary project folder fldr = join(gettempdir(), uuid4().hex) # And create our Coquimbo project project = create_example(fldr, "coquimbo") .. GENERATED FROM PYTHON SOURCE LINES 40-41 As Coquimbo already has centroids and centroid connectors, we should remove them for the sake of this example .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: Python with project.db_connection as conn: conn.execute("DELETE FROM links WHERE name LIKE 'centroid connector%';") conn.execute("DELETE FROM nodes WHERE is_centroid=1;") conn.commit() centroids = conn.execute("SELECT COUNT(node_id) FROM nodes WHERE is_centroid=1;").fetchone()[0] .. GENERATED FROM PYTHON SOURCE LINES 49-51 If you want to make sure your deletion process has worked, you can check the number of centroids! .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python print("Current number of centroids: ", centroids) .. rst-class:: sphx-glr-script-out .. code-block:: none Current number of centroids: 0 .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python zones = project.tables.zones .. GENERATED FROM PYTHON SOURCE LINES 57-64 This centroid connector creation is effective because it uses the existing zone layer to create the centroids and connect them to the existing network. First thing to do is add the centroids to all zones that doesn't have a centroid at the geographic centroid of the zone, using ``add_centroids()``, which has a ``robust`` argument set to ``True`` as default. This means that it will automatically move the centroid location around to avoid conflicts with existing nodes. .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python zones.add_centroids() .. rst-class:: sphx-glr-script-out .. code-block:: none Adding centroids : 0%| | 0/133 [00:00
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 104-106 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.868 seconds) .. _sphx_glr_download__auto_examples_network_manipulation_plot_centroid_connection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_centroid_connection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_centroid_connection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_centroid_connection.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_