ObjectNat 1.2.0__py3-none-any.whl → 1.2.1__py3-none-any.whl

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.

Potentially problematic release.


This version of ObjectNat might be problematic. Click here for more details.

Files changed (35) hide show
  1. objectnat/__init__.py +9 -13
  2. objectnat/_api.py +14 -14
  3. objectnat/_config.py +47 -47
  4. objectnat/_version.py +1 -1
  5. objectnat/methods/coverage_zones/__init__.py +3 -3
  6. objectnat/methods/coverage_zones/graph_coverage.py +98 -108
  7. objectnat/methods/coverage_zones/radius_voronoi_coverage.py +37 -45
  8. objectnat/methods/coverage_zones/stepped_coverage.py +126 -142
  9. objectnat/methods/isochrones/__init__.py +1 -1
  10. objectnat/methods/isochrones/isochrone_utils.py +167 -167
  11. objectnat/methods/isochrones/isochrones.py +262 -299
  12. objectnat/methods/noise/__init__.py +3 -4
  13. objectnat/methods/noise/noise_init_data.py +10 -10
  14. objectnat/methods/noise/noise_reduce.py +155 -155
  15. objectnat/methods/noise/noise_simulation.py +452 -440
  16. objectnat/methods/noise/noise_simulation_simplified.py +209 -135
  17. objectnat/methods/point_clustering/__init__.py +1 -1
  18. objectnat/methods/point_clustering/cluster_points_in_polygons.py +115 -116
  19. objectnat/methods/provision/__init__.py +1 -1
  20. objectnat/methods/provision/provision.py +117 -110
  21. objectnat/methods/provision/provision_exceptions.py +59 -59
  22. objectnat/methods/provision/provision_model.py +337 -337
  23. objectnat/methods/utils/__init__.py +1 -1
  24. objectnat/methods/utils/geom_utils.py +173 -173
  25. objectnat/methods/utils/graph_utils.py +306 -320
  26. objectnat/methods/utils/math_utils.py +32 -32
  27. objectnat/methods/visibility/__init__.py +6 -6
  28. objectnat/methods/visibility/visibility_analysis.py +470 -511
  29. {objectnat-1.2.0.dist-info → objectnat-1.2.1.dist-info}/LICENSE.txt +28 -28
  30. objectnat-1.2.1.dist-info/METADATA +115 -0
  31. objectnat-1.2.1.dist-info/RECORD +33 -0
  32. objectnat/methods/noise/noise_exceptions.py +0 -14
  33. objectnat-1.2.0.dist-info/METADATA +0 -148
  34. objectnat-1.2.0.dist-info/RECORD +0 -34
  35. {objectnat-1.2.0.dist-info → objectnat-1.2.1.dist-info}/WHEEL +0 -0
@@ -1,142 +1,126 @@
1
- from typing import Literal
2
-
3
- import geopandas as gpd
4
- import networkx as nx
5
- import numpy as np
6
- import pandas as pd
7
- from pyproj.exceptions import CRSError
8
- from shapely import Point, concave_hull
9
-
10
- from objectnat.methods.isochrones.isochrone_utils import create_separated_dist_polygons
11
- from objectnat.methods.utils.graph_utils import get_closest_nodes_from_gdf, reverse_graph
12
-
13
-
14
- def get_stepped_graph_coverage(
15
- gdf_to: gpd.GeoDataFrame,
16
- nx_graph: nx.Graph,
17
- weight_type: Literal["time_min", "length_meter"],
18
- step_type: Literal["voronoi", "separate"],
19
- weight_value_cutoff: float = None,
20
- zone: gpd.GeoDataFrame = None,
21
- step: float = None,
22
- ):
23
- """
24
- Calculate stepped coverage zones from source points through a graph network using Dijkstra's algorithm
25
- and Voronoi-based or buffer-based isochrone steps.
26
-
27
- This function combines graph-based accessibility with stepped isochrone logic. It:
28
- 1. Finds nearest graph nodes for each input point
29
- 2. Computes reachability for increasing weights (e.g. time or distance) in defined steps
30
- 3. Generates Voronoi-based or separate buffer zones around network nodes
31
- 4. Aggregates zones into stepped coverage layers
32
- 5. Optionally clips results to a boundary zone
33
-
34
- Parameters
35
- ----------
36
- gdf_to : gpd.GeoDataFrame
37
- Source points from which stepped coverage is calculated.
38
- nx_graph : nx.Graph
39
- NetworkX graph representing the transportation network.
40
- weight_type : Literal["time_min", "length_meter"]
41
- Type of edge weight to use for path calculation:
42
- - "time_min": Edge travel time in minutes
43
- - "length_meter": Edge length in meters
44
- step_type : Literal["voronoi", "separate"]
45
- Method for generating stepped zones:
46
- - "voronoi": Stepped zones based on Voronoi polygons around graph nodes
47
- - "separate": Independent buffer zones per step
48
- weight_value_cutoff : float, optional
49
- Maximum weight value (e.g., max travel time or distance) to limit the coverage extent.
50
- zone : gpd.GeoDataFrame, optional
51
- Optional boundary polygon to clip resulting stepped zones. If None, concave hull of reachable area is used.
52
- step : float, optional
53
- Step interval for coverage zone construction. Defaults to:
54
- - 100 meters for distance-based weight
55
- - 1 minute for time-based weight
56
-
57
- Returns
58
- -------
59
- gpd.GeoDataFrame
60
- GeoDataFrame with polygons representing stepped coverage zones for each input point, annotated by step range.
61
-
62
- Notes
63
- -----
64
- - Input graph must have a valid CRS defined.
65
- - MultiGraph or MultiDiGraph inputs will be simplified.
66
- - Designed for accessibility and spatial equity analyses over multimodal networks.
67
-
68
- Examples
69
- --------
70
- >>> from iduedu import get_intermodal_graph
71
- >>> points = gpd.read_file('destinations.geojson')
72
- >>> graph = get_intermodal_graph(osm_id=1114252)
73
- >>> stepped_coverage = get_stepped_graph_coverage(
74
- ... points, graph, "time_min", step_type="voronoi", weight_value_cutoff=30, step=5
75
- ... )
76
- >>> # Using buffer-style zones
77
- >>> stepped_separate = get_stepped_graph_coverage(
78
- ... points, graph, "length_meter", step_type="separate", weight_value_cutoff=1000, step=200
79
- ... )
80
- """
81
- if step is None:
82
- if weight_type == "length_meter":
83
- step = 100
84
- else:
85
- step = 1
86
- original_crs = gdf_to.crs
87
- try:
88
- local_crs = nx_graph.graph["crs"]
89
- except KeyError as exc:
90
- raise ValueError("Graph does not have crs attribute") from exc
91
-
92
- try:
93
- points = gdf_to.copy()
94
- points.to_crs(local_crs, inplace=True)
95
- except CRSError as e:
96
- raise CRSError(f"Graph crs ({local_crs}) has invalid format.") from e
97
-
98
- nx_graph, reversed_graph = reverse_graph(nx_graph, weight_type)
99
-
100
- points.geometry = points.representative_point()
101
-
102
- distances, nearest_nodes = get_closest_nodes_from_gdf(points, nx_graph)
103
-
104
- points["nearest_node"] = nearest_nodes
105
- points["distance"] = distances
106
-
107
- dist = nx.multi_source_dijkstra_path_length(
108
- reversed_graph, nearest_nodes, weight=weight_type, cutoff=weight_value_cutoff
109
- )
110
-
111
- graph_points = pd.DataFrame(
112
- data=[{"node": node, "geometry": Point(data["x"], data["y"])} for node, data in nx_graph.nodes(data=True)]
113
- )
114
-
115
- nearest_nodes = pd.DataFrame.from_dict(dist, orient="index", columns=["dist"]).reset_index()
116
-
117
- graph_nodes_gdf = gpd.GeoDataFrame(
118
- graph_points.merge(nearest_nodes, left_on="node", right_on="index", how="left").reset_index(drop=True),
119
- geometry="geometry",
120
- crs=local_crs,
121
- )
122
- graph_nodes_gdf.drop(columns=["index", "node"], inplace=True)
123
- if weight_value_cutoff is None:
124
- weight_value_cutoff = max(nearest_nodes["dist"])
125
- if step_type == "voronoi":
126
- graph_nodes_gdf["dist"] = np.minimum(np.ceil(graph_nodes_gdf["dist"] / step) * step, weight_value_cutoff)
127
- voronois = gpd.GeoDataFrame(geometry=graph_nodes_gdf.voronoi_polygons(), crs=local_crs)
128
- zone_coverages = voronois.sjoin(graph_nodes_gdf).dissolve(by="dist", as_index=False, dropna=False)
129
- zone_coverages = zone_coverages[["dist", "geometry"]].explode(ignore_index=True)
130
- if zone is None:
131
- zone = concave_hull(graph_nodes_gdf[~graph_nodes_gdf["node_to"].isna()].union_all(), ratio=0.5)
132
- else:
133
- zone = zone.to_crs(local_crs)
134
- zone_coverages = zone_coverages.clip(zone).to_crs(original_crs)
135
- else: # step_type == 'separate':
136
- speed = 83.33 # TODO HARDCODED WALK SPEED
137
- weight_value = weight_value_cutoff
138
- zone_coverages = create_separated_dist_polygons(graph_nodes_gdf, weight_value, weight_type, step, speed)
139
- if zone is not None:
140
- zone = zone.to_crs(local_crs)
141
- zone_coverages = zone_coverages.clip(zone).to_crs(original_crs)
142
- return zone_coverages
1
+ from typing import Literal
2
+
3
+ import geopandas as gpd
4
+ import networkx as nx
5
+ import numpy as np
6
+ import pandas as pd
7
+ from pyproj.exceptions import CRSError
8
+ from shapely import Point, concave_hull
9
+
10
+ from objectnat.methods.isochrones.isochrone_utils import create_separated_dist_polygons
11
+ from objectnat.methods.utils.graph_utils import get_closest_nodes_from_gdf, reverse_graph
12
+
13
+
14
+ def get_stepped_graph_coverage(
15
+ gdf_to: gpd.GeoDataFrame,
16
+ nx_graph: nx.Graph,
17
+ weight_type: Literal["time_min", "length_meter"],
18
+ step_type: Literal["voronoi", "separate"],
19
+ weight_value_cutoff: float = None,
20
+ zone: gpd.GeoDataFrame = None,
21
+ step: float = None,
22
+ ):
23
+ """
24
+ Calculate stepped coverage zones from source points through a graph network using Dijkstra's algorithm
25
+ and Voronoi-based or buffer-based isochrone steps.
26
+
27
+ This function combines graph-based accessibility with stepped isochrone logic. It:
28
+ 1. Finds nearest graph nodes for each input point
29
+ 2. Computes reachability for increasing weights (e.g. time or distance) in defined steps
30
+ 3. Generates Voronoi-based or separate buffer zones around network nodes
31
+ 4. Aggregates zones into stepped coverage layers
32
+ 5. Optionally clips results to a boundary zone
33
+
34
+ Parameters:
35
+ gdf_to (gpd.GeoDataFrame):
36
+ Source points from which stepped coverage is calculated.
37
+ nx_graph (nx.Graph):
38
+ NetworkX graph representing the transportation network.
39
+ weight_type (Literal["time_min", "length_meter"]):
40
+ Type of edge weight to use for path calculation:
41
+ - "time_min": Edge travel time in minutes
42
+ - "length_meter": Edge length in meters
43
+ step_type (Literal["voronoi", "separate"]):
44
+ Method for generating stepped zones:
45
+ - "voronoi": Stepped zones based on Voronoi polygons around graph nodes
46
+ - "separate": Independent buffer zones per step
47
+ weight_value_cutoff (float, optional):
48
+ Maximum weight value (e.g., max travel time or distance) to limit the coverage extent.
49
+ zone (gpd.GeoDataFrame, optional):
50
+ Optional boundary polygon to clip resulting stepped zones. If None, concave hull of reachable area is used.
51
+ step (float, optional):
52
+ Step interval for coverage zone construction. Defaults to:
53
+ - 100 meters for distance-based weight
54
+ - 1 minute for time-based weight
55
+
56
+ Returns:
57
+ (gpd.GeoDataFrame): GeoDataFrame with polygons representing stepped coverage zones for each input point,
58
+ annotated by step range.
59
+
60
+ Notes:
61
+ - Input graph must have a valid CRS defined.
62
+ - MultiGraph or MultiDiGraph inputs will be simplified to Graph/DiGraph.
63
+ - Designed for accessibility and spatial equity analyses over multimodal networks.
64
+ """
65
+ if step is None:
66
+ if weight_type == "length_meter":
67
+ step = 100
68
+ else:
69
+ step = 1
70
+ original_crs = gdf_to.crs
71
+ try:
72
+ local_crs = nx_graph.graph["crs"]
73
+ except KeyError as exc:
74
+ raise ValueError("Graph does not have crs attribute") from exc
75
+
76
+ try:
77
+ points = gdf_to.copy()
78
+ points.to_crs(local_crs, inplace=True)
79
+ except CRSError as e:
80
+ raise CRSError(f"Graph crs ({local_crs}) has invalid format.") from e
81
+
82
+ nx_graph, reversed_graph = reverse_graph(nx_graph, weight_type)
83
+
84
+ points.geometry = points.representative_point()
85
+
86
+ distances, nearest_nodes = get_closest_nodes_from_gdf(points, nx_graph)
87
+
88
+ points["nearest_node"] = nearest_nodes
89
+ points["distance"] = distances
90
+
91
+ dist = nx.multi_source_dijkstra_path_length(
92
+ reversed_graph, nearest_nodes, weight=weight_type, cutoff=weight_value_cutoff
93
+ )
94
+
95
+ graph_points = pd.DataFrame(
96
+ data=[{"node": node, "geometry": Point(data["x"], data["y"])} for node, data in nx_graph.nodes(data=True)]
97
+ )
98
+
99
+ nearest_nodes = pd.DataFrame.from_dict(dist, orient="index", columns=["dist"]).reset_index()
100
+
101
+ graph_nodes_gdf = gpd.GeoDataFrame(
102
+ graph_points.merge(nearest_nodes, left_on="node", right_on="index", how="left").reset_index(drop=True),
103
+ geometry="geometry",
104
+ crs=local_crs,
105
+ )
106
+ graph_nodes_gdf.drop(columns=["index", "node"], inplace=True)
107
+ if weight_value_cutoff is None:
108
+ weight_value_cutoff = max(nearest_nodes["dist"])
109
+ if step_type == "voronoi":
110
+ graph_nodes_gdf["dist"] = np.minimum(np.ceil(graph_nodes_gdf["dist"] / step) * step, weight_value_cutoff)
111
+ voronois = gpd.GeoDataFrame(geometry=graph_nodes_gdf.voronoi_polygons(), crs=local_crs)
112
+ zone_coverages = voronois.sjoin(graph_nodes_gdf).dissolve(by="dist", as_index=False, dropna=False)
113
+ zone_coverages = zone_coverages[["dist", "geometry"]].explode(ignore_index=True)
114
+ if zone is None:
115
+ zone = concave_hull(graph_nodes_gdf[~graph_nodes_gdf["node_to"].isna()].union_all(), ratio=0.5)
116
+ else:
117
+ zone = zone.to_crs(local_crs)
118
+ zone_coverages = zone_coverages.clip(zone).to_crs(original_crs)
119
+ else: # step_type == 'separate':
120
+ speed = 83.33 # TODO HARDCODED WALK SPEED
121
+ weight_value = weight_value_cutoff
122
+ zone_coverages = create_separated_dist_polygons(graph_nodes_gdf, weight_value, weight_type, step, speed)
123
+ if zone is not None:
124
+ zone = zone.to_crs(local_crs)
125
+ zone_coverages = zone_coverages.clip(zone).to_crs(original_crs)
126
+ return zone_coverages
@@ -1 +1 @@
1
- from .isochrones import get_accessibility_isochrones, get_accessibility_isochrone_stepped
1
+ from .isochrones import get_accessibility_isochrones, get_accessibility_isochrone_stepped
@@ -1,167 +1,167 @@
1
- from typing import Literal
2
-
3
- import geopandas as gpd
4
- import networkx as nx
5
- import numpy as np
6
- import pandas as pd
7
- from pyproj.exceptions import CRSError
8
- from shapely.ops import polygonize
9
-
10
- from objectnat import config
11
- from objectnat.methods.utils.geom_utils import polygons_to_multilinestring
12
- from objectnat.methods.utils.graph_utils import get_closest_nodes_from_gdf, remove_weakly_connected_nodes
13
-
14
- logger = config.logger
15
-
16
-
17
- def _validate_inputs(
18
- points: gpd.GeoDataFrame, weight_value: float, weight_type: Literal["time_min", "length_meter"], nx_graph: nx.Graph
19
- ) -> tuple[str, str]:
20
- """Validate common inputs for accessibility functions."""
21
- if weight_value <= 0:
22
- raise ValueError("Weight value must be greater than 0")
23
- if weight_type not in ["time_min", "length_meter"]:
24
- raise UserWarning("Weight type should be either 'time_min' or 'length_meter'")
25
-
26
- try:
27
- local_crs = nx_graph.graph["crs"]
28
- except KeyError as exc:
29
- raise ValueError("Graph does not have crs attribute") from exc
30
- try:
31
- graph_type = nx_graph.graph["type"]
32
- except KeyError as exc:
33
- raise ValueError("Graph does not have type attribute") from exc
34
-
35
- try:
36
- points.to_crs(local_crs, inplace=True)
37
- except CRSError as e:
38
- raise CRSError(f"Graph crs ({local_crs}) has invalid format.") from e
39
-
40
- return local_crs, graph_type
41
-
42
-
43
- def _prepare_graph_and_nodes(
44
- points: gpd.GeoDataFrame, nx_graph: nx.Graph, graph_type: str, weight_type: str, weight_value: float
45
- ) -> tuple[nx.Graph, gpd.GeoDataFrame, pd.DataFrame, float]:
46
- """Prepare graph and calculate nearest nodes with distances."""
47
- nx_graph = remove_weakly_connected_nodes(nx_graph)
48
- distances, nearest_nodes = get_closest_nodes_from_gdf(points, nx_graph)
49
- points["nearest_node"] = nearest_nodes
50
-
51
- dist_nearest = pd.DataFrame(data=distances, index=nearest_nodes, columns=["dist"]).drop_duplicates()
52
-
53
- # Calculate speed adjustment if needed
54
- speed = 0
55
- if graph_type in ["walk", "intermodal"] and weight_type == "time_min":
56
- try:
57
- speed = nx_graph.graph["walk_speed"]
58
- except KeyError:
59
- logger.warning("There is no walk_speed in graph, set to the default speed - 83.33 m/min")
60
- speed = 83.33
61
- dist_nearest = dist_nearest / speed
62
- elif weight_type == "time_min":
63
- speed = 20 * 1000 / 60
64
- dist_nearest = dist_nearest / speed
65
-
66
- if (dist_nearest > weight_value).all().all():
67
- raise RuntimeError(
68
- "The point(s) lie further from the graph than weight_value, it's impossible to "
69
- "construct isochrones. Check the coordinates of the point(s)/their projection"
70
- )
71
-
72
- return nx_graph, points, dist_nearest, speed
73
-
74
-
75
- def _process_pt_data(
76
- nodes: gpd.GeoDataFrame, edges: gpd.GeoDataFrame, graph_type: str
77
- ) -> tuple[gpd.GeoDataFrame, gpd.GeoDataFrame] | tuple[None, None]:
78
- """Process public transport data if available."""
79
- if "type" in nodes.columns and "platform" in nodes["type"].unique():
80
- pt_nodes = nodes[(nodes["type"] != "platform") & (~nodes["type"].isna())]
81
- if graph_type == "intermodal":
82
- edges = edges[~edges["type"].isin(["walk", "boarding"])]
83
- pt_nodes = pt_nodes[["type", "route", "geometry"]]
84
- edges = edges[["type", "route", "geometry"]]
85
- return pt_nodes, edges
86
- return None, None
87
-
88
-
89
- def _calculate_distance_matrix(
90
- nx_graph: nx.Graph,
91
- nearest_nodes: np.ndarray,
92
- weight_type: str,
93
- weight_value: float,
94
- dist_nearest: pd.DataFrame,
95
- ) -> tuple[pd.DataFrame, nx.Graph]:
96
- """Calculate distance matrix from nearest nodes."""
97
-
98
- data = {}
99
- for source in nearest_nodes:
100
- dist = nx.single_source_dijkstra_path_length(nx_graph, source, weight=weight_type, cutoff=weight_value)
101
- data.update({source: dist})
102
-
103
- dist_matrix = pd.DataFrame.from_dict(data, orient="index")
104
- dist_matrix = dist_matrix.add(dist_nearest.dist, axis=0)
105
- dist_matrix = dist_matrix.mask(dist_matrix > weight_value, np.nan)
106
- dist_matrix.dropna(how="all", inplace=True)
107
- dist_matrix.dropna(how="all", axis=1, inplace=True)
108
-
109
- subgraph = nx_graph.subgraph(dist_matrix.columns.to_list())
110
-
111
- return dist_matrix, subgraph
112
-
113
-
114
- def _create_isochrones_gdf(
115
- points: gpd.GeoDataFrame,
116
- results: list,
117
- dist_matrix: pd.DataFrame,
118
- local_crs: str,
119
- weight_type: str,
120
- weight_value: float,
121
- ) -> gpd.GeoDataFrame:
122
- """Create final isochrones GeoDataFrame."""
123
- isochrones = gpd.GeoDataFrame(geometry=results, index=dist_matrix.index, crs=local_crs)
124
- isochrones = (
125
- points.drop(columns="geometry")
126
- .merge(isochrones, left_on="nearest_node", right_index=True, how="left")
127
- .drop(columns="nearest_node")
128
- )
129
- isochrones = gpd.GeoDataFrame(isochrones, geometry="geometry", crs=local_crs)
130
- isochrones["weight_type"] = weight_type
131
- isochrones["weight_value"] = weight_value
132
- return isochrones
133
-
134
-
135
- def create_separated_dist_polygons(
136
- points: gpd.GeoDataFrame, weight_value, weight_type, step, speed
137
- ) -> gpd.GeoDataFrame:
138
- points["dist"] = points["dist"].clip(lower=0.1)
139
- steps = np.arange(0, weight_value + step, step)
140
- if steps[-1] > weight_value:
141
- steps[-1] = weight_value # Ensure last step doesn't exceed weight_value
142
- for i in range(len(steps) - 1):
143
- min_dist = steps[i]
144
- max_dist = steps[i + 1]
145
- nodes_in_step = points["dist"].between(min_dist, max_dist, inclusive="left")
146
- nodes_in_step = nodes_in_step[nodes_in_step].index
147
- if not nodes_in_step.empty:
148
- buffer_size = (max_dist - points.loc[nodes_in_step, "dist"]) * 0.7
149
- if weight_type == "time_min":
150
- buffer_size = buffer_size * speed
151
- points.loc[nodes_in_step, "buffer_size"] = buffer_size
152
- points.geometry = points.geometry.buffer(points["buffer_size"])
153
- points["dist"] = np.minimum(np.ceil(points["dist"] / step) * step, weight_value)
154
- points = points.dissolve(by="dist", as_index=False)
155
- polygons = gpd.GeoDataFrame(
156
- geometry=list(polygonize(points.geometry.apply(polygons_to_multilinestring).union_all())),
157
- crs=points.crs,
158
- )
159
- polygons_points = polygons.copy()
160
- polygons_points.geometry = polygons.representative_point()
161
- stepped_polygons = polygons_points.sjoin(points, predicate="within").reset_index()
162
- stepped_polygons = stepped_polygons.groupby("index").agg({"dist": "mean"})
163
- stepped_polygons["dist"] = np.minimum(np.floor(stepped_polygons["dist"] / step) * step, weight_value)
164
- stepped_polygons["geometry"] = polygons
165
- stepped_polygons = gpd.GeoDataFrame(stepped_polygons, geometry="geometry", crs=points.crs).reset_index(drop=True)
166
- stepped_polygons = stepped_polygons.dissolve(by="dist", as_index=False).explode(ignore_index=True)
167
- return stepped_polygons
1
+ from typing import Literal
2
+
3
+ import geopandas as gpd
4
+ import networkx as nx
5
+ import numpy as np
6
+ import pandas as pd
7
+ from pyproj.exceptions import CRSError
8
+ from shapely.ops import polygonize
9
+
10
+ from objectnat import config
11
+ from objectnat.methods.utils.geom_utils import polygons_to_multilinestring
12
+ from objectnat.methods.utils.graph_utils import get_closest_nodes_from_gdf, remove_weakly_connected_nodes
13
+
14
+ logger = config.logger
15
+
16
+
17
+ def _validate_inputs(
18
+ points: gpd.GeoDataFrame, weight_value: float, weight_type: Literal["time_min", "length_meter"], nx_graph: nx.Graph
19
+ ) -> tuple[str, str]:
20
+ """Validate common inputs for accessibility functions."""
21
+ if weight_value <= 0:
22
+ raise ValueError("Weight value must be greater than 0")
23
+ if weight_type not in ["time_min", "length_meter"]:
24
+ raise UserWarning("Weight type should be either 'time_min' or 'length_meter'")
25
+
26
+ try:
27
+ local_crs = nx_graph.graph["crs"]
28
+ except KeyError as exc:
29
+ raise ValueError("Graph does not have crs attribute") from exc
30
+ try:
31
+ graph_type = nx_graph.graph["type"]
32
+ except KeyError as exc:
33
+ raise ValueError("Graph does not have type attribute") from exc
34
+
35
+ try:
36
+ points.to_crs(local_crs, inplace=True)
37
+ except CRSError as e:
38
+ raise CRSError(f"Graph crs ({local_crs}) has invalid format.") from e
39
+
40
+ return local_crs, graph_type
41
+
42
+
43
+ def _prepare_graph_and_nodes(
44
+ points: gpd.GeoDataFrame, nx_graph: nx.Graph, graph_type: str, weight_type: str, weight_value: float
45
+ ) -> tuple[nx.Graph, gpd.GeoDataFrame, pd.DataFrame, float]:
46
+ """Prepare graph and calculate nearest nodes with distances."""
47
+ nx_graph = remove_weakly_connected_nodes(nx_graph)
48
+ distances, nearest_nodes = get_closest_nodes_from_gdf(points, nx_graph)
49
+ points["nearest_node"] = nearest_nodes
50
+
51
+ dist_nearest = pd.DataFrame(data=distances, index=nearest_nodes, columns=["dist"]).drop_duplicates()
52
+
53
+ # Calculate speed adjustment if needed
54
+ speed = 0
55
+ if graph_type in ["walk", "intermodal"] and weight_type == "time_min":
56
+ try:
57
+ speed = nx_graph.graph["walk_speed"]
58
+ except KeyError:
59
+ logger.warning("There is no walk_speed in graph, set to the default speed - 83.33 m/min")
60
+ speed = 83.33
61
+ dist_nearest = dist_nearest / speed
62
+ elif weight_type == "time_min":
63
+ speed = 20 * 1000 / 60
64
+ dist_nearest = dist_nearest / speed
65
+
66
+ if (dist_nearest > weight_value).all().all():
67
+ raise RuntimeError(
68
+ "The point(s) lie further from the graph than weight_value, it's impossible to "
69
+ "construct isochrones. Check the coordinates of the point(s)/their projection"
70
+ )
71
+
72
+ return nx_graph, points, dist_nearest, speed
73
+
74
+
75
+ def _process_pt_data(
76
+ nodes: gpd.GeoDataFrame, edges: gpd.GeoDataFrame, graph_type: str
77
+ ) -> tuple[gpd.GeoDataFrame, gpd.GeoDataFrame] | tuple[None, None]:
78
+ """Process public transport data if available."""
79
+ if "type" in nodes.columns and "platform" in nodes["type"].unique():
80
+ pt_nodes = nodes[(nodes["type"] != "platform") & (~nodes["type"].isna())]
81
+ if graph_type == "intermodal":
82
+ edges = edges[~edges["type"].isin(["walk", "boarding"])]
83
+ pt_nodes = pt_nodes[["type", "route", "geometry"]]
84
+ edges = edges[["type", "route", "geometry"]]
85
+ return pt_nodes, edges
86
+ return None, None
87
+
88
+
89
+ def _calculate_distance_matrix(
90
+ nx_graph: nx.Graph,
91
+ nearest_nodes: np.ndarray,
92
+ weight_type: str,
93
+ weight_value: float,
94
+ dist_nearest: pd.DataFrame,
95
+ ) -> tuple[pd.DataFrame, nx.Graph]:
96
+ """Calculate distance matrix from nearest nodes."""
97
+
98
+ data = {}
99
+ for source in nearest_nodes:
100
+ dist = nx.single_source_dijkstra_path_length(nx_graph, source, weight=weight_type, cutoff=weight_value)
101
+ data.update({source: dist})
102
+
103
+ dist_matrix = pd.DataFrame.from_dict(data, orient="index")
104
+ dist_matrix = dist_matrix.add(dist_nearest.dist, axis=0)
105
+ dist_matrix = dist_matrix.mask(dist_matrix > weight_value, np.nan)
106
+ dist_matrix.dropna(how="all", inplace=True)
107
+ dist_matrix.dropna(how="all", axis=1, inplace=True)
108
+
109
+ subgraph = nx_graph.subgraph(dist_matrix.columns.to_list())
110
+
111
+ return dist_matrix, subgraph
112
+
113
+
114
+ def _create_isochrones_gdf(
115
+ points: gpd.GeoDataFrame,
116
+ results: list,
117
+ dist_matrix: pd.DataFrame,
118
+ local_crs: str,
119
+ weight_type: str,
120
+ weight_value: float,
121
+ ) -> gpd.GeoDataFrame:
122
+ """Create final isochrones GeoDataFrame."""
123
+ isochrones = gpd.GeoDataFrame(geometry=results, index=dist_matrix.index, crs=local_crs)
124
+ isochrones = (
125
+ points.drop(columns="geometry")
126
+ .merge(isochrones, left_on="nearest_node", right_index=True, how="left")
127
+ .drop(columns="nearest_node")
128
+ )
129
+ isochrones = gpd.GeoDataFrame(isochrones, geometry="geometry", crs=local_crs)
130
+ isochrones["weight_type"] = weight_type
131
+ isochrones["weight_value"] = weight_value
132
+ return isochrones
133
+
134
+
135
+ def create_separated_dist_polygons(
136
+ points: gpd.GeoDataFrame, weight_value, weight_type, step, speed
137
+ ) -> gpd.GeoDataFrame:
138
+ points["dist"] = points["dist"].clip(lower=0.1)
139
+ steps = np.arange(0, weight_value + step, step)
140
+ if steps[-1] > weight_value:
141
+ steps[-1] = weight_value # Ensure last step doesn't exceed weight_value
142
+ for i in range(len(steps) - 1):
143
+ min_dist = steps[i]
144
+ max_dist = steps[i + 1]
145
+ nodes_in_step = points["dist"].between(min_dist, max_dist, inclusive="left")
146
+ nodes_in_step = nodes_in_step[nodes_in_step].index
147
+ if not nodes_in_step.empty:
148
+ buffer_size = (max_dist - points.loc[nodes_in_step, "dist"]) * 0.7
149
+ if weight_type == "time_min":
150
+ buffer_size = buffer_size * speed
151
+ points.loc[nodes_in_step, "buffer_size"] = buffer_size
152
+ points.geometry = points.geometry.buffer(points["buffer_size"])
153
+ points["dist"] = np.minimum(np.ceil(points["dist"] / step) * step, weight_value)
154
+ points = points.dissolve(by="dist", as_index=False)
155
+ polygons = gpd.GeoDataFrame(
156
+ geometry=list(polygonize(points.geometry.apply(polygons_to_multilinestring).union_all())),
157
+ crs=points.crs,
158
+ )
159
+ polygons_points = polygons.copy()
160
+ polygons_points.geometry = polygons.representative_point()
161
+ stepped_polygons = polygons_points.sjoin(points, predicate="within").reset_index()
162
+ stepped_polygons = stepped_polygons.groupby("index").agg({"dist": "mean"})
163
+ stepped_polygons["dist"] = np.minimum(np.floor(stepped_polygons["dist"] / step) * step, weight_value)
164
+ stepped_polygons["geometry"] = polygons
165
+ stepped_polygons = gpd.GeoDataFrame(stepped_polygons, geometry="geometry", crs=points.crs).reset_index(drop=True)
166
+ stepped_polygons = stepped_polygons.dissolve(by="dist", as_index=False).explode(ignore_index=True)
167
+ return stepped_polygons