{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "e0bf8629", "metadata": {}, "source": [ "# RA2CE feature: Hazard overlay" ] }, { "attachments": {}, "cell_type": "markdown", "id": "43bce1c4", "metadata": {}, "source": [ "In this notebook, we will guide you through performing a hazard overlay with a network using RA2CE.\n", "This notebook follows the RA2CE_basics notebook, in which the user creates a network from OSM. \n", "This is needed first before being able to execute this notebook\n", "\n", "First of all, we will import the packages we need to execute the notebook:" ] }, { "cell_type": "code", "execution_count": null, "id": "e67d001f", "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "import matplotlib.pyplot as plt\n", "from pathlib import Path\n", "import rasterio" ] }, { "attachments": {}, "cell_type": "markdown", "id": "40432885", "metadata": {}, "source": [ "Import RA2CE by importing the RA2CEHandler" ] }, { "cell_type": "code", "execution_count": null, "id": "0d6562dd", "metadata": {}, "outputs": [], "source": [ "from ra2ce.ra2ce_handler import Ra2ceHandler # import the ra2cehandler to run ra2ce analyses" ] }, { "attachments": {}, "cell_type": "markdown", "id": "cc39da83", "metadata": {}, "source": [ "Specify the path to the folder holding your input data and where you want to store your output (use the RA2CE folder structure!)" ] }, { "cell_type": "code", "execution_count": null, "id": "a9af27c0", "metadata": {}, "outputs": [], "source": [ "root_dir = Path (\"data\", \"hazard_overlay\") #specify the path to the folder holding the RA2CE folder and input data" ] }, { "attachments": {}, "cell_type": "markdown", "id": "81311f7f", "metadata": {}, "source": [ "Specify the names of the initialization files and check whether they exist and can be found" ] }, { "cell_type": "code", "execution_count": null, "id": "be1e6805", "metadata": {}, "outputs": [], "source": [ "_network_ini_name = \"network.ini\" #set the name for the network.ini\n", "_analyses_ini_name = \"analyses.ini\" #set the name for the analyses.ini\n", "\n", "network_ini = root_dir / _network_ini_name\n", "analyses_ini = root_dir / _analyses_ini_name\n", "\n", "assert network_ini.is_file() # check whether the network.ini file exists\n", "assert analyses_ini.is_file() # check whether the analyses.ini file exists" ] }, { "attachments": {}, "cell_type": "markdown", "id": "97e562c5", "metadata": {}, "source": [ "## Hazard map\n", "Now we need a hazard map! \n", "You can use any hazard map, as long as it is in .tif format and you specify the correct projection in the network.ini file.\n", "\n", "Save the map in the static/hazard folder. If you are not familiar with the RA2CE folder structure, first open the ra2ce_basics notebook.\n", "\n", "*You can for example download flood maps from the Global Flood Database.*\n", "\n", "*Note: the hazard map needs to be in **.tif** format*" ] }, { "cell_type": "code", "execution_count": null, "id": "1d7be432", "metadata": {}, "outputs": [], "source": [ "hazard_folder = root_dir / \"static\" / \"hazard\" # find the hazard folder where you locate your hazard map\n", "hazard_map = hazard_folder / \"max_flood_depth.tif\" # set the name of the hazard map" ] }, { "cell_type": "code", "execution_count": null, "id": "01d3d84d", "metadata": {}, "outputs": [], "source": [ "# Open the TIF file using rasterio\n", "with rasterio.open(hazard_map) as src:\n", " # Read the TIF file as a numpy array\n", " tif_array = src.read(1) # Change the band index (1) if necessary\n", "\n", "plt.figure(figsize=(10, 10))\n", "plt.imshow(tif_array, cmap='Blues') # Change the colormap if desired\n", "plt.colorbar(label='Pixel Values')\n", "plt.title('hazard map')\n", "plt.show() \n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b4c52aca", "metadata": {}, "source": [ "## Network.ini file" ] }, { "attachments": {}, "cell_type": "markdown", "id": "80f9cc77", "metadata": {}, "source": [ "To use the hazard map with RA2CE, we need to fill in the **[hazard]** section in the network.ini. \n", "\n", "Specify the hazard map name in the **hazard_map** parameter in network.ini. RA2CE expects the hazard map to be located in the *hazard* folder. \n", "\n", "Set the right CRS for the hazard map in the **hazard_crs** parameter. This CRS can be different from that of origins, destinations and the network. RA2CE will reproject the network to the CRS of the flood map and will reproject the network back to the original CRS when the CRS differs.\n", "\n", "*Advanced: If you have a hazard map with continous scale variables, the **aggregate_wl** parameter in analysis.ini can be set to either 'max', 'min' or 'mean' to take the maximum, minimum or mean value per network segment when the exposure of the network to a certain hazard (map) is determined.*
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "4d8e4999", "metadata": {}, "source": [ "**Network.ini content**\n", "
\n", "
\n", "> [project]
\n", "name = *project name*
\n", "
\n", "[network]
\n", "directed = False
\n", "source = OSM download
\n", "primary_file = None
\n", "diversion_file = None
\n", "file_id = rfid_c
\n", "polygon = map.geojson
\n", "network_type = drive
\n", "road_types = motorway,motorway_link,primary,primary_link,secondary,secondary_link,tertiary,tertiary_link
\n", "save_gpkg = True
\n", "
\n", "[origins_destinations]
\n", "origins = None
\n", "destinations = None
\n", "origins_names = None
\n", "destinations_names = None
\n", "id_name_origin_destination = None
\n", "origin_count = None
\n", "origin_out_fraction = None
\n", "category = None
\n", "
\n", "[hazard]
\n", "**hazard_map = max_flood_depth.tif**
\n", "hazard_id = None
\n", "hazard_field_name = waterdepth
\n", "**aggregate_wl = max**
\n", "**hazard_crs = EPSG:32736**
\n", "
\n", "*[cleanup]
\n", "snapping_threshold = None
\n", "segmentation_length = None
\n", "merge_lines = True
\n", "merge_on_id = False
\n", "cut_at_intersections = False
*\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "741b18f1", "metadata": {}, "source": [ "## Run RA2CE" ] }, { "attachments": {}, "cell_type": "markdown", "id": "74816669", "metadata": {}, "source": [ "Now, run RA2CE using the network.ini" ] }, { "cell_type": "code", "execution_count": null, "id": "89fa744e", "metadata": {}, "outputs": [], "source": [ "handler = Ra2ceHandler(network=network_ini, analysis=None)\n", "handler.configure()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "532a280f", "metadata": {}, "source": [ "## Results" ] }, { "attachments": {}, "cell_type": "markdown", "id": "91f52105", "metadata": {}, "source": [ "You can run a RA2CE analysis only performing a hazard overlay. The results can be found in the output_graph folder. The data with *'\\*_hazard'*, contains the result of the overlay with the hazard. Here, we will load the data from the example graph folder. \n", "\n", "Notice the column **EV1_ma**. This refers to the hazard. This column holds the water depth of the road segment. 'EV1' stands for 'Event 1' (you can run multiple hzard maps, the column results will be called EV1, EV2, EV3, etc.). '_ma' refers to maximum flood depth, which is the parameter specified in the network.ini. \n", "\n", "*When performing RA2CE with flooding and a road network, we often use the maximum water depth for the analysis because a vehicle can only use a road segment when it can drive through the largest water depth on that road segment.*" ] }, { "attachments": {}, "cell_type": "markdown", "id": "5ae7c590", "metadata": {}, "source": [ "*Note: when there is an existing base_graph in the output_graph folder, RA2CE will always use this. However, it can be that you want to update something to that base_graph. In that case, you first have to remove the graph from the folder manually before rerunning.* " ] }, { "cell_type": "code", "execution_count": null, "id": "621e51cc", "metadata": {}, "outputs": [], "source": [ "hazard_output = root_dir / \"static\" / \"output_graph\" / \"base_graph_hazard_edges.gpkg\"\n", "hazard_gdf = gpd.read_file(hazard_output, driver = \"GPKG\")\n", "hazard_gdf.head()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "70f2e3e6", "metadata": {}, "source": [ "Let's inspect the results visually. Below, we show the hazard values on the network. A value of 0 indicates no hazard intersection." ] }, { "cell_type": "code", "execution_count": null, "id": "9f716a91", "metadata": {}, "outputs": [], "source": [ "hazard_gdf.explore(column=\"EV1_ma\", cmap = \"Reds\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }