ra2ce.network.network_wrappers.osm_network_wrapper package#

Submodules#

ra2ce.network.network_wrappers.osm_network_wrapper.extremities_data module#

GNU GENERAL PUBLIC LICENSE

Version 3, 29 June 2007

Risk Assessment and Adaptation for Critical Infrastructure (RA2CE). Copyright (C) 2023 Stichting Deltares

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

class ExtremitiesData(from_id=None, to_id=None, from_to_id=None, to_from_id=None, from_to_coor=None, to_from_coor=None)[source]#

Bases: object

Parameters:
  • from_id (int)

  • to_id (int)

  • from_to_id (tuple)

  • to_from_id (tuple)

  • from_to_coor (tuple)

  • to_from_coor (tuple)

static arrange_extremities_data(from_node_id, to_node_id, graph)[source]#
Parameters:
  • from_node_id (int)

  • to_node_id (int)

  • graph (MultiDiGraph)

static find_node_id_by_coor(graph, target_x, target_y)[source]#

finds the node in unique graph with the same coor

Parameters:
  • graph (MultiDiGraph)

  • target_x (float)

  • target_y (float)

from_id: int = None#
from_to_coor: tuple = None#
from_to_id: tuple = None#
static get_extremities_data_for_sub_graph(from_node_id, to_node_id, sub_graph, graph, shared_elements)[source]#

Both extremities should be in the unique_graph still makes an edge between similar node to u (the node with u coordinates and different id, included in the unique_graph) and v Here, sub_graph is the unique_graph and graph is complex_graph Shared elements are shared btw sub_graph and graph, which are elements to include when dropping duplicates

Parameters:
  • from_node_id (int)

  • to_node_id (int)

  • sub_graph (MultiDiGraph)

  • graph (MultiDiGraph)

  • shared_elements (set)

to_from_coor: tuple = None#
to_from_id: tuple = None#
to_id: int = None#

ra2ce.network.network_wrappers.osm_network_wrapper.osm_network_wrapper module#

GNU GENERAL PUBLIC LICENSE

Version 3, 29 June 2007

Risk Assessment and Adaptation for Critical Infrastructure (RA2CE). Copyright (C) 2023 Stichting Deltares This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

class OsmNetworkWrapper(config_data)[source]#

Bases: NetworkWrapperProtocol

Parameters:

config_data (NetworkConfigData)

static drop_duplicates(complex_graph)[source]#
Return type:

MultiDiGraph

Parameters:

complex_graph (MultiDiGraph)

static drop_duplicates_in_edges(unique_elements, unique_graph, graph)[source]#

Checks if both extremities are in the unique_graph (u has not the same coor of v, no line from u to itself is allowed). Checks if an edge is already made between such extremities with the given id and coordinates before considering it in the unique graph

Parameters:
  • unique_elements (set)

  • unique_graph (MultiDiGraph)

  • graph (MultiDiGraph)

static drop_duplicates_in_nodes(unique_elements, graph)[source]#
Return type:

MultiDiGraph

Parameters:
  • unique_elements (set)

  • graph (MultiDiGraph)

static get_clean_graph(complex_graph)[source]#
Parameters:

complex_graph (MultiDiGraph)

get_network()[source]#

Gets a network built within this wrapper instance. No arguments are accepted, the __init__ method is meant to assign all required attributes for a wrapper.

Return type:

tuple[MultiGraph, GeoDataFrame]

Returns:

tuple[MultiGraph, GeoDataFrame]: Tuple of MultiGraph representing the graph and GeoDataFrame representing the network.

static get_network_from_geojson(config_data)[source]#

Gets a valid network (MultiGraph and GeoDataFrame) for the given network configuration, given that its network section contains a valid path for the polygon property.

Args:

config_data (NetworkConfigData): Network data configuration required for OSM download.

Raises:

ValueError: When the NetworkConfigData does not contain a valid polygon path for its network section.

Returns:

tuple[MultiGraph, GeoDataFrame]: Resulting network representations.

Parameters:

config_data (NetworkConfigData)

static get_network_from_polygon(config_data, polygon)[source]#

Gets a valid network (MultiGraph and GeoDataFrame) for the given network configuration and boundary box (represented by a shapely.BaseGeometry).

Return type:

tuple[MultiGraph, GeoDataFrame]

Parameters:
Args:

config_data (NetworkConfigData): Network data configuration required for OSM download. polygon (BaseGeometry): Polygon representing the boundary box.

Returns:

tuple[MultiGraph, GeoDataFrame]: Resulting network representations.

network_type: NetworkTypeEnum#
polygon_graph: MultiDiGraph#
road_types: list[RoadTypeEnum]#
static snap_nodes_to_edges(graph, threshold)[source]#
Return type:

MultiDiGraph

Parameters:
  • graph (MultiDiGraph)

  • threshold (float)

static snap_nodes_to_nodes(graph, threshold)[source]#
Return type:

MultiDiGraph

Parameters:
  • graph (MultiDiGraph)

  • threshold (float)

classmethod with_polygon(config_data, polygon)[source]#

Gets an OsmNetworkWrapper with the given polygon transformed into a clean graph as the polygon_graph property.

Return type:

OsmNetworkWrapper

Parameters:
Args:

config_data (NetworkConfigData): Network data configuration required for OSM download. polygon (BaseGeometry): Base polygon from which to generate the graph.

Returns:

OsmNetworkWrapper: Wrapper with valid polygon_graph property.

ra2ce.network.network_wrappers.osm_network_wrapper.osm_utils module#

GNU GENERAL PUBLIC LICENSE

Version 3, 29 June 2007

Risk Assessment and Adaptation for Critical Infrastructure (RA2CE). Copyright (C) 2023 Stichting Deltares

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

create_edge(graph, u, v, data)[source]#

Create an edge in the graph if it does not already exist.

Return type:

MultiDiGraph

Parameters:
  • graph (MultiDiGraph)

  • u (int)

  • v (int)

  • data (dict)

Args:

graph (nx.MultiDiGraph): The graph to update. u (int): Starting node of the edge. v (int): Ending node of the edge.

Returns:

nx.MultiDiGraph: The updated graph containing the new edge, or the original graph if the edge already exists.

create_edges(graph, u, v, key_data)[source]#

Create or update edges between nodes u and v in the graph with the given data.

Return type:

MultiDiGraph

Parameters:
  • graph (MultiDiGraph)

  • u (int)

  • v (int)

  • key_data (dict)

Args:

graph (nx.MultiDiGraph): The graph to modify. u (int): The ID of the source node. v (int): The ID of the target node. data (dict): The attributes for the new edge(s).

Returns:

nx.MultiDiGraph: The modified graph.

find_existing_node(graph, new_node)[source]#

Check if a newly created node already exists in a graph.

Return type:

tuple[int, dict] | tuple[None, None]

Parameters:
  • graph (MultiDiGraph)

  • new_node (Point)

Args:

graph (nx.MultiDiGraph): The graph in which to search for the node. new_node (Point): A Shapely Point representing the new node.

Returns:

tuple[int, dict] | tuple[None, None]: Returns (node_id, node_data) if the node exists, or (None, None) if it does not exist.

from_shapefile_to_poly(shapefile, out_path, outname='')[source]#

This function will create the .poly files from an input shapefile. If the shapefile contains multiple polygons, this function creates a seperate .polygon file for each region .poly files can then be used to extract data from the openstreetmap files.

This function is adapted from the OSMPoly function in QGIS, and Elco Koks GMTRA model. This code is maintained on a GitHub repository: github.com/keesvanginkel/OSdaMage

Arguments:

shapefile (string/Pathlib Path) : path to the shapefile out_path (string/Pathlib Path): path to the directory where the .poly files should be written outname (string) : optional prefix to add to outfile name

Returns:

.poly file for each region, in a new dir in the working directory (in the CRS of te input file)

Parameters:
  • shapefile (Path)

  • out_path (Path)

  • outname (str)

get_node_nearest_edge(graph, node, return_geom=True, return_dist=True)[source]#

Based on osmnx.

Return type:

dict

Parameters:
  • graph (MultiDiGraph)

  • node (tuple)

graph_to_gdf(graph, nodes, edges, node_geometry, fill_edge_geometry)[source]#
Return type:

GeoDataFrame

Parameters:
  • graph (Graph)

  • nodes (bool)

  • edges (bool)

  • node_geometry (bool)

  • fill_edge_geometry (bool)

is_endnode_check(graph, node_id)[source]#

Based on osmnx. osmnx rules 3 and 4 are removed. Hence, the name is_endpoint_simplified. Determine if a node is a true endpoint of an edge.

Return True if the node is a “true” endpoint of an edge in the network, otherwise False. OpenStreetMap’s data includes many nodes that exist only as geometric vertices to allow ways to curve. A true edge endpoint is a node that satisfies at least 1 of the following 4 rules:

  1. It is its own neighbor (ie, it self-loops).

2) Or, it has no incoming edges or no outgoing edges (ie, all its incident edges are inbound or all its incident edges are outbound).

graph : networkx.MultiDiGraph input graph node_id : int the node to examine

Return type:

bool

Parameters:
  • graph (MultiDiGraph)

  • node_id (int)

modify_graph(graph, node_nearest_edge_data)[source]#
Parameters:
  • graph (MultiDiGraph)

  • node_nearest_edge_data (dict)

remove_key(element_data, keys_to_exclude)[source]#

Removes keys such as geometry information from the new node data to be created

Return type:

dict

Parameters:
  • element_data (dict)

  • keys_to_exclude (list)

Args:

element_data (dict): dict, node or edge data from a networkx.Graph object. keys_to_exclude (list): list, information key to remove from element_data.

Returns:

dict, filtered element_data for the new node or edge to be created in a networkx.Graph object

update_edges_by_node(graph, node_edges, node_id, new_node_geom)[source]#

Update connected edges of a node by replacing node_id and node_geom with new_node_id and new_node_geom.

Return type:

None

Parameters:
  • graph (MultiDiGraph)

  • node_edges (list)

  • node_id (int)

  • new_node_geom (dict)

Args:

node_edges: edges connected to the node in teh original graph to modify graph (nx.MultiDiGraph): The graph to modify. node_id (int): The ID of the node to update. new_node_geom (dict): Dictionary containing geometry information for the new node.

Returns:

None

Module contents#