{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Editing network geometry: Splitting link\n\nIn this example, we split a link right in the middle, while keeping all fields\nin the database equal. Distance is proportionally computed automatically in the database.\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.ops import substring\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": "markdown",
      "metadata": {},
      "source": [
        "We will split link 37 right in the middle.\nLet's get the link and check its length.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = project.network.links\nall_nodes = project.network.nodes\n\nlink = links.get(37)\nprint(link.distance)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The idea is basically to copy a link and allocate the appropriate geometries\nto split the geometry we use Shapely's substring.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "new_link = links.copy_link(37)\n\nfirst_geometry = substring(link.geometry, 0, 0.5, normalized=True)\nsecond_geometry = substring(link.geometry, 0.5, 1, normalized=True)\n\nlink.geometry = first_geometry\nnew_link.geometry = second_geometry\nlinks.save()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The link objects in memory still don't have their ID fields updated, so we refresh them.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links.refresh()\n\nlink = links.get(37)\nnew_link = links.get(new_link.link_id)\nprint(link.distance, new_link.distance)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We can plot the two links only\nplt.clf()\nplt.plot(*link.geometry.xy, color=\"blue\")\nplt.plot(*new_link.geometry.xy, color=\"blue\")\n\nfor node in [link.a_node, link.b_node, new_link.b_node]:\n    geo = all_nodes.get(node).geometry\n    plt.plot(*geo.xy, \"o\", color=\"black\")\nplt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Or we plot the entire network\nplt.clf()\n\nlink_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": "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
}