{
  "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": [
        ".. admonition:: References\n\n  * `aequilibrae_to_gmns` \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.export_to_gmns`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nfrom uuid import uuid4\nimport os\nfrom tempfile import gettempdir\n\nfrom aequilibrae.utils.create_example import create_example\nimport folium\nimport geopandas as gpd\nimport pandas as pd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We load the example project inside a temp folder\nfldr = 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/develop/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\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We turn the links and nodes DataFrames into GeoDataFrames so we can plot them more easily.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = gpd.GeoDataFrame(links, geometry=gpd.GeoSeries.from_wkt(links[\"geometry\"]), crs=4326)\nnodes = gpd.GeoDataFrame(nodes, geometry=gpd.GeoSeries.from_xy(nodes[\"x_coord\"], nodes[\"y_coord\"]), crs=4326)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's plot our map!\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "map = links.explore(color=\"black\", style_kwds={\"weight\": 2}, tool_tip=\"link_type\", name=\"links\")\nmap = nodes.explore(m=map, color=\"red\", style_kwds={\"radius\": 5, \"fillOpacity\": 1.0}, name=\"nodes\")\n\nfolium.LayerControl().add_to(map) # Add a layer control button to our map\nmap"
      ]
    },
    {
      "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
}