
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/editing_networks/plot_moving_link_extremity.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_moving_link_extremity.py>`
        to download the full example code.

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

.. _sphx_glr__auto_examples_editing_networks_plot_moving_link_extremity.py:


.. _editing_network_links:

Editing network geometry: Links
===============================

In this example, we move a link extremity from one point to another
and see what happens to the network.

.. 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 22-31

.. 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.geometry import LineString, Point
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 32-38

.. 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 39-42

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 43-44

Let's move node one from the upper left corner of the image above, a bit to the left and to the bottom

.. GENERATED FROM PYTHON SOURCE LINES 44-60

.. code-block:: Python


    # We edit the link that goes from node 1 to node 2
    link = links.get(1)
    node = all_nodes.get(1)
    new_extremity = Point(node.geometry.x + 0.02, node.geometry.y - 0.02)
    link.geometry = LineString([node.geometry, new_extremity])

    # and the link that goes from node 2 to node 1
    link = links.get(3)
    node2 = all_nodes.get(2)
    link.geometry = LineString([new_extremity, node2.geometry])

    # We save the changes and refresh the links in memory for usage
    links.save()
    links.refresh()








.. GENERATED FROM PYTHON SOURCE LINES 61-64

Because each link is unidirectional, you can no longer go from node 1 to node 2, obviously.

We do NOT recommend this, though.... It is very slow for real networks.

.. GENERATED FROM PYTHON SOURCE LINES 64-83

.. code-block:: Python


    # We plot the entire network.
    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_moving_link_extremity_001.png
   :alt: plot moving link extremity
   :srcset: /_auto_examples/editing_networks/images/sphx_glr_plot_moving_link_extremity_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 84-85

Now look at the network and how it used to be.

.. GENERATED FROM PYTHON SOURCE LINES 87-88

.. code-block:: Python

    project.close()








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

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


.. _sphx_glr_download__auto_examples_editing_networks_plot_moving_link_extremity.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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