Skip to content

Commit

Permalink
ENH: Add support for spatial aggregation arguments to exactextract (t…
Browse files Browse the repository at this point in the history
  • Loading branch information
KriWay-LV authored Feb 10, 2025
1 parent ad4902e commit d21b153
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local/

.vs/*
.vscode/*
.mypy_cache/*

# Local History for Visual Studio Code
.history/
Expand Down
2 changes: 1 addition & 1 deletion cropclassification/preprocess/_timeseries_calc_openeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def calculate_periodic_timeseries(
id_column=conf.columns["id"],
rasters_bands=images_bands,
output_dir=timeseries_periodic_dir,
stats=["count", "mean", "median", "std", "min", "max"], # type: ignore[arg-type]
stats=["count", "mean", "median", "std", "min", "max"],
engine="pyqgis",
nb_parallel=nb_parallel,
)
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,15 @@ def zonal_stats_band_tofile(
for band in bands:
index = raster_info.bands[band].band_index
band_columns = include_cols.copy()
band_columns.extend([f"band_{index}_{stat}" for stat in stats])
band_columns.extend(
[f"band_{index}_{stat}" for stat in [stat.split("(")[0] for stat in stats]]
)
band_stats_df = stats_df[band_columns].copy()
band_stats_df.rename(
columns={f"band_{index}_{stat}": stat for stat in stats},
columns={
f"band_{index}_{stat}": stat
for stat in [stat.split("(")[0] for stat in stats]
},
inplace=True,
)
# Add fid column to the beginning of the dataframe
Expand All @@ -288,6 +293,19 @@ def zonal_stats_band_tofile(
f"Write data for {len(band_stats_df.index)} parcels found to {output_paths[band]}" # noqa: E501
)
if not output_paths[band].exists():
pdh.to_file(band_stats_df, output_paths[band], index=False)
# Write the info table to the output file
pdh.to_file(df=band_stats_df, path=output_paths[band], index=False)

# Write the parameters table to the output file
spatial_aggregation_args_df = pd.DataFrame(
data=stats, columns=["stats"]
)
pdh.to_file(
df=spatial_aggregation_args_df,
path=output_paths[band],
table_name="params",
index=False,
append=True,
)

return output_paths
30 changes: 27 additions & 3 deletions tests/test_zonal_stats_bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@
import geofileops as gfo
import pytest

from cropclassification.helpers import config_helper as conf
from cropclassification.helpers import pandas_helper as pdh
from cropclassification.util import zonal_stats_bulk
from cropclassification.util.zonal_stats_bulk._zonal_stats_bulk_pyqgis import HAS_QGIS
from tests.test_helper import SampleData


@pytest.mark.parametrize("engine", ["pyqgis", "rasterstats", "exactextract"])
def test_zonal_stats_bulk(tmp_path, engine):
@pytest.mark.parametrize(
"engine, stats",
[
("pyqgis", ["mean", "count"]),
("rasterstats", ["mean", "count"]),
(
"exactextract",
[
"mean(min_coverage_frac=0.5,coverage_weight=none)",
"count(min_coverage_frac=0.5,coverage_weight=none)",
],
),
],
)
def test_zonal_stats_bulk(tmp_path, engine, stats):
if engine == "pyqgis" and not HAS_QGIS:
pytest.skip("QGIS is not available on this system.")

Expand All @@ -19,6 +33,16 @@ def test_zonal_stats_bulk(tmp_path, engine):
test_dir = tmp_path / sample_dir.name
shutil.copytree(sample_dir, test_dir)

# Read the configuration
config_paths = [
SampleData.config_dir / "cropgroup.ini",
SampleData.tasks_dir / "local_overrule.ini",
]
conf.read_config(
config_paths=config_paths,
default_basedir=sample_dir,
)

# Make sure the s2-agri input file was copied
test_image_roi_dir = test_dir / SampleData.image_dir.name / SampleData.roi_name
test_s1_asc_dir = test_image_roi_dir / SampleData.image_s1_asc_path.parent.name
Expand All @@ -34,7 +58,7 @@ def test_zonal_stats_bulk(tmp_path, engine):
id_column="UID",
rasters_bands=images_bands,
output_dir=tmp_path,
stats=["mean", "count"],
stats=stats,
engine=engine,
)

Expand Down

0 comments on commit d21b153

Please sign in to comment.