{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Create project from GMNS\n\nIn this example, we import a simple network in GMNS format.\nThe source files of this network are publicly available in the \n[GMNS GitHub repository](https://github.com/zephyr-data-specs/GMNS) itself.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n  * `importing_from_gmns_file` \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.create_from_gmns`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nfrom uuid import uuid4\nfrom os.path import join\nfrom tempfile import gettempdir\n\nfrom aequilibrae import Project\nfrom aequilibrae.parameters import Parameters\nimport folium"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We load the example file from the GMNS GitHub repository\nlink_file = \"https://raw.githubusercontent.com/zephyr-data-specs/GMNS/main/examples/Arlington_Signals/link.csv\"\nnode_file = \"https://raw.githubusercontent.com/zephyr-data-specs/GMNS/main/examples/Arlington_Signals/node.csv\"\nuse_group_file = \"https://raw.githubusercontent.com/zephyr-data-specs/GMNS/main/examples/Arlington_Signals/use_group.csv\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We create the example project inside our temp folder\nfldr = join(gettempdir(), uuid4().hex)\n\nproject = Project()\nproject.new(fldr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this cell, we modify the AequilibraE parameters.yml file so it contains additional\nfields to be read in the GMNS link and/or node tables. Remember to always keep the\n\"required\" key set to False, since we are adding a non-required field.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "new_link_fields = {\n    \"bridge\": {\"description\": \"bridge flag\", \"type\": \"text\", \"required\": False},\n    \"tunnel\": {\"description\": \"tunnel flag\", \"type\": \"text\", \"required\": False},\n}\nnew_node_fields = {\n    \"port\": {\"description\": \"port flag\", \"type\": \"text\", \"required\": False},\n    \"hospital\": {\"description\": \"hospital flag\", \"type\": \"text\", \"required\": False},\n}\n\npar = Parameters()\npar.parameters[\"network\"][\"gmns\"][\"link\"][\"fields\"].update(new_link_fields)\npar.parameters[\"network\"][\"gmns\"][\"node\"][\"fields\"].update(new_node_fields)\npar.write_back()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As it is specified that the geometries are in the coordinate system EPSG:32619,\nwhich is different than the system supported by AequilibraE (EPSG:4326), we inform\nthe srid in the method call:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "project.network.create_from_gmns(\n    link_file_path=link_file, node_file_path=node_file, use_group_path=use_group_file, srid=32619\n)"
      ]
    },
    {
      "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 = project.network.links.data\nnodes = project.network.nodes.data"
      ]
    },
    {
      "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
}