diff --git a/docs/examples/basic_examples.rst b/docs/examples/basic_examples.rst index b7227d56f..7901bf59f 100644 --- a/docs/examples/basic_examples.rst +++ b/docs/examples/basic_examples.rst @@ -21,6 +21,7 @@ Below you can access some of the main examples and go through them in an off-lin ../_examples/example_isolated_locations ../_examples/example_optimal_route_origin_destination ../_examples/example_ra2ce_basics_fromgpkg + ../_examples/example_download_network_using_a_polygon .. toctree:: :maxdepth: 1 diff --git a/examples/data/download_network_from_polygon/static/network/map.geojson b/examples/data/download_network_from_polygon/static/network/map.geojson new file mode 100644 index 000000000..2a06e3ffe --- /dev/null +++ b/examples/data/download_network_from_polygon/static/network/map.geojson @@ -0,0 +1 @@ +{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"coordinates":[[[4.6901839644356755,52.26968798297585],[4.6901839644356755,51.922677612645174],[5.4646075827030245,51.922677612645174],[5.4646075827030245,52.26968798297585],[4.6901839644356755,52.26968798297585]]],"type":"Polygon"}}]} \ No newline at end of file diff --git a/examples/example_download_network_using_a_polygon.ipynb b/examples/example_download_network_using_a_polygon.ipynb new file mode 100644 index 000000000..9dbe4c358 --- /dev/null +++ b/examples/example_download_network_using_a_polygon.ipynb @@ -0,0 +1,405 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Download a Network using a polygon" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook contains examples for the RA2CE tool. We will guide you through the basics of RA2CE : Configuring a road network from a geojson file from your local drive or from polygon coordinates.\n", + "\n", + "\n", + "First of all, we will import the packages we need to execute this notebook:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from pathlib import Path\n", + "import geopandas as gpd\n", + "import folium \n", + "from shapely.geometry import LineString, Polygon, box" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next we will call the necessary RA2CE scripts for our code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import ra2ce.network.networks_utils as nut\n", + "from ra2ce.network.network_config_data.enums.network_type_enum import NetworkTypeEnum\n", + "from ra2ce.network.network_config_data.enums.road_type_enum import RoadTypeEnum\n", + "from ra2ce.network.network_config_data.network_config_data import (NetworkConfigData,NetworkSection,)\n", + "from ra2ce.network.network_wrappers.osm_network_wrapper.osm_network_wrapper import (OsmNetworkWrapper,)\n", + "from ra2ce.network.exporters.geodataframe_network_exporter import GeoDataFrameNetworkExporter\n", + "from ra2ce.network.exporters.multi_graph_network_exporter import MultiGraphNetworkExporter\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Folder structure\n", + "\n", + "Before starting the examples, it is vital that you familiarize yourself with the RA2CE folder structure. RA2CE requires this folder structure to succesfully perform analyses. You can find the folder structure in the **documentation**.\n", + "\n", + "\n", + "A project folder must contain the subfolders 'output' and 'static'. An 'input' folder is optional for additional data sets but we will not use that in our example. It must also contain the network.ini and/or analyses.ini files. Within the subfolder 'static', RA2CE expects three subfolders: hazard, network, and output_graph. See below an example folder structure of “Project A”. This folder structure must be created by the user before running RA2CE." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Below you can add the path to your project directory to your RA2CE analysis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# specify the name of the path to the project folder where you created the RA2CE folder setup\n", + "root_dir = Path('data','download_network_from_polygon')\n", + "assert root_dir.exists()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Method A : Configuring a road network from OSM using a polygon" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First we need to define the polygon. This should be a shapely polygon.\n", + "In this notebook, the polygon is defined through geojson coordinates. These coordinates you can copy paste from https://geojson.io/#map=2/0/20" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Defining a polygon" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "geojson_data = {\n", + " \"type\": \"FeatureCollection\",\n", + " \"features\": [\n", + " {\n", + " \"type\": \"Feature\",\n", + " \"properties\": {},\n", + " \"geometry\": {\n", + " \"coordinates\": [\n", + " [\n", + " [\n", + " 4.925796685034555,\n", + " 52.15567004009617\n", + " ],\n", + " [\n", + " 4.925796685034555,\n", + " 51.969875228118696\n", + " ],\n", + " [\n", + " 5.263478289905265,\n", + " 51.969875228118696\n", + " ],\n", + " [\n", + " 5.263478289905265,\n", + " 52.15567004009617\n", + " ],\n", + " [\n", + " 4.925796685034555,\n", + " 52.15567004009617\n", + " ]\n", + " ]\n", + " ],\n", + " \"type\": \"Polygon\"\n", + " }\n", + " }\n", + " ]\n", + "}\n", + "\n", + "minx, miny = geojson_data['features'][0]['geometry']['coordinates'][0][0]\n", + "maxx, maxy = geojson_data['features'][0]['geometry']['coordinates'][0][2]\n", + "\n", + "polygon = box(minx, miny, maxx, maxy)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's explore the location of the polygon:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "gdf_polygon = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon])\n", + "gdf_polygon.explore(tiles=\"CartoDB positron\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Set parameters and initalize a RA2CE run" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "Next we need to define the Network characteristics that needs to be downloaded\n", + "\n", + "First we choose what kind of network that we need for the analysis. This is handled by the variable network_type in this notebook.\n", + "A network type can be :NONE/WALK/BIKE/DRIVE/DRIVE_SERVICE/ALL/INVALID \n", + "\n", + "The we can specify the resolution of the selected network by defining road_types.\n", + "Road types includes :NONE,MOTORWAY,MOTORWAY_LINK,TRUNK,TRUNK_LINK,PRIMARY,PRIMARY_LINK,SECONDARY,SECONDARY_LINK,TERTIARY,TERTIARY_LINK,RESIDENTIAL,ROAD,TUNNEL,BRIDGE,CULVERT,UNCLASSIFIED,INVALID\n", + "\n", + "After that we can start accessing the data from OSM. \n", + "Bear in mind that larger regions and / or the more detail included in the road network may increase download time (significantly)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "#First we define which roads we will want to download from OSM to create a network with\n", + "_network_section = NetworkSection(\n", + " network_type=NetworkTypeEnum.DRIVE,\n", + " road_types=[RoadTypeEnum.MOTORWAY,RoadTypeEnum.PRIMARY,RoadTypeEnum.ROAD], \n", + ")\n", + "\n", + "_network_config_data = NetworkConfigData(\n", + " root_path= root_dir,\n", + " output_path= root_dir/\"output\",\n", + " network=_network_section,\n", + " )\n", + "\n", + "#pass the specified sections as arguments for configuration\n", + "_network_config_data = NetworkConfigData(network=_network_section, static_path=root_dir.joinpath('static'))\n", + "#download the network based on the polygon extent and the specified road characteristics\n", + "_graph,_gdf = OsmNetworkWrapper.get_network_from_polygon(_network_config_data, polygon)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Explore the results" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lets explore the downloaded network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = _gdf.explore(column='highway', tiles=\"CartoDB positron\")\n", + "folium.GeoJson(gdf_polygon,\n", + " style_function=lambda x: {'color': 'grey', 'fillOpacity': 0.05, 'weight': 3},\n", + " tooltip='polygon').add_to(m)\n", + "\n", + "folium.LayerControl().add_to(m)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Method B : Configuring a road network from OSM using a geojson file from a local drive" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also use a geojson file in your local drive to to carry out the same process.\n", + "\n", + "An easy way to use this option is by drawing a polygon at the location of choice at [geojson.io](http://geojson.io/). \n", + "Another option is creating a polygon layer in a GIS and saving it as a *.geojson*.\n", + "\n", + "The geojson polygon should be saved in the **static/network** folder." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Add the path to your Geojson file. \n", + "_geojson_path = root_dir / 'static'/'network'/\"map.geojson\"\n", + "\n", + "#visualize the polygon\n", + "gdf_polygon = gpd.read_file(_geojson_path)\n", + "gdf_polygon = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon])\n", + "gdf_polygon.explore(tiles=\"CartoDB positron\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Set parameters and initalize a RA2CE run" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then you can specify the network characteristics the same way as defined in Method A" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#First we define which roads we will want to download from OSM to create a network with\n", + "_network_section = NetworkSection(\n", + " polygon= Path(_geojson_path),\n", + " network_type=NetworkTypeEnum.DRIVE,\n", + " road_types=[RoadTypeEnum.MOTORWAY,RoadTypeEnum.PRIMARY,RoadTypeEnum.ROAD], \n", + " save_gpkg= True,\n", + " \n", + ")\n", + "\n", + "#pass the specified sections as arguments for configuration\n", + "_network_config_data = NetworkConfigData(\n", + " root_path= root_dir,\n", + " output_path= root_dir/\"output\",\n", + " static_path=root_dir.joinpath('static'),\n", + " network=_network_section,\n", + " )\n", + "\n", + "#download the network based on the polygon extent and the specified road characteristics\n", + "_graph,_gdf = OsmNetworkWrapper.get_network_from_geojson(_network_config_data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Explore the results" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lets explore the downloaded network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = _gdf.explore(column='highway', tiles=\"CartoDB positron\")\n", + "folium.GeoJson(gpd.read_file(_geojson_path),\n", + " style_function=lambda x: {'color': 'grey', 'fillOpacity': 0.05, 'weight': 3},\n", + " tooltip='polygon').add_to(a)\n", + "\n", + "folium.LayerControl().add_to(a)\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Finally we can save our downloaded network to the RA2CE folder setup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, you can save the network that you downloaded through Method A or Method B to the output_graph folder in the RA2CE folder setup" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Export the graph\n", + "_exporter = MultiGraphNetworkExporter(basename='base_graph', export_types=['gpkg', 'pickle'])\n", + "_exporter.export(export_path=root_dir.joinpath('static','output_graph'), export_data=_graph)\n", + "\n", + "# Export the network\n", + "_exporter = GeoDataFrameNetworkExporter(basename='base_network', export_types=['gpkg', 'pickle'])\n", + "_exporter.export(export_path=root_dir.joinpath('static','output_graph'), export_data=_gdf)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "ra2ce_env", + "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": 2 +}