Skip to content

Commit 5813e55

Browse files
authored
Merge pull request #307 from GispoCoding/293-add-smoothingfilter-cli-functions
293 add smoothing / filter cli functions
2 parents a261d5f + 0e078ae commit 5813e55

File tree

2 files changed

+297
-6
lines changed

2 files changed

+297
-6
lines changed

eis_toolkit/cli.py

+291
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ class NodataHandling(str, Enum):
200200
remove = "remove"
201201

202202

203+
class FocalFilterMethod(str, Enum):
204+
"""Focal filter methods."""
205+
206+
mean = "mean"
207+
median = "median"
208+
209+
210+
class FocalFilterShape(str, Enum):
211+
"""Shape of the filter window."""
212+
213+
square = "square"
214+
circle = "circle"
215+
216+
217+
class MexicanHatFilterDirection(str, Enum):
218+
"""Direction of calculating kernel values."""
219+
220+
rectangular = "rectangular"
221+
circular = "circular"
222+
223+
203224
class LocalMoranWeightType(str, Enum):
204225
"""Weight type for Local Moran's I."""
205226

@@ -533,6 +554,276 @@ def local_morans_i_cli(
533554
# --- RASTER PROCESSING ---
534555

535556

557+
# FOCAL FILTER
558+
@app.command()
559+
def focal_filter_cli(
560+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
561+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
562+
method: FocalFilterMethod = FocalFilterMethod.mean,
563+
size: int = 3,
564+
shape: FocalFilterShape = FocalFilterShape.circle,
565+
):
566+
"""Apply a basic focal filter to the input raster."""
567+
from eis_toolkit.raster_processing.filters.focal import focal_filter
568+
569+
typer.echo("Progress: 10%")
570+
571+
with rasterio.open(input_raster) as raster:
572+
typer.echo("Progress: 25%")
573+
out_image, out_meta = focal_filter(raster=raster, method=method, size=size, shape=shape)
574+
typer.echo("Progress: 75%")
575+
576+
with rasterio.open(output_raster, "w", **out_meta) as dest:
577+
dest.write(out_image, 1)
578+
typer.echo("Progress: 100%")
579+
580+
typer.echo(f"Focal filter applied, output raster written to {output_raster}.")
581+
582+
583+
# GAUSSIAN FILTER
584+
@app.command()
585+
def gaussian_filter_cli(
586+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
587+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
588+
sigma: float = 1.0,
589+
truncate: float = 4.0,
590+
size: int = None,
591+
):
592+
"""Apply a gaussian filter to the input raster."""
593+
from eis_toolkit.raster_processing.filters.focal import gaussian_filter
594+
595+
typer.echo("Progress: 10%")
596+
597+
with rasterio.open(input_raster) as raster:
598+
typer.echo("Progress: 25%")
599+
out_image, out_meta = gaussian_filter(raster=raster, sigma=sigma, truncate=truncate, size=size)
600+
typer.echo("Progress: 75%")
601+
602+
with rasterio.open(output_raster, "w", **out_meta) as dest:
603+
dest.write(out_image, 1)
604+
typer.echo("Progress: 100%")
605+
606+
typer.echo(f"Gaussial filter applied, output raster written to {output_raster}.")
607+
608+
609+
# MEXICAN HAT FILTER
610+
@app.command()
611+
def mexican_hat_filter_cli(
612+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
613+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
614+
sigma: float = 1.0,
615+
truncate: float = 4.0,
616+
size: int = None,
617+
direction: MexicanHatFilterDirection = MexicanHatFilterDirection.circular,
618+
):
619+
"""Apply a mexican hat filter to the input raster."""
620+
from eis_toolkit.raster_processing.filters.focal import mexican_hat_filter
621+
622+
typer.echo("Progress: 10%")
623+
624+
with rasterio.open(input_raster) as raster:
625+
typer.echo("Progress: 25%")
626+
out_image, out_meta = mexican_hat_filter(
627+
raster=raster, sigma=sigma, truncate=truncate, size=size, direction=direction
628+
)
629+
typer.echo("Progress: 75%")
630+
631+
with rasterio.open(output_raster, "w", **out_meta) as dest:
632+
dest.write(out_image, 1)
633+
typer.echo("Progress: 100%")
634+
635+
typer.echo(f"Mexican hat filter applied, output raster written to {output_raster}.")
636+
637+
638+
# LEE ADDITIVE NOISE FILTER
639+
@app.command()
640+
def lee_additive_noise_filter_cli(
641+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
642+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
643+
size: int = 3,
644+
add_noise_var: float = 0.25,
645+
):
646+
"""Apply a Lee filter considering additive noise components in the input raster."""
647+
from eis_toolkit.raster_processing.filters.speckle import lee_additive_noise_filter
648+
649+
typer.echo("Progress: 10%")
650+
651+
with rasterio.open(input_raster) as raster:
652+
typer.echo("Progress: 25%")
653+
out_image, out_meta = lee_additive_noise_filter(raster=raster, size=size, add_noise_var=add_noise_var)
654+
typer.echo("Progress: 75%")
655+
656+
with rasterio.open(output_raster, "w", **out_meta) as dest:
657+
dest.write(out_image, 1)
658+
typer.echo("Progress: 100%")
659+
660+
typer.echo(f"Additive Lee noise filter applied, output raster written to {output_raster}.")
661+
662+
663+
# LEE MULTIPLICATIVE NOISE FILTER
664+
@app.command()
665+
def lee_multiplicative_noise_filter_cli(
666+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
667+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
668+
size: int = 3,
669+
multi_noise_mean: float = 1.0,
670+
n_looks: int = 1,
671+
):
672+
"""Apply a Lee filter considering multiplicative noise components in the input raster."""
673+
from eis_toolkit.raster_processing.filters.speckle import lee_multiplicative_noise_filter
674+
675+
typer.echo("Progress: 10%")
676+
677+
with rasterio.open(input_raster) as raster:
678+
typer.echo("Progress: 25%")
679+
out_image, out_meta = lee_multiplicative_noise_filter(
680+
raster=raster, size=size, mult_noise_mean=multi_noise_mean, n_looks=n_looks
681+
)
682+
typer.echo("Progress: 75%")
683+
684+
with rasterio.open(output_raster, "w", **out_meta) as dest:
685+
dest.write(out_image, 1)
686+
typer.echo("Progress: 100%")
687+
688+
typer.echo(f"Multiplicative Lee noise filter applied, output raster written to {output_raster}.")
689+
690+
691+
# LEE ADDITIVE MULTIPLICATIVE NOISE FILTER
692+
@app.command()
693+
def lee_additive_multiplicative_noise_filter_cli(
694+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
695+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
696+
size: int = 3,
697+
add_noise_var: float = 0.25,
698+
add_noise_mean: float = 0,
699+
multi_noise_mean: float = 1.0,
700+
):
701+
"""Apply a Lee filter considering both additive and multiplicative noise components in the input raster."""
702+
from eis_toolkit.raster_processing.filters.speckle import lee_additive_multiplicative_noise_filter
703+
704+
typer.echo("Progress: 10%")
705+
706+
with rasterio.open(input_raster) as raster:
707+
typer.echo("Progress: 25%")
708+
out_image, out_meta = lee_additive_multiplicative_noise_filter(
709+
raster=raster,
710+
size=size,
711+
add_noise_var=add_noise_var,
712+
add_noise_mean=add_noise_mean,
713+
mult_noise_mean=multi_noise_mean,
714+
)
715+
typer.echo("Progress: 75%")
716+
717+
with rasterio.open(output_raster, "w", **out_meta) as dest:
718+
dest.write(out_image, 1)
719+
typer.echo("Progress: 100%")
720+
721+
typer.echo(f"Additive multiplicative Lee noise filter applied, output raster written to {output_raster}.")
722+
723+
724+
# LEE ENHANCED FILTER
725+
@app.command()
726+
def lee_enhanced_filter_cli(
727+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
728+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
729+
size: int = 3,
730+
n_looks: int = 1,
731+
damping_factor: float = 1.0,
732+
):
733+
"""Apply an enhanced Lee filter to the input raster."""
734+
from eis_toolkit.raster_processing.filters.speckle import lee_enhanced_filter
735+
736+
typer.echo("Progress: 10%")
737+
738+
with rasterio.open(input_raster) as raster:
739+
typer.echo("Progress: 25%")
740+
out_image, out_meta = lee_enhanced_filter(
741+
raster=raster, size=size, n_looks=n_looks, damping_factor=damping_factor
742+
)
743+
typer.echo("Progress: 75%")
744+
745+
with rasterio.open(output_raster, "w", **out_meta) as dest:
746+
dest.write(out_image, 1)
747+
typer.echo("Progress: 100%")
748+
749+
typer.echo(f"Enhanced Lee filter applied, output raster written to {output_raster}.")
750+
751+
752+
# GAMMA FILTER
753+
@app.command()
754+
def gamma_filter_cli(
755+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
756+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
757+
size: int = 3,
758+
n_looks: int = 1,
759+
):
760+
"""Apply a Gamma filter to the input raster."""
761+
from eis_toolkit.raster_processing.filters.speckle import gamma_filter
762+
763+
typer.echo("Progress: 10%")
764+
765+
with rasterio.open(input_raster) as raster:
766+
typer.echo("Progress: 25%")
767+
out_image, out_meta = gamma_filter(raster=raster, size=size, n_looks=n_looks)
768+
typer.echo("Progress: 75%")
769+
770+
with rasterio.open(output_raster, "w", **out_meta) as dest:
771+
dest.write(out_image, 1)
772+
typer.echo("Progress: 100%")
773+
774+
typer.echo(f"Gamma filter applied, output raster written to {output_raster}.")
775+
776+
777+
# FROST FILTER
778+
@app.command()
779+
def frost_filter_cli(
780+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
781+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
782+
size: int = 3,
783+
damping_factor: float = 1.0,
784+
):
785+
"""Apply a Frost filter to the input raster."""
786+
from eis_toolkit.raster_processing.filters.speckle import frost_filter
787+
788+
typer.echo("Progress: 10%")
789+
790+
with rasterio.open(input_raster) as raster:
791+
typer.echo("Progress: 25%")
792+
out_image, out_meta = frost_filter(raster=raster, size=size, damping_factor=damping_factor)
793+
typer.echo("Progress: 75%")
794+
795+
with rasterio.open(output_raster, "w", **out_meta) as dest:
796+
dest.write(out_image, 1)
797+
typer.echo("Progress: 100%")
798+
799+
typer.echo(f"Frost filter applied, output raster written to {output_raster}.")
800+
801+
802+
# KUAN FILTER
803+
@app.command()
804+
def kuan_filter_cli(
805+
input_raster: Annotated[Path, INPUT_FILE_OPTION],
806+
output_raster: Annotated[Path, OUTPUT_FILE_OPTION],
807+
size: int = 3,
808+
n_looks: int = 1,
809+
):
810+
"""Apply a Kuan filter to the input raster."""
811+
from eis_toolkit.raster_processing.filters.speckle import kuan_filter
812+
813+
typer.echo("Progress: 10%")
814+
815+
with rasterio.open(input_raster) as raster:
816+
typer.echo("progress: 25%")
817+
out_image, out_meta = kuan_filter(raster=raster, size=size, n_looks=n_looks)
818+
typer.echo("Progress: 75%")
819+
820+
with rasterio.open(output_raster, "w", **out_meta) as dest:
821+
dest.write(out_image, 1)
822+
typer.echo("Progress: 100%")
823+
824+
typer.echo(f"Kuan filter applied, output raster written to {output_raster}.")
825+
826+
536827
# CHECK RASTER GRIDS
537828
@app.command()
538829
def check_raster_grids_cli(input_rasters: INPUT_FILES_ARGUMENT, same_extent: bool = False):

eis_toolkit/raster_processing/filters/speckle.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _gamma(window: np.ndarray, n_looks: int) -> np.ndarray:
179179

180180

181181
@beartype
182-
def _frost(window: np.ndarray, damping_factor: int) -> Number:
182+
def _frost(window: np.ndarray, damping_factor: Number) -> Number:
183183
"""
184184
Calculate the weighted value for a Frost filter from a window of pixels.
185185
@@ -381,8 +381,8 @@ def lee_additive_multiplicative_noise_filter(
381381
def lee_enhanced_filter(
382382
raster: rasterio.io.DatasetReader,
383383
size: int = 3,
384-
n_looks: Number = 1,
385-
damping_factor: Number = 1,
384+
n_looks: int = 1,
385+
damping_factor: Number = 1.0,
386386
) -> tuple[np.ndarray, dict]:
387387
"""
388388
Apply an enhanced Lee filter to the input raster.
@@ -431,7 +431,7 @@ def lee_enhanced_filter(
431431
def gamma_filter(
432432
raster: rasterio.io.DatasetReader,
433433
size: int = 3,
434-
n_looks: Number = 1,
434+
n_looks: int = 1,
435435
) -> tuple[np.ndarray, dict]:
436436
"""
437437
Apply a Gamma filter to the input raster.
@@ -476,7 +476,7 @@ def gamma_filter(
476476
def frost_filter(
477477
raster: rasterio.io.DatasetReader,
478478
size: int = 3,
479-
damping_factor: Number = 1,
479+
damping_factor: Number = 1.0,
480480
) -> tuple[np.ndarray, dict]:
481481
"""
482482
Apply a Frost filter to the input raster.
@@ -521,7 +521,7 @@ def frost_filter(
521521
def kuan_filter(
522522
raster: rasterio.io.DatasetReader,
523523
size: int = 3,
524-
n_looks: Number = 1,
524+
n_looks: int = 1,
525525
) -> tuple[np.ndarray, dict]:
526526
"""
527527
Apply a Kuan filter to the input raster.

0 commit comments

Comments
 (0)