{
  "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": [
        "Imports\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from 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": "markdown",
      "metadata": {},
      "source": [
        "We create the example project inside our temp folder\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fldr = 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\n\n# 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# 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\nlinks.save()\nlinks.refresh()\n\n# Because each link is unidirectional, you can no longer go from node 1 to node 2, obviously"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We do NOT recommend this, though....  It is very slow for real networks\nWe plot the entire network\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "curr = project.conn.cursor()\ncurr.execute(\"Select link_id from links;\")\n\nfor lid in curr.fetchall():\n    geo = links.get(lid[0]).geometry\n    plt.plot(*geo.xy, color=\"blue\")\n\nall_nodes = project.network.nodes\ncurr = project.conn.cursor()\ncurr.execute(\"Select node_id from nodes;\")\n\nfor nid in curr.fetchall():\n    geo = all_nodes.get(nid[0]).geometry\n    plt.plot(*geo.xy, \"o\", color=\"black\")\n\nplt.show()\n\n# Now look at the network and how it used to be"
      ]
    },
    {
      "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.9.16"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}