{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Editing network geometry: Links\n\nIn this example, we move a link extremity from one point to another\nand see what happens to the network.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n  * `modifications_on_links_layer` \n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. seealso::\n    Several functions, methods, classes and modules are used in this example:\n\n    * :func:`aequilibrae.project.network.Links`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nfrom uuid import uuid4\nfrom tempfile import gettempdir\nfrom os.path import join\nfrom aequilibrae.utils.create_example import create_example\nfrom shapely.geometry import LineString, Point\nimport matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We create the example project inside our temp folder\nfldr = join(gettempdir(), uuid4().hex)\n\nproject = create_example(fldr)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "all_nodes = project.network.nodes\nlinks = project.network.links"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's move node one from the upper left corner of the image above, a bit to the left and to the bottom\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We edit the link that goes from node 1 to node 2\nlink = links.get(1)\nnode = all_nodes.get(1)\nnew_extremity = Point(node.geometry.x + 0.02, node.geometry.y - 0.02)\nlink.geometry = LineString([node.geometry, new_extremity])\n\n# and the link that goes from node 2 to node 1\nlink = links.get(3)\nnode2 = all_nodes.get(2)\nlink.geometry = LineString([new_extremity, node2.geometry])\n\n# We save the changes and refresh the links in memory for usage\nlinks.save()\nlinks.refresh()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Because each link is unidirectional, you can no longer go from node 1 to node 2, obviously.\n\nWe do NOT recommend this, though.... It is very slow for real networks.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "link_ids = links.data[\"link_id\"].values.tolist()\nfor lid in link_ids:\n    geo = links.get(lid).geometry\n    plt.plot(*geo.xy, color=\"blue\")\n\nnode_ids = all_nodes.data[\"node_id\"].values.tolist()\nfor nid in node_ids:\n    geo = all_nodes.get(nid).geometry\n    plt.plot(*geo.xy, \"o\", color=\"black\")\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now look at the network and how it used to be.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "project.close()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}