{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Network from OSM download\n", "This tutorial shows how to download and configure a road network from OpenStreetMap (OSM) and process it using the RA2CE library." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1. Import the Required Packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import geopandas as gpd\n", "from shapely.geometry.polygon import Polygon\n", "\n", "from ra2ce.network import RoadTypeEnum\n", "from ra2ce.network.network_config_data.enums.network_type_enum import NetworkTypeEnum\n", "from ra2ce.network.network_config_data.network_config_data import NetworkSection, NetworkConfigData\n", "from ra2ce.network.network_config_data.enums.source_enum import SourceEnum\n", "from ra2ce.ra2ce_handler import Ra2ceHandler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2. Define a Region of Interest\n", "Create a bounding box for the area of interest and save it as a geojson file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "root_dir = Path(\"data\", \"network_from_OSM\")\n", "network_path = root_dir / \"network\"\n", "\n", "polygon = Polygon([\n", " [4.925796685034555, 52.15567004009617],\n", " [4.925796685034555, 51.969875228118696],\n", " [5.263478289905265, 51.969875228118696],\n", " [5.263478289905265, 52.15567004009617],\n", " [4.925796685034555, 52.15567004009617]\n", "])\n", "\n", "# Convert polygon into geojson file\n", "gdf_polygon = gpd.GeoDataFrame(index=[0], crs=\"EPSG:4326\", geometry=[polygon])\n", "gdf_polygon_path = network_path.joinpath(\"polygon.geojson\")\n", "gdf_polygon.to_file(gdf_polygon_path, driver=\"GeoJSON\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3. Network Configuration\n", "Define the network configuration using [NetworkConfigData](../api/ra2ce.network.network_config_data.html#ra2ce.network.network_config_data.network_config_data.NetworkConfigData){.api-ref} and [NetworkSection](../api/ra2ce.network.network_config_data.html#ra2ce.network.network_config_data.network_config_data.NetworkSection){.api-ref} classes., specifying OSM as the source and selecting road types." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "network_section = NetworkSection(\n", " source=SourceEnum.OSM_DOWNLOAD,\n", " network_type=NetworkTypeEnum.DRIVE,\n", " road_types=[RoadTypeEnum.MOTORWAY, RoadTypeEnum.PRIMARY],\n", " polygon=gdf_polygon_path,\n", " save_gpkg=True,\n", ")\n", "\n", "network_config_data = NetworkConfigData(\n", " root_path=root_dir,\n", " static_path=root_dir / \"static\",\n", " network=network_section,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4. Initialize and Configure RA2CE\n", "Generate the network and store results in `static/output_graph`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "handler = Ra2ceHandler.from_config(network=network_config_data, analysis=None)\n", "handler.configure()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 5. Load and Inspect the Output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path_output_graph = root_dir.joinpath(\"static\", \"output_graph\")\n", "base_graph_edges = path_output_graph.joinpath(\"base_graph_edges.gpkg\")\n", "edges_gdf = gpd.read_file(base_graph_edges, driver=\"GPKG\")\n", "\n", "base_graph_nodes = path_output_graph.joinpath(\"base_graph_nodes.gpkg\")\n", "nodes_gdf = gpd.read_file(base_graph_nodes, driver=\"GPKG\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 6. Plot Nodes and Edges" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "fig, ax = plt.subplots(figsize=(15, 15))\n", "\n", "# Plot edges first\n", "baseplot = edges_gdf.plot(ax=ax, color=\"grey\")\n", "\n", "# Overlay nodes\n", "nodes_gdf.plot(ax=baseplot, color=\"blue\", markersize=20)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![RA2CE OSM-based network](/_resources/figures/network_osm.png)" ] } ], "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.11.13" } }, "nbformat": 4, "nbformat_minor": 5 }