{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Create project from Overture Maps (or OpenStreetMap)\n\nIn this example we show how to create an empty project and populate it with a\nnetwork. Overture Maps is the recommended default source; importing from\nOpenStreetMap is also supported and is shown as commented-out code that you can\nswitch to.\n\nInstall the optional dependencies first::\n\n    pip install aequilibrae[create]\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n  * `importing_from_overture`\n  * `importing_from_osm`\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. seealso::\n    Several functions, methods, classes and modules are used in this example:\n\n    * ``project.tables.importer.overture()`` through :attr:`aequilibrae.Project.tables`\n    * ``project.tables.importer.osm()`` through :attr:`aequilibrae.Project.tables`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from os.path import join\nfrom tempfile import gettempdir\nfrom uuid import uuid4\n\nfrom shapely.geometry import box\n\nfrom aequilibrae import Project"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "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\nproject = Project.create(fldr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Both Overture and OSM imports require an EPSG:4326 polygon.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model_area = box(-112.185, 36.59, -112.179, 36.60)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import from Overture Maps (the recommended default). The importer always uses\nthe latest release advertised by Overture's STAC catalog and records that\nrelease in the project's ``about`` table.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "project.tables.importer.overture(model_area=model_area, modes=(\"car\", \"walk\"), simplify=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To import from OpenStreetMap instead, comment out the Overture call above and\nuncomment the block below. OSM data is fetched from an Overpass endpoint; for\nlarge areas you should point at your own/regional Overpass server and increase\nthe request timeout (the \"sleep\"/wait budget for Overpass) to avoid throttling.\nBoth are set on the project parameters before importing:\n\n```python\nparams = project.project_parameters\nparams.parameters[\"osm\"][\"overpass_endpoint\"] = \"https://overpass-api.de/api\"  # your own/regional server\nparams.parameters[\"osm\"][\"timeout\"] = 180  # seconds to wait for Overpass before giving up\nparams.write_back()\n\nproject.tables.importer.osm(model_area=model_area, modes=(\"car\", \"walk\"), simplify=False)\n# or import a whole place by name:\n# project.tables.importer.osm(place_name=\"Nauru\")\n# or import from a local extract:\n# project.tables.importer.osm(pbf_path=\"path/to/extract.osm.pbf\")\n```\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = project.tables.links.data\nlinks.explore(color=\"blue\", style_kwds={\"weight\": 2}, tooltip=\"link_type\")"
      ]
    },
    {
      "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.11.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}