{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Create project from a link layer\n\nIn this example, we show how to create an empty project and populate it with a\nnetwork coming from a link layer we load from a text file. It can easily be\nreplaced with a different form of loading the data (GeoPandas, for example).\n\nWe use Folium to visualize the resulting network.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n  * :doc:`../../aequilibrae_project/project_components`\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    * :func:`aequilibrae.project.network.Nodes` \n    * :func:`aequilibrae.project.network.Modes`\n    * :func:`aequilibrae.project.network.LinkTypes` \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nfrom uuid import uuid4\nimport urllib.request\nfrom string import ascii_lowercase\nfrom tempfile import gettempdir\nfrom os.path import join\nfrom shapely.wkt import loads as load_wkt\nimport pandas as pd\n\nfrom aequilibrae import Project"
      ]
    },
    {
      "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 = Project()\nproject.new(fldr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we obtain the link data for our example (in this case from a link layer\nwe will download from the AequilibraE website).\nWith data, we load it on Pandas\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dest_path = join(fldr, \"queluz.csv\")\nurllib.request.urlretrieve(\"https://aequilibrae.com/data/queluz.csv\", dest_path)\n\ndf = pd.read_csv(dest_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's see if we have to add new link_types to the model before we add links\nThe links we have in the data are:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "link_types = df.link_type.unique()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And the existing link types are\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "lt = project.network.link_types\nlt_dict = lt.all_types()\nexisting_types = [ltype.link_type for ltype in lt_dict.values()]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We could also get it directly from the project database\n\nwith project.db_connection as conn:\n  existing_types = [x[0] for x in conn.execute('Select link_type from link_types')]\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We add the link types that do not exist yet.\nThe trickier part is to choose a unique link type ID for each link type.\nYou might want to tailor the link type for your use, but here we get letters\nin alphabetical order.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "types_to_add = [ltype for ltype in link_types if ltype not in existing_types]\nfor i, ltype in enumerate(types_to_add):\n    new_type = lt.new(ascii_lowercase[i])\n    new_type.link_type = ltype\n    # new_type.description = 'Your custom description here if you have one'\n    new_type.save()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We need to use a similar process for modes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "md = project.network.modes\nmd_dict = md.all_modes()\nexisting_modes = {k: v.mode_name for k, v in md_dict.items()}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's see the modes we have in the network that we DON'T have already in\nthe model.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We get all the unique mode combinations and merge them into a single string\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "all_variations_string = \"\".join(df.modes.unique())\n\n# We then get all the unique modes in that string above\nall_modes = set(all_variations_string)\n\n# This would all fit nicely in a single line of code, btw. Try it!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's add any new mode to the project\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "modes_to_add = [mode for mode in all_modes if mode not in existing_modes]\nfor i, mode_id in enumerate(modes_to_add):\n    new_mode = md.new(mode_id)\n    # You would need to figure out the right name for each one, but this will do\n    new_mode.mode_name = f\"Mode_from_original_data_{mode_id}\"\n    # new_type.description = 'Your custom description here if you have one'\n\n    # It is a little different because you need to add it to the project\n    project.network.modes.add(new_mode)\n    new_mode.save()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We cannot use the existing link_id, so we create a new field to not loose\nthis information\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = project.network.links\nlink_data = links.fields\n\n# Create the field and add a good description for it\nlink_data.add(\"source_id\", \"link_id from the data source\")\n\n# We need to refresh the fields so the adding method can see it\nlinks.refresh_fields()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now add all links to the project!\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for idx, record in df.iterrows():\n    new_link = links.new()\n\n    # Now let's add all the fields we had\n    new_link.source_id = record.link_id\n    new_link.direction = record.direction\n    new_link.modes = record.modes\n    new_link.link_type = record.link_type\n    new_link.name = record.name\n    new_link.geometry = load_wkt(record.WKT)\n    new_link.save()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We grab all the links data as a geopandas GeoDataFrame so we can process it easier\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = project.network.links.data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's plot our network!\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links.explore(color=\"blue\", style_kwds={\"weight\": 2}, tooltip=\"link_type\")"
      ]
    },
    {
      "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
}