{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Exploring the network on a notebook\n\nIn this example, we show how to use Folium to plot a network for different modes.\n\nWe will need Folium for this example, and we will focus on creating a layer for\neach mode in the network, a layer for all links and a layer for all nodes.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Imports\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from uuid import uuid4\nfrom tempfile import gettempdir\nfrom os.path import join\nfrom aequilibrae.utils.create_example import create_example\nimport folium"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We create an empty project on an arbitrary folder\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fldr = join(gettempdir(), uuid4().hex)\n\n# Let's use the Nauru example project for display\nproject = create_example(fldr, \"nauru\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We grab all the links data as a Pandas dataframe so we can process it easier\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = project.network.links.data\nnodes = project.network.nodes.data\n\n# We create our Folium layers\nnetwork_links = folium.FeatureGroup(\"links\")\nnetwork_nodes = folium.FeatureGroup(\"nodes\")\ncar = folium.FeatureGroup(\"Car\")\nwalk = folium.FeatureGroup(\"Walk\")\nbike = folium.FeatureGroup(\"Bike\")\ntransit = folium.FeatureGroup(\"Transit\")\nlayers = [network_links, network_nodes, car, walk, bike, transit]\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.wkt.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.modes}\", color=\"gray\", weight=2\n    ).add_to(network_links)\n\n    if \"w\" in row.modes:\n        _ = folium.vector_layers.PolyLine(\n            points, popup=f\"<b>link_id: {row.link_id}</b>\", tooltip=f\"{row.modes}\", color=\"green\", weight=4\n        ).add_to(walk)\n\n    if \"b\" in row.modes:\n        _ = folium.vector_layers.PolyLine(\n            points, popup=f\"<b>link_id: {row.link_id}</b>\", tooltip=f\"{row.modes}\", color=\"green\", weight=4\n        ).add_to(bike)\n\n    if \"c\" in row.modes:\n        _ = folium.vector_layers.PolyLine(\n            points, popup=f\"<b>link_id: {row.link_id}</b>\", tooltip=f\"{row.modes}\", color=\"red\", weight=4\n        ).add_to(car)\n\n    if \"t\" in row.modes:\n        _ = folium.vector_layers.PolyLine(\n            points, popup=f\"<b>link_id: {row.link_id}</b>\", tooltip=f\"{row.modes}\", color=\"yellow\", weight=4\n        ).add_to(transit)\n\n# And now we get the nodes\n\nfor i, row in nodes.iterrows():\n    point = (row.geometry.y, row.geometry.x)\n\n    _ = folium.vector_layers.CircleMarker(\n        point,\n        popup=f\"<b>link_id: {row.node_id}</b>\",\n        tooltip=f\"{row.modes}\",\n        color=\"black\",\n        radius=5,\n        fill=True,\n        fillColor=\"black\",\n        fillOpacity=1.0,\n    ).add_to(network_nodes)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We get the center of the region we are working with some SQL magic\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_osm = folium.Map(location=[lat, long], zoom_start=14)\n\n# add all layers\nfor layer in layers:\n    layer.add_to(map_osm)\n\n# And Add layer control before we display it\nfolium.LayerControl().add_to(map_osm)\nmap_osm"
      ]
    },
    {
      "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
}