{ "cells": [ { "cell_type": "markdown", "id": "0875d8ca", "metadata": {}, "source": [ "# Network from shapefile\n", "This tutorial shows how to create a road network from a shapefile using **RA2CE**.\n", "You can use this workflow when you already have a geospatial dataset (instead of downloading from OpenStreetMap).\n", "\n", "> **Note:** Make sure your shapefile contains valid road geometries (e.g. `LineString`) and that it’s projected in a suitable CRS (e.g. UTM). For more information on projections, see the [CRS documentation](https://proj.org/en/)." ] }, { "cell_type": "markdown", "id": "88f0fed7", "metadata": {}, "source": [ "## Step 1. Import the Required Packages" ] }, { "cell_type": "code", "execution_count": null, "id": "48ee6a29", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import geopandas as gpd\n", "import matplotlib.pyplot as plt\n", "\n", "from ra2ce.network.network_config_data.network_config_data import (\n", " NetworkSection, NetworkConfigData\n", ")\n", "from ra2ce.network.network_config_data.enums.source_enum import SourceEnum\n", "from ra2ce.ra2ce_handler import Ra2ceHandler" ] }, { "cell_type": "markdown", "id": "dcff0dec", "metadata": {}, "source": [ "## Step 2. Define Paths and Network Configuration\n", "Indicate the path to the root directory of your project and to the network shapefile. We recommend to follow the structure shown in the [Getting Started Guide](../getting_started/index.rst).\n", "\n", "As a user, define the network configuration using the [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." ] }, { "cell_type": "code", "execution_count": null, "id": "5c6d9bd9", "metadata": {}, "outputs": [], "source": [ "root_dir = Path(\"data\", \"network_from_shapefile\")\n", "network_path = root_dir / \"network\"\n", "\n", "network_section = NetworkSection(\n", " source=SourceEnum.SHAPEFILE,\n", " primary_file=network_path.joinpath(\"base_shapefile.shp\"),\n", " save_gpkg=True,\n", ")\n", "\n", "network_config_data = NetworkConfigData(\n", " root_path=root_dir,\n", " static_path=root_dir.joinpath(\"static\"),\n", " network=network_section,\n", ")" ] }, { "cell_type": "markdown", "id": "92f75ddd", "metadata": {}, "source": [ "## Step 3. Initialize and Configure RA2CE\n", "Running the `configure` method from the `Ra2ceHandler` will generate the network and store the results in the `static/output_graph` folder." ] }, { "cell_type": "code", "execution_count": null, "id": "2402337a", "metadata": {}, "outputs": [], "source": [ "handler = Ra2ceHandler.from_config(network=network_config_data, analysis=None)\n", "handler.configure()" ] }, { "cell_type": "markdown", "id": "4dc0829f", "metadata": {}, "source": [ "## Step 4. Load and Inspect the Output\n", "A few geopackages are created in the `static/output_graph` folder, you can load and inspect them using `geopandas`." ] }, { "cell_type": "code", "execution_count": null, "id": "51a6bae9", "metadata": {}, "outputs": [], "source": [ "path_output_graph = root_dir / \"static\" / \"output_graph\"\n", "base_graph_edges = path_output_graph / \"base_graph_edges.gpkg\"\n", "edges_gdf = gpd.read_file(base_graph_edges, driver = \"GPKG\")\n", "\n", "base_graph_nodes = path_output_graph / \"base_graph_nodes.gpkg\" #specify where to find the nodes file\n", "nodes_gdf = gpd.read_file(base_graph_nodes, driver = \"GPKG\") #read in the nodes file" ] }, { "cell_type": "markdown", "id": "a2c79a94", "metadata": {}, "source": [ "## Step 5. Plot Nodes and Edges" ] }, { "cell_type": "code", "execution_count": null, "id": "63af8358", "metadata": {}, "outputs": [], "source": [ "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", "id": "1f461605", "metadata": {}, "source": [ "![RA2CE shapefile-based network](/_resources/figures/network_shapefile.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 }