Skip to content

Commit

Permalink
fix(raster_processing): add missing max_distance parameter in distanc…
Browse files Browse the repository at this point in the history
…e_to_anomaly_gdal
  • Loading branch information
nmaarnio committed Oct 23, 2024
1 parent f124c2d commit ccc9675
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion eis_toolkit/raster_processing/distance_to_anomaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from osgeo import gdal
from rasterio import profiles

from eis_toolkit.exceptions import EmptyDataException, InvalidParameterValueException
from eis_toolkit.exceptions import EmptyDataException, InvalidParameterValueException, NumericValueSignException
from eis_toolkit.utilities.checks.raster import check_raster_profile
from eis_toolkit.utilities.miscellaneous import row_points, toggle_gdal_exceptions
from eis_toolkit.vector_processing.distance_computation import distance_computation
Expand Down Expand Up @@ -89,6 +89,7 @@ def distance_to_anomaly_gdal(
anomaly_raster_data: np.ndarray,
threshold_criteria_value: Union[Tuple[Number, Number], Number],
threshold_criteria: Literal["lower", "higher", "in_between", "outside"],
max_distance: Optional[Number] = None,
) -> Tuple[np.ndarray, profiles.Profile]:
"""Calculate distance from raster cell to nearest anomaly.
Expand All @@ -111,6 +112,7 @@ def distance_to_anomaly_gdal(
the first value should be the minimum and the second
the maximum value.
threshold_criteria: Method to define anomalous.
max_distance: The maximum distance in the output array.
Returns:
A 2D numpy array with the distances to anomalies computed
Expand All @@ -121,12 +123,15 @@ def distance_to_anomaly_gdal(
_check_threshold_criteria_and_value(
threshold_criteria=threshold_criteria, threshold_criteria_value=threshold_criteria_value
)
if max_distance is not None and max_distance <= 0:
raise NumericValueSignException("Expected max distance to be a positive number.")

out_array, out_meta = _distance_to_anomaly_gdal(
anomaly_raster_profile=anomaly_raster_profile,
anomaly_raster_data=anomaly_raster_data,
threshold_criteria=threshold_criteria,
threshold_criteria_value=threshold_criteria_value,
max_distance=max_distance,
)

return out_array, out_meta
Expand Down Expand Up @@ -218,6 +223,7 @@ def _distance_to_anomaly_gdal(
anomaly_raster_data: np.ndarray,
threshold_criteria_value: Union[Tuple[Number, Number], Number],
threshold_criteria: Literal["lower", "higher", "in_between", "outside"],
max_distance: Optional[Number],
) -> Tuple[np.ndarray, profiles.Profile]:

data_fits_criteria = _validate_threshold_criteria(
Expand Down Expand Up @@ -256,6 +262,8 @@ def _distance_to_anomaly_gdal(

# Create outputs
out_array = out_band.ReadAsArray()
if max_distance is not None:
out_array[out_array > max_distance] = max_distance
out_meta = anomaly_raster_profile.copy()

# Update metadata
Expand Down

0 comments on commit ccc9675

Please sign in to comment.