Skip to content

Commit

Permalink
Merge pull request #256 from geoalchemy/pr248
Browse files Browse the repository at this point in the history
Elements are no more SQL-related objects ; Rasters use the same base classes as other types
  • Loading branch information
adrien-berchet committed Apr 10, 2020
2 parents 785e007 + 0ca1a28 commit 7107198
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 111 deletions.
112 changes: 34 additions & 78 deletions geoalchemy2/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
from .exc import ArgumentError


class _SpatialElement(functions.Function):
if PY3:
BinasciiError = binascii.Error
else:
BinasciiError = TypeError


class HasFunction(object):
pass


class _SpatialElement(HasFunction):
"""
The base class for :class:`geoalchemy2.elements.WKTElement` and
:class:`geoalchemy2.elements.WKBElement`.
Expand All @@ -40,11 +50,6 @@ def __init__(self, data, srid=-1, extended=False):
self.srid = srid
self.data = data
self.extended = extended
if self.extended:
args = [self.geom_from_extended_version, self.data]
else:
args = [self.geom_from, self.data, self.srid]
functions.Function.__init__(self, *args)

def __str__(self):
return self.desc
Expand Down Expand Up @@ -72,7 +77,6 @@ def __getattr__(self, name):
# We create our own _FunctionGenerator here, and use it in place of
# SQLAlchemy's "func" object. This is to be able to "bind" the
# function to the SQL expression. See also GenericFunction above.

func_ = functions._FunctionGenerator(expr=self)
return getattr(func_, name)

Expand All @@ -81,39 +85,19 @@ def __getstate__(self):
'srid': self.srid,
'data': str(self),
'extended': self.extended,
'name': self.name,
}
return state

def __setstate__(self, state):
self.__dict__.update(state)
self.srid = state['srid']
self.extended = state['extended']
self.data = self._data_from_desc(state['data'])
args = [self.name, self.data]
if not self.extended:
args.append(self.srid)
# we need to call Function.__init__ to properly initialize SQLAlchemy's
# internal states
functions.Function.__init__(self, *args)

@staticmethod
def _data_from_desc(desc):
raise NotImplementedError()


# Default handlers are required for SQLAlchemy < 1.1
# See more details in https://github.com/geoalchemy/geoalchemy2/issues/213
@compiles(_SpatialElement)
def compile_spatialelement_default(element, compiler, **kw):
return "{}({})".format(element.name,
compiler.process(element.clauses, **kw))


@compiles(_SpatialElement, 'sqlite')
def compile_spatialelement_sqlite(element, compiler, **kw):
return "{}({})".format(element.name.lstrip("ST_"),
compiler.process(element.clauses, **kw))


class WKTElement(_SpatialElement):
"""
Instances of this class wrap a WKT or EWKT value.
Expand Down Expand Up @@ -220,77 +204,49 @@ def _data_from_desc(desc):
return binascii.unhexlify(desc)


class RasterElement(FunctionElement):
class RasterElement(_SpatialElement):
"""
Instances of this class wrap a ``raster`` value. Raster values read
from the database are converted to instances of this type. In
most cases you won't need to create ``RasterElement`` instances
yourself.
"""

name = 'raster'
geom_from_extended_version = 'raster'

def __init__(self, data):
self.data = data
FunctionElement.__init__(self, self.data)

def __str__(self):
return self.desc # pragma: no cover

def __repr__(self):
return "<%s at 0x%x; %r>" % \
(self.__class__.__name__, id(self), self.desc) # pragma: no cover
# read srid from the WKB (binary or hexadecimal format)
# The WKB structure is documented in the file
# raster/doc/RFC2-WellKnownBinaryFormat of the PostGIS sources.
try:
bin_data = binascii.unhexlify(data[:114])
except BinasciiError:
bin_data = data
data = str(binascii.hexlify(data).decode(encoding='utf-8'))
byte_order = bin_data[0]
srid = bin_data[53:57]
if not PY3:
byte_order = bytearray(byte_order)[0]
srid = struct.unpack('<I' if byte_order else '>I', srid)[0]
_SpatialElement.__init__(self, data, srid, True)

@property
def desc(self):
"""
This element's description string.
"""
desc = binascii.hexlify(self.data)
if PY3:
# hexlify returns a bytes object on py3
desc = str(desc, encoding="utf-8")

if len(desc) < 30:
return desc

return desc[:30] + '...' # pragma: no cover

def __getattr__(self, name):
#
# This is how things like ocean.rast.ST_Value(...) creates
# SQL expressions of this form:
#
# ST_Value(:ST_GeomFromWKB_1), :param_1)
#

# We create our own _FunctionGenerator here, and use it in place of
# SQLAlchemy's "func" object. This is to be able to "bind" the
# function to the SQL expression. See also GenericFunction.

func_ = functions._FunctionGenerator(expr=self)
return getattr(func_, name)

return self.data

@compiles(RasterElement)
def compile_RasterElement(element, compiler, **kw):
"""
This function makes sure the :class:`geoalchemy2.elements.RasterElement`
contents are correctly casted to the ``raster`` type before using it.
The other elements in this module don't need such a function because
they are derived from :class:`functions.Function`. For the
:class:`geoalchemy2.elements.RasterElement` class however it would not be
of any use to have it compile to ``raster('...')`` so it is compiled to
``'...'::raster`` by this function.
"""
return "%s::raster" % compiler.process(element.clauses)
@staticmethod
def _data_from_desc(desc):
return desc


class CompositeElement(FunctionElement):
"""
Instances of this class wrap a Postgres composite type.
"""

def __init__(self, base, field, type_):
self.name = field
self.type = to_instance(type_)
Expand Down
13 changes: 12 additions & 1 deletion geoalchemy2/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from sqlalchemy.ext.compiler import compiles

from . import types
from . import elements


class GenericFunction(functions.GenericFunction):
Expand Down Expand Up @@ -90,8 +91,18 @@ class ST_TransScale(GenericFunction):

def __init__(self, *args, **kwargs):
expr = kwargs.pop('expr', None)
args = list(args)
if expr is not None:
args = (expr,) + args
args = [expr] + args
for idx, elem in enumerate(args):
if isinstance(elem, elements.HasFunction):
if elem.extended:
func_name = elem.geom_from_extended_version
func_args = [elem.data]
else:
func_name = elem.geom_from
func_args = [elem.data, elem.srid]
args[idx] = getattr(functions.func, func_name)(*func_args)
functions.GenericFunction.__init__(self, *args, **kwargs)


Expand Down
52 changes: 40 additions & 12 deletions geoalchemy2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ class _GISType(UserDefinedType):
geometry/geography columns. """

def __init__(self, geometry_type='GEOMETRY', srid=-1, dimension=2,
spatial_index=True, management=False, use_typmod=None):
spatial_index=True, management=False, use_typmod=None,
from_text=None, name=None):
geometry_type, srid = self.check_ctor_args(
geometry_type, srid, dimension, management, use_typmod)
self.geometry_type = geometry_type
self.srid = srid
if name is not None:
self.name = name
if from_text is not None:
self.from_text = from_text
self.dimension = dimension
self.spatial_index = spatial_index
self.management = management
Expand All @@ -151,7 +156,12 @@ def column_expression(self, col):
def result_processor(self, dialect, coltype):
def process(value):
if value is not None:
return WKBElement(value, srid=self.srid, extended=self.extended)
kwargs = {}
if self.srid > 0:
kwargs['srid'] = self.srid
if self.extended is not None:
kwargs['extended'] = self.extended
return self.ElementType(value, **kwargs)
return process

def bind_expression(self, bindvalue):
Expand Down Expand Up @@ -245,6 +255,10 @@ class Geometry(_GISType):
""" The "as binary" function to use. Used by the parent class'
``column_expression`` method. """

ElementType = WKBElement
""" The element class to use. Used by the parent class'
``result_processor`` method. """


class Geography(_GISType):
"""
Expand All @@ -270,8 +284,12 @@ class Geography(_GISType):
""" The "as binary" function to use. Used by the parent class'
``column_expression`` method. """

ElementType = WKBElement
""" The element class to use. Used by the parent class'
``result_processor`` method. """


class Raster(UserDefinedType):
class Raster(_GISType):
"""
The Raster column type.
Expand All @@ -297,17 +315,27 @@ class Raster(UserDefinedType):
defined for raster columns.
"""

def __init__(self, spatial_index=True):
self.spatial_index = spatial_index
name = 'raster'
""" Type name used for defining raster columns in ``CREATE TABLE``. """

def get_col_spec(self):
return 'raster'
from_text = 'raster'
""" The "from text" raster constructor. Used by the parent class'
``bind_expression`` method. """

def result_processor(self, dialect, coltype):
def process(value):
if value is not None:
return RasterElement(value)
return process
as_binary = 'raster'
""" The "as binary" function to use. Used by the parent class'
``column_expression`` method. """

ElementType = RasterElement
""" The element class to use. Used by the parent class'
``result_processor`` method. """

def __init__(self, *args, **kwargs):
# Enforce default values
kwargs['geometry_type'] = None
kwargs['srid'] = -1
super(Raster, self).__init__(*args, **kwargs)
self.extended = None


class CompositeType(UserDefinedType):
Expand Down
22 changes: 22 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest


def skip_postgis1(postgis_version):
return pytest.mark.skipif(
postgis_version.startswith('1.'),
reason="requires PostGIS != 1",
)


def skip_postgis2(postgis_version):
return pytest.mark.skipif(
postgis_version.startswith('2.'),
reason="requires PostGIS != 2",
)


def skip_postgis3(postgis_version):
return pytest.mark.skipif(
postgis_version.startswith('3.'),
reason="requires PostGIS != 3",
)
Loading

0 comments on commit 7107198

Please sign in to comment.