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

Copy over the required shapefile files upon saving forcing #457

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 ([#457](https://github.com/eWaterCycle/ewatercycle/pull/457)).
BSchilperoort marked this conversation as resolved.
Show resolved Hide resolved

### [2.3.0] (2024-08-29)

## Changed
Expand Down
9 changes: 8 additions & 1 deletion src/ewatercycle/base/forcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,16 @@ 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)
)
# Also copy other required files:
for ext in [".dbf", ".shx", ".prj"]:
Copy link
Member

@sverhoeven sverhoeven Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The '.prj' file is optional. Can you check that file exists before copying it?

If I run

from ewatercycle.testing.fixtures import rhine_shape
from ewatercycle.forcing import sources
from pathlib import Path
import shutil
fc = sources['CaravanForcing']
Path('/tmp/shptest').mkdir(exist_ok=True)
s = rhine_shape()
shutil.copy(s, '/tmp/shptest/rhine.shp')
shutil.copy(s.with_suffix('.dbf'), '/tmp/shptest/')
shutil.copy(s.with_suffix('.shx'), '/tmp/shptest/')
fd = Path('/tmp/cf')
fd.mkdir()
f = fc.generate(start_time="1991-01-01T00:00:00Z", end_time="1991-12-31T00:00:00Z", shape='/tmp/shptest/rhine.shp', directory=fd, basin_id='camels_01022500')

I get

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[5], [line 1](vscode-notebook-cell:?execution_count=5&line=1)
----> [1](vscode-notebook-cell:?execution_count=5&line=1) f = fc.generate(start_time="1991-01-01T00:00:00Z", end_time="1991-12-31T00:00:00Z", shape='/tmp/shptest/Rhine.shp', directory=fd, basin_id='camels_01022500')

File ~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:244, in CaravanForcing.generate(cls, start_time, end_time, directory, variables, shape, **kwargs)
    [229](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:229)     ds_basin_time[var].to_netcdf(
    [230](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:230)         Path(directory)
    [231](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:231)         / f"{basin_id}_{start_time_name}_{end_time_name}_{var}.nc"
    [232](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:232)     )
    [234](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:234) forcing = cls(
    [235](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:235)     directory=Path(directory),
    [236](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:236)     start_time=start_time,
   (...)
    [242](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:242)     },
    [243](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:243) )
--> [244](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:244) forcing.save()
    [245](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/_forcings/caravan.py:245) return forcing

File ~/git/eWaterCycle/ewatercycle/src/ewatercycle/base/forcing.py:230, in DefaultForcing.save(self)
    [228](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/base/forcing.py:228)     # Also copy other required files:
    [229](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/base/forcing.py:229)     for ext in [".dbf", ".shx", ".prj"]:
--> [230](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/base/forcing.py:230)         shutil.copy(
    [231](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/base/forcing.py:231)             clone.shape.with_suffix(ext),
    [232](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/git/eWaterCycle/ewatercycle/src/ewatercycle/base/forcing.py:232)             clone.directory / clone.shape.with_suffix(ext).name,
...
    [261](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/miniforge3/envs/ewatercycle/lib/python3.12/shutil.py:261)         try:
    [262](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/miniforge3/envs/ewatercycle/lib/python3.12/shutil.py:262)             with open(dst, 'wb') as fdst:
    [263](https://file+.vscode-resource.vscode-cdn.net/home/verhoes/git/eWaterCycle/ewatercycle/~/miniforge3/envs/ewatercycle/lib/python3.12/shutil.py:263)                 # macOS

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/shptest/Rhine.prj'

which is not something we would like to bother a user with

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.prj files are required for our workflow, as we cannot reliably guess what projection the shapefile is in (probably WGS-84, but who knows). If we guess the projection incorrectly the user will get incorrect results without ever knowing (clearly) why.

Therefore I think it's best to keep it as mandatory. I will add a more clear error.

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