Skip to content

Commit 88abd03

Browse files
authored
Merge pull request #377 from GispoCoding/352-add-cli-function-for-distance-to-anomaly
Add CLI function for Distance to anomaly
2 parents 472b395 + 85202fa commit 88abd03

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

eis_toolkit/cli.py

+50
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ class RandomForestRegressorCriterion(str, Enum):
252252
poisson = "poisson"
253253

254254

255+
class ThresholdCriteria(str, Enum):
256+
"""Threshold criteria for distance to anomaly."""
257+
258+
lower = "lower"
259+
higher = "higher"
260+
in_between = "in_between"
261+
outside = "outside"
262+
263+
255264
INPUT_FILE_OPTION = Annotated[
256265
Path,
257266
typer.Option(
@@ -1177,6 +1186,47 @@ def create_constant_raster_from_template_cli(
11771186
typer.echo(f"Creating constant raster completed, writing raster to {output_raster}.")
11781187

11791188

1189+
# DISTANCE TO ANOMALY
1190+
@app.command()
1191+
def distance_to_anomaly_cli(
1192+
input_raster: INPUT_FILE_OPTION,
1193+
output_raster: OUTPUT_FILE_OPTION,
1194+
threshold_criteria: Annotated[ThresholdCriteria, typer.Option(case_sensitive=False)],
1195+
first_threshold_criteria_value: float = typer.Option(),
1196+
second_threshold_criteria_value: float = None,
1197+
):
1198+
"""
1199+
Calculate distance from each raster cell to nearest anomaly cell.
1200+
1201+
Uses only the first band of the raster.
1202+
"""
1203+
from eis_toolkit.raster_processing.distance_to_anomaly import distance_to_anomaly
1204+
1205+
typer.echo("Progress: 10%")
1206+
1207+
if second_threshold_criteria_value is not None:
1208+
threshold_criteria_value = (first_threshold_criteria_value, second_threshold_criteria_value)
1209+
else:
1210+
threshold_criteria_value = first_threshold_criteria_value
1211+
1212+
with rasterio.open(input_raster) as raster:
1213+
typer.echo("Progress: 25%")
1214+
out_image, out_meta = distance_to_anomaly(
1215+
anomaly_raster_profile=raster.profile,
1216+
anomaly_raster_data=raster.read(1),
1217+
threshold_criteria_value=threshold_criteria_value,
1218+
threshold_criteria=get_enum_values(threshold_criteria),
1219+
)
1220+
1221+
typer.echo("Progress: 75%")
1222+
1223+
with rasterio.open(output_raster, "w", **out_meta) as dest:
1224+
dest.write(out_image, 1)
1225+
typer.echo("Progress: 100%")
1226+
1227+
typer.echo(f"Computing distance to anomaly completed, writing raster to {output_raster}.")
1228+
1229+
11801230
# EXTRACT VALUES FROM RASTER
11811231
@app.command()
11821232
def extract_values_from_raster_cli(

0 commit comments

Comments
 (0)