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

geopackage support #350

Merged
merged 12 commits into from
Mar 12, 2024
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Changelog of threedigrid-builder
- Make package compatible with Python 3.12
- Make package compatible with GDAL 3.4 and 3.6. Note that in GDAL 3.6 refinements where the area exactly matches
the grid may slightly differ from those in GDAL 3.4 and older.
- Add geopackage schematisation to test data
- Use geopackage, instead of spatialite, for tests
- Add geopackage to schematisations tested in integration test


1.13.1 (2024-02-19)
Expand Down
5 changes: 4 additions & 1 deletion integration_tests/test_bergermeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def count_unique(arr):
return dict(zip(*np.unique(arr, return_counts=True)))


@pytest.mark.parametrize("filename", ["v2_bergermeer.sqlite"])
@pytest.mark.parametrize(
"filename",
["v2_bergermeer.sqlite", "v2_bergermeer.gpkg"],
)
def test_integration(tmp_path, filename):
shutil.copyfile(unittests_data_path / filename, tmp_path / filename)
progress_callback = Mock()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_version():

install_requires = [
"numpy>=1.15",
"threedi-schema==0.219.*",
"threedi-schema==0.220.*",
"shapely>=2",
"pyproj>=3",
"condenser[geo]>=0.1.1",
Expand Down
2 changes: 1 addition & 1 deletion threedigrid_builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from .exceptions import * # NOQA

# fmt: off
__version__ = '1.13.2.dev0'
__version__ = '1.13.2.dev1'
# fmt: on
7 changes: 6 additions & 1 deletion threedigrid_builder/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ def _make_gridadmin(
meta=None,
progress_callback=None,
upgrade=False,
convert_to_geopackage=False,
):
"""Compute interpolated channel nodes"""
progress_callback(0.0, "Reading input schematisation...")
db = SQLite(sqlite_path, upgrade=upgrade)
db = SQLite(
sqlite_path, upgrade=upgrade, convert_to_geopackage=convert_to_geopackage
)

node_id_counter = itertools.count()
line_id_counter = itertools.count()
Expand Down Expand Up @@ -221,6 +224,7 @@ def make_gridadmin(
meta: dict = None,
progress_callback: Optional[Callable[[float, str], None]] = None,
upgrade: bool = False,
convert_to_geopackage: bool = False,
):
"""Create a Grid instance from sqlite and DEM paths

Expand Down Expand Up @@ -272,6 +276,7 @@ def make_gridadmin(
meta=meta,
progress_callback=progress_callback,
upgrade=upgrade,
convert_to_geopackage=convert_to_geopackage,
)

progress_callback(0.99, "Writing gridadmin...")
Expand Down
10 changes: 6 additions & 4 deletions threedigrid_builder/interface/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _set_initialization_type(


class SQLite:
def __init__(self, path: pathlib.Path, upgrade=False):
def __init__(self, path: pathlib.Path, upgrade=False, convert_to_geopackage=False):
if not path.exists():
raise FileNotFoundError(f"File not found: {path}")
self.db = ThreediDatabase(path)
Expand All @@ -106,7 +106,7 @@ def __init__(self, path: pathlib.Path, upgrade=False):
version = self.get_version()
if version < MIN_SQLITE_VERSION:
if upgrade:
self.upgrade()
self.upgrade(convert_to_geopackage=convert_to_geopackage)
else:
raise SchematisationError(f"Too old sqlite version {version}.")

Expand All @@ -115,9 +115,11 @@ def get_version(self) -> int:
schema = ModelSchema(self.db)
return schema.get_version()

def upgrade(self):
def upgrade(self, convert_to_geopackage=False):
schema = ModelSchema(self.db)
schema.upgrade(backup=False, set_views=False)
schema.upgrade(
backup=False, set_views=False, convert_to_geopackage=convert_to_geopackage
)

@contextmanager
def get_session(self) -> ContextManager[Session]:
Expand Down
4 changes: 2 additions & 2 deletions threedigrid_builder/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
def db(tmp_path_factory):
"""Yields a threedigrid_builder.interface.db.SQLite object with access
to the test v2_bergermeer.sqlite."""
fn = tmp_path_factory.mktemp("data") / "v2_bergermeer.sqlite"
sqlite_path = data_path / "v2_bergermeer.sqlite"
fn = tmp_path_factory.mktemp("data") / "v2_bergermeer.gpkg"
sqlite_path = data_path / "v2_bergermeer.gpkg"
shutil.copyfile(sqlite_path, fn)
if not os.path.isfile(fn):
pytest.skip("sample sqlite is not available", allow_module_level=True)
Expand Down
Binary file added threedigrid_builder/tests/data/v2_bergermeer.gpkg
Binary file not shown.
2 changes: 1 addition & 1 deletion threedigrid_builder/tests/data/v2_bergermeer.sqlite
Git LFS file not shown
2 changes: 1 addition & 1 deletion threedigrid_builder/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_init_bad_version(tmp_path):


def test_get_version(db):
assert db.get_version() == 218
assert db.get_version() == 220


def test_get_boundary_conditions_1d(db):
Expand Down
Loading