pyorps.graph
Graph operations and routing algorithms for optimal path finding.
This module provides: 1. The main RasterGraph class for creating paths on cost surfaces 2. Path and PathCollection classes for storing and analyzing paths 3. Dynamic loading of graph implementations via get_graph_api_class
- class pyorps.graph.PathFinder(dataset_source, source_coords, target_coords, search_space_buffer_m=None, neighborhood_str='r2', steps=None, ignore_max_cost=True, graph_api='cython', cost_assumptions=None, datasets_to_modify=None, crs=None, bbox=None, mask=None, transform=None, raster_save_path=None, **kwargs)[source]
Bases:
objectA class that encapsulates RasterReader and graph-based routing capabilities.
This class provides functionality to: 1. Read raster data using RasterReader or create a raster using GeoRasterizer 2. Create a graph representation of the raster with a defined Graph library 3. Find the shortest paths between coordinates 4. Convert resulting paths of graph node indices back to coordinates 5. Create GeoDataFrames of paths and export to other geo-formats for further analysis
The class supports various graph APIs to create a graph from a raster.
Initialize the RasterGraph with a dataset source and routing parameters.
- Parameters:
dataset_source (str | dict | GeoDataFrame | GeoSeries | ndarray) – Either: - Path to a file (str) - Tuple of (data_array, crs, transform) - GeoDataset object - Dictionary with url/layer for WFS
source_coords (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None) – CoordinateInput Can be: tuple, list of tuples, array of arrays, shapely Point, shapely MultiPoint, GeoSeries of points, or GeoDataFrame of points.
target_coords (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None) – CoordinateInput Can be: tuple, list of tuples, array of arrays, shapely Point, shapely MultiPoint, GeoSeries of points, or GeoDataFrame of points.
search_space_buffer_m (float | None) – Buffer around the source and target coordinates in meters. If set to 0, the entire Raster will be considered!
neighborhood_str (str | int | None) – Neighborhood type. Defaults to “r2”.
steps (ndarray[int] | None) – Steps which define the neighborhood. If None, will be created from neighborhood_str.
ignore_max_cost (bool) – Whether to ignore all cells in the raster which have the maximum cost value or not
graph_api (str) –
Graph API to use. Available graph libraries:
”networkit” (default), “rustworkx”, “igraph”, “networkx”
cost_assumptions (dict | str | CostAssumptions | None) – Cost assumptions to use for rasterization. Required if dataset_source is vector data.
datasets_to_modify (list[dict[str, Any]] | None) – List of datasets to use to modify the raster using GeoRasterizer.modify_raster_from_dataset
crs (str | None) – The coordinate reference system to be used as project crs (crs of the dataset_source and all other datasets will be converted to this crs)
bbox (Polygon | GeoDataFrame | GeoSeries | tuple[float, float, float, float] | None) – The bounding box to be used as project bounding box. Defines the area in which path finding is processed.
mask (Polygon | GeoDataFrame | tuple | None) – Defines the area in which path finding is processed similar to the bbox parameter. In this case a more complex Polygon, a Multipolygon or even a GeoSeries/GeoDataFrame with multiple Polygons can be used to define the search space for path finding.
transform (Affine | None) – Affine transformation describing the transform of a RasterDataset. Can be used ia a raster dataset is passed directly to dataset_source.
raster_save_path (str | None) – Path to save the raster dataset to.
**kwargs – Additional keyword arguments to pass to the rasterize function of the RasterHandler (if a VectorDataset or a source to a VectorDataset has been provided with dataset_source) or to the load function of the RasterDataset (if a source to a RasterDataset has been provided with dataset_source).
Minimal example: >>> from pyorps import PathFinder >>> source = (472000, 5593400) >>> target = (472800, 5594000) >>> raster_path = r”./data/raster/sample_raster.tiff” >>> path_finder = PathFinder( >>> dataset_source=raster_path, >>> source_coords=source, >>> target_coords=target, >>> ) >>> path_finder.find_route() Path(path_id=0, source=(472000, 5593400), target=(472800, 5594000),
total_length=1192.43, total_cost=133578.05)
- __init__(dataset_source, source_coords, target_coords, search_space_buffer_m=None, neighborhood_str='r2', steps=None, ignore_max_cost=True, graph_api='cython', cost_assumptions=None, datasets_to_modify=None, crs=None, bbox=None, mask=None, transform=None, raster_save_path=None, **kwargs)[source]
Initialize the RasterGraph with a dataset source and routing parameters.
- Parameters:
dataset_source (str | dict | GeoDataFrame | GeoSeries | ndarray) – Either: - Path to a file (str) - Tuple of (data_array, crs, transform) - GeoDataset object - Dictionary with url/layer for WFS
source_coords (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None) – CoordinateInput Can be: tuple, list of tuples, array of arrays, shapely Point, shapely MultiPoint, GeoSeries of points, or GeoDataFrame of points.
target_coords (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None) – CoordinateInput Can be: tuple, list of tuples, array of arrays, shapely Point, shapely MultiPoint, GeoSeries of points, or GeoDataFrame of points.
search_space_buffer_m (float | None) – Buffer around the source and target coordinates in meters. If set to 0, the entire Raster will be considered!
neighborhood_str (str | int | None) – Neighborhood type. Defaults to “r2”.
steps (ndarray[int] | None) – Steps which define the neighborhood. If None, will be created from neighborhood_str.
ignore_max_cost (bool) – Whether to ignore all cells in the raster which have the maximum cost value or not
graph_api (str) –
Graph API to use. Available graph libraries:
”networkit” (default), “rustworkx”, “igraph”, “networkx”
cost_assumptions (dict | str | CostAssumptions | None) – Cost assumptions to use for rasterization. Required if dataset_source is vector data.
datasets_to_modify (list[dict[str, Any]] | None) – List of datasets to use to modify the raster using GeoRasterizer.modify_raster_from_dataset
crs (str | None) – The coordinate reference system to be used as project crs (crs of the dataset_source and all other datasets will be converted to this crs)
bbox (Polygon | GeoDataFrame | GeoSeries | tuple[float, float, float, float] | None) – The bounding box to be used as project bounding box. Defines the area in which path finding is processed.
mask (Polygon | GeoDataFrame | tuple | None) – Defines the area in which path finding is processed similar to the bbox parameter. In this case a more complex Polygon, a Multipolygon or even a GeoSeries/GeoDataFrame with multiple Polygons can be used to define the search space for path finding.
transform (Affine | None) – Affine transformation describing the transform of a RasterDataset. Can be used ia a raster dataset is passed directly to dataset_source.
raster_save_path (str | None) – Path to save the raster dataset to.
**kwargs – Additional keyword arguments to pass to the rasterize function of the RasterHandler (if a VectorDataset or a source to a VectorDataset has been provided with dataset_source) or to the load function of the RasterDataset (if a source to a RasterDataset has been provided with dataset_source).
Minimal example: >>> from pyorps import PathFinder >>> source = (472000, 5593400) >>> target = (472800, 5594000) >>> raster_path = r”./data/raster/sample_raster.tiff” >>> path_finder = PathFinder( >>> dataset_source=raster_path, >>> source_coords=source, >>> target_coords=target, >>> ) >>> path_finder.find_route() Path(path_id=0, source=(472000, 5593400), target=(472800, 5594000),
total_length=1192.43, total_cost=133578.05)
- calculate_path_metrics(path_indices, path)[source]
Calculate metrics about the path and add directly to the Path object.
- Parameters:
path_indices – List of node indices for the path.
path – Path object to update with metrics.
- create_path_geodataframe()[source]
Create a GeoDataFrame containing all stored paths.
- Returns:
GeoDataFrame containing path data, or None if no paths available
- create_raster_handler(cost_assumptions=None, datasets_to_modify=None, raster_save_path=None, **kwargs)[source]
Create a RasterReader object for the specified file and parameters.
- Parameters:
cost_assumptions (dict | str | CostAssumptions | None) – Cost assumptions to use for rasterization. Required if dataset_source is vector data
datasets_to_modify (list[dict[str, Any]] | None) – List of datasets to use to modify the raster using GeoRasterizer.modify_raster_from_dataset
raster_save_path (str | None) – Path to save the raster dataset to.
- Returns:
The created RasterReader object
- Return type:
RasterReader
- find_route(source=None, target=None, algorithm='dijkstra', calculate_metrics=True, pairwise=False, raster_parameters=None, **kwargs)[source]
Find the shortest path between source and target coordinates.
- Parameter:
- source: CoordinateInput - Source coordinates. If None, uses the
source_coords provided at initialization. Can be: tuple, list of tuples, array of arrays, shapely Point, shapely MultiPoint, GeoSeries of points, or GeoDataFrame of points.
- target: Target coordinates. If None, uses the target_coords provided at
initialization. Can be a single pair (x, y) or a list of pairs [(x1, y1), (x2, y2), …].
algorithm: Algorithm to use for shortest path. Defaults to “dijkstra”. calculate_metrics: Whether to calculate path metrics. Defaults to True. pairwise: Whether to calculate paths pairwise (requires equal number of
sources and targets). Default is False.
- Returns:
Dictionary or list of dictionaries containing path information
- Parameters:
source (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None)
target (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None)
algorithm (str)
calculate_metrics (bool)
pairwise (bool)
- Return type:
- get_node_indices_from_coords(coords)[source]
Convert coordinates to node indices.
- Parameters:
coords (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]]) – Either: - A single coordinate pair (x, y) - A list of coordinate pairs [(x1, y1), (x2, y2), …]
- Returns:
List of node indices.
- Return type:
int | int32 | int64 | uint32 | uint64 | list[int | int32 | int64 | uint32 | uint64] | ndarray[int] | list[list[int | int32 | int64 | uint32 | uint64] | ndarray[int]]
- get_path(path_id=None, source=None, target=None)[source]
Retrieve a stored path by ID, or by source AND target.
- Parameters:
path_id – Numerical ID of the path
source – Source coordinates to search for
target – Target coordinates to search for
- Returns:
Path object or None if not found
- static normalize_coordinates(input_data)[source]
Normalize different coordinate formats into tuples or lists of tuples.
- Parameters:
input_data (tuple[float, float] | list[float] | list[tuple[float, float] | list[float]] | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame | None) – Can be a tuple, a list of tuples, an array of arrays, a shapely Point, a shapely MultiPoint, a GeoSeries of points, or a GeoDataFrame of points.
- Returns:
- A single coordinate tuple (x, y) or list of coordinate
tuples [(x1, y1), (x2, y2), …]
- Return type:
CoordinateOutput
- plot_paths(paths=None, plot_all=True, subplots=True, subplot_size=(10, 8), source_color='green', target_color='red', path_colors=None, source_marker='o', target_marker='x', path_line_width=2, show_raster=True, title=None, sup_title=None, path_id=None, reverse_colors=False)[source]
Plot paths with customizable styling and layout options.
This method visualizes the calculated paths, allowing for detailed customization of the plot appearance. It delegates to the PathPlotter class to handle the actual visualization.
- Parameters:
paths (Path | PathCollection | list[Path] | None) – Specific path(s) to plot. If None, uses all paths in this PathFinder instance. Can be a single Path object, a list of Path objects, or a PathCollection.
plot_all (bool) – If True, plots all paths. If False, plots only the path with path_id.
subplots (bool) – If True and multiple paths are plotted, creates separate subplots for each path.
subplot_size (tuple[int, int]) – Size of each individual subplot in inches (width, height).
source_color (str) – Color for source markers.
target_color (str) – Color for target markers.
path_colors (str | list[str] | None) – Colors for path lines. Can be a single color or a list of colors. If None, default color scheme is used.
source_marker (str) – Marker style for source points.
target_marker (str) – Marker style for target points.
path_line_width (int) – Line width for the paths.
show_raster (bool) – Whether to display the raster data as background.
title (str | list[str] | None) – Title for the plot or individual subplot titles if a list is provided.
sup_title (str | None) – Overall title for the figure (when using multiple subplots).
path_id (list[int] | int | None) – ID of specific path to plot when plot_all is False. Can be a single ID or a list of IDs.
reverse_colors (bool) – Whether to reverse the color scheme for raster data (dark=low cost, bright=high cost).
- Returns:
The matplotlib axes object(s) for the plot. Returns a list of axes if multiple subplots are created, otherwise returns a single axes object.
- Return type:
- Runtime Notes:
The plotting operation itself is generally quick (0.1-0.5 seconds)
Most time is spent on data preparation in the initial PathFinder setup
When plotting many paths, using subplots=True can improve readability
Displaying the raster background (show_raster=True) adds minimal overhead once the PathFinder is initialized
- save_paths(save_file_path=None)[source]
Save all calculated paths to a file in a GIS-compatible format.
This method creates a GeoDataFrame containing all paths from the PathCollection and saves it to the specified file. The file format is automatically determined from the file extension (e.g., ‘.shp’ for Shapefile, ‘.gpkg’ for GeoPackage).
- Parameters:
save_file_path (str | None) – Path to save the paths file. If None, no file is saved. Common formats include: - Shapefile (.shp) - GeoPackage (.gpkg) - GeoJSON (.geojson) - CSV (.csv)
- Returns:
None
- Return type:
None
Notes
The saved file includes all path attributes (ID, length, cost data)
The geometries are saved as LineString features with the CRS from the
source dataset - If no paths have been calculated, an empty GeoDataFrame will be created first
- save_raster(save_path=None)[source]
Save the raster data used for path calculations to a GeoTIFF file.
This method exports the current raster data to the specified file location. The raster contains the cost values used for path calculations, including any modifications from additional datasets. The exported file includes complete geo referencing information and preserves the original CRS.
- Parameters:
save_path (str | None) – Path where the raster file should be saved. If None, uses the default filename “pyorps_raster.tiff” in the current directory.
- Returns:
None
- Return type:
None
Notes
The saved raster includes all cost modifications from additional datasets
The file is saved in GeoTIFF format which preserves geo referencing
information - If the PathFinder uses a GeoRasterizer, the complete raster is saved - Otherwise, only the section loaded in the RasterHandler is saved - For large areas, the resulting file size may be substantial
- pyorps.graph.get_graph_api_class(graph_api)[source]
Return the graph API class based on the selected graph API using pattern matching.
- Parameters:
graph_api (str) – The name of the graph API to use (“networkit”, “igraph”,
installed! ("networkx" or "rustworkx). Respective graph library must be)
automatically. (Networkit is a dependency of pyorps and will be installed)
- Returns:
The corresponding graph API class.
- Return type:
class
- Raises:
ImportError – If the specified graph API module cannot be imported.
ValueError – If the specified graph API is not supported.
- class pyorps.graph.Path(source, target, algorithm, graph_api, path_indices, path_coords, path_geometry, euclidean_distance, runtimes, path_id, search_space_buffer_m, neighborhood, total_length=None, total_cost=None, length_by_category=None, length_by_category_percent=None)[source]
Bases:
objectDataclass representing a path in a raster graph. Used as container for all path metrics and information.
- Parameters:
- __init__(source, target, algorithm, graph_api, path_indices, path_coords, path_geometry, euclidean_distance, runtimes, path_id, search_space_buffer_m, neighborhood, total_length=None, total_cost=None, length_by_category=None, length_by_category_percent=None)
- Parameters:
- Return type:
None
- __str__()[source]
Return a string representation of the path including the path_id, source and target, as well as the path’s total length and total cost.
- Returns:
A string representation of the path.
- Return type:
- to_geodataframe_dict()[source]
Convert Path object to a dictionary suitable for GeoDataFrame creation.
- Returns:
dictionary with path data formatted for GeoDataFrame
- Return type:
- path_geometry: LineString
- class pyorps.graph.PathCollection[source]
Bases:
objectContainer for Path objects with O(1) retrieval by path ID and O(n) lookup for source and target information. Paths can be added with new id by replacing a Path object with the same ID already existing in th PathCollection.
Create an empty PathCollection for collecting Paths with their IDs in a dictionary.
- __eq__(other)[source]
Check if PathCollections are equal. They do not have to be in the same order to be equal!
- Return type:
- __init__()[source]
Create an empty PathCollection for collecting Paths with their IDs in a dictionary.
- add(path, replace=False)[source]
Add a path to the PathCollection. If the Path’s path_id is None or if replace is False, the path_id of the Path object will set to self._next_id and self._next_id will be incremented. If the Path’s path_id is not None and replace is True, a Path with the same path_id (if present) will be replaced with the new Path object.
- property all
Return all Path objects from the values of the PathCollection’s _paths dictionary as a list.
- Returns:
A list of all Path objects in the PathCollection.
- get(path_id=None, source=None, target=None)[source]
Retrieve a stored path by ID, or by source AND target.
- Parameters:
path_id (int) – The ID of the Path object to retrieve (must be None if path should be found by source and target)
source (Any) – The source Path object to retrieve (only used if path_id is None and target os set too; neglected otherwise)
target (Any) – The target Path object to retrieve (only used if path_id is None and target os set too; neglected otherwise)
- Returns:
The Path object with the specified ID or source/target pair. None if no such path exists.
- Return type:
Path | None
- class pyorps.graph.GraphAPI(raster_data, steps, ignore_max=True)[source]
Bases:
ABCBase class for all graph APIs defining the minimal required interface.
Initialize the base graph API with raster data and neighborhood steps.
- Parameters:
- __init__(raster_data, steps, ignore_max=True)[source]
Initialize the base graph API with raster data and neighborhood steps.
- abstractmethod shortest_path(source_indices, target_indices, algorithm='dijkstra', **kwargs)[source]
Find the shortest path(s) between source and target indices.
- Parameters:
source_indices (int | list[int] | ndarray[int] | tuple[int, int]) – Source node indices
target_indices (int | list[int] | ndarray[int] | tuple[int, int]) – Target node indices
algorithm (str) – Algorithm name (e.g., “dijkstra”, “astar”)
**kwargs –
- pairwisebool
If True, compute pairwise shortest paths between source_indices and target_indices. Only allowed if len(source_indices) == len(target_indices)
- heuristiccallable, optional
A function that takes two node indices (u, target) and returns an estimate of the distance between them. Only used when algorithm=”astar”.
- Returns:
list of path indices for each source-target pair
- Return type:
list[int | int32 | int64 | uint32 | uint64] | ndarray[int] | list[list[int | int32 | int64 | uint32 | uint64] | ndarray[int]]
- class pyorps.graph.GraphLibraryAPI(raster_data, steps, ignore_max=True, from_nodes=None, to_nodes=None, cost=None, **kwargs)[source]
Bases:
GraphAPIBase class for all graph library-based APIs.
This class extends GraphAPI with common functionality needed by standard graph libraries that require edge data to be explicitly provided and a graph to be constructed.
Initialize the graph library API.
- Parameters:
raster_data (ndarray[int]) – 2D numpy array representing the raster
steps (ndarray[int]) – Array defining the neighborhood connections
ignore_max (bool | None) – Ignore edges whose weights are greater or equal to the maximum
data (value in the raster)
from_nodes (ndarray | None) – Source node indices for edges
to_nodes (ndarray | None) – Target node indices for edges
cost (ndarray | None) – Edge weights
- __init__(raster_data, steps, ignore_max=True, from_nodes=None, to_nodes=None, cost=None, **kwargs)[source]
Initialize the graph library API.
- Parameters:
raster_data (ndarray[int]) – 2D numpy array representing the raster
steps (ndarray[int]) – Array defining the neighborhood connections
ignore_max (bool | None) – Ignore edges whose weights are greater or equal to the maximum
data (value in the raster)
from_nodes (ndarray | None) – Source node indices for edges
to_nodes (ndarray | None) – Target node indices for edges
cost (ndarray | None) – Edge weights
- abstractmethod create_graph(from_nodes, to_nodes, cost=None, **kwargs)[source]
Creates a graph object with the graph library specified in the selected interface.
- Parameters:
- Returns:
The graph object
- Return type:
- get_a_star_heuristic(target, source=None, **kwargs)[source]
Calculate the A* heuristic based on the Euclidean distance from the target node.
- Parameters:
- Returns:
An array of node indices in the graph
An array of heuristic values corresponding to each node
- Return type:
tuple containing
- get_advanced_a_star_heuristic(target, source=None, **kwargs)[source]
Calculate the A* heuristic based on the Euclidean distance from the target node.
- Parameters:
- Returns:
An array of node indices in the graph
An array of heuristic values corresponding to each node
- Return type:
tuple containing
- abstractmethod get_nodes()[source]
This method returns the nodes in the graph as a list or numpy array of node indices.
- abstractmethod get_number_of_edges()[source]
Returns the number of edges in the graph.
- Returns:
The number of edges
- Return type:
- abstractmethod get_number_of_nodes()[source]
Returns the number of nodes in the graph.
- Returns:
The number of nodes
- Return type:
- abstractmethod remove_isolates()[source]
If the graph object was initialized with the maximum number of nodes, this function helps to reduce the occupied memory by removing nodes without any edge (degree == 0).
- Returns:
None
- Return type:
None
- shortest_path(source_indices, target_indices, algorithm='dijkstra', **kwargs)[source]
This method applies the specified shortest path algorithm on the created graph object and finds the shortest path between source(s) and target(s) as a list of node indices.
- Parameters:
source_indices (int | int32 | int64 | uint32 | uint64 | list[int | int32 | int64 | uint32 | uint64] | ndarray[int] | None) – Index or indices of source node(s) (int or list[int])
target_indices (int | int32 | int64 | uint32 | uint64 | list[int | int32 | int64 | uint32 | uint64] | ndarray[int] | None) – Index or indices of target node(s) (int or list[int])
algorithm (str) – Algorithm to use for shortest path computation. Options depend on the specific library implementation.
kwargs – Additional parameters for specific algorithms library including
and (specific keywords) – “pairwise”: If True, compute pairwise shortest paths between source_indices and target_indices. Only allowed if len(source_indices) == len(target_indices)
- Returns:
List of node indices representing the shortest path(s)
- Return type:
list[int | int32 | int64 | uint32 | uint64] | ndarray[int] | list[list[int | int32 | int64 | uint32 | uint64] | ndarray[int]]
- exception pyorps.graph.NoPathFoundError(source, target, add_message='')[source]
Bases:
ExceptionCustom exception if no path can be found in the graph for source and target
- exception pyorps.graph.AlgorithmNotImplementedError(algorithm, graph_library)[source]
Bases:
ExceptionCustom exception if a specific algorithm is not implemented in the API or the graph library
Modules
Graph API abstractions for different graph libraries and routing algorithms. |
|
PYORPS: An Open-Source Tool for Automated Power Line Routing |