{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Update exposure data with user-input data\n", "\n", "This notebook demonstrates how to update the exposure data in an existing Delft-FIAT model with user-input data. Examples will be shown about how to update the ground floor height and the max potential damages (this will be added later!).\n", "\n", "## **Step 0**: Import required packages\n", "First we need to import the necessary python packages." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "from hydromt_fiat.fiat import FiatModel\n", "from hydromt.log import setuplog\n", "from pathlib import Path\n", "import shutil\n", "import copy\n", "import geopandas as gpd\n", "from hydromt.gis_utils import utm_crs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Step 1**: Configure\n", "The first step is to set up the configuration needed to initialize the FIAT model. Begin by specifying where the model that should be updated is saved, locating the required data catalog, and setting up the logger." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2023-10-18 13:36:08,112 - hydromt_fiat - log - INFO - HydroMT version: 0.8.0\n" ] } ], "source": [ "model_folder = Path().absolute() / \"data\" / \"update_ground_floor_height\" / \"fiat_model\"\n", "data_catalog = Path().absolute() / \"data\" / \"hydromt_fiat_catalog_USA.yml\"\n", "logger = setuplog(\"hydromt_fiat\", log_level=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Step 2**: Initialize and read existing model\n", "In this step we initialize HydroMT-FIAT in read-mode (`mode=\"r\"`) with the `model_folder`, `data_catalog`, and `logger` that we defined above. We also read in the existing dummy model." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2023-10-18 13:36:08,144 - hydromt_fiat - data_catalog - INFO - Parsing data catalog from c:\\Repositories\\hydromt_fiat\\examples\\data\\hydromt_fiat_catalog_USA.yml\n", "2023-10-18 13:36:08,148 - hydromt_fiat - log - DEBUG - Appending log messages to file c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\fiat_model\\hydromt.log.\n", "2023-10-18 13:36:08,148 - hydromt_fiat - model_api - INFO - Initializing fiat model from hydromt_fiat (v0.2.1.dev0).\n", "2023-10-18 13:36:08,149 - hydromt_fiat - fiat - INFO - Reading model data from c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\fiat_model\n", "2023-10-18 13:36:08,150 - hydromt_fiat - model_api - DEBUG - User defined config read from c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\fiat_model\\settings.toml\n", "2023-10-18 13:36:08,150 - hydromt_fiat - fiat - INFO - Reading model table files.\n", "2023-10-18 13:36:08,151 - hydromt_fiat - fiat - DEBUG - Reading vulnerability table c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\fiat_model\\vulnerability\\vulnerability_curves.csv\n", "2023-10-18 13:36:08,208 - hydromt_fiat - fiat - DEBUG - Reading exposure table c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\fiat_model\\exposure\\exposure.csv\n", "2023-10-18 13:36:08,220 - hydromt_fiat - fiat - INFO - Reading exposure geometries.\n", "2023-10-18 13:36:08,221 - hydromt_fiat - exposure_vector - INFO - Setting geometry name to building_points...\n", "2023-10-18 13:36:08,256 - hydromt_fiat - exposure_vector - INFO - Setting exposure geometries...\n" ] } ], "source": [ "fm = FiatModel(root=model_folder, mode=\"r\", data_libs=[data_catalog], logger=logger)\n", "fm.read()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Step 3**: Update the Ground Floor Height of the exposure data\n", "Next, we will update the Ground Floor Height of some of the exposure assets in our model. We have created some ficticious data that represents elevation certificates. We will first inspect the current exposure data and ground floor heights and the \"elevation certificate\" data.\n", "\n", "As can be seen below, apparently the ground floor height of all assets is set to 1 (foot)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1], dtype=int64)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "original_exposure = fm.exposure.get_full_gdf(fm.exposure.exposure_db)\n", "original_exposure[\"Ground Floor Height\"].unique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will load the \"elevation certificate\" data and visualize it together with the exposure assets below on the map." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elevation_certificates_path = Path().absolute() / \"data\" / \"update_ground_floor_height\" / \"fake_elevation_certificates.gpkg\"\n", "elevation_certificates = gpd.read_file(elevation_certificates_path)\n", "\n", "# Plot the exposure and elevation certificates data\n", "m = elevation_certificates.explore(column='FFE', cmap=\"Reds\", style_kwds={'radius': 5})\n", "m = original_exposure.explore(m=m, column=\"Ground Floor Height\", cmap=\"Reds\")\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use a maximum distance of 10 meter to join the elevation certificate data to their nearest building. The attribute in the elevation certificate data that we need to use is `FFE`, which stands for First Floor Elevation and is often used in the US to indicate the Ground Floor Height with." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2023-10-18 13:51:17,288 - hydromt_fiat - data_catalog - INFO - DataCatalog: Getting fake_elevation_certificates GeoDataFrame vector data from c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\fake_elevation_certificates.gpkg\n", "2023-10-18 13:51:17,293 - hydromt_fiat - geodataframe - INFO - GeoDataFrame: Read vector data.\n" ] } ], "source": [ "attribute = \"FFE\"\n", "method = \"nearest\"\n", "max_dist = 50\n", "\n", "fm.exposure.setup_ground_floor_height(\n", " ground_floor_height=elevation_certificates_path,\n", " attr_name=attribute,\n", " method=method,\n", " max_dist=max_dist,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Step 4**: Check the resulting exposure data\n", "As you can see below, the ground floor height of the exposure assets also contains other values." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 11, 20, 10, 12], dtype=int64)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "updated_exposure = fm.exposure.get_full_gdf(fm.exposure.exposure_db)\n", "updated_exposure[\"Ground Floor Height\"].unique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will make a buffer of 10 meter around the " ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check if the Ground Floor Heigh attribute is set correctly\n", "nearest_utm = utm_crs(elevation_certificates.total_bounds)\n", "elevation_certificates_projected = elevation_certificates.to_crs(nearest_utm)\n", "elevation_certificates_projected.geometry = elevation_certificates_projected.geometry.buffer(max_dist)\n", "\n", "# Plot the exposure and elevation certificates data\n", "m = elevation_certificates_projected.explore(column='FFE', cmap=\"Reds\")\n", "m = updated_exposure.explore(m=m, column=\"Ground Floor Height\", cmap=\"Reds\")\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Step 5**: Save the updated FIAT model\n", "Set the folder where you want to save the model to and write it to that folder." ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2023-10-18 15:53:42,160 - hydromt_fiat - log - DEBUG - Writing log messages to new file c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\updated_fiat_model\\hydromt.log.\n", "2023-10-18 15:53:42,161 - hydromt_fiat - fiat - INFO - Updating all data objects...\n", "2023-10-18 15:53:42,162 - hydromt_fiat - model_api - WARNING - Replacing geom: building_points\n", "2023-10-18 15:53:42,163 - hydromt_fiat - fiat - INFO - Writing model data to c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\updated_fiat_model\n", "2023-10-18 15:53:42,163 - hydromt_fiat - model_api - INFO - Writing model config to c:\\Repositories\\hydromt_fiat\\examples\\data\\update_ground_floor_height\\updated_fiat_model\\settings.toml\n", "2023-10-18 15:53:42,165 - hydromt_fiat - model_api - DEBUG - Writing file exposure/building_points.gpkg\n", "2023-10-18 15:53:42,256 - hydromt_fiat - fiat - INFO - Writing model vulnerability_curves table file to vulnerability/vulnerability_curves.csv.\n", "2023-10-18 15:53:42,260 - hydromt_fiat - fiat - INFO - Writing model exposure table file to exposure/exposure.csv.\n" ] } ], "source": [ "save_folder = Path().absolute() / \"data\" / \"update_ground_floor_height\" / \"updated_fiat_model\"\n", "\n", "# Remove the new root folder if it already exists\n", "if save_folder.exists():\n", " shutil.rmtree(save_folder)\n", "\n", "# Set the new root and write the model\n", "fm.set_root(save_folder)\n", "fm.write()" ] } ], "metadata": { "kernelspec": { "display_name": "hydromt-fiat-dev", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }