Skip to content

Commit

Permalink
Merge d8d9764 into 785e007
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Mar 30, 2020
2 parents 785e007 + d8d9764 commit c0f16d8
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 116 deletions.
169 changes: 72 additions & 97 deletions geoalchemy2/elements.py
Expand Up @@ -14,7 +14,39 @@
from .exc import ArgumentError


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


class HasFunction(object):
pass


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

@staticmethod
def _data_from_desc(desc):
if PY3:
desc = desc.encode(encoding="utf-8")
return binascii.unhexlify(desc)


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

self.function_expr = getattr(functions.func, func_name)(*args)

def __str__(self):
return self.desc
Expand Down Expand Up @@ -72,48 +107,37 @@ 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)
func_ = functions._FunctionGenerator(expr=self.function_expr)
return getattr(func_, name)

@property
def name(self):
return self.function_expr.name

def __getstate__(self):
state = {
'srid': self.srid,
'data': str(self),
'extended': self.extended,
'name': self.name,
'name': self.function_expr.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]
func_name = state['name']
args = [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)
self.function_expr = getattr(functions.func, func_name)(*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 @@ -155,7 +179,7 @@ def _data_from_desc(desc):
return desc


class WKBElement(_SpatialElement):
class WKBElement(HasBinaryDesc, _SpatialElement):
"""
Instances of this class wrap a WKB or EWKB value.
Expand Down Expand Up @@ -199,98 +223,49 @@ def __init__(self, data, srid=-1, extended=False):
srid = struct.unpack('<I' if byte_order else '>I', srid)[0]
_SpatialElement.__init__(self, data, srid, extended)

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

@staticmethod
def _data_from_desc(desc):
if PY3:
desc = desc.encode(encoding="utf-8")
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
4 changes: 4 additions & 0 deletions geoalchemy2/functions.py
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 @@ -92,6 +93,9 @@ def __init__(self, *args, **kwargs):
expr = kwargs.pop('expr', None)
if expr is not None:
args = (expr,) + args
args = [
elem.function_expr if isinstance(elem, elements.HasFunction) else elem for elem in args
]
functions.GenericFunction.__init__(self, *args, **kwargs)


Expand Down
27 changes: 24 additions & 3 deletions geoalchemy2/types.py
Expand Up @@ -129,11 +129,15 @@ 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 Down Expand Up @@ -271,7 +275,7 @@ class Geography(_GISType):
``column_expression`` method. """


class Raster(UserDefinedType):
class Raster(_GISType):
"""
The Raster column type.
Expand All @@ -297,18 +301,35 @@ class Raster(UserDefinedType):
defined for raster columns.
"""

from_text = 'raster'
""" The "from text" raster constructor. Used by the parent class'
``bind_expression`` method. """

as_binary = 'raster'
""" The "as binary" function to use. Used by the parent class'
``column_expression`` method. """

name = 'raster'
""" Type name used for defining raster columns in ``CREATE TABLE``. """

def __init__(self, spatial_index=True):
self.spatial_index = spatial_index

def get_col_spec(self):
return 'raster'
return self.name

def column_expression(self, col):
return getattr(func, self.as_binary)(col, type_=self)

def result_processor(self, dialect, coltype):
def process(value):
if value is not None:
return RasterElement(value)
return process

def bind_expression(self, bindvalue):
return getattr(func, self.from_text)(bindvalue, type_=self)


class CompositeType(UserDefinedType):
"""
Expand Down
22 changes: 22 additions & 0 deletions tests/__init__.py
@@ -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",
)

0 comments on commit c0f16d8

Please sign in to comment.