
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/editing_networks/plot_splitting_link.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__auto_examples_editing_networks_plot_splitting_link.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__auto_examples_editing_networks_plot_splitting_link.py:


.. _editing_network_splitting_link:

Editing network geometry: Splitting link
========================================

In this example, we split a link right in the middle, while keeping all fields
in the database equal. Distance is proportionally computed automatically in the database.

.. GENERATED FROM PYTHON SOURCE LINES 11-14

.. admonition:: References

  * :ref:`modifications_on_links_layer` 

.. GENERATED FROM PYTHON SOURCE LINES 16-20

.. seealso::
    Several functions, methods, classes and modules are used in this example:

    * :func:`aequilibrae.project.network.Links`

.. GENERATED FROM PYTHON SOURCE LINES 23-32

.. code-block:: Python


    # Imports
    from uuid import uuid4
    from tempfile import gettempdir
    from os.path import join
    from aequilibrae.utils.create_example import create_example
    from shapely.ops import substring
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 33-39

.. code-block:: Python


    # We create the example project inside our temp folder
    fldr = join(gettempdir(), uuid4().hex)

    project = create_example(fldr)








.. GENERATED FROM PYTHON SOURCE LINES 40-42

We will split link 37 right in the middle.
Let's get the link and check its length.

.. GENERATED FROM PYTHON SOURCE LINES 42-48

.. code-block:: Python

    links = project.network.links
    all_nodes = project.network.nodes

    link = links.get(37)
    print(link.distance)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    6010.108655014215




.. GENERATED FROM PYTHON SOURCE LINES 49-51

The idea is basically to copy a link and allocate the appropriate geometries
to split the geometry we use Shapely's substring.

.. GENERATED FROM PYTHON SOURCE LINES 51-61

.. code-block:: Python


    new_link = links.copy_link(37)

    first_geometry = substring(link.geometry, 0, 0.5, normalized=True)
    second_geometry = substring(link.geometry, 0.5, 1, normalized=True)

    link.geometry = first_geometry
    new_link.geometry = second_geometry
    links.save()








.. GENERATED FROM PYTHON SOURCE LINES 62-63

The link objects in memory still don't have their ID fields updated, so we refresh them.

.. GENERATED FROM PYTHON SOURCE LINES 63-69

.. code-block:: Python

    links.refresh()

    link = links.get(37)
    new_link = links.get(new_link.link_id)
    print(link.distance, new_link.distance)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    3005.040184141035 3005.0684894898027




.. GENERATED FROM PYTHON SOURCE LINES 70-81

.. code-block:: Python


    # We can plot the two links only
    plt.clf()
    plt.plot(*link.geometry.xy, color="blue")
    plt.plot(*new_link.geometry.xy, color="blue")

    for node in [link.a_node, link.b_node, new_link.b_node]:
        geo = all_nodes.get(node).geometry
        plt.plot(*geo.xy, "o", color="black")
    plt.show()




.. image-sg:: /_auto_examples/editing_networks/images/sphx_glr_plot_splitting_link_001.png
   :alt: plot splitting link
   :srcset: /_auto_examples/editing_networks/images/sphx_glr_plot_splitting_link_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 82-102

.. code-block:: Python


    # Or we plot the entire network
    plt.clf()
    curr = project.conn.cursor()
    curr.execute("Select link_id from links;")

    for lid in curr.fetchall():
        geo = links.get(lid[0]).geometry
        plt.plot(*geo.xy, color="blue")

    all_nodes = project.network.nodes
    curr = project.conn.cursor()
    curr.execute("Select node_id from nodes;")

    for nid in curr.fetchall():
        geo = all_nodes.get(nid[0]).geometry
        plt.plot(*geo.xy, "o", color="black")

    plt.show()




.. image-sg:: /_auto_examples/editing_networks/images/sphx_glr_plot_splitting_link_002.png
   :alt: plot splitting link
   :srcset: /_auto_examples/editing_networks/images/sphx_glr_plot_splitting_link_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 103-104

.. code-block:: Python

    project.close()








.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download__auto_examples_editing_networks_plot_splitting_link.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_splitting_link.ipynb <plot_splitting_link.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_splitting_link.py <plot_splitting_link.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_splitting_link.zip <plot_splitting_link.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
