Skip to content

Commit

Permalink
Remove all the raster-related code for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Éric Lemoine committed Jan 21, 2020
1 parent d6f3f27 commit 9b08101
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 214 deletions.
18 changes: 0 additions & 18 deletions geoalchemy2/__init__.py
@@ -1,13 +1,11 @@
from .types import ( # NOQA
Geometry,
Geography,
Raster
)

from .elements import ( # NOQA
WKTElement,
WKBElement,
RasterElement
)

from .exc import ArgumentError
Expand Down Expand Up @@ -114,22 +112,6 @@ def dispatch(event, table, bind):
(table.name, c.name, table.name, c.name))
else:
raise ArgumentError('dialect {} is not supported'.format(bind.dialect.name))

# Add spatial indices for the Raster columns
#
# Note the use of ST_ConvexHull since most raster operators are
# based on the convex hull of the rasters.
if isinstance(c.type, Raster) and c.type.spatial_index is True:
if table.schema:
bind.execute('CREATE INDEX "idx_%s_%s" ON "%s"."%s" '
'USING GIST (ST_ConvexHull("%s"))' %
(table.name, c.name, table.schema,
table.name, c.name))
else:
bind.execute('CREATE INDEX "idx_%s_%s" ON "%s" '
'USING GIST (ST_ConvexHull("%s"))' %
(table.name, c.name, table.name, c.name))

elif event == 'after-drop':
# Restore original column list including managed Geometry columns
table.columns = table.info.pop('_saved_columns')
Expand Down
53 changes: 0 additions & 53 deletions geoalchemy2/elements.py
Expand Up @@ -213,59 +213,6 @@ def _data_from_desc(desc):
return binascii.unhexlify(desc)


class RasterElement(HasFunction):
"""
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'

def __init__(self, data):
self.data = data
self.function_expr = functions.Function(self.name, 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

@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.function_expr)
return getattr(func_, name)



class CompositeElement(FunctionElement):
"""
Instances of this class wrap a Postgres composite type.
Expand Down
7 changes: 0 additions & 7 deletions geoalchemy2/functions.py
Expand Up @@ -664,13 +664,6 @@ def __init__(self, *args, **kwargs):
'Returns a geometry that represents the point set union of the '
'Geometries.'),

#
# Raster Constructors
#

('ST_AsRaster', types.Raster,
('Converts a PostGIS geometry to a PostGIS raster.', 'RT_ST_AsRaster')),

#
# Raster Accessors
#
Expand Down
44 changes: 2 additions & 42 deletions geoalchemy2/types.py
Expand Up @@ -20,8 +20,8 @@
SHAPELY = False


from .comparator import BaseComparator, Comparator
from .elements import WKBElement, WKTElement, RasterElement, CompositeElement
from .comparator import Comparator
from .elements import WKBElement, WKTElement, CompositeElement
from .exc import ArgumentError


Expand Down Expand Up @@ -275,45 +275,6 @@ class Geography(_GISType):
``column_expression`` method. """


class Raster(UserDefinedType):
"""
The Raster column type.
Creating a raster column is done like this::
Column(Raster)
This class defines the ``result_processor`` method, so that raster values
received from the database are converted to
:class:`geoalchemy2.elements.RasterElement` objects.
Constructor arguments:
``spatial_index``
Indicate if a spatial index should be created. Default is ``True``.
"""

comparator_factory = BaseComparator
"""
This is the way by which spatial operators and functions are
defined for raster columns.
"""

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

def get_col_spec(self):
return 'raster'

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


class CompositeType(UserDefinedType):
"""
A wrapper for :class:`geoalchemy2.elements.CompositeElement`, that can be
Expand Down Expand Up @@ -352,4 +313,3 @@ class GeometryDump(CompositeType):
# subsystem.
ischema_names['geometry'] = Geometry
ischema_names['geography'] = Geography
ischema_names['raster'] = Raster
15 changes: 1 addition & 14 deletions tests/test_elements.py
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy import Table, MetaData, Column, String, func
from geoalchemy2.types import Geometry
from geoalchemy2.elements import (
WKTElement, WKBElement, RasterElement, CompositeElement
WKTElement, WKBElement, CompositeElement
)
from geoalchemy2.compat import buffer as buffer_, bytes as bytes_, str as str_
from geoalchemy2.exc import ArgumentError
Expand Down Expand Up @@ -271,19 +271,6 @@ def test_function_str(self):
assert isinstance(str(e), str)


class TestRasterElement():

def test_desc(self):
e = RasterElement(b'\x01\x02')
assert e.desc == '0102'

def test_function_call(self):
e = RasterElement(b'\x01\x02')
f = e.ST_Height()
eq_sql(f, 'ST_Height(:raster_1::raster)')
assert f.compile().params == {u'raster_1': b'\x01\x02'}


class TestCompositeElement():

def test_compile(self):
Expand Down
48 changes: 2 additions & 46 deletions tests/test_functional.py
Expand Up @@ -18,8 +18,8 @@
from sqlalchemy.sql import select, func
from sqlalchemy.sql.expression import type_coerce

from geoalchemy2 import Geometry, Geography, Raster
from geoalchemy2.elements import WKTElement, WKBElement, RasterElement
from geoalchemy2 import Geometry, Geography
from geoalchemy2.elements import WKTElement, WKBElement
from geoalchemy2.shape import from_shape

from shapely.geometry import LineString
Expand Down Expand Up @@ -87,16 +87,6 @@ class IndexTestWithoutSchema(Base):
# parameter use_typmod for AddGeometryColumn was added in PostGIS 2.0
Summit.__table__.c.geom.type.use_typmod = False

# The raster type is only available on PostGIS 2.0 and above
class Ocean(Base):
__tablename__ = 'ocean'
__table_args__ = {'schema': 'public'}
id = Column(Integer, primary_key=True)
rast = Column(Raster)

def __init__(self, rast):
self.rast = rast

postgis2_required = pytest.mark.skipif(
postgis_version.startswith('1.'),
reason="requires PostGIS 2 or higher",
Expand Down Expand Up @@ -317,34 +307,6 @@ def test_WKBElement(self):
srid = session.execute(l.geom.ST_SRID()).scalar()
assert srid == 4326

@postgis2_required
def test_Raster(self):
polygon = WKTElement('POLYGON((0 0,1 1,0 1,0 0))', srid=4326)
o = Ocean(polygon.ST_AsRaster(5, 5))
session.add(o)
session.flush()
session.expire(o)

assert isinstance(o.rast, RasterElement)

height = session.execute(o.rast.ST_Height()).scalar()
assert height == 5

width = session.execute(o.rast.ST_Width()).scalar()
assert width == 5

# The top left corner is covered by the polygon
top_left_point = WKTElement('Point(0 1)', srid=4326)
top_left = session.execute(
o.rast.ST_Value(top_left_point)).scalar()
assert top_left == 1

# The bottom right corner has NODATA
bottom_right_point = WKTElement('Point(1 0)', srid=4326)
bottom_right = session.execute(
o.rast.ST_Value(bottom_right_point)).scalar()
assert bottom_right is None


class TestPickle():

Expand Down Expand Up @@ -572,9 +534,3 @@ def test_reflection(self):
else:
assert type_.geometry_type == 'LINESTRING'
assert type_.srid == 4326

@postgis2_required
def test_raster_reflection(self):
t = Table('ocean', MetaData(), autoload=True, autoload_with=engine)
type_ = t.c.rast.type
assert isinstance(type_, Raster)
35 changes: 1 addition & 34 deletions tests/test_types.py
Expand Up @@ -3,7 +3,7 @@

from sqlalchemy import Table, MetaData, Column
from sqlalchemy.sql import select, insert, func, text
from geoalchemy2.types import Geometry, Geography, Raster
from geoalchemy2.types import Geometry, Geography
from geoalchemy2.exc import ArgumentError


Expand All @@ -24,12 +24,6 @@ def geography_table():
return table


@pytest.fixture
def raster_table():
table = Table('table', MetaData(), Column('rast', Raster))
return table


class TestGeometry():

def test_get_col_spec(self):
Expand Down Expand Up @@ -215,33 +209,6 @@ def test_get_col_spec(self):
assert g.get_col_spec() == 'geometry(GEOMETRYCOLLECTION,900913)'


class TestRaster():

def test_get_col_spec(self):
r = Raster()
assert r.get_col_spec() == 'raster'

def test_column_expression(self, raster_table):
s = select([raster_table.c.rast])
eq_sql(s, 'SELECT "table".rast FROM "table"')

def test_insert_bind_expression(self, raster_table):
i = insert(raster_table).values(rast=b'\x01\x02')
eq_sql(i, 'INSERT INTO "table" (rast) VALUES (:rast)')
assert i.compile().params == {'rast': b'\x01\x02'}

def test_function_call(self, raster_table):
s = select([raster_table.c.rast.ST_Height()])
eq_sql(s,
'SELECT ST_Height("table".rast) '
'AS "ST_Height_1" FROM "table"')

def test_non_ST_function_call(self, raster_table):

with pytest.raises(AttributeError):
raster_table.c.geom.Height()


class TestCompositeType():

def test_ST_Dump(self, geography_table):
Expand Down

0 comments on commit 9b08101

Please sign in to comment.