Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proximity computation CLI function #438

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions eis_toolkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,58 @@ def cell_based_association_cli(
typer.echo(f"Cell based association completed, writing raster to {output_raster}.")


# PROXIMITY COMPUTATION
@app.command()
def proximity_computation_cli(
input_vector: INPUT_FILE_OPTION,
output_raster: OUTPUT_FILE_OPTION,
max_distance: float = typer.Option(),
max_distance_value: float = 0.0,
anomaly_value: float = 1.0,
base_raster: INPUT_FILE_OPTION = None,
pixel_size: float = None,
extent: Tuple[float, float, float, float] = (None, None, None, None),
):
"""Calculate distance from raster cell to nearest geometry."""
from eis_toolkit.exceptions import InvalidParameterValueException
from eis_toolkit.utilities.raster import profile_from_extent_and_pixel_size
from eis_toolkit.vector_processing.proximity_computation import proximity_computation

typer.echo("Progress: 10%")

geodataframe = gpd.read_file(input_vector)
typer.echo("Progress: 25%")

if base_raster is None or base_raster == "":
if any(bound is None for bound in extent) or pixel_size is None or pixel_size <= 0:
raise InvalidParameterValueException(
"Expected positive pixel size and defined extent in absence of base raster. "
+ f"Pixel size: {pixel_size}, extent: {extent}."
)
profile = profile_from_extent_and_pixel_size(extent, (pixel_size, pixel_size))
profile["crs"] = geodataframe.crs
profile["driver"] = "GTiff"
profile["dtype"] = "float32"
else:
with rasterio.open(base_raster) as raster:
profile = raster.profile.copy()

out_image = proximity_computation(
geodataframe=geodataframe,
raster_profile=profile,
maximum_distance=max_distance,
scale_range=(anomaly_value, max_distance_value),
)
profile["count"] = 1
typer.echo("Progress: 75%")

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

typer.echo(f"Proximity computation completed, writing raster to {output_raster}.")


# --- PREDICTION ---


Expand Down
Loading