ObjectNat 1.3.3__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.
Files changed (33) hide show
  1. objectnat/__init__.py +9 -0
  2. objectnat/_api.py +14 -0
  3. objectnat/_config.py +43 -0
  4. objectnat/_version.py +1 -0
  5. objectnat/methods/__init__.py +0 -0
  6. objectnat/methods/coverage_zones/__init__.py +3 -0
  7. objectnat/methods/coverage_zones/graph_coverage.py +105 -0
  8. objectnat/methods/coverage_zones/radius_voronoi_coverage.py +39 -0
  9. objectnat/methods/coverage_zones/stepped_coverage.py +136 -0
  10. objectnat/methods/isochrones/__init__.py +1 -0
  11. objectnat/methods/isochrones/isochrone_utils.py +167 -0
  12. objectnat/methods/isochrones/isochrones.py +282 -0
  13. objectnat/methods/noise/__init__.py +3 -0
  14. objectnat/methods/noise/noise_init_data.py +10 -0
  15. objectnat/methods/noise/noise_reduce.py +155 -0
  16. objectnat/methods/noise/noise_simulation.py +453 -0
  17. objectnat/methods/noise/noise_simulation_simplified.py +222 -0
  18. objectnat/methods/point_clustering/__init__.py +1 -0
  19. objectnat/methods/point_clustering/cluster_points_in_polygons.py +115 -0
  20. objectnat/methods/provision/__init__.py +1 -0
  21. objectnat/methods/provision/provision.py +213 -0
  22. objectnat/methods/provision/provision_exceptions.py +59 -0
  23. objectnat/methods/provision/provision_model.py +323 -0
  24. objectnat/methods/utils/__init__.py +1 -0
  25. objectnat/methods/utils/geom_utils.py +173 -0
  26. objectnat/methods/utils/graph_utils.py +306 -0
  27. objectnat/methods/utils/math_utils.py +32 -0
  28. objectnat/methods/visibility/__init__.py +6 -0
  29. objectnat/methods/visibility/visibility_analysis.py +485 -0
  30. objectnat-1.3.3.dist-info/METADATA +202 -0
  31. objectnat-1.3.3.dist-info/RECORD +33 -0
  32. objectnat-1.3.3.dist-info/WHEEL +4 -0
  33. objectnat-1.3.3.dist-info/licenses/LICENSE.txt +28 -0
@@ -0,0 +1,282 @@
1
+ from typing import Any, Literal
2
+
3
+ import geopandas as gpd
4
+ import networkx as nx
5
+ import numpy as np
6
+
7
+ from objectnat import config
8
+ from objectnat.methods.isochrones.isochrone_utils import (
9
+ _calculate_distance_matrix,
10
+ _create_isochrones_gdf,
11
+ _prepare_graph_and_nodes,
12
+ _process_pt_data,
13
+ _validate_inputs,
14
+ create_separated_dist_polygons,
15
+ )
16
+ from objectnat.methods.utils.geom_utils import remove_inner_geom
17
+ from objectnat.methods.utils.graph_utils import graph_to_gdf
18
+
19
+ logger = config.logger
20
+
21
+
22
+ def get_accessibility_isochrone_stepped(
23
+ isochrone_type: Literal["radius", "ways", "separate"],
24
+ point: gpd.GeoDataFrame,
25
+ weight_value: float,
26
+ weight_type: Literal["time_min", "length_meter"],
27
+ nx_graph: nx.Graph,
28
+ step: float = None,
29
+ **kwargs: Any,
30
+ ) -> tuple[gpd.GeoDataFrame, gpd.GeoDataFrame | None, gpd.GeoDataFrame | None]:
31
+ """
32
+ Calculate stepped accessibility isochrones for a single point with specified intervals.
33
+
34
+ Args:
35
+ isochrone_type:
36
+ Visualization method for stepped isochrones:
37
+
38
+ - ``"radius"``: Voronoi-based in circular buffers
39
+ - ``"ways"``: Voronoi-based in road network polygons
40
+ - ``"separate"``: Circular buffers for each step
41
+
42
+ point (gpd.GeoDataFrame):
43
+ Single source point for isochrone calculation (uses first geometry if multiple provided).
44
+
45
+ weight_value (float):
46
+ Maximum travel time (minutes) or distance (meters) threshold.
47
+
48
+ weight_type:
49
+ Type of weight calculation:
50
+
51
+ - "time_min": Time-based in minutes
52
+ - "length_meter": Distance-based in meters
53
+
54
+ nx_graph (nx.Graph):
55
+ NetworkX graph representing the transportation network.
56
+
57
+ step (float, optional):
58
+ Interval between isochrone steps. Defaults to:
59
+
60
+ - 100 meters for distance-based
61
+ - 1 minute for time-based
62
+
63
+ **kwargs: Additional parameters:
64
+
65
+ - buffer_factor: Size multiplier for buffers (default: 0.7)
66
+ - road_buffer_size: Buffer size for road edges in meters (default: 5)
67
+
68
+ Returns:
69
+ tuple[gpd.GeoDataFrame, gpd.GeoDataFrame | None, gpd.GeoDataFrame | None]:
70
+ Tuple containing:
71
+
72
+ - stepped_isochrones: GeoDataFrame with stepped polygons and distance/time attributes
73
+ - pt_stops: Public transport stops within isochrones (if available)
74
+ - pt_routes: Public transport routes within isochrones (if available)
75
+ """
76
+ buffer_params = {
77
+ "buffer_factor": 0.7,
78
+ "road_buffer_size": 5,
79
+ }
80
+
81
+ buffer_params.update(kwargs)
82
+ original_crs = point.crs
83
+ point = point.copy()
84
+ if len(point) > 1:
85
+ logger.warning(
86
+ f"This method processes only single point. The GeoDataFrame contains {len(point)} points - "
87
+ "only the first geometry will be used for isochrone calculation. "
88
+ )
89
+ point = point.iloc[[0]]
90
+
91
+ local_crs, graph_type = _validate_inputs(point, weight_value, weight_type, nx_graph)
92
+
93
+ if step is None:
94
+ if weight_type == "length_meter":
95
+ step = 100
96
+ else:
97
+ step = 1
98
+ nx_graph, points, dist_nearest, speed = _prepare_graph_and_nodes(
99
+ point, nx_graph, graph_type, weight_type, weight_value
100
+ )
101
+
102
+ dist_matrix, subgraph = _calculate_distance_matrix(
103
+ nx_graph, points["nearest_node"].values, weight_type, weight_value, dist_nearest
104
+ )
105
+
106
+ logger.info("Building isochrones geometry...")
107
+ nodes, edges = graph_to_gdf(subgraph)
108
+ nodes.loc[dist_matrix.columns, "dist"] = dist_matrix.iloc[0]
109
+
110
+ if isochrone_type == "separate":
111
+ stepped_iso = create_separated_dist_polygons(nodes, weight_value, weight_type, step, speed)
112
+ else:
113
+ if isochrone_type == "radius":
114
+ isochrone_geoms = _build_radius_isochrones(
115
+ dist_matrix, weight_value, weight_type, speed, nodes, buffer_params["buffer_factor"]
116
+ )
117
+ else: # isochrone_type == 'ways':
118
+ if graph_type in ["intermodal", "walk"]:
119
+ isochrone_edges = edges[edges["type"] == "walk"]
120
+ else:
121
+ isochrone_edges = edges.copy()
122
+ all_isochrones_edges = isochrone_edges.buffer(buffer_params["road_buffer_size"], resolution=1).union_all()
123
+ all_isochrones_edges = gpd.GeoDataFrame(geometry=[all_isochrones_edges], crs=local_crs)
124
+ isochrone_geoms = _build_ways_isochrones(
125
+ dist_matrix=dist_matrix,
126
+ weight_value=weight_value,
127
+ weight_type=weight_type,
128
+ speed=speed,
129
+ nodes=nodes,
130
+ all_isochrones_edges=all_isochrones_edges,
131
+ buffer_factor=buffer_params["buffer_factor"],
132
+ )
133
+ nodes = nodes.clip(isochrone_geoms[0], keep_geom_type=True)
134
+ nodes["dist"] = np.minimum(np.ceil(nodes["dist"] / step) * step, weight_value)
135
+ voronois = gpd.GeoDataFrame(geometry=nodes.voronoi_polygons(), crs=local_crs)
136
+ stepped_iso = (
137
+ voronois.sjoin(nodes[["dist", "geometry"]]).dissolve(by="dist", as_index=False).drop(columns="index_right")
138
+ )
139
+ stepped_iso = stepped_iso.clip(isochrone_geoms[0], keep_geom_type=True)
140
+
141
+ pt_nodes, pt_edges = _process_pt_data(nodes, edges, graph_type)
142
+ if pt_nodes is not None:
143
+ pt_nodes.to_crs(original_crs, inplace=True)
144
+ if pt_edges is not None:
145
+ pt_edges.to_crs(original_crs, inplace=True)
146
+ return stepped_iso.to_crs(original_crs), pt_nodes, pt_edges
147
+
148
+
149
+ def get_accessibility_isochrones(
150
+ isochrone_type: Literal["radius", "ways"],
151
+ points: gpd.GeoDataFrame,
152
+ weight_value: float,
153
+ weight_type: Literal["time_min", "length_meter"],
154
+ nx_graph: nx.Graph,
155
+ **kwargs: Any,
156
+ ) -> tuple[gpd.GeoDataFrame, gpd.GeoDataFrame | None, gpd.GeoDataFrame | None]:
157
+ """
158
+ Calculate accessibility isochrones from input points based on the provided city graph.
159
+
160
+ Supports two types of isochrones:
161
+ - 'radius': Circular buffer-based isochrones
162
+ - 'ways': Road network-based isochrones
163
+
164
+ Args:
165
+ isochrone_type:
166
+ Type of isochrone to calculate:
167
+
168
+ - "radius": Creates circular buffers around reachable nodes
169
+ - "ways": Creates polygons based on reachable road network
170
+
171
+ points (gpd.GeoDataFrame):
172
+ GeoDataFrame containing source points for isochrone calculation.
173
+
174
+ weight_value (float):
175
+ Maximum travel time (minutes) or distance (meters) threshold.
176
+
177
+ weight_type:
178
+ Type of weight calculation:
179
+
180
+ - "time_min": Time-based accessibility in minutes
181
+ - "length_meter": Distance-based accessibility in meters
182
+
183
+ nx_graph (nx.Graph):
184
+ NetworkX graph representing the transportation network.
185
+ Must contain CRS and speed attributes for time calculations.
186
+
187
+ **kwargs: Additional parameters:
188
+
189
+ - buffer_factor: Size multiplier for buffers (default: 0.7)
190
+ - road_buffer_size: Buffer size for road edges in meters (default: 5)
191
+
192
+ Returns:
193
+ tuple[gpd.GeoDataFrame, gpd.GeoDataFrame | None, gpd.GeoDataFrame | None]:
194
+ Tuple containing:
195
+
196
+ - isochrones: GeoDataFrame with calculated isochrone polygons
197
+ - pt_stops: Public transport stops within isochrones (if available)
198
+ - pt_routes: Public transport routes within isochrones (if available)
199
+
200
+ """
201
+
202
+ buffer_params = {
203
+ "buffer_factor": 0.7,
204
+ "road_buffer_size": 5,
205
+ }
206
+ original_crs = points.crs
207
+ buffer_params.update(kwargs)
208
+
209
+ points = points.copy()
210
+ local_crs, graph_type = _validate_inputs(points, weight_value, weight_type, nx_graph)
211
+
212
+ nx_graph, points, dist_nearest, speed = _prepare_graph_and_nodes(
213
+ points, nx_graph, graph_type, weight_type, weight_value
214
+ )
215
+
216
+ weight_cutoff = (
217
+ weight_value + (100 if weight_type == "length_meter" else 1) if isochrone_type == "ways" else weight_value
218
+ )
219
+
220
+ dist_matrix, subgraph = _calculate_distance_matrix(
221
+ nx_graph, points["nearest_node"].values, weight_type, weight_cutoff, dist_nearest
222
+ )
223
+
224
+ logger.info("Building isochrones geometry...")
225
+ nodes, edges = graph_to_gdf(subgraph)
226
+ if isochrone_type == "radius":
227
+ isochrone_geoms = _build_radius_isochrones(
228
+ dist_matrix, weight_value, weight_type, speed, nodes, buffer_params["buffer_factor"]
229
+ )
230
+ else: # isochrone_type == 'ways':
231
+ if graph_type in ["intermodal", "walk"]:
232
+ isochrone_edges = edges[edges["type"] == "walk"]
233
+ else:
234
+ isochrone_edges = edges.copy()
235
+ all_isochrones_edges = isochrone_edges.buffer(buffer_params["road_buffer_size"], resolution=1).union_all()
236
+ all_isochrones_edges = gpd.GeoDataFrame(geometry=[all_isochrones_edges], crs=local_crs)
237
+ isochrone_geoms = _build_ways_isochrones(
238
+ dist_matrix=dist_matrix,
239
+ weight_value=weight_value,
240
+ weight_type=weight_type,
241
+ speed=speed,
242
+ nodes=nodes,
243
+ all_isochrones_edges=all_isochrones_edges,
244
+ buffer_factor=buffer_params["buffer_factor"],
245
+ )
246
+ isochrones = _create_isochrones_gdf(points, isochrone_geoms, dist_matrix, local_crs, weight_type, weight_value)
247
+ pt_nodes, pt_edges = _process_pt_data(nodes, edges, graph_type)
248
+ if pt_nodes is not None:
249
+ pt_nodes.to_crs(original_crs, inplace=True)
250
+ if pt_edges is not None:
251
+ pt_edges.to_crs(original_crs, inplace=True)
252
+ return isochrones.to_crs(original_crs), pt_nodes, pt_edges
253
+
254
+
255
+ def _build_radius_isochrones(dist_matrix, weight_value, weight_type, speed, nodes, buffer_factor):
256
+ results = []
257
+ for source in dist_matrix.index:
258
+ buffers = (weight_value - dist_matrix.loc[source]) * buffer_factor
259
+ if weight_type == "time_min":
260
+ buffers = buffers * speed
261
+ buffers = nodes.merge(buffers, left_index=True, right_index=True)
262
+ buffers.geometry = buffers.geometry.buffer(buffers[source], resolution=8)
263
+ results.append(buffers.union_all())
264
+ return results
265
+
266
+
267
+ def _build_ways_isochrones(dist_matrix, weight_value, weight_type, speed, nodes, all_isochrones_edges, buffer_factor):
268
+ results = []
269
+ for source in dist_matrix.index:
270
+ reachable_nodes = dist_matrix.loc[source]
271
+ reachable_nodes = reachable_nodes[reachable_nodes <= weight_value]
272
+ reachable_nodes = (weight_value - reachable_nodes) * buffer_factor
273
+ if weight_type == "time_min":
274
+ reachable_nodes = reachable_nodes * speed
275
+ reachable_nodes = nodes.merge(reachable_nodes, left_index=True, right_index=True)
276
+ clip_zone = reachable_nodes.buffer(reachable_nodes[source], resolution=4).union_all()
277
+
278
+ isochrone_edges = all_isochrones_edges.clip(clip_zone, keep_geom_type=True).explode(ignore_index=True)
279
+ geom_to_keep = isochrone_edges.sjoin(reachable_nodes, how="inner").index.unique()
280
+ isochrone = remove_inner_geom(isochrone_edges.loc[geom_to_keep].union_all())
281
+ results.append(isochrone)
282
+ return results
@@ -0,0 +1,3 @@
1
+ from .noise_simulation import simulate_noise
2
+ from .noise_reduce import dist_to_target_db, green_noise_reduce_db
3
+ from .noise_simulation_simplified import calculate_simplified_noise_frame
@@ -0,0 +1,10 @@
1
+ import pandas as pd
2
+
3
+ data = {
4
+ 30: {63: 0, 125: 0.0002, 250: 0.0009, 500: 0.003, 1000: 0.0075, 2000: 0.014, 4000: 0.025, 8000: 0.064},
5
+ 20: {63: 0, 125: 0.0003, 250: 0.0011, 500: 0.0028, 1000: 0.0052, 2000: 0.0096, 4000: 0.025, 8000: 0.083},
6
+ 10: {63: 0, 125: 0.0004, 250: 0.001, 500: 0.002, 1000: 0.0039, 2000: 0.01, 4000: 0.035, 8000: 0.125},
7
+ 0: {63: 0, 125: 0.0004, 250: 0.0008, 500: 0.0017, 1000: 0.0049, 2000: 0.017, 4000: 0.058, 8000: 0.156},
8
+ }
9
+
10
+ air_resist_ratio = pd.DataFrame(data)
@@ -0,0 +1,155 @@
1
+ import numpy as np
2
+ from scipy.optimize import fsolve
3
+
4
+ from objectnat import config
5
+
6
+ from .noise_init_data import air_resist_ratio
7
+
8
+ logger = config.logger
9
+
10
+
11
+ def get_air_resist_ratio(temp, freq, check_temp_freq=False):
12
+ if check_temp_freq:
13
+ if temp > max(air_resist_ratio.columns) or temp < min(air_resist_ratio.columns):
14
+ logger.warning(
15
+ f"The specified temperature of {temp}°C is outside the tabulated data range. "
16
+ f"The air resistance coefficient for these values may be inaccurate. "
17
+ f"Recommended temperature range: {min(air_resist_ratio.columns)}°C "
18
+ f"to {max(air_resist_ratio.columns)}°C."
19
+ )
20
+
21
+ if freq > max(air_resist_ratio.index) or freq < min(air_resist_ratio.index):
22
+ logger.warning(
23
+ f"The specified geometric mean frequency of {freq} Hz is outside the tabulated data range."
24
+ f" The air resistance coefficient for these values may be inaccurate."
25
+ f" Recommended frequency range: {min(air_resist_ratio.index)} Hz to {max(air_resist_ratio.index)} Hz."
26
+ )
27
+
28
+ def get_nearest_values(array, value):
29
+ sorted_array = sorted(array)
30
+ if value in sorted_array:
31
+ return [value]
32
+ if value > max(sorted_array):
33
+ return [sorted_array[-1]]
34
+ if value < min(sorted_array):
35
+ return [sorted_array[0]]
36
+
37
+ for i, val in enumerate(sorted_array):
38
+ if value < val:
39
+ return sorted_array[max(i - 1, 0)], sorted_array[i]
40
+ return sorted_array[-2], sorted_array[-1]
41
+
42
+ nearest_temp = get_nearest_values(air_resist_ratio.columns, temp)
43
+ nearest_freq = get_nearest_values(air_resist_ratio.index, freq)
44
+
45
+ if len(nearest_temp) == 1 and len(nearest_freq) == 1:
46
+ return air_resist_ratio.loc[nearest_freq[0], nearest_temp[0]]
47
+
48
+ if len(nearest_temp) == 2 and len(nearest_freq) == 2:
49
+ freq1, freq2 = nearest_freq
50
+ temp1, temp2 = nearest_temp
51
+
52
+ coef_temp1_freq1 = air_resist_ratio.loc[freq1, temp1]
53
+ coef_temp1_freq2 = air_resist_ratio.loc[freq2, temp1]
54
+ coef_temp2_freq1 = air_resist_ratio.loc[freq1, temp2]
55
+ coef_temp2_freq2 = air_resist_ratio.loc[freq2, temp2]
56
+
57
+ weight_temp1 = (temp2 - temp) / (temp2 - temp1)
58
+ weight_temp2 = (temp - temp1) / (temp2 - temp1)
59
+ weight_freq1 = (freq2 - freq) / (freq2 - freq1)
60
+ weight_freq2 = (freq - freq1) / (freq2 - freq1)
61
+
62
+ coef_freq1 = coef_temp1_freq1 * weight_temp1 + coef_temp2_freq1 * weight_temp2
63
+ coef_freq2 = coef_temp1_freq2 * weight_temp1 + coef_temp2_freq2 * weight_temp2
64
+
65
+ final_coef = coef_freq1 * weight_freq1 + coef_freq2 * weight_freq2
66
+
67
+ return final_coef
68
+
69
+ if len(nearest_temp) == 2 and len(nearest_freq) == 1:
70
+ temp1, temp2 = nearest_temp
71
+ freq1 = nearest_freq[0]
72
+
73
+ coef_temp1 = air_resist_ratio.loc[freq1, temp1]
74
+ coef_temp2 = air_resist_ratio.loc[freq1, temp2]
75
+
76
+ weight_temp1 = (temp2 - temp) / (temp2 - temp1)
77
+ weight_temp2 = (temp - temp1) / (temp2 - temp1)
78
+
79
+ return coef_temp1 * weight_temp1 + coef_temp2 * weight_temp2
80
+
81
+ if len(nearest_temp) == 1 and len(nearest_freq) == 2:
82
+ temp1 = nearest_temp[0]
83
+ freq1, freq2 = nearest_freq
84
+
85
+ coef_freq1 = air_resist_ratio.loc[freq1, temp1]
86
+ coef_freq2 = air_resist_ratio.loc[freq2, temp1]
87
+
88
+ weight_freq1 = (freq2 - freq) / (freq2 - freq1)
89
+ weight_freq2 = (freq - freq1) / (freq2 - freq1)
90
+
91
+ return coef_freq1 * weight_freq1 + coef_freq2 * weight_freq2
92
+
93
+
94
+ def dist_to_target_db(
95
+ init_noise_db, target_noise_db, geometric_mean_freq_hz, air_temperature, return_desc=False, check_temp_freq=False
96
+ ) -> float | str:
97
+ """
98
+ Calculates the distance required for a sound wave to decay from an initial noise level to a target noise level,
99
+ based on the geometric mean frequency of the sound and the air temperature. Optionally, can return a description
100
+ of the sound propagation behavior.
101
+
102
+ Args:
103
+ init_noise_db (float): The initial noise level of the source in decibels (dB). This is the starting sound
104
+ intensity.
105
+ target_noise_db (float): The target noise level in decibels (dB), representing the level to which the sound
106
+ decays over distance.
107
+ geometric_mean_freq_hz (float): The geometric mean frequency of the sound (in Hz). This frequency influences
108
+ the attenuation of sound over distance. Higher frequencies decay faster than lower ones.
109
+ air_temperature (float): The temperature of the air in degrees Celsius. This influences the air's resistance
110
+ to sound propagation.
111
+ return_desc (bool, optional): If set to `True`, the function will return a description of the sound decay
112
+ process instead of the calculated distance.
113
+ check_temp_freq (bool, optional): If `True`, the function will check whether the temperature and frequency
114
+ are within valid ranges.
115
+
116
+ Returns:
117
+ float or str: If `return_desc` is `False`, the function returns the distance (in meters) over which the sound
118
+ decays from `init_noise_db` to `target_noise_db`. If `return_desc` is `True`, a descriptive string is returned
119
+ explaining the calculation and the conditions.
120
+ """
121
+
122
+ def equation(r):
123
+ return l - l_ist + 20 * np.log10(r) + k * r
124
+
125
+ l_ist = init_noise_db
126
+ l = target_noise_db
127
+ k = get_air_resist_ratio(air_temperature, geometric_mean_freq_hz, check_temp_freq)
128
+ initial_guess = 1
129
+ r_solution = fsolve(equation, initial_guess)
130
+ if return_desc:
131
+ string = (
132
+ f"Noise level of {init_noise_db} dB "
133
+ f"with a geometric mean frequency of {geometric_mean_freq_hz} Hz "
134
+ f"at an air temperature of {air_temperature}°C decays to {target_noise_db} dB "
135
+ f"over a distance of {r_solution[0]} meters. Air resistance coefficient: {k}."
136
+ )
137
+ return string
138
+ return r_solution[0]
139
+
140
+
141
+ def green_noise_reduce_db(geometric_mean_freq_hz, r_tree) -> float:
142
+ """
143
+ Calculates the amount of noise reduction (in dB) provided by vegetation of a given thickness at a specified
144
+ geometric mean frequency. The function models the reduction based on the interaction of the sound with trees or
145
+ vegetation.
146
+
147
+ Args:
148
+ geometric_mean_freq_hz (float): The geometric mean frequency of the sound (in Hz).
149
+ r_tree (float): The thickness or density of the vegetation (in meters).
150
+
151
+ Returns:
152
+ float: The noise reduction (in dB) achieved by the vegetation. This value indicates how much quieter the sound
153
+ will be after passing through or interacting with the vegetation of the specified thickness.
154
+ """
155
+ return round(0.08 * r_tree * ((geometric_mean_freq_hz ** (1 / 3)) / 8), 1)