{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "e0bf8629", "metadata": {}, "source": [ "# RA2CE feature: Isolated locations\n", "\n", "This notebook contains an example of the **isolated locations analysis** of the RA2CE model. This analysis can be used to assess whether locations can still reach the main part of a network during a disruption. RA2CE specifies the reason of isolation: whether the location is isolated because of link disruption close to the location or because of link disruptions in other locations further away. For example, a residential home could be disrupted in using the road network because of high water on the road right in front of the house or because they cannot leave their neighbourhood because of road flooding.\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 pandas as pd\n", "import matplotlib.pyplot as plt\n", "from pathlib import Path\n", "from ra2ce.ra2ce_handler import Ra2ceHandler #import the ra2cehandler to run ra2ce analyses\n", "import rasterio\n", "import shutil" ] }, { "attachments": {}, "cell_type": "markdown", "id": "0c5e8b7c", "metadata": {}, "source": [ "First, we will set the path to the RA2CE example folder and we will check if the network and analysis ini files indeed exist. We remove any files that might already be inside the output folders." ] }, { "cell_type": "code", "execution_count": null, "id": "0d6562dd", "metadata": {}, "outputs": [], "source": [ "root_dir = Path(\"data\",\"isolated_locations\") # set the root directory for the data\n", "\n", "network_ini = root_dir / \"network.ini\"\n", "assert network_ini.is_file()\n", "\n", "analyses_ini = root_dir / \"analyses.ini\"\n", "assert analyses_ini.is_file()\n", "\n", "output_path = root_dir / \"output\"\n", "output_graph_path = root_dir / \"static\" / \"output_graph\"\n", "\n", "for p in [output_path, output_graph_path]:\n", " if p.exists():\n", " shutil.rmtree(p)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "363f0bdb", "metadata": {}, "source": [ "## Configuring the network\n", "Firstly we initialize RA2CE and configure the network with the *network.ini* and *analyses.ini* files. See below the contents of the *network.ini* file and pay special attention to the configurations in **bold**. These are filled in especially for this isolated locations analysis." ] }, { "attachments": {}, "cell_type": "markdown", "id": "4d8e4999", "metadata": {}, "source": [ "**Network.ini content**\n", "
\n", "
\n", "> [project]
\n", "name = isolated_locations
\n", "
\n", "[network]
\n", "directed = False
\n", "**source = OSM download**
\n", "primary_file = None
\n", "diversion_file = None
\n", "file_id = None
\n", "**polygon = map.geojson**
\n", "**network_type = drive**
\n", "**road_types = motorway,motorway_link,trunk,trunk_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", "[isolation]
\n", "**locations = POI.gpkg**
\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] # Can only be used if a network is created from a shapefile
\n", "snapping_threshold = None
\n", "segmentation_length = None
\n", "merge_lines = False
\n", "merge_on_id = False
\n", "cut_at_intersections = False
*" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "As you can see in the *network.ini* file above, we submit a hazard map in the configuration file: *max_flood_depth.tif*. We will inspect this hazard map below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hazard_folder = root_dir / \"static\" / \"hazard\" # find the hazard folder where you locate your hazard map\n", "hazard_map = hazard_folder / \"fake_flood.tif\" # set the name of the hazard map\n", "\n", "# 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', vmin=0, vmax=1) # Change the colormap if desired\n", "plt.colorbar(label='Pixel Values')\n", "plt.title('hazard map')\n", "plt.show() " ] }, { "attachments": {}, "cell_type": "markdown", "id": "c02f18fa", "metadata": {}, "source": [ "Including, amongst others, this hazard map we initialize RA2CE and configure the network. This takes around 2 minutes, depending on internet and computer speed." ] }, { "cell_type": "code", "execution_count": null, "id": "9dd1e91b", "metadata": {}, "outputs": [], "source": [ "handler = Ra2ceHandler(network=network_ini, analysis=analyses_ini)\n", "handler.configure()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The resulting network (edges and nodes) including hazard overlay is shown in the map below, bounded by the region of interest (*map.geojson*) that was submitted in the *network.ini* file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# REGION\n", "region = root_dir / \"static\" / \"network\" / \"map.geojson\"\n", "region = gpd.read_file(region)\n", "m = region.explore(style_kwds={\"color\": \"purple\", \"fill\": False, \"weight\": 5})\n", "\n", "# EDGES\n", "edges_with_hazard = root_dir / \"static\" / \"output_graph\" / \"base_graph_hazard_edges.gpkg\"\n", "edges = gpd.read_file(edges_with_hazard)\n", "edges.explore(m=m, column=\"EV1_ma\", cmap=\"brg_r\", style_kwds={\"weight\": 3})\n", "\n", "# LOCATIONS TO CHECK ISOLATION\n", "locations_path = root_dir / \"static\" / \"network\" / \"POI.gpkg\"\n", "locations = gpd.read_file(locations_path)\n", "locations.explore(m=m, column=\"building\")\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Running the analysis\n", "Next, we run the isolated location analysis with ra2ce, which is already configured with the following *analyses.ini* file. See below the contents of this file." ] }, { "attachments": {}, "cell_type": "markdown", "id": "3f1e2c70", "metadata": {}, "source": [ "**Analyses.ini content**\n", "
\n", "
\n", "> [project]
\n", "name = isolated_locations
\n", "
\n", "[analysis1]
\n", "name = multilink isolated locations
\n", "analysis = multi_link_isolated_locations
\n", "aggregate_wl = max
\n", "threshold = 1
\n", "weighing = length
\n", "category_field_name = category
\n", "save_gpkg = True
\n", "save_csv = True
" ] }, { "cell_type": "code", "execution_count": null, "id": "a18f0f2a", "metadata": {}, "outputs": [], "source": [ "handler.run_analysis()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Inspect results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## ROADS\n", "m = edges.explore(column=\"EV1_ma\", cmap=\"brg_r\", style_kwds={\"weight\": 3})\n", "\n", "## ISOLATED LOCATIONS\n", "locations_results_path = root_dir / \"output\" / \"multi_link_isolated_locations\" / \"multi_link_isolated_locations.gpkg\"\n", "locations_results = gpd.read_file(locations_results_path)\n", "locations_results.explore(m=m, column=\"i_type_EV1\", cmap=\"gist_rainbow\")\n", "m" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We also check the summarized results. As you can see in the table below, most locations are isolated due to flooding. Some are isolated due to road disruptions in another part of the network." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tabulated_results_path = root_dir / \"output\" / \"multi_link_isolated_locations\" / \"multi_link_isolated_locations_results.csv\"\n", "tabulated_results = pd.read_csv(tabulated_results_path)\n", "tabulated_results.sort_values(\"i_type_EV1\")" ] } ], "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 }