{
  "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, see its [first publication](https://xl-optim.com/delaunay/).\n\nWe use the Sioux Falls example once again.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. seealso::\n    Several functions, methods, classes and modules are used in this example:\n\n    * :func:`aequilibrae.utils.create_delaunay_network.DelaunayAnalysis`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nimport pandas as pd\nfrom uuid import uuid4\nfrom os.path import join\nimport sqlite3\nfrom tempfile import gettempdir\nfrom geopandas import read_postgis\n\nfrom aequilibrae.utils.create_example import create_example\nfrom aequilibrae.utils.create_delaunay_network import DelaunayAnalysis"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We create an empty project on an arbitrary folder\nfldr = 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\"])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And we will call it 'delaunay_test'./ It will also be saved in the results_database.sqlite\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "da.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": [
        "with project.db_connection as conn:\n    links = read_postgis(\n        \"Select link_id, st_asBinary(geometry) geometry from delaunay_network\", \n        conn, \n        geom_col=\"geometry\", \n        crs=4326\n    )\n    links.set_index(\"link_id\", inplace=True)\n\ndf = links.join(results)\n\nmax_vol = df.matrix_tot.max()\n\ndf.plot(linewidth=4 * df[\"matrix_tot\"] / max_vol, color=\"blue\")"
      ]
    },
    {
      "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.10.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}