{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb053094-8052-4532-bfd1-1bd1dc249d9f",
   "metadata": {},
   "source": [
    "# Python and VGI - 03/06: OHSOME. Flooding in Germany: assessing damage by detecting affected buildings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b5197ae-d138-49d7-ae43-70d0c0618154",
   "metadata": {},
   "source": [
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75d6c38a-3124-42f4-8e3d-e7174d6c02c1",
   "metadata": {},
   "source": [
    "In this chapter, you will assess the damage of a flood by detecting the affected buildings in the area using the ohsome package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24b70612",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import necessary packages\n",
    "from ohsome import OhsomeClient\n",
    "import pandas as pd\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c5c26b9-52a6-4a01-8d19-da1955a163f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OhsomeClient()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cd44109",
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can convert a shapefile to a geojson for example in QGIS\n",
    "# The AOI Geojson file needs to be a Feature Collection, meaning it contains several Polygons.\n",
    "# You can also create  geojson files with https://geojson.io/#map=2/20.0/0.0\n",
    "# For more Information about how to use boundaries in ohsome, see : https://docs.ohsome.org/ohsome-api/v1/boundaries.html \n",
    " \n",
    "AOI_path = './Flooding_Germany.geojson' # Provide the path to your geojson file\n",
    "\n",
    "# Open the file and extract the content as a string (text)\n",
    "with open(AOI_path) as jsonAOI:\n",
    "    aoi_json = json.load(jsonAOI)\n",
    "json_aoi_txt = json.dumps(aoi_json)\n",
    "print(json_aoi_txt[:500]) # print the first 500 characters of the geojson string"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d97da475",
   "metadata": {},
   "source": [
    "# Count buildings affected by flooding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "532a11f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Time of your investigation\n",
    "time = \"2021-06-15\"\n",
    "\n",
    "# Filter for a desired variable\n",
    "fltr = \"building=* and type:way\"\n",
    "\n",
    "response = client.elements.count.post(bpolys=json_aoi_txt, time=time, filter=fltr)\n",
    "response.as_dataframe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e26b4fd0",
   "metadata": {},
   "source": [
    "# Download all Buildings in the affected area"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5700244b",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = client.elements.geometry.post(bpolys=json_aoi_txt, time=time, filter=fltr)\n",
    "response.to_json(\"./Affected_buildings.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
}
