Skip to content

Commit

Permalink
Copy over the required shapefile files upon saving forcing (#457)
Browse files Browse the repository at this point in the history
* Copy over the required shapefile files upon saving forcing

* Update changelog

* Correct the link in changelog

Co-authored-by: Stefan Verhoeven <[email protected]>

* Add better error message if prj file is missing from shape

---------

Co-authored-by: Stefan Verhoeven <[email protected]>
  • Loading branch information
BSchilperoort and sverhoeven authored Sep 16, 2024
1 parent d121adf commit 2f2d7c0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/ewatercycle/base/forcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions tests/src/base/test_forcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2f2d7c0

Please sign in to comment.