Skip to content

Commit 0bb9ed6

Browse files
authored
Merge pull request #382 from GispoCoding/354-distance-computation-criteria
354 distance computation criteria
2 parents f1b9c0c + 6e7ed91 commit 0bb9ed6

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

eis_toolkit/cli.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ def distance_to_anomaly_cli(
11941194
threshold_criteria: Annotated[ThresholdCriteria, typer.Option(case_sensitive=False)],
11951195
first_threshold_criteria_value: float = typer.Option(),
11961196
second_threshold_criteria_value: float = None,
1197+
max_distance: float = None,
11971198
):
11981199
"""
11991200
Calculate distance from each raster cell to nearest anomaly cell.
@@ -1216,6 +1217,7 @@ def distance_to_anomaly_cli(
12161217
anomaly_raster_data=raster.read(1),
12171218
threshold_criteria_value=threshold_criteria_value,
12181219
threshold_criteria=get_enum_values(threshold_criteria),
1220+
max_distance=max_distance,
12191221
)
12201222

12211223
typer.echo("Progress: 75%")
@@ -1979,6 +1981,7 @@ def distance_computation_cli(
19791981
base_raster: INPUT_FILE_OPTION = None,
19801982
pixel_size: float = None,
19811983
extent: Tuple[float, float, float, float] = (None, None, None, None),
1984+
max_distance: float = None,
19821985
):
19831986
"""Calculate distance from raster cell to nearest geometry."""
19841987
from eis_toolkit.exceptions import InvalidParameterValueException
@@ -2004,7 +2007,7 @@ def distance_computation_cli(
20042007
with rasterio.open(base_raster) as raster:
20052008
profile = raster.profile.copy()
20062009

2007-
out_image = distance_computation(geodataframe=geodataframe, raster_profile=profile)
2010+
out_image = distance_computation(geodataframe=geodataframe, raster_profile=profile, max_distance=max_distance)
20082011
profile["count"] = 1
20092012
typer.echo("Progress: 75%")
20102013

eis_toolkit/raster_processing/distance_to_anomaly.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def distance_to_anomaly(
4040
anomaly_raster_data: np.ndarray,
4141
threshold_criteria_value: Union[Tuple[Number, Number], Number],
4242
threshold_criteria: Literal["lower", "higher", "in_between", "outside"],
43+
max_distance: Optional[Number] = None,
4344
) -> Tuple[np.ndarray, Union[profiles.Profile, dict]]:
4445
"""Calculate distance from raster cell to nearest anomaly.
4546
@@ -59,6 +60,7 @@ def distance_to_anomaly(
5960
the first value should be the minimum and the second
6061
the maximum value.
6162
threshold_criteria: Method to define anomalous.
63+
max_distance: The maximum distance in the output array.
6264
6365
Returns:
6466
A 2D numpy array with the distances to anomalies computed
@@ -75,6 +77,7 @@ def distance_to_anomaly(
7577
anomaly_raster_data=anomaly_raster_data,
7678
threshold_criteria=threshold_criteria,
7779
threshold_criteria_value=threshold_criteria_value,
80+
max_distance=max_distance,
7881
)
7982
return out_image, anomaly_raster_profile
8083

@@ -201,6 +204,7 @@ def _distance_to_anomaly(
201204
anomaly_raster_data: np.ndarray,
202205
threshold_criteria_value: Union[Tuple[Number, Number], Number],
203206
threshold_criteria: Literal["lower", "higher", "in_between", "outside"],
207+
max_distance: Optional[Number],
204208
) -> np.ndarray:
205209
data_fits_criteria = _fits_criteria(
206210
threshold_criteria=threshold_criteria,
@@ -230,6 +234,8 @@ def _distance_to_anomaly(
230234
all_points = list(chain(*all_points_by_rows))
231235
all_points_gdf = gpd.GeoDataFrame(geometry=all_points, crs=anomaly_raster_profile["crs"])
232236

233-
distance_array = distance_computation(raster_profile=anomaly_raster_profile, geodataframe=all_points_gdf)
237+
distance_array = distance_computation(
238+
raster_profile=anomaly_raster_profile, geodataframe=all_points_gdf, max_distance=max_distance
239+
)
234240

235241
return distance_array

eis_toolkit/vector_processing/distance_computation.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
from numbers import Number
2+
13
import geopandas as gpd
24
import numpy as np
35
from beartype import beartype
4-
from beartype.typing import Union
6+
from beartype.typing import Optional, Union
57
from rasterio import profiles, transform
68
from shapely.geometry.base import BaseGeometry, BaseMultipartGeometry
79

8-
from eis_toolkit.exceptions import EmptyDataFrameException, NonMatchingCrsException
10+
from eis_toolkit.exceptions import EmptyDataFrameException, NonMatchingCrsException, NumericValueSignException
911
from eis_toolkit.utilities.checks.raster import check_raster_profile
1012
from eis_toolkit.utilities.miscellaneous import row_points
1113

1214

1315
@beartype
14-
def distance_computation(geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict]) -> np.ndarray:
16+
def distance_computation(
17+
geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict], max_distance: Optional[Number] = None
18+
) -> np.ndarray:
1519
"""Calculate distance from raster cell to nearest geometry.
1620
1721
Args:
1822
geodataframe: The GeoDataFrame with geometries to determine distance to.
1923
raster_profile: The raster profile of the raster in which the distances
2024
to the nearest geometry are determined.
25+
max_distance: The maximum distance in the output array.
2126
2227
Returns:
2328
A 2D numpy array with the distances computed.
@@ -30,19 +35,25 @@ def distance_computation(geodataframe: gpd.GeoDataFrame, raster_profile: Union[p
3035
raise NonMatchingCrsException("Expected coordinate systems to match between raster and GeoDataFrame.")
3136
if geodataframe.shape[0] == 0:
3237
raise EmptyDataFrameException("Expected GeoDataFrame to not be empty.")
38+
if max_distance is not None and max_distance <= 0:
39+
raise NumericValueSignException("Expected max distance to be a positive number.")
3340

3441
check_raster_profile(raster_profile=raster_profile)
3542

3643
raster_width = raster_profile.get("width")
3744
raster_height = raster_profile.get("height")
3845
raster_transform = raster_profile.get("transform")
3946

40-
return _distance_computation(
47+
distance_matrix = _distance_computation(
4148
raster_width=raster_width,
4249
raster_height=raster_height,
4350
raster_transform=raster_transform,
4451
geodataframe=geodataframe,
4552
)
53+
if max_distance is not None:
54+
distance_matrix[distance_matrix > max_distance] = max_distance
55+
56+
return distance_matrix
4657

4758

4859
def _calculate_row_distances(

0 commit comments

Comments
 (0)