{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1fa644d3",
   "metadata": {},
   "source": [
    "# Python and VGI - 06/06: osmnx Buildings and roads in Bochum Querenburg added to OSM"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fec43f3f-17e9-4999-8649-1954d7c22005",
   "metadata": {},
   "source": [
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d289d469",
   "metadata": {},
   "outputs": [],
   "source": [
    "import osmnx as ox # osmnx is easy to use for downloading OpenStreetMap data\n",
    "import matplotlib.pyplot as plt # matplotlib is used for plotting the data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d92854ac",
   "metadata": {},
   "source": [
    "## Specify the AOI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd4c2e6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "place_name = \"Querenburg, Bochum, Germany\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d358ab82",
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = ox.graph_from_place(place_name)\n",
    "type(graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7362f112",
   "metadata": {},
   "source": [
    "#### Nodes include intersections, but they also include all the points along a single street segment where the street curves.\n",
    "Convert a MultiDiGraph to node and/or edge GeoDataFrames."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f4b6172",
   "metadata": {},
   "outputs": [],
   "source": [
    "nodes, edges = ox.graph_to_gdfs(graph)\n",
    "nodes.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e64f4167",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get buildings as a separate variable\n",
    "buildings = ox.geometries.geometries_from_place(query=place_name, tags={'building':True}) \n",
    "# Here you can use different OSM Tags to filter (e.g. building, highway, amenity, landuse, etc.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44850538",
   "metadata": {},
   "source": [
    "## Plot the data\n",
    "#### It's important to note that buildings and roads shown are just the ones added to OSM. It is not a status quo of buildings; instead it can be a time frame of user activity in OSM. If used in a research aspect accuracy has to be checked."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "daf690a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "plt.title('Buildings and roads in Bochum Querenburg added to OSM')\n",
    "plt.figure(figsize=(30,8))\n",
    "edges.plot(ax=ax, linewidth=1, edgecolor='#808080')\n",
    "buildings.plot(ax=ax, facecolor='red', alpha=0.7)\n",
    "plt.tight_layout()\n",
    "ax.axis('off')"
   ]
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
