{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Exporting network to GMNS\n\nIn this example, we export a simple network to GMNS format.\nThe source AequilibraE model used as input for this is the result of the import process\n(``create_from_gmns()``) using the GMNS example of Arlington Signals, which can be found\nin the GMNS repository on GitHub: https://github.com/zephyr-data-specs/GMNS\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Imports\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from uuid import uuid4\nimport os\nfrom tempfile import gettempdir\nfrom aequilibrae.utils.create_example import create_example\nimport pandas as pd\nimport folium"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We load the example project inside a temp folder\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fldr = os.path.join(gettempdir(), uuid4().hex)\n\nproject = create_example(fldr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We export the network to csv files in GMNS format, that will be saved inside the project folder\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "output_fldr = os.path.join(gettempdir(), uuid4().hex)\nif not os.path.exists(output_fldr):\n    os.mkdir(output_fldr)\n\nproject.network.export_to_gmns(path=output_fldr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, let's plot a map. This map can be compared with the images of the README.md\nfile located in this example repository on GitHub:\nhttps://github.com/zephyr-data-specs/GMNS/blob/development/Small_Network_Examples/Arlington_Signals/README.md\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = pd.read_csv(os.path.join(output_fldr, \"link.csv\"))\nnodes = pd.read_csv(os.path.join(output_fldr, \"node.csv\"))\n\n# We create our Folium layers\nnetwork_links = folium.FeatureGroup(\"links\")\nnetwork_nodes = folium.FeatureGroup(\"nodes\")\nlayers = [network_links, network_nodes]\n\n# We do some Python magic to transform this dataset into the format required by Folium\n# We are only getting link_id and link_type into the map, but we could get other pieces of info as well\nfor i, row in links.iterrows():\n    points = row.geometry.replace(\"LINESTRING \", \"\").replace(\"(\", \"\").replace(\")\", \"\").split(\", \")\n    points = \"[[\" + \"],[\".join([p.replace(\" \", \", \") for p in points]) + \"]]\"\n    # we need to take from x/y to lat/long\n    points = [[x[1], x[0]] for x in eval(points)]\n\n    _ = folium.vector_layers.PolyLine(\n        points, popup=f\"<b>link_id: {row.link_id}</b>\", tooltip=f\"{row.facility_type}\", color=\"black\", weight=2\n    ).add_to(network_links)\n\n# And now we get the nodes\n\nfor i, row in nodes.iterrows():\n    point = (row.y_coord, row.x_coord)\n\n    _ = folium.vector_layers.CircleMarker(\n        point,\n        popup=f\"<b>link_id: {row.node_id}</b>\",\n        tooltip=f\"{row.node_type}\",\n        color=\"red\",\n        radius=5,\n        fill=True,\n        fillColor=\"red\",\n        fillOpacity=1.0,\n    ).add_to(network_nodes)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We get the center of the region\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "curr = project.conn.cursor()\ncurr.execute(\"select avg(xmin), avg(ymin) from idx_links_geometry\")\nlong, lat = curr.fetchone()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We create the map\nmap_gmns = folium.Map(location=[lat, long], zoom_start=12)\n\n# add all layers\nfor layer in layers:\n    layer.add_to(map_gmns)\n\n# And Add layer control before we display it\nfolium.LayerControl().add_to(map_gmns)\nmap_gmns"
      ]
    },
    {
      "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.16"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}