{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Project from OpenStreetMap\n\nIn this example, we show how to create an empty project and populate it with a network from OpenStreetMap.\n\nThis time we will use Folium to visualize the network.\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 import Project\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)\nproject = Project()\nproject.new(fldr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we can download the network from any place in the world (as long as you have memory for all the download\nand data wrangling that will be done)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We can create from a bounding box or a named place.\n# For the sake of this example, we will choose the small nation of Nauru.\nproject.network.create_from_osm(place_name=\"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\n\n# We create a Folium layer\nnetwork_links = folium.FeatureGroup(\"links\")\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    line = folium.vector_layers.PolyLine(\n        points, popup=f\"<b>link_id: {row.link_id}</b>\", tooltip=f\"{row.link_type}\", color=\"blue\", weight=10\n    ).add_to(network_links)"
      ]
    },
    {
      "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": [
        "map_osm = folium.Map(location=[lat, long], zoom_start=14)\nnetwork_links.add_to(map_osm)\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
}