{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82d8ec02",
   "metadata": {},
   "source": [
    "# Python and VGI - 02/06: OHSOME Buildings extraction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a2e4102-0aa6-4824-a59a-003c9e18da09",
   "metadata": {},
   "source": [
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "472d826c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing necessary packages\n",
    "from ohsome import OhsomeClient\n",
    "import matplotlib.pyplot as plt\n",
    "client = OhsomeClient()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9e54d7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setting the AOI-Box using coordinates\n",
    "bboxes = [7.24122,51.45557,7.28003,51.43241]\n",
    "\n",
    "# Time of the investigation\n",
    "time = \"2021-07-01\"\n",
    "\n",
    "# Filter for a variable\n",
    "fltr = \"building=* and type:way\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c0eaecb",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = client.elements.geometry.post(bboxes=bboxes, time=time, filter=fltr, properties=\"tags\")\n",
    "buildings_df = response.as_dataframe()\n",
    "buildings_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1e9d56e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot extracted features\n",
    "fig, ax = plt.subplots()\n",
    "plt.title('Buildings of Bochum Querenburg')\n",
    "plt.figure(figsize=(30,8))\n",
    "buildings_df.plot(ax=ax, facecolor='red', alpha=0.7)\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72da28d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save the data to a geojson (at the location of the script)\n",
    "response.to_json(\"./Buildings_Bochum.json\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91d1e96a",
   "metadata": {},
   "source": [
    "# COVID-19 vaccination centre extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d955580",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbox = [5.778809,47.234490,15.161133,55.416544]\n",
    "\n",
    "# Time of the investiation\n",
    "time = \"2021-07-01\"\n",
    "\n",
    "# Set a variable to filter\n",
    "fltr = \"vaccination=covid19 and type:node\"\n",
    "\n",
    "response = client.elements.geometry.post(bboxes=bbox, time=time, filter=fltr)#, properties=\"tags\")\n",
    "cov19 = response.as_dataframe()\n",
    "\n",
    "# Plotting extracted features\n",
    "fig, ax = plt.subplots()\n",
    "plt.title('COVID-19 vaccination centres Germany')\n",
    "plt.figure(figsize=(30,8))\n",
    "cov19.plot(ax=ax, facecolor='red', alpha=0.7)\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b1fbbad",
   "metadata": {},
   "outputs": [],
   "source": [
    "response.to_json(\"./Covid19_vaccination_centres.json\")"
   ]
  }
 ],
 "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
}
