pyorps 0.3.0__tar.gz → 0.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyorps-0.3.0/pyorps.egg-info → pyorps-0.3.2}/PKG-INFO +1 -1
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/__init__.py +31 -14
- pyorps-0.3.2/pyorps/core/__init__.py +69 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/core/cost_assumptions.py +78 -89
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/core/exceptions.py +11 -11
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/core/path.py +25 -20
- pyorps-0.3.2/pyorps/core/types.py +51 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/__init__.py +3 -4
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/cython_api.py +14 -15
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/graph_api.py +5 -6
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/graph_library_api.py +32 -35
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/igraph_api.py +11 -13
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/networkit_api.py +12 -16
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/networkx_api.py +11 -13
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/rustworkx_api.py +8 -7
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/path_finder.py +86 -67
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/io/__init__.py +16 -13
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/io/geo_dataset.py +52 -52
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/io/raster_loader.py +23 -24
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/io/vector_loader.py +47 -47
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/raster/handler.py +39 -37
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/raster/rasterizer.py +53 -43
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/__init__.py +23 -25
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_delta_stepping.cpp +152 -152
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_dijkstra.cpp +152 -152
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_heap.cpp +152 -152
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_raster_context.cpp +152 -152
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_traversal_numba.py +23 -25
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/neighborhood.py +7 -9
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/plotting.py +43 -44
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/traversal.py +19 -24
- {pyorps-0.3.0 → pyorps-0.3.2/pyorps.egg-info}/PKG-INFO +1 -1
- {pyorps-0.3.0 → pyorps-0.3.2}/pyproject.toml +1 -1
- pyorps-0.3.0/pyorps/core/__init__.py +0 -46
- pyorps-0.3.0/pyorps/core/types.py +0 -121
- {pyorps-0.3.0 → pyorps-0.3.2}/LICENSE +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/MANIFEST.in +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/README.md +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/graph/api/__init__.py +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/raster/__init__.py +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_delta_stepping.pyx +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_dijkstra.pyx +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_heap.pxd +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_heap.pyx +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_raster_context.pxd +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/_raster_context.pyx +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/atomic_cas.h +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/path_algorithms.py +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps/utils/path_core.py +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps.egg-info/SOURCES.txt +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps.egg-info/dependency_links.txt +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps.egg-info/not-zip-safe +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps.egg-info/requires.txt +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/pyorps.egg-info/top_level.txt +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/setup.cfg +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/setup.py +0 -0
- {pyorps-0.3.0 → pyorps-0.3.2}/tests/test_security_hardening.py +0 -0
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"""PYORPS - Python for Optimal Routes in Power Systems."""
|
|
2
2
|
|
|
3
|
-
__version__ = "0.3.
|
|
3
|
+
__version__ = "0.3.2"
|
|
4
4
|
|
|
5
5
|
# Suppress third-party deprecation warnings triggered during import
|
|
6
6
|
import warnings as _warnings
|
|
7
|
+
|
|
7
8
|
_warnings.filterwarnings(
|
|
8
9
|
"ignore",
|
|
9
10
|
message="The 'shapely.geos' module is deprecated",
|
|
@@ -12,22 +13,38 @@ _warnings.filterwarnings(
|
|
|
12
13
|
del _warnings
|
|
13
14
|
|
|
14
15
|
# Import key components for easy access
|
|
15
|
-
from .
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
from .core.cost_assumptions import (
|
|
17
|
+
CostAssumptions,
|
|
18
|
+
detect_feature_columns,
|
|
19
|
+
get_zero_cost_assumptions,
|
|
20
|
+
save_empty_cost_assumptions,
|
|
20
21
|
)
|
|
21
|
-
from .raster.rasterizer import GeoRasterizer
|
|
22
|
-
from .graph.path_finder import PathFinder
|
|
23
|
-
from .core.path import Path, PathCollection # Fixed: import from core.path instead of graph
|
|
24
|
-
from .core.cost_assumptions import (CostAssumptions, get_zero_cost_assumptions, detect_feature_columns,
|
|
25
|
-
save_empty_cost_assumptions)
|
|
26
22
|
from .core.exceptions import (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
AlgorithmNotImplementedError,
|
|
24
|
+
CostAssumptionsError,
|
|
25
|
+
NoPathFoundError,
|
|
26
|
+
PairwiseError,
|
|
27
|
+
PyorpsError,
|
|
28
|
+
RasterShapeError,
|
|
29
|
+
WFSError,
|
|
30
|
+
)
|
|
31
|
+
from .core.path import ( # Fixed: import from core.path instead of graph
|
|
32
|
+
Path,
|
|
33
|
+
PathCollection,
|
|
30
34
|
)
|
|
35
|
+
from .graph.path_finder import PathFinder
|
|
36
|
+
from .io.geo_dataset import (
|
|
37
|
+
GeoDataset,
|
|
38
|
+
InMemoryRasterDataset,
|
|
39
|
+
InMemoryVectorDataset,
|
|
40
|
+
LocalRasterDataset,
|
|
41
|
+
LocalVectorDataset,
|
|
42
|
+
RasterDataset,
|
|
43
|
+
VectorDataset,
|
|
44
|
+
WFSVectorDataset,
|
|
45
|
+
initialize_geo_dataset,
|
|
46
|
+
)
|
|
47
|
+
from .raster.rasterizer import GeoRasterizer
|
|
31
48
|
|
|
32
49
|
__all__ = [
|
|
33
50
|
# Core dataset classes
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Core types and base classes for geospatial data processing."""
|
|
2
|
+
|
|
3
|
+
from .cost_assumptions import (
|
|
4
|
+
CostAssumptions,
|
|
5
|
+
detect_feature_columns,
|
|
6
|
+
get_zero_cost_assumptions,
|
|
7
|
+
save_empty_cost_assumptions,
|
|
8
|
+
)
|
|
9
|
+
from .exceptions import (
|
|
10
|
+
AlgorithmNotImplementedError,
|
|
11
|
+
ColumnAnalysisError,
|
|
12
|
+
# Cost assumption exceptions
|
|
13
|
+
CostAssumptionsError,
|
|
14
|
+
FeatureColumnError,
|
|
15
|
+
FileLoadError,
|
|
16
|
+
FormatError,
|
|
17
|
+
InvalidSourceError,
|
|
18
|
+
NoPathFoundError,
|
|
19
|
+
NoSuitableColumnsError,
|
|
20
|
+
PairwiseError,
|
|
21
|
+
# Base exception
|
|
22
|
+
PyorpsError,
|
|
23
|
+
# Graph API exceptions
|
|
24
|
+
RasterShapeError,
|
|
25
|
+
WFSConnectionError,
|
|
26
|
+
# WFS exceptions
|
|
27
|
+
WFSError,
|
|
28
|
+
WFSLayerNotFoundError,
|
|
29
|
+
WFSResponseParsingError,
|
|
30
|
+
)
|
|
31
|
+
from .path import Path, PathCollection
|
|
32
|
+
from .types import (
|
|
33
|
+
IMPASSABLE_CELL_COST,
|
|
34
|
+
BboxType,
|
|
35
|
+
CoordinateInput,
|
|
36
|
+
CoordinateList,
|
|
37
|
+
CoordinateTuple,
|
|
38
|
+
CostAssumptionsType,
|
|
39
|
+
GeometryMaskType,
|
|
40
|
+
InputDataType,
|
|
41
|
+
NormalizedCoordinate,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
# Cost assumptions
|
|
46
|
+
"CostAssumptions", "get_zero_cost_assumptions", "detect_feature_columns", "save_empty_cost_assumptions",
|
|
47
|
+
|
|
48
|
+
# Types
|
|
49
|
+
"InputDataType", "CostAssumptionsType", "BboxType", "GeometryMaskType",
|
|
50
|
+
"CoordinateTuple", "CoordinateList", "CoordinateInput",
|
|
51
|
+
"NormalizedCoordinate", "IMPASSABLE_CELL_COST",
|
|
52
|
+
|
|
53
|
+
# Path classes
|
|
54
|
+
"Path", "PathCollection",
|
|
55
|
+
|
|
56
|
+
# Exceptions - Base
|
|
57
|
+
"PyorpsError",
|
|
58
|
+
|
|
59
|
+
# Exceptions - Cost assumptions
|
|
60
|
+
"CostAssumptionsError", "FileLoadError", "InvalidSourceError", "FormatError",
|
|
61
|
+
"FeatureColumnError", "NoSuitableColumnsError", "ColumnAnalysisError",
|
|
62
|
+
|
|
63
|
+
# Exceptions - WFS
|
|
64
|
+
"WFSError", "WFSConnectionError", "WFSResponseParsingError", "WFSLayerNotFoundError",
|
|
65
|
+
|
|
66
|
+
# Exceptions - Graph API
|
|
67
|
+
"RasterShapeError", "NoPathFoundError", "AlgorithmNotImplementedError",
|
|
68
|
+
"PairwiseError"
|
|
69
|
+
]
|
|
@@ -6,18 +6,23 @@ Reference:
|
|
|
6
6
|
Automated Power Line Routing', CIRED 2025 - 28th Conference and Exhibition on
|
|
7
7
|
Electricity Distribution, 16 - 19 June 2025, Geneva, Switzerland
|
|
8
8
|
"""
|
|
9
|
-
from typing import Union, Optional, Any, Callable
|
|
10
|
-
import os
|
|
11
|
-
from pathlib import Path
|
|
12
9
|
import csv
|
|
13
10
|
import json
|
|
11
|
+
import os
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
14
16
|
import numpy as np
|
|
15
17
|
import pandas as pd
|
|
16
|
-
|
|
17
18
|
from geopandas import GeoDataFrame, GeoSeries
|
|
18
19
|
|
|
19
|
-
from .exceptions import (
|
|
20
|
-
|
|
20
|
+
from .exceptions import (
|
|
21
|
+
FileLoadError,
|
|
22
|
+
FormatError,
|
|
23
|
+
InvalidSourceError,
|
|
24
|
+
NoSuitableColumnsError,
|
|
25
|
+
)
|
|
21
26
|
|
|
22
27
|
|
|
23
28
|
class CostAssumptions:
|
|
@@ -33,7 +38,7 @@ class CostAssumptions:
|
|
|
33
38
|
|
|
34
39
|
def __init__(
|
|
35
40
|
self,
|
|
36
|
-
source:
|
|
41
|
+
source: str | dict | None = None
|
|
37
42
|
):
|
|
38
43
|
"""
|
|
39
44
|
Initialize the CostAssumptions object.
|
|
@@ -64,7 +69,7 @@ class CostAssumptions:
|
|
|
64
69
|
|
|
65
70
|
def load(
|
|
66
71
|
self,
|
|
67
|
-
source:
|
|
72
|
+
source: str | dict
|
|
68
73
|
) -> dict:
|
|
69
74
|
"""
|
|
70
75
|
Load cost assumptions from a file or dictionary.
|
|
@@ -123,7 +128,7 @@ class CostAssumptions:
|
|
|
123
128
|
for encoding in encodings:
|
|
124
129
|
try:
|
|
125
130
|
# Read a sample to detect the dialect
|
|
126
|
-
with open(filepath,
|
|
131
|
+
with open(filepath, encoding=encoding) as f:
|
|
127
132
|
sample = f.read(4096)
|
|
128
133
|
|
|
129
134
|
sniffer = csv.Sniffer()
|
|
@@ -146,7 +151,7 @@ class CostAssumptions:
|
|
|
146
151
|
return self.cost_assumptions
|
|
147
152
|
except (pd.errors.ParserError, ValueError):
|
|
148
153
|
continue
|
|
149
|
-
except (csv.Error, UnicodeDecodeError
|
|
154
|
+
except (OSError, csv.Error, UnicodeDecodeError):
|
|
150
155
|
# If auto-detection fails, try common delimiters
|
|
151
156
|
for delimiter in common_delimiters:
|
|
152
157
|
for decimal in decimal_separators:
|
|
@@ -186,7 +191,7 @@ class CostAssumptions:
|
|
|
186
191
|
|
|
187
192
|
for encoding in encodings:
|
|
188
193
|
try:
|
|
189
|
-
with open(filepath,
|
|
194
|
+
with open(filepath, encoding=encoding) as f:
|
|
190
195
|
data = json.load(f)
|
|
191
196
|
|
|
192
197
|
# Check if it's the new format with metadata
|
|
@@ -246,7 +251,7 @@ class CostAssumptions:
|
|
|
246
251
|
df = self._convert_numeric_columns(df)
|
|
247
252
|
self.cost_assumptions = self.convert_df_to_cost_dict(df)
|
|
248
253
|
return self.cost_assumptions
|
|
249
|
-
except (pd.errors.ParserError, ValueError
|
|
254
|
+
except (OSError, pd.errors.ParserError, ValueError) as first_error:
|
|
250
255
|
# If there's an issue, try reading as strings and convert manually
|
|
251
256
|
try:
|
|
252
257
|
df = pd.read_excel(filepath, dtype=str)
|
|
@@ -257,10 +262,10 @@ class CostAssumptions:
|
|
|
257
262
|
df = self._convert_numeric_columns(df)
|
|
258
263
|
self.cost_assumptions = self.convert_df_to_cost_dict(df)
|
|
259
264
|
return self.cost_assumptions
|
|
260
|
-
except (pd.errors.ParserError, ValueError
|
|
265
|
+
except (OSError, pd.errors.ParserError, ValueError) as e:
|
|
261
266
|
msg = (f"Failed to read Excel file {filepath}. Original error: "
|
|
262
267
|
f"{first_error}. Second attempt error: {e}")
|
|
263
|
-
raise FileLoadError(msg)
|
|
268
|
+
raise FileLoadError(msg) from e
|
|
264
269
|
|
|
265
270
|
def convert_df_to_cost_dict(
|
|
266
271
|
self,
|
|
@@ -353,8 +358,8 @@ class CostAssumptions:
|
|
|
353
358
|
def apply_to_geodataframe(
|
|
354
359
|
self,
|
|
355
360
|
gdf: GeoDataFrame,
|
|
356
|
-
main_feature:
|
|
357
|
-
side_features:
|
|
361
|
+
main_feature: str | None = None,
|
|
362
|
+
side_features: list[str] | None = None
|
|
358
363
|
):
|
|
359
364
|
"""
|
|
360
365
|
Apply cost assumptions to a GeoDataFrame.
|
|
@@ -407,8 +412,8 @@ class CostAssumptions:
|
|
|
407
412
|
def _apply_tuple_costs(
|
|
408
413
|
self,
|
|
409
414
|
gdf: GeoDataFrame,
|
|
410
|
-
main_feature:
|
|
411
|
-
side_features:
|
|
415
|
+
main_feature: str | None = None,
|
|
416
|
+
side_features: list[str] | None = None
|
|
412
417
|
):
|
|
413
418
|
"""
|
|
414
419
|
Apply costs to the GeoDataFrame based on tuple keys in cost assumptions.
|
|
@@ -442,8 +447,8 @@ class CostAssumptions:
|
|
|
442
447
|
def _apply_nested_costs(
|
|
443
448
|
self,
|
|
444
449
|
gdf: GeoDataFrame,
|
|
445
|
-
main_feature:
|
|
446
|
-
side_features:
|
|
450
|
+
main_feature: str | None = None,
|
|
451
|
+
side_features: list[str] | None = None
|
|
447
452
|
):
|
|
448
453
|
"""
|
|
449
454
|
Apply costs to the GeoDataFrame based on nested dictionary cost assumptions.
|
|
@@ -483,6 +488,27 @@ class CostAssumptions:
|
|
|
483
488
|
combined_mask = main_mask & side_mask
|
|
484
489
|
gdf.loc[combined_mask, 'cost'] = cost
|
|
485
490
|
|
|
491
|
+
@staticmethod
|
|
492
|
+
def _add_label(value_to_names: dict[int, list[str]],
|
|
493
|
+
cost_int: int, label: str) -> None:
|
|
494
|
+
names = value_to_names.setdefault(cost_int, [])
|
|
495
|
+
if label and label not in names:
|
|
496
|
+
names.append(label)
|
|
497
|
+
|
|
498
|
+
def _labels_from_nested(self, value_to_names: dict[int, list[str]]) -> None:
|
|
499
|
+
for main_val, inner in self.cost_assumptions.items():
|
|
500
|
+
wildcard_cost = inner.get("", None)
|
|
501
|
+
for sub_val, cost in inner.items():
|
|
502
|
+
cost_int = int(cost)
|
|
503
|
+
if sub_val and wildcard_cost is not None \
|
|
504
|
+
and int(wildcard_cost) == cost_int:
|
|
505
|
+
label = str(main_val)
|
|
506
|
+
elif sub_val:
|
|
507
|
+
label = f"{main_val} > {sub_val}"
|
|
508
|
+
else:
|
|
509
|
+
label = str(main_val)
|
|
510
|
+
self._add_label(value_to_names, cost_int, label)
|
|
511
|
+
|
|
486
512
|
def build_cost_labels(self) -> dict[int, str]:
|
|
487
513
|
"""
|
|
488
514
|
Build a reverse mapping from cost values to terrain type names.
|
|
@@ -498,39 +524,14 @@ class CostAssumptions:
|
|
|
498
524
|
first_key = next(iter(self.cost_assumptions), None)
|
|
499
525
|
|
|
500
526
|
if isinstance(first_key, tuple):
|
|
501
|
-
# Tuple-keyed structure: {('Wald', 'Nadelholz'): 405, ...}
|
|
502
527
|
for keys, cost in self.cost_assumptions.items():
|
|
503
|
-
cost_int = int(cost)
|
|
504
528
|
label = " > ".join(str(k) for k in keys if k != "")
|
|
505
|
-
|
|
506
|
-
if label and label not in value_to_names[cost_int]:
|
|
507
|
-
value_to_names[cost_int].append(label)
|
|
529
|
+
self._add_label(value_to_names, int(cost), label)
|
|
508
530
|
elif isinstance(next(iter(self.cost_assumptions.values()), None), dict):
|
|
509
|
-
|
|
510
|
-
for main_val, inner in self.cost_assumptions.items():
|
|
511
|
-
wildcard_cost = inner.get("", None)
|
|
512
|
-
for sub_val, cost in inner.items():
|
|
513
|
-
cost_int = int(cost)
|
|
514
|
-
# If a wildcard ("") exists at the same cost, use just
|
|
515
|
-
# the main category name instead of "Main > Sub"
|
|
516
|
-
if sub_val and wildcard_cost is not None \
|
|
517
|
-
and int(wildcard_cost) == cost_int:
|
|
518
|
-
label = str(main_val)
|
|
519
|
-
elif sub_val:
|
|
520
|
-
label = f"{main_val} > {sub_val}"
|
|
521
|
-
else:
|
|
522
|
-
label = str(main_val)
|
|
523
|
-
value_to_names.setdefault(cost_int, [])
|
|
524
|
-
if label not in value_to_names[cost_int]:
|
|
525
|
-
value_to_names[cost_int].append(label)
|
|
531
|
+
self._labels_from_nested(value_to_names)
|
|
526
532
|
else:
|
|
527
|
-
# Simple dict: {'Wald': 365, ...}
|
|
528
533
|
for name, cost in self.cost_assumptions.items():
|
|
529
|
-
|
|
530
|
-
value_to_names.setdefault(cost_int, [])
|
|
531
|
-
label = str(name)
|
|
532
|
-
if label not in value_to_names[cost_int]:
|
|
533
|
-
value_to_names[cost_int].append(label)
|
|
534
|
+
self._add_label(value_to_names, int(cost), str(name))
|
|
534
535
|
|
|
535
536
|
return {v: " / ".join(names) for v, names in value_to_names.items()}
|
|
536
537
|
|
|
@@ -653,7 +654,7 @@ class CostAssumptions:
|
|
|
653
654
|
|
|
654
655
|
return pd.DataFrame(data)
|
|
655
656
|
|
|
656
|
-
|
|
657
|
+
if (self.side_features and
|
|
657
658
|
isinstance(next(iter(cost_dict.values()), None), dict)):
|
|
658
659
|
# Handle nested dictionary structure
|
|
659
660
|
data = []
|
|
@@ -668,19 +669,18 @@ class CostAssumptions:
|
|
|
668
669
|
|
|
669
670
|
return pd.DataFrame(data)
|
|
670
671
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
})
|
|
672
|
+
# Simple mapping
|
|
673
|
+
return pd.DataFrame({
|
|
674
|
+
self.main_feature: list(cost_dict.keys()),
|
|
675
|
+
'cost': list(cost_dict.values())
|
|
676
|
+
})
|
|
677
677
|
|
|
678
678
|
|
|
679
679
|
def save_empty_cost_assumptions(
|
|
680
680
|
geo_dataset: Any,
|
|
681
|
-
save_path:
|
|
682
|
-
main_feature:
|
|
683
|
-
side_features:
|
|
681
|
+
save_path: str | Path,
|
|
682
|
+
main_feature: str | None = None,
|
|
683
|
+
side_features: list[str] | None = None,
|
|
684
684
|
file_type: str = 'csv',
|
|
685
685
|
**kwargs
|
|
686
686
|
) -> dict:
|
|
@@ -811,6 +811,18 @@ def find_side_features(
|
|
|
811
811
|
return side_features if side_features else None
|
|
812
812
|
|
|
813
813
|
|
|
814
|
+
def _has_partial_null_pattern(crosstab: pd.DataFrame) -> bool:
|
|
815
|
+
"""Check if some main values have side values while others don't."""
|
|
816
|
+
null_cols = [col for col in crosstab.columns if pd.isna(col) or col == '']
|
|
817
|
+
if not null_cols:
|
|
818
|
+
return False
|
|
819
|
+
non_null_main_values = sum(
|
|
820
|
+
1 for _, row in crosstab.iterrows()
|
|
821
|
+
if row.drop(null_cols, errors='ignore').sum() > 0
|
|
822
|
+
)
|
|
823
|
+
return 0 < non_null_main_values < len(crosstab.index)
|
|
824
|
+
|
|
825
|
+
|
|
814
826
|
def column_shows_relationship_to_main_feature(
|
|
815
827
|
gdf: GeoDataFrame, main_feature: str,
|
|
816
828
|
side_feature: str
|
|
@@ -827,50 +839,27 @@ def column_shows_relationship_to_main_feature(
|
|
|
827
839
|
True if the column shows a meaningful relationship, False otherwise
|
|
828
840
|
"""
|
|
829
841
|
try:
|
|
830
|
-
# Create a cross-tabulation of the two columns
|
|
831
842
|
crosstab = pd.crosstab(gdf[main_feature], gdf[side_feature])
|
|
832
843
|
|
|
833
|
-
# Skip columns with too many unique values
|
|
834
844
|
if len(crosstab.columns) > 100:
|
|
835
845
|
return False
|
|
836
846
|
|
|
837
|
-
# Check for non-empty cells density
|
|
838
847
|
non_empty_cells = pd.DataFrame(crosstab > 0).sum().sum()
|
|
839
848
|
total_cells = crosstab.size
|
|
840
849
|
|
|
841
|
-
# If there's a good density of non-empty combinations, that's a good sign
|
|
842
850
|
if non_empty_cells / total_cells > 0.05:
|
|
843
851
|
return True
|
|
844
852
|
|
|
845
|
-
#
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
continue
|
|
852
|
-
|
|
853
|
-
# Check if there's diversity in the values
|
|
854
|
-
if len(non_zero_vals) >= 2:
|
|
855
|
-
return True
|
|
856
|
-
|
|
857
|
-
# Special check for columns with many nulls:
|
|
858
|
-
# If certain main values have side values while others don't, that's meaningful
|
|
859
|
-
null_cols = [col for col in crosstab.columns if pd.isna(col) or col == '']
|
|
860
|
-
if null_cols:
|
|
861
|
-
non_null_main_values = 0
|
|
862
|
-
for _, row in crosstab.iterrows():
|
|
863
|
-
if row.drop(null_cols, errors='ignore').sum() > 0:
|
|
864
|
-
non_null_main_values += 1
|
|
865
|
-
|
|
866
|
-
# If some main values have side values and others don't, that's meaningful
|
|
867
|
-
if 0 < non_null_main_values < len(crosstab.index):
|
|
868
|
-
return True
|
|
853
|
+
# Check if any row has diversity (2+ non-zero values)
|
|
854
|
+
has_diversity = any(
|
|
855
|
+
(row > 0).sum() >= 2 for _, row in crosstab.iterrows()
|
|
856
|
+
)
|
|
857
|
+
if has_diversity:
|
|
858
|
+
return True
|
|
869
859
|
|
|
870
|
-
return
|
|
860
|
+
return _has_partial_null_pattern(crosstab)
|
|
871
861
|
|
|
872
862
|
except (ValueError, TypeError):
|
|
873
|
-
# If analysis fails, be conservative and return False
|
|
874
863
|
return False
|
|
875
864
|
|
|
876
865
|
|
|
@@ -914,7 +903,7 @@ def get_zero_cost_assumptions(
|
|
|
914
903
|
|
|
915
904
|
|
|
916
905
|
def calculate_geometry_area(
|
|
917
|
-
geometries:
|
|
906
|
+
geometries: GeoSeries
|
|
918
907
|
) -> float:
|
|
919
908
|
"""
|
|
920
909
|
Calculate the sum of areas for a collection of geometries.
|
|
@@ -24,37 +24,37 @@ class FileLoadError(CostAssumptionsError):
|
|
|
24
24
|
"""
|
|
25
25
|
Exception raised when loading files fails.
|
|
26
26
|
"""
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
|
|
29
29
|
class InvalidSourceError(CostAssumptionsError):
|
|
30
30
|
"""
|
|
31
31
|
Exception raised when the provided source is invalid.
|
|
32
32
|
"""
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
|
|
35
35
|
class FormatError(CostAssumptionsError):
|
|
36
36
|
"""
|
|
37
37
|
Exception raised when data format is invalid.
|
|
38
38
|
"""
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
|
|
41
41
|
class FeatureColumnError(PyorpsError):
|
|
42
42
|
"""
|
|
43
43
|
Base exception for feature column detection errors
|
|
44
44
|
"""
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
|
|
47
47
|
class NoSuitableColumnsError(FeatureColumnError):
|
|
48
48
|
"""
|
|
49
49
|
Exception raised when no suitable columns are found
|
|
50
50
|
"""
|
|
51
|
-
|
|
51
|
+
|
|
52
52
|
|
|
53
53
|
class ColumnAnalysisError(FeatureColumnError):
|
|
54
54
|
"""
|
|
55
55
|
Exception raised when column analysis fails
|
|
56
56
|
"""
|
|
57
|
-
|
|
57
|
+
|
|
58
58
|
|
|
59
59
|
"""
|
|
60
60
|
Exceptions for vector_loader
|
|
@@ -65,25 +65,25 @@ class WFSError(PyorpsError):
|
|
|
65
65
|
"""
|
|
66
66
|
Base exception for WFS-related errors.
|
|
67
67
|
"""
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
|
|
70
70
|
class WFSConnectionError(WFSError):
|
|
71
71
|
"""
|
|
72
72
|
Exception raised for connection issues with WFS services.
|
|
73
73
|
"""
|
|
74
|
-
|
|
74
|
+
|
|
75
75
|
|
|
76
76
|
class WFSResponseParsingError(WFSError):
|
|
77
77
|
"""
|
|
78
78
|
Exception raised when parsing WFS responses fails.
|
|
79
79
|
"""
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
|
|
82
82
|
class WFSLayerNotFoundError(WFSError):
|
|
83
83
|
"""
|
|
84
84
|
Exception raised when a requested layer cannot be found.
|
|
85
85
|
"""
|
|
86
|
-
|
|
86
|
+
|
|
87
87
|
|
|
88
88
|
"""
|
|
89
89
|
Exceptions for graph library API
|
|
@@ -128,4 +128,4 @@ class PairwiseError(PyorpsError):
|
|
|
128
128
|
def __init__(self) -> None:
|
|
129
129
|
message = ("Pairwise computation failed! Source and target lists must have "
|
|
130
130
|
"the same length for pairwise computation!")
|
|
131
|
-
super().__init__(message)
|
|
131
|
+
super().__init__(message)
|
|
@@ -8,13 +8,13 @@ Reference:
|
|
|
8
8
|
"""
|
|
9
9
|
from __future__ import annotations
|
|
10
10
|
|
|
11
|
-
from typing import Optional, Any, TYPE_CHECKING
|
|
12
11
|
from dataclasses import dataclass, field
|
|
12
|
+
from typing import TYPE_CHECKING, Any
|
|
13
13
|
|
|
14
14
|
from numpy import ndarray
|
|
15
15
|
from shapely.geometry import LineString
|
|
16
16
|
|
|
17
|
-
from pyorps.core.types import CoordinateTuple, NodeList
|
|
17
|
+
from pyorps.core.types import CoordinateList, CoordinateTuple, NodeList
|
|
18
18
|
|
|
19
19
|
if TYPE_CHECKING:
|
|
20
20
|
from pyorps.core.cost_assumptions import CostAssumptions
|
|
@@ -40,19 +40,32 @@ class Path:
|
|
|
40
40
|
neighborhood: str
|
|
41
41
|
|
|
42
42
|
# Optional metrics that may be calculated
|
|
43
|
-
total_length:
|
|
44
|
-
total_cost:
|
|
45
|
-
total_cell_cost:
|
|
46
|
-
length_by_category:
|
|
47
|
-
length_by_category_percent:
|
|
43
|
+
total_length: float | None = None
|
|
44
|
+
total_cost: float | None = None
|
|
45
|
+
total_cell_cost: float | None = None
|
|
46
|
+
length_by_category: dict[float, float] | None = None
|
|
47
|
+
length_by_category_percent: dict[float, float] | None = None
|
|
48
48
|
|
|
49
49
|
# Optional mapping from cost values to terrain type names
|
|
50
|
-
cost_labels:
|
|
50
|
+
cost_labels: dict[int, str] | None = field(default=None, repr=False)
|
|
51
|
+
|
|
52
|
+
def _resolve_labels(
|
|
53
|
+
self,
|
|
54
|
+
cost_assumptions: CostAssumptions | None,
|
|
55
|
+
cost_labels: dict[int, str] | None,
|
|
56
|
+
) -> dict[int, str]:
|
|
57
|
+
if cost_labels is not None:
|
|
58
|
+
return cost_labels
|
|
59
|
+
if cost_assumptions is not None:
|
|
60
|
+
return cost_assumptions.build_cost_labels()
|
|
61
|
+
if self.cost_labels is not None:
|
|
62
|
+
return self.cost_labels
|
|
63
|
+
return {}
|
|
51
64
|
|
|
52
65
|
def analyze(
|
|
53
66
|
self,
|
|
54
|
-
cost_assumptions:
|
|
55
|
-
cost_labels:
|
|
67
|
+
cost_assumptions: CostAssumptions | None = None,
|
|
68
|
+
cost_labels: dict[int, str] | None = None,
|
|
56
69
|
) -> str:
|
|
57
70
|
"""
|
|
58
71
|
Return a human-readable breakdown of the path by terrain category.
|
|
@@ -72,14 +85,7 @@ class Path:
|
|
|
72
85
|
Returns:
|
|
73
86
|
Formatted string with path analysis.
|
|
74
87
|
"""
|
|
75
|
-
|
|
76
|
-
labels = cost_labels
|
|
77
|
-
if labels is None and cost_assumptions is not None:
|
|
78
|
-
labels = cost_assumptions.build_cost_labels()
|
|
79
|
-
if labels is None:
|
|
80
|
-
labels = self.cost_labels
|
|
81
|
-
if labels is None:
|
|
82
|
-
labels = {}
|
|
88
|
+
labels = self._resolve_labels(cost_assumptions, cost_labels)
|
|
83
89
|
|
|
84
90
|
lines = []
|
|
85
91
|
lines.append(f"Path Analysis (ID={self.path_id})")
|
|
@@ -111,7 +117,6 @@ class Path:
|
|
|
111
117
|
length_m = self.length_by_category[cost_val]
|
|
112
118
|
pct = self.length_by_category_percent.get(cost_val, 0)
|
|
113
119
|
label = labels.get(int(cost_val), f"Cost {int(cost_val)}")
|
|
114
|
-
# Truncate long labels
|
|
115
120
|
if len(label) > 35:
|
|
116
121
|
label = label[:32] + "..."
|
|
117
122
|
lines.append(
|
|
@@ -254,7 +259,7 @@ class PathCollection:
|
|
|
254
259
|
path_id: int = None,
|
|
255
260
|
source: Any = None,
|
|
256
261
|
target: Any = None
|
|
257
|
-
) ->
|
|
262
|
+
) -> Path | None:
|
|
258
263
|
"""
|
|
259
264
|
Retrieve a stored path by ID, or by source AND target.
|
|
260
265
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PYORPS: An Open-Source Tool for Automated Power Line Routing
|
|
3
|
+
|
|
4
|
+
Reference:
|
|
5
|
+
[1] Hofmann, M., Stetz, T., Kammer, F., Repo, S.: 'PYORPS: An Open-Source Tool for
|
|
6
|
+
Automated Power Line Routing', CIRED 2025 - 28th Conference and Exhibition on
|
|
7
|
+
Electricity Distribution, 16 - 19 June 2025, Geneva, Switzerland
|
|
8
|
+
"""
|
|
9
|
+
from typing import TypeAlias
|
|
10
|
+
|
|
11
|
+
from geopandas import GeoDataFrame, GeoSeries
|
|
12
|
+
from numpy import int32, int64, ndarray, uint32, uint64
|
|
13
|
+
from shapely.geometry import MultiPoint, Point, Polygon
|
|
14
|
+
|
|
15
|
+
from .cost_assumptions import CostAssumptions
|
|
16
|
+
|
|
17
|
+
# Input for geodata sources
|
|
18
|
+
InputDataType: TypeAlias = str | dict | GeoDataFrame | GeoSeries | ndarray
|
|
19
|
+
|
|
20
|
+
CostAssumptionsType: TypeAlias = dict | str | CostAssumptions
|
|
21
|
+
|
|
22
|
+
BboxType: TypeAlias = Polygon | GeoDataFrame | GeoSeries | tuple[float, float, float, float]
|
|
23
|
+
|
|
24
|
+
GeometryMaskType: TypeAlias = Polygon | GeoDataFrame | tuple
|
|
25
|
+
|
|
26
|
+
# A pair of float coordinates
|
|
27
|
+
CoordinateTuple: TypeAlias = tuple[float, float] | list[float]
|
|
28
|
+
|
|
29
|
+
# List of pairs of float coordinates
|
|
30
|
+
CoordinateList: TypeAlias = list[CoordinateTuple]
|
|
31
|
+
|
|
32
|
+
CoordinateInput: TypeAlias = CoordinateTuple | CoordinateList | list[Point] | list[MultiPoint] | ndarray | Point | MultiPoint | GeoSeries | GeoDataFrame
|
|
33
|
+
|
|
34
|
+
NormalizedCoordinate: TypeAlias = CoordinateTuple | CoordinateList
|
|
35
|
+
|
|
36
|
+
CoordinatePath: TypeAlias = list[CoordinateTuple] | ndarray
|
|
37
|
+
|
|
38
|
+
# A node in the graph
|
|
39
|
+
Node: TypeAlias = int | int32 | int64 | uint32 | uint64
|
|
40
|
+
|
|
41
|
+
# A list of node indexes
|
|
42
|
+
NodeList: TypeAlias = list[Node] | ndarray[int]
|
|
43
|
+
|
|
44
|
+
SourceTargetType: TypeAlias = Node | NodeList
|
|
45
|
+
|
|
46
|
+
# A list of multiple NodeList type objects
|
|
47
|
+
NodePathList: TypeAlias = list[NodeList]
|
|
48
|
+
|
|
49
|
+
# Maximum cost value for uint16 rasters — cells with this value are impassable
|
|
50
|
+
IMPASSABLE_CELL_COST: int = 65535
|
|
51
|
+
|