{ "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": [ "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.ops import substring\nimport matplotlib.pyplot as plt\n\n# 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()\nlink = links.get(37)\nnew_link = links.get(new_link.link_id)\nprint(link.distance, new_link.distance)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plot the two links only\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.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": "markdown", "metadata": {}, "source": [ "Or we plot the entire network\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.clf()\ncurr = 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()" ] }, { "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.18" } }, "nbformat": 4, "nbformat_minor": 0 }