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

Allow overwriting a model that is open in QGIS #1881

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all 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
10 changes: 9 additions & 1 deletion python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import shutil
from collections.abc import Generator
from os import PathLike
from pathlib import Path
Expand Down Expand Up @@ -190,7 +191,12 @@ def _write_toml(self, fn: Path) -> Path:
return fn

def _save(self, directory: DirectoryPath, input_dir: DirectoryPath):
db_path = directory / input_dir / "database.gpkg"
# We write all tables to a temporary GeoPackage with a dot prefix,
# and at the end move this over the target file.
# This does not throw a PermissionError if the file is open in QGIS.
db_path = directory / input_dir / ".database.gpkg"

# avoid adding tables to existing model
db_path.parent.mkdir(parents=True, exist_ok=True)
db_path.unlink(missing_ok=True)
context_file_writing.get()["database"] = db_path
Expand All @@ -207,6 +213,8 @@ def _save(self, directory: DirectoryPath, input_dir: DirectoryPath):
for sub in self._nodes():
sub._save(directory, input_dir)

shutil.move(db_path, db_path.with_name("database.gpkg"))

def set_crs(self, crs: str) -> None:
"""Set the coordinate reference system of the data in the model.

Expand Down
Loading