Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Jul 3, 2023
1 parent e15a51f commit ab5e033
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 153 deletions.
4 changes: 2 additions & 2 deletions geoalchemy2/admin/dialects/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _format_select_args(*args):


def check_management(*args):
"""Default function to check management."""
"""Default function to check management (always True by default)."""
return True


Expand Down Expand Up @@ -53,7 +53,7 @@ def _get_dispatch_info(table, bind, check_col_management=None):
"""Get info required for dispatch events."""
dialect = bind.dialect

# Filter Geometry columns from the table with management=True
# Filter Geometry columns from the table
# Note: Geography and PostGIS >= 2.0 don't need this
gis_cols = _get_gis_cols(table, Geometry, dialect, check_col_management=check_col_management)

Expand Down
10 changes: 2 additions & 8 deletions geoalchemy2/admin/dialects/geopackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from geoalchemy2.admin.dialects.common import _check_spatial_type
from geoalchemy2.admin.dialects.common import _format_select_args
from geoalchemy2.admin.dialects.common import _spatial_idx_name
from geoalchemy2.admin.dialects.common import check_management
from geoalchemy2.admin.dialects.common import setup_create_drop
from geoalchemy2.admin.dialects.sqlite import _SQLITE_FUNCTIONS
from geoalchemy2.admin.dialects.sqlite import get_col_dim
Expand Down Expand Up @@ -174,7 +173,6 @@ def reflect_geometry_column(inspector, table, column_info):
def before_create(table, bind, **kw):
"""Handle spatial indexes during the before_create event."""
dialect, gis_cols, regular_cols = setup_create_drop(table, bind)
dialect_name = dialect.name

# Remove the spatial indexes from the table metadata because they should not be
# created during the table.create() step since the associated columns do not exist
Expand All @@ -183,10 +181,7 @@ def before_create(table, bind, **kw):
current_indexes = set(table.indexes)
for idx in current_indexes:
for col in table.info["_saved_columns"]:
if (
_check_spatial_type(col.type, Geometry, dialect)
and check_management(col, dialect_name)
) and col in idx.columns.values():
if _check_spatial_type(col.type, Geometry, dialect) and col in idx.columns.values():
table.indexes.remove(idx)
if idx.name != _spatial_idx_name(table.name, col.name) or not getattr(
col.type, "spatial_index", False
Expand Down Expand Up @@ -215,11 +210,10 @@ def before_create(table, bind, **kw):
def after_create(table, bind, **kw):
"""Handle spatial indexes during the after_create event."""
dialect = bind.dialect
dialect_name = dialect.name

for col in table.columns:
# Add the managed Geometry columns with gpkgAddGeometryColumn()
if _check_spatial_type(col.type, Geometry, dialect) and check_management(col, dialect_name):
if _check_spatial_type(col.type, Geometry, dialect):
col.type = col._actual_type
del col._actual_type
dimension = get_col_dim(col)
Expand Down
12 changes: 2 additions & 10 deletions geoalchemy2/admin/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from geoalchemy2 import functions
from geoalchemy2.admin.dialects.common import _check_spatial_type
from geoalchemy2.admin.dialects.common import _spatial_idx_name
from geoalchemy2.admin.dialects.common import check_management
from geoalchemy2.admin.dialects.common import setup_create_drop
from geoalchemy2.types import Geography
from geoalchemy2.types import Geometry
Expand Down Expand Up @@ -70,7 +69,6 @@ def reflect_geometry_column(inspector, table, column_info):
def before_create(table, bind, **kw):
"""Handle spatial indexes during the before_create event."""
dialect, gis_cols, regular_cols = setup_create_drop(table, bind)
dialect_name = dialect.name

# Remove the spatial indexes from the table metadata because they should not be
# created during the table.create() step since the associated columns do not exist
Expand All @@ -79,10 +77,7 @@ def before_create(table, bind, **kw):
current_indexes = set(table.indexes)
for idx in current_indexes:
for col in table.info["_saved_columns"]:
if (
_check_spatial_type(col.type, Geometry, dialect)
and check_management(col, dialect_name)
) and col in idx.columns.values():
if (_check_spatial_type(col.type, Geometry, dialect)) and col in idx.columns.values():
table.indexes.remove(idx)
if idx.name != _spatial_idx_name(table.name, col.name) or not getattr(
col.type, "spatial_index", False
Expand All @@ -96,7 +91,6 @@ def after_create(table, bind, **kw):
"""Handle spatial indexes during the after_create event."""
# Restore original column list including managed Geometry columns
dialect = bind.dialect
dialect_name = dialect.name

# table.columns = table.info.pop("_saved_columns")

Expand All @@ -107,9 +101,7 @@ def after_create(table, bind, **kw):
and col.type.spatial_index is True
):
# If the index does not exist, define it and create it
if not [i for i in table.indexes if col in i.columns.values()] and check_management(
col, dialect_name
):
if not [i for i in table.indexes if col in i.columns.values()]:
sql = "ALTER TABLE {} ADD SPATIAL INDEX({});".format(table.name, col.name)
q = text(sql)
bind.execute(q)
Expand Down
1 change: 0 additions & 1 deletion geoalchemy2/admin/dialects/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from geoalchemy2.admin.dialects.common import _check_spatial_type
from geoalchemy2.admin.dialects.common import _format_select_args
from geoalchemy2.admin.dialects.common import _spatial_idx_name
from geoalchemy2.admin.dialects.common import check_management
from geoalchemy2.admin.dialects.common import setup_create_drop
from geoalchemy2.types import Geography
from geoalchemy2.types import Geometry
Expand Down
1 change: 0 additions & 1 deletion geoalchemy2/admin/dialects/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from geoalchemy2.admin.dialects.common import _check_spatial_type
from geoalchemy2.admin.dialects.common import _format_select_args
from geoalchemy2.admin.dialects.common import _spatial_idx_name
from geoalchemy2.admin.dialects.common import check_management
from geoalchemy2.admin.dialects.common import setup_create_drop
from geoalchemy2.types import Geography
from geoalchemy2.types import Geometry
Expand Down
66 changes: 7 additions & 59 deletions geoalchemy2/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,41 +68,18 @@ class _GISType(UserDefinedType):
:class:`geoalchemy2.types.Geography`.
When set to ``None`` then no "geometry type" constraints will be
attached to the geometry type declaration. Using ``None`` here
is not compatible with setting ``management`` to ``True``.
attached to the geometry type declaration.
Default is ``"GEOMETRY"``.
srid: The SRID for this column. E.g. 4326. Default is ``-1``.
dimension: The dimension of the geometry. Default is ``2``.
With ``management`` set to ``True``, that is when ``AddGeometryColumn`` is used
to add the geometry column, there are two constraints:
* The ``geometry_type`` must not end with ``"ZM"``. This is due to PostGIS'
``AddGeometryColumn`` failing with ZM geometry types. Instead the "simple"
geometry type (e.g. POINT rather POINTZM) should be used with ``dimension``
set to ``4``.
* When the ``geometry_type`` ends with ``"Z"`` or ``"M"`` then ``dimension``
must be set to ``3``.
With ``management`` set to ``False`` (the default) ``dimension`` is not
taken into account, and the actual dimension is fully defined with the
``geometry_type``.
spatial_index: Indicate if a spatial index should be created. Default is ``True``.
use_N_D_index: Use the N-D index instead of the standard 2-D index.
management: Indicate if the ``AddGeometryColumn`` and ``DropGeometryColumn``
managements functions should be called when adding and dropping the
geometry column. Should be set to ``True`` for PostGIS 1.x and SQLite. Default is
``False``. Note that this option has no effect for
:class:`geoalchemy2.types.Geography`.
use_typmod: By default PostgreSQL type modifiers are used to create the geometry
column. To use check constraints instead set ``use_typmod`` to
``False``. By default this option is not included in the call to
``AddGeometryColumn``. Note that this option is only taken
into account if ``management`` is set to ``True`` and is only available
for PostGIS 2.x.
``AddGeometryColumn``. Note that this option is only available for PostGIS 2.x.
"""

name = None
Expand Down Expand Up @@ -131,15 +108,14 @@ def __init__(
dimension=2,
spatial_index=True,
use_N_D_index=False,
management=None,
use_typmod=None,
from_text=None,
name=None,
nullable=True,
_spatial_index_reflected=None,
):
geometry_type, srid = self.check_ctor_args(
geometry_type, srid, dimension, management, use_typmod, nullable
geometry_type, srid, dimension, use_typmod, nullable
)
self.geometry_type = geometry_type
self.srid = srid
Expand All @@ -150,13 +126,6 @@ def __init__(
self.dimension = dimension
self.spatial_index = spatial_index
self.use_N_D_index = use_N_D_index
if management is not None:
warnings.warn(
"The 'management' parameter is deprecated and will raise an error in the "
"version 0.14",
DeprecationWarning,
)
self.management = management is True
self.use_typmod = use_typmod
self.extended = self.as_binary == "ST_AsEWKB"
self.nullable = nullable
Expand Down Expand Up @@ -198,36 +167,16 @@ def process(bindvalue):
return process

@staticmethod
def check_ctor_args(geometry_type, srid, dimension, management, use_typmod, nullable):
def check_ctor_args(geometry_type, srid, dimension, use_typmod, nullable):
try:
srid = int(srid)
except ValueError:
raise ArgumentError("srid must be convertible to an integer")
if geometry_type:
geometry_type = geometry_type.upper()
if management:
if geometry_type.endswith("ZM"):
# PostGIS' AddGeometryColumn does not work with ZM geometry types. Instead
# the simple geometry type (e.g. POINT rather POINTZM) should be used with
# dimension set to 4
raise ArgumentError(
"with management=True use geometry_type={!r} and "
"dimension=4 for {!r} geometries".format(geometry_type[:-2], geometry_type)
)
elif geometry_type[-1] in ("Z", "M") and dimension != 3:
# If a Z or M geometry type is used then dimension must be set to 3
raise ArgumentError(
"with management=True dimension must be 3 for "
"{!r} geometries".format(geometry_type)
)
else:
if management:
raise ArgumentError("geometry_type set to None not compatible with management")
if srid > 0:
warnings.warn("srid not enforced when geometry_type is None")

if use_typmod and not management:
warnings.warn("use_typmod ignored when management is False")
elif srid > 0:
warnings.warn("srid not enforced when geometry_type is None")

if use_typmod is not None and not nullable:
raise ArgumentError(
'The "nullable" and "use_typmod" arguments can not be used together'
Expand Down Expand Up @@ -363,7 +312,6 @@ def __init__(self, spatial_index=True, from_text=None, name=None, nullable=True)
dimension=2,
spatial_index=spatial_index,
use_N_D_index=False,
management=False,
use_typmod=False,
from_text=from_text,
name=name,
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ def engine(tmpdir, db_url, _engine_echo):
return engine


@pytest.fixture
def check_spatialite():
if "SPATIALITE_LIBRARY_PATH" not in os.environ:
pytest.skip("SPATIALITE_LIBRARY_PATH is not defined, skip SpatiaLite tests")


@pytest.fixture
def dialect_name(engine):
return engine.dialect.name
Expand Down
21 changes: 7 additions & 14 deletions tests/schema_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

@pytest.fixture
def Lake(base, postgis_version, schema):
with_management = postgis_version == 1

class Lake(base):
__tablename__ = "lake"
__table_args__ = {"schema": schema}
id = Column(Integer, primary_key=True)
geom = Column(Geometry(geometry_type="LINESTRING", srid=4326, management=with_management))
geom = Column(Geometry(geometry_type="LINESTRING", srid=4326))

def __init__(self, geom):
self.geom = geom
Expand All @@ -29,15 +27,15 @@ def __init__(self, geom):


@pytest.fixture
def Poi(base, engine, schema):
def Poi(base, schema, dialect_name):
class Poi(base):
__tablename__ = "poi"
__table_args__ = {"schema": schema}
id = Column(Integer, primary_key=True)
geom = Column(Geometry(geometry_type="POINT", srid=4326))
geog = (
Column(Geography(geometry_type="POINT", srid=4326))
if engine.dialect.name == "postgresql"
if dialect_name == "postgresql"
else None
)

Expand All @@ -59,7 +57,6 @@ class Summit(base):
Geometry(
geometry_type="POINT",
srid=4326,
management=True,
use_typmod=with_use_typemod,
)
)
Expand Down Expand Up @@ -136,13 +133,9 @@ def LocalPoint(base):
class LocalPoint(base):
__tablename__ = "local_point"
id = Column(Integer, primary_key=True)
geom = Column(
TransformedGeometry(
db_srid=2154, app_srid=4326, geometry_type="POINT", management=False
)
)
geom = Column(TransformedGeometry(db_srid=2154, app_srid=4326, geometry_type="POINT"))
managed_geom = Column(
TransformedGeometry(db_srid=2154, app_srid=4326, geometry_type="POINT", management=True)
TransformedGeometry(db_srid=2154, app_srid=4326, geometry_type="POINT")
)

return LocalPoint
Expand All @@ -155,7 +148,7 @@ class IndexTestWithSchema(base):
__table_args__ = {"schema": schema} if schema else {}
id = Column(Integer, primary_key=True)
geom1 = Column(Geometry(geometry_type="POINT", srid=4326))
geom2 = Column(Geometry(geometry_type="POINT", srid=4326, management=True))
geom2 = Column(Geometry(geometry_type="POINT", srid=4326))

return IndexTestWithSchema

Expand All @@ -177,7 +170,7 @@ class IndexTestWithoutSchema(base):
__tablename__ = "indextestwithoutschema"
id = Column(Integer, primary_key=True)
geom1 = Column(Geometry(geometry_type="POINT", srid=4326))
geom2 = Column(Geometry(geometry_type="POINT", srid=4326, management=True))
geom2 = Column(Geometry(geometry_type="POINT", srid=4326))

return IndexTestWithoutSchema

Expand Down
3 changes: 0 additions & 3 deletions tests/test_alembic_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_no_diff(self, conn, Lake, setup_tables, use_alembic_monkeypatch, dialec
Geometry(
geometry_type="LINESTRING",
srid=4326,
management=Lake.__table__.c.geom.type.management,
nullable=dialect_name != "mysql",
),
),
Expand Down Expand Up @@ -71,15 +70,13 @@ def test_diff(self, conn, Lake, setup_tables, use_alembic_monkeypatch):
Geometry(
geometry_type="LINESTRING",
srid=4326,
management=Lake.__table__.c.geom.type.management,
),
),
Column(
"new_geom_col",
Geometry(
geometry_type="LINESTRING",
srid=4326,
management=Lake.__table__.c.geom.type.management,
),
),
schema=Lake.__table__.schema,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ class ConstrainedLake(base):
id = Column(Integer, primary_key=True)
a_str = Column(String, nullable=True)
checked_str = Column(String, nullable=True)
geom = Column(Geometry(geometry_type="LINESTRING", srid=4326, management=False))
geom = Column(Geometry(geometry_type="LINESTRING", srid=4326))

def __init__(self, geom):
self.geom = geom
Expand Down Expand Up @@ -990,7 +990,6 @@ def test_sqlite_reflection_with_discarded_col(self, conn, Lake, setup_tables, di
assert t.c.geom.type.geometry_type == "GEOMETRY"
assert t.c.geom.type.dimension == 2
assert t.c.geom.type.extended
assert not t.c.geom.type.management
assert t.c.geom.type.nullable
assert t.c.geom.type.spatial_index
assert t.c.geom.type.srid == -1
Expand Down
Loading

0 comments on commit ab5e033

Please sign in to comment.