Skip to content

Commit

Permalink
Revert removal of customized load_spatialite
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm committed Aug 26, 2024
1 parent 1e5eacc commit 3f22011
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog of threedi-schema
0.224.1 (unreleased)
--------------------

- Nothing changed yet.
- Revert removing on customized load_spatialite function


0.224.0 (2024-08-16)
Expand Down
50 changes: 38 additions & 12 deletions threedi_schema/application/threedi_database.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import os
import shutil
import tempfile
import uuid
from contextlib import contextmanager
from pathlib import Path

from geoalchemy2 import load_spatialite, load_spatialite_gpkg
from sqlalchemy import create_engine, event, inspect, text
from sqlalchemy.engine import Engine
from sqlalchemy.event import listen
Expand All @@ -14,8 +12,6 @@

from .schema import ModelSchema

os.environ["SPATIALITE_LIBRARY_PATH"] = "mod_spatialite.so"

__all__ = ["ThreediDatabase"]


Expand All @@ -39,6 +35,40 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
cursor.close()


def load_spatialite(con, connection_record):
"""Load spatialite extension as described in
https://geoalchemy-2.readthedocs.io/en/latest/spatialite_tutorial.html"""
import sqlite3

con.enable_load_extension(True)
cur = con.cursor()
libs = [
# SpatiaLite >= 4.2 and Sqlite >= 3.7.17, should work on all platforms
("mod_spatialite", "sqlite3_modspatialite_init"),
# SpatiaLite >= 4.2 and Sqlite < 3.7.17 (Travis)
("mod_spatialite.so", "sqlite3_modspatialite_init"),
# SpatiaLite < 4.2 (linux)
("libspatialite.so", "sqlite3_extension_init"),
]
found = False
for lib, entry_point in libs:
try:
cur.execute("select load_extension('{}', '{}')".format(lib, entry_point))
except sqlite3.OperationalError:
continue
else:
found = True
break
try:
cur.execute("select EnableGpkgAmphibiousMode()")
except sqlite3.OperationalError:
pass
if not found:
raise RuntimeError("Cannot find any suitable spatialite module")
cur.close()
con.enable_load_extension(False)


class ThreediDatabase:
def __init__(self, path, echo=False):
self.path = path
Expand Down Expand Up @@ -68,14 +98,10 @@ def get_engine(self, get_seperate_engine=False):
poolclass = None
else:
poolclass = NullPool
if path.suffix.lower() == ".gpkg":
engine_path = f"gpkg:///{path}"
engine_fn = load_spatialite_gpkg
else:
engine_path = "sqlite:///" if path == Path("") else f"sqlite:///{path}"
engine_fn = load_spatialite
engine = create_engine(engine_path, echo=self.echo, poolclass=poolclass)
listen(engine, "connect", engine_fn)
engine = create_engine(
"sqlite:///{0}".format(self.path), echo=self.echo, poolclass=poolclass
)
listen(engine, "connect", load_spatialite)
if get_seperate_engine:
return engine
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import sqlalchemy as sa
from alembic import op
from geoalchemy2 import load_spatialite
from sqlalchemy import Boolean, Column, Float, Integer, String, Text
from sqlalchemy.event import listen
from sqlalchemy.orm import declarative_base

from threedi_schema.application.threedi_database import load_spatialite
from threedi_schema.domain.custom_types import Geometry

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

import sqlalchemy as sa
from alembic import op
from geoalchemy2 import load_spatialite
from sqlalchemy import Boolean, Column, Float, Integer, String, Text
from sqlalchemy.event import listen
from sqlalchemy.orm import declarative_base

from threedi_schema.domain.custom_types import Geometry
Expand Down

0 comments on commit 3f22011

Please sign in to comment.