diff --git a/CHANGELOG.md b/CHANGELOG.md index c5e7e794..b6271041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ Formatted as described on [https://keepachangelog.com](https://keepachangelog.co ## [Unreleased] +## Fixed + +- all required shapefile files (`.shp`, `.shx`, `.dbf`, `.prj`) are now copied to the new directory when saving a forcing object ([#430](https://github.com/eWaterCycle/ewatercycle/issues/430)). + ### [2.3.0] (2024-08-29) ## Changed diff --git a/src/ewatercycle/base/forcing.py b/src/ewatercycle/base/forcing.py index 632545bc..6051b3a8 100644 --- a/src/ewatercycle/base/forcing.py +++ b/src/ewatercycle/base/forcing.py @@ -222,9 +222,23 @@ def save(self): # Copy shapefile so statistics like area can be derived if clone.shape is not None: if not clone.shape.is_relative_to(clone.directory): - clone.shape = Path( + new_shp_path = Path( shutil.copy(clone.shape, clone.directory / clone.shape.name) ) + if not clone.shape.with_suffix(".prj").exists(): + msg = ( + "Your shape file is missing the .prj projection file.\n" + "This file is required, as we cannot guess what projection your" + "shapefile is in." + ) + raise FileNotFoundError(msg) + # Also copy other required files: + for ext in [".dbf", ".shx", ".prj"]: + shutil.copy( + clone.shape.with_suffix(ext), + clone.directory / clone.shape.with_suffix(ext).name, + ) + clone.shape = new_shp_path clone.shape = clone.shape.relative_to(clone.directory) fdict = clone.model_dump(exclude={"directory"}, exclude_none=True, mode="json") diff --git a/tests/src/base/test_forcing.py b/tests/src/base/test_forcing.py index 0e122034..aa10fd17 100644 --- a/tests/src/base/test_forcing.py +++ b/tests/src/base/test_forcing.py @@ -59,6 +59,10 @@ def test_save(self, tmp_path: Path, sample_shape: str): assert content == expected + # make sure all mandatory (shape)files exist + for ext in [".dbf", ".prj", ".shp", ".shx"]: + assert (forcing.directory / forcing.shape.with_suffix(ext)).exists() + class TestGenericDistributedForcingWithInternalShape: def test_save(self, tmp_path: Path, sample_shape: str):