Skip to content

Commit

Permalink
Support geometry column with no typmod
Browse files Browse the repository at this point in the history
  • Loading branch information
Éric Lemoine committed Jul 30, 2017
1 parent 30f1c06 commit a7cc505
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 13 additions & 2 deletions geoalchemy2/types.py
Expand Up @@ -6,6 +6,7 @@
Reference
---------
"""
import warnings

from sqlalchemy.types import UserDefinedType, Integer
from sqlalchemy.sql import func
Expand Down Expand Up @@ -47,11 +48,15 @@ class _GISType(UserDefinedType):
* ``"MULTILINESTRING"``,
* ``"MULTIPOLYGON"``,
* ``"GEOMETRYCOLLECTION"``
* ``"CURVE"``.
* ``"CURVE"``,
* ``None``.
The latter is actually not supported with
:class:`geoalchemy2.types.Geography`.
When set to ``None`` then no "geometry type" constraints will be attached to the geometry
type declaration.
Default is ``"GEOMETRY"``.
``srid``
Expand Down Expand Up @@ -103,15 +108,21 @@ class _GISType(UserDefinedType):

def __init__(self, geometry_type='GEOMETRY', srid=-1, dimension=2,
spatial_index=True, management=False, use_typmod=None):
self.geometry_type = geometry_type.upper()
self.geometry_type = geometry_type
self.srid = int(srid)
if self.geometry_type:
self.geometry_type = self.geometry_type.upper()
elif self.srid > 0:
warnings.warn('srid not enforced when geometry_type is None')
self.dimension = dimension
self.spatial_index = spatial_index
self.management = management
self.use_typmod = use_typmod
self.extended = self.as_binary == 'ST_AsEWKB'

def get_col_spec(self):
if not self.geometry_type:
return self.name
return '%s(%s,%d)' % (self.name, self.geometry_type, self.srid)

def column_expression(self, col):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_types.py
Expand Up @@ -35,6 +35,10 @@ def test_get_col_spec(self):
g = Geometry(srid=900913)
assert g.get_col_spec() == 'geometry(GEOMETRY,900913)'

def test_get_col_spec_no_typmod(self):
g = Geometry(geometry_type=None)
assert g.get_col_spec() == 'geometry'

def test_column_expression(self, geometry_table):
s = select([geometry_table.c.geom])
eq_sql(s, 'SELECT ST_AsEWKB("table".geom) AS geom FROM "table"')
Expand Down Expand Up @@ -77,6 +81,10 @@ def test_get_col_spec(self):
g = Geography(srid=900913)
assert g.get_col_spec() == 'geography(GEOMETRY,900913)'

def test_get_col_spec_no_typmod(self):
g = Geography(geometry_type=None)
assert g.get_col_spec() == 'geography'

def test_column_expression(self, geography_table):
s = select([geography_table.c.geom])
eq_sql(s, 'SELECT ST_AsBinary("table".geom) AS geom FROM "table"')
Expand Down

0 comments on commit a7cc505

Please sign in to comment.