Skip to content

Commit

Permalink
Merge pull request #377 from GispoCoding/352-add-cli-function-for-dis…
Browse files Browse the repository at this point in the history
…tance-to-anomaly

Add CLI function for Distance to anomaly
  • Loading branch information
nmaarnio committed Apr 25, 2024
2 parents 472b395 + 85202fa commit 88abd03
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions eis_toolkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ class RandomForestRegressorCriterion(str, Enum):
poisson = "poisson"


class ThresholdCriteria(str, Enum):
"""Threshold criteria for distance to anomaly."""

lower = "lower"
higher = "higher"
in_between = "in_between"
outside = "outside"


INPUT_FILE_OPTION = Annotated[
Path,
typer.Option(
Expand Down Expand Up @@ -1177,6 +1186,47 @@ def create_constant_raster_from_template_cli(
typer.echo(f"Creating constant raster completed, writing raster to {output_raster}.")


# DISTANCE TO ANOMALY
@app.command()
def distance_to_anomaly_cli(
input_raster: INPUT_FILE_OPTION,
output_raster: OUTPUT_FILE_OPTION,
threshold_criteria: Annotated[ThresholdCriteria, typer.Option(case_sensitive=False)],
first_threshold_criteria_value: float = typer.Option(),
second_threshold_criteria_value: float = None,
):
"""
Calculate distance from each raster cell to nearest anomaly cell.
Uses only the first band of the raster.
"""
from eis_toolkit.raster_processing.distance_to_anomaly import distance_to_anomaly

typer.echo("Progress: 10%")

if second_threshold_criteria_value is not None:
threshold_criteria_value = (first_threshold_criteria_value, second_threshold_criteria_value)
else:
threshold_criteria_value = first_threshold_criteria_value

with rasterio.open(input_raster) as raster:
typer.echo("Progress: 25%")
out_image, out_meta = distance_to_anomaly(
anomaly_raster_profile=raster.profile,
anomaly_raster_data=raster.read(1),
threshold_criteria_value=threshold_criteria_value,
threshold_criteria=get_enum_values(threshold_criteria),
)

typer.echo("Progress: 75%")

with rasterio.open(output_raster, "w", **out_meta) as dest:
dest.write(out_image, 1)
typer.echo("Progress: 100%")

typer.echo(f"Computing distance to anomaly completed, writing raster to {output_raster}.")


# EXTRACT VALUES FROM RASTER
@app.command()
def extract_values_from_raster_cli(
Expand Down

0 comments on commit 88abd03

Please sign in to comment.