pyorps.core
Core types and base classes for geospatial data processing.
- class pyorps.core.CostAssumptions(source=None)[source]
Bases:
objectA class for handling cost assumptions for rasterization.
This class handles: - Loading cost assumptions from files (CSV, Excel, JSON) or generating of cost assumptions from a dictionary or a GeoDataFrame. - Mapping costs to features in a GeoDataFrame - Managing hierarchical cost structures
Initialize the CostAssumptions object.
- Parameters:
Path to a cost assumptions file
A dictionary of cost values
- apply_to_geodataframe(gdf, main_feature=None, side_features=None)[source]
Apply cost assumptions to a GeoDataFrame.
- Parameters:
gdf (GeoDataFrame) – GeoDataFrame to apply costs to
main_feature (str | None) – Main feature column name
side_features (list[str] | None) – list of side feature column names or single side feature name
- Returns:
GeoDataFrame with ‘cost’ column added
- convert_df_to_cost_dict(df)[source]
Convert a DataFrame to a nested dictionary for cost assumptions.
- Parameters:
df (DataFrame) – DataFrame containing cost assumptions with hierarchical structure
- Returns:
dictionary of cost assumptions with nested structure based on DataFrame columns
- Return type:
Uses one numeric column for costs, and all other columns as a hierarchical index: - The first column is the ‘main_feature’ - All additional columns are ‘side_features’
- to_csv(filepath, separator=';', decimal='.', encoding='ISO-8859-1')[source]
Save the cost assumptions to a CSV file.
- to_excel(filepath, sheet_name='CostAssumptions', index=False)[source]
Save the cost assumptions to an Excel file.
- pyorps.core.get_zero_cost_assumptions(gdf, main_feature, side_features)[source]
Generate cost assumptions with zero values for all feature combinations.
Creates structures matching format for CostAssumptions: - Without side features: {main_feature: {val1: 0, val2: 0, …}} - With side features: {(main_feature, side_feature1, …): {(val1, val2, …): 0, …}}
- Parameters:
gdf (GeoDataFrame) – GeoDataFrame with feature columns
main_feature (str) – Primary feature column name
side_features (list[str]) – List of secondary feature column names
- Returns:
Instacne of zero-cost assumptions
- Return type:
- pyorps.core.detect_feature_columns(gdf, max_features_per_column=50)[source]
Analyze columns in a geodataframe to identify the best candidates for main_feature and side_features based on statistical metrics.
- Parameters:
gdf (GeoDataFrame) – GeoDataFrame to analyze
max_features_per_column (int) – Maximum number of unique values allowed in a
column (categorical)
- Returns:
tuple of (main_feature, side_features)
- Raises:
NoSuitableColumnsError – When no suitable columns are found for feature selection
- Return type:
- pyorps.core.save_empty_cost_assumptions(geo_dataset, save_path, main_feature=None, side_features=None, file_type='csv', **kwargs)[source]
Generate and save empty cost assumptions with zero values for a geo dataset.
This function analyzes the given dataset to detect appropriate feature columns, creates a CostAssumptions object with zero costs for all feature combinations, and saves it to the specified path in the requested format.
- Parameters:
geo_dataset (Any) – GeoDataset object with a ‘data’ attribute containing a GeoDataFrame
save_path (str | Path) – File path where the cost assumptions should be saved
main_feature (str | None) – Column name for the primary feature
side_features (list[str] | None) – List containing a single column name for the secondary feature
file_type (str) – Output file format - one of ‘json’, ‘csv’, or ‘excel’ (default is ‘json’)
- Raises:
TypeError – If file_type is not one of the supported formats
NoSuitableColumnsError – If no suitable columns can be detected in the dataset
- Returns:
This function saves to a file and doesn’t return a value
- Return type:
None
- class pyorps.core.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.core.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
- exception pyorps.core.CostAssumptionsError[source]
Bases:
ExceptionBase exception for CostAssumptions class.
- exception pyorps.core.FileLoadError[source]
Bases:
CostAssumptionsErrorException raised when loading files fails.
- exception pyorps.core.InvalidSourceError[source]
Bases:
CostAssumptionsErrorException raised when the provided source is invalid.
- exception pyorps.core.FormatError[source]
Bases:
CostAssumptionsErrorException raised when data format is invalid.
- exception pyorps.core.FeatureColumnError[source]
Bases:
ExceptionBase exception for feature column detection errors
- exception pyorps.core.NoSuitableColumnsError[source]
Bases:
FeatureColumnErrorException raised when no suitable columns are found
- exception pyorps.core.ColumnAnalysisError[source]
Bases:
FeatureColumnErrorException raised when column analysis fails
- exception pyorps.core.WFSConnectionError[source]
Bases:
WFSErrorException raised for connection issues with WFS services.
- exception pyorps.core.WFSResponseParsingError[source]
Bases:
WFSErrorException raised when parsing WFS responses fails.
- exception pyorps.core.WFSLayerNotFoundError[source]
Bases:
WFSErrorException raised when a requested layer cannot be found.
- exception pyorps.core.RasterShapeError(raster_shape)[source]
Bases:
ExceptionCustom exception if the raster shape is not supported
- exception pyorps.core.NoPathFoundError(source, target, add_message='')[source]
Bases:
ExceptionCustom exception if no path can be found in the graph for source and target
- exception pyorps.core.AlgorithmNotImplementedError(algorithm, graph_library)[source]
Bases:
ExceptionCustom exception if a specific algorithm is not implemented in the API or the graph library
Modules
PYORPS: An Open-Source Tool for Automated Power Line Routing |
|
PYORPS: An Open-Source Tool for Automated Power Line Routing |
|
PYORPS: An Open-Source Tool for Automated Power Line Routing |
|
PYORPS: An Open-Source Tool for Automated Power Line Routing |