{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Creating Delaunay Lines\n\nIn this example, we show how to create AequilibraE's famous Delaunay Lines, but in Python.\n\nFor more on this topic, the first publication is [here](https://xl-optim.com/delaunay/).\n\nWe use the Sioux Falls example once again.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas as pd\nfrom uuid import uuid4\nfrom os.path import join\nimport sqlite3\nfrom tempfile import gettempdir\nimport matplotlib.pyplot as plt\nimport shapely.wkb\n\nfrom aequilibrae.utils.create_example import create_example\nfrom aequilibrae.utils.create_delaunay_network import DelaunayAnalysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create an empty project on an arbitrary 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": "markdown", "metadata": {}, "source": [ "Get the Delaunay Lines generation class\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "da = DelaunayAnalysis(project)\n\n# Let's create the triangulation based on the zones, but we could create based on the network (centroids) too\nda.create_network(\"zones\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we get the matrix we want and create the Delaunay Lines\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "demand = project.matrices.get_matrix(\"demand_omx\")\ndemand.computational_view([\"matrix\"])\n\n# And we will call it 'delaunay_test'./ It will also be saved in the results_database.sqlite\nda.assign_matrix(demand, \"delaunay_test\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we retrieve the results\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "conn = sqlite3.connect(join(fldr, \"results_database.sqlite\"))\nresults = pd.read_sql(\"Select * from delaunay_test\", conn).set_index(\"link_id\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we get the matrix we want and create the Delaunay Lines\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "links = pd.read_sql(\"Select link_id, st_asBinary(geometry) geometry from delaunay_network\", project.conn)\nlinks.geometry = links.geometry.apply(shapely.wkb.loads)\nlinks.set_index(\"link_id\", inplace=True)\n\ndf = links.join(results)\n\nmax_vol = df.matrix_tot.max()\n\nfor idx, lnk in df.iterrows():\n geo = lnk.geometry\n plt.plot(*geo.xy, color=\"blue\", linewidth=4 * lnk.matrix_tot / max_vol)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Close the project\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.9.18" } }, "nbformat": 4, "nbformat_minor": 0 }