Kaydet (Commit) bbfad84d authored tarafından Daniel Wiesmann's avatar Daniel Wiesmann Kaydeden (comit) Tim Graham

Fixed #25588 -- Added spatial lookups to RasterField.

Thanks Tim Graham for the review.
üst 03efa304
...@@ -6,16 +6,27 @@ from __future__ import unicode_literals ...@@ -6,16 +6,27 @@ from __future__ import unicode_literals
from psycopg2 import Binary from psycopg2 import Binary
from psycopg2.extensions import ISQLQuote from psycopg2.extensions import ISQLQuote
from django.contrib.gis.db.backends.postgis.pgraster import to_pgraster
from django.contrib.gis.geometry.backend import Geometry
class PostGISAdapter(object): class PostGISAdapter(object):
def __init__(self, geom, geography=False): def __init__(self, obj, geography=False):
"Initializes on the geometry." """
Initialize on the spatial object.
"""
self.is_geometry = isinstance(obj, Geometry)
# Getting the WKB (in string form, to allow easy pickling of # Getting the WKB (in string form, to allow easy pickling of
# the adaptor) and the SRID from the geometry. # the adaptor) and the SRID from the geometry or raster.
self.ewkb = bytes(geom.ewkb) if self.is_geometry:
self.srid = geom.srid self.ewkb = bytes(obj.ewkb)
self._adapter = Binary(self.ewkb)
else:
self.ewkb = to_pgraster(obj)
self.srid = obj.srid
self.geography = geography self.geography = geography
self._adapter = Binary(self.ewkb)
def __conform__(self, proto): def __conform__(self, proto):
# Does the given protocol conform to what Psycopg2 expects? # Does the given protocol conform to what Psycopg2 expects?
...@@ -40,12 +51,19 @@ class PostGISAdapter(object): ...@@ -40,12 +51,19 @@ class PostGISAdapter(object):
This method allows escaping the binary in the style required by the This method allows escaping the binary in the style required by the
server's `standard_conforming_string` setting. server's `standard_conforming_string` setting.
""" """
self._adapter.prepare(conn) if self.is_geometry:
self._adapter.prepare(conn)
def getquoted(self): def getquoted(self):
"Returns a properly quoted string for use in PostgreSQL/PostGIS." """
# psycopg will figure out whether to use E'\\000' or '\000' Return a properly quoted string for use in PostgreSQL/PostGIS.
return str('%s(%s)' % ( """
'ST_GeogFromWKB' if self.geography else 'ST_GeomFromEWKB', if self.is_geometry:
self._adapter.getquoted().decode()) # Psycopg will figure out whether to use E'\\000' or '\000'.
) return str('%s(%s)' % (
'ST_GeogFromWKB' if self.geography else 'ST_GeomFromEWKB',
self._adapter.getquoted().decode())
)
else:
# For rasters, add explicit type cast to WKB string.
return "'%s'::raster" % self.ewkb
...@@ -4,30 +4,83 @@ from django.conf import settings ...@@ -4,30 +4,83 @@ from django.conf import settings
from django.contrib.gis.db.backends.base.operations import \ from django.contrib.gis.db.backends.base.operations import \
BaseSpatialOperations BaseSpatialOperations
from django.contrib.gis.db.backends.utils import SpatialOperator from django.contrib.gis.db.backends.utils import SpatialOperator
from django.contrib.gis.gdal import GDALRaster
from django.contrib.gis.geometry.backend import Geometry from django.contrib.gis.geometry.backend import Geometry
from django.contrib.gis.measure import Distance from django.contrib.gis.measure import Distance
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.db.backends.postgresql.operations import DatabaseOperations from django.db.backends.postgresql.operations import DatabaseOperations
from django.db.utils import ProgrammingError from django.db.utils import ProgrammingError
from django.utils import six
from django.utils.functional import cached_property from django.utils.functional import cached_property
from .adapter import PostGISAdapter from .adapter import PostGISAdapter
from .models import PostGISGeometryColumns, PostGISSpatialRefSys from .models import PostGISGeometryColumns, PostGISSpatialRefSys
from .pgraster import from_pgraster, get_pgraster_srid, to_pgraster from .pgraster import from_pgraster, get_pgraster_srid, to_pgraster
# Identifier to mark raster lookups as bilateral.
BILATERAL = 'bilateral'
class PostGISOperator(SpatialOperator): class PostGISOperator(SpatialOperator):
def __init__(self, geography=False, **kwargs): def __init__(self, geography=False, raster=False, **kwargs):
# Only a subset of the operators and functions are available # Only a subset of the operators and functions are available for the
# for the geography type. # geography type.
self.geography = geography self.geography = geography
# Only a subset of the operators and functions are available for the
# raster type. Lookups that don't suport raster will be converted to
# polygons. If the raster argument is set to BILATERAL, then the
# operator cannot handle mixed geom-raster lookups.
self.raster = raster
super(PostGISOperator, self).__init__(**kwargs) super(PostGISOperator, self).__init__(**kwargs)
def as_sql(self, connection, lookup, *args): def as_sql(self, connection, lookup, template_params, *args):
if lookup.lhs.output_field.geography and not self.geography: if lookup.lhs.output_field.geography and not self.geography:
raise ValueError('PostGIS geography does not support the "%s" ' raise ValueError('PostGIS geography does not support the "%s" '
'function/operator.' % (self.func or self.op,)) 'function/operator.' % (self.func or self.op,))
return super(PostGISOperator, self).as_sql(connection, lookup, *args)
template_params = self.check_raster(lookup, template_params)
return super(PostGISOperator, self).as_sql(connection, lookup, template_params, *args)
def check_raster(self, lookup, template_params):
# Get rhs value.
if isinstance(lookup.rhs, (tuple, list)):
rhs_val = lookup.rhs[0]
spheroid = lookup.rhs[-1] == 'spheroid'
else:
rhs_val = lookup.rhs
spheroid = False
# Check which input is a raster.
lhs_is_raster = lookup.lhs.field.geom_type == 'RASTER'
rhs_is_raster = isinstance(rhs_val, GDALRaster)
# Look for band indices and inject them if provided.
if lookup.band_lhs is not None and lhs_is_raster:
if not self.func:
raise ValueError('Band indices are not allowed for this operator, it works on bbox only.')
template_params['lhs'] = '%s, %s' % (template_params['lhs'], lookup.band_lhs)
if lookup.band_rhs is not None and rhs_is_raster:
if not self.func:
raise ValueError('Band indices are not allowed for this operator, it works on bbox only.')
template_params['rhs'] = '%s, %s' % (template_params['rhs'], lookup.band_rhs)
# Convert rasters to polygons if necessary.
if not self.raster or spheroid:
# Operators without raster support.
if lhs_is_raster:
template_params['lhs'] = 'ST_Polygon(%s)' % template_params['lhs']
if rhs_is_raster:
template_params['rhs'] = 'ST_Polygon(%s)' % template_params['rhs']
elif self.raster == BILATERAL:
# Operators with raster support but don't support mixed (rast-geom)
# lookups.
if lhs_is_raster and not rhs_is_raster:
template_params['lhs'] = 'ST_Polygon(%s)' % template_params['lhs']
elif rhs_is_raster and not lhs_is_raster:
template_params['rhs'] = 'ST_Polygon(%s)' % template_params['rhs']
return template_params
class PostGISDistanceOperator(PostGISOperator): class PostGISDistanceOperator(PostGISOperator):
...@@ -35,6 +88,7 @@ class PostGISDistanceOperator(PostGISOperator): ...@@ -35,6 +88,7 @@ class PostGISDistanceOperator(PostGISOperator):
def as_sql(self, connection, lookup, template_params, sql_params): def as_sql(self, connection, lookup, template_params, sql_params):
if not lookup.lhs.output_field.geography and lookup.lhs.output_field.geodetic(connection): if not lookup.lhs.output_field.geography and lookup.lhs.output_field.geodetic(connection):
template_params = self.check_raster(lookup, template_params)
sql_template = self.sql_template sql_template = self.sql_template
if len(lookup.rhs) == 3 and lookup.rhs[-1] == 'spheroid': if len(lookup.rhs) == 3 and lookup.rhs[-1] == 'spheroid':
template_params.update({'op': self.op, 'func': 'ST_Distance_Spheroid'}) template_params.update({'op': self.op, 'func': 'ST_Distance_Spheroid'})
...@@ -58,33 +112,33 @@ class PostGISOperations(BaseSpatialOperations, DatabaseOperations): ...@@ -58,33 +112,33 @@ class PostGISOperations(BaseSpatialOperations, DatabaseOperations):
Adapter = PostGISAdapter Adapter = PostGISAdapter
gis_operators = { gis_operators = {
'bbcontains': PostGISOperator(op='~'), 'bbcontains': PostGISOperator(op='~', raster=True),
'bboverlaps': PostGISOperator(op='&&', geography=True), 'bboverlaps': PostGISOperator(op='&&', geography=True, raster=True),
'contained': PostGISOperator(op='@'), 'contained': PostGISOperator(op='@', raster=True),
'contains': PostGISOperator(func='ST_Contains'), 'overlaps_left': PostGISOperator(op='&<', raster=BILATERAL),
'overlaps_left': PostGISOperator(op='&<'), 'overlaps_right': PostGISOperator(op='&>', raster=BILATERAL),
'overlaps_right': PostGISOperator(op='&>'),
'overlaps_below': PostGISOperator(op='&<|'), 'overlaps_below': PostGISOperator(op='&<|'),
'overlaps_above': PostGISOperator(op='|&>'), 'overlaps_above': PostGISOperator(op='|&>'),
'left': PostGISOperator(op='<<'), 'left': PostGISOperator(op='<<'),
'right': PostGISOperator(op='>>'), 'right': PostGISOperator(op='>>'),
'strictly_below': PostGISOperator(op='<<|'), 'strictly_below': PostGISOperator(op='<<|'),
'strictly_above': PostGISOperator(op='|>>'), 'strictly_above': PostGISOperator(op='|>>'),
'same_as': PostGISOperator(op='~='), 'same_as': PostGISOperator(op='~=', raster=BILATERAL),
'exact': PostGISOperator(op='~='), # alias of same_as 'exact': PostGISOperator(op='~=', raster=BILATERAL), # alias of same_as
'contains_properly': PostGISOperator(func='ST_ContainsProperly'), 'contains': PostGISOperator(func='ST_Contains', raster=BILATERAL),
'coveredby': PostGISOperator(func='ST_CoveredBy', geography=True), 'contains_properly': PostGISOperator(func='ST_ContainsProperly', raster=BILATERAL),
'covers': PostGISOperator(func='ST_Covers', geography=True), 'coveredby': PostGISOperator(func='ST_CoveredBy', geography=True, raster=BILATERAL),
'covers': PostGISOperator(func='ST_Covers', geography=True, raster=BILATERAL),
'crosses': PostGISOperator(func='ST_Crosses'), 'crosses': PostGISOperator(func='ST_Crosses'),
'disjoint': PostGISOperator(func='ST_Disjoint'), 'disjoint': PostGISOperator(func='ST_Disjoint', raster=BILATERAL),
'equals': PostGISOperator(func='ST_Equals'), 'equals': PostGISOperator(func='ST_Equals'),
'intersects': PostGISOperator(func='ST_Intersects', geography=True), 'intersects': PostGISOperator(func='ST_Intersects', geography=True, raster=BILATERAL),
'isvalid': PostGISOperator(func='ST_IsValid'), 'isvalid': PostGISOperator(func='ST_IsValid'),
'overlaps': PostGISOperator(func='ST_Overlaps'), 'overlaps': PostGISOperator(func='ST_Overlaps', raster=BILATERAL),
'relate': PostGISOperator(func='ST_Relate'), 'relate': PostGISOperator(func='ST_Relate'),
'touches': PostGISOperator(func='ST_Touches'), 'touches': PostGISOperator(func='ST_Touches', raster=BILATERAL),
'within': PostGISOperator(func='ST_Within'), 'within': PostGISOperator(func='ST_Within', raster=BILATERAL),
'dwithin': PostGISOperator(func='ST_DWithin', geography=True), 'dwithin': PostGISOperator(func='ST_DWithin', geography=True, raster=BILATERAL),
'distance_gt': PostGISDistanceOperator(func='ST_Distance', op='>', geography=True), 'distance_gt': PostGISDistanceOperator(func='ST_Distance', op='>', geography=True),
'distance_gte': PostGISDistanceOperator(func='ST_Distance', op='>=', geography=True), 'distance_gte': PostGISDistanceOperator(func='ST_Distance', op='>=', geography=True),
'distance_lt': PostGISDistanceOperator(func='ST_Distance', op='<', geography=True), 'distance_lt': PostGISDistanceOperator(func='ST_Distance', op='<', geography=True),
...@@ -272,14 +326,14 @@ class PostGISOperations(BaseSpatialOperations, DatabaseOperations): ...@@ -272,14 +326,14 @@ class PostGISOperations(BaseSpatialOperations, DatabaseOperations):
def get_geom_placeholder(self, f, value, compiler): def get_geom_placeholder(self, f, value, compiler):
""" """
Provides a proper substitution value for Geometries that are not in the Provide a proper substitution value for Geometries or rasters that are
SRID of the field. Specifically, this routine will substitute in the not in the SRID of the field. Specifically, this routine will
ST_Transform() function call. substitute in the ST_Transform() function call.
""" """
# Get the srid for this object # Get the srid for this object
if value is None: if value is None:
value_srid = None value_srid = None
elif f.geom_type == 'RASTER': elif f.geom_type == 'RASTER' and isinstance(value, six.string_types):
value_srid = get_pgraster_srid(value) value_srid = get_pgraster_srid(value)
else: else:
value_srid = value.srid value_srid = value.srid
...@@ -288,7 +342,7 @@ class PostGISOperations(BaseSpatialOperations, DatabaseOperations): ...@@ -288,7 +342,7 @@ class PostGISOperations(BaseSpatialOperations, DatabaseOperations):
# is not equal to the field srid. # is not equal to the field srid.
if value_srid is None or value_srid == f.srid: if value_srid is None or value_srid == f.srid:
placeholder = '%s' placeholder = '%s'
elif f.geom_type == 'RASTER': elif f.geom_type == 'RASTER' and isinstance(value, six.string_types):
placeholder = '%s((%%s)::raster, %s)' % (self.transform, f.srid) placeholder = '%s((%%s)::raster, %s)' % (self.transform, f.srid)
else: else:
placeholder = '%s(%%s, %s)' % (self.transform, f.srid) placeholder = '%s(%%s, %s)' % (self.transform, f.srid)
......
from django.contrib.gis import forms from django.contrib.gis import forms
from django.contrib.gis.db.models.lookups import gis_lookups from django.contrib.gis.db.models.lookups import (
RasterBandTransform, gis_lookups,
)
from django.contrib.gis.db.models.proxy import SpatialProxy from django.contrib.gis.db.models.proxy import SpatialProxy
from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.gdal import HAS_GDAL
from django.contrib.gis.gdal.error import GDALException
from django.contrib.gis.geometry.backend import Geometry, GeometryException from django.contrib.gis.geometry.backend import Geometry, GeometryException
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.db.models.expressions import Expression from django.db.models.expressions import Expression
...@@ -157,6 +160,82 @@ class BaseSpatialField(Field): ...@@ -157,6 +160,82 @@ class BaseSpatialField(Field):
""" """
return connection.ops.get_geom_placeholder(self, value, compiler) return connection.ops.get_geom_placeholder(self, value, compiler)
def get_srid(self, obj):
"""
Return the default SRID for the given geometry or raster, taking into
account the SRID set for the field. For example, if the input geometry
or raster doesn't have an SRID, then the SRID of the field will be
returned.
"""
srid = obj.srid # SRID of given geometry.
if srid is None or self.srid == -1 or (srid == -1 and self.srid != -1):
return self.srid
else:
return srid
def get_db_prep_save(self, value, connection):
"""
Prepare the value for saving in the database.
"""
if not value:
return None
else:
return connection.ops.Adapter(self.get_prep_value(value))
def get_prep_value(self, value):
"""
Spatial lookup values are either a parameter that is (or may be
converted to) a geometry or raster, or a sequence of lookup values
that begins with a geometry or raster. This routine sets up the
geometry or raster value properly and preserves any other lookup
parameters.
"""
from django.contrib.gis.gdal import GDALRaster
value = super(BaseSpatialField, self).get_prep_value(value)
# For IsValid lookups, boolean values are allowed.
if isinstance(value, (Expression, bool)):
return value
elif isinstance(value, (tuple, list)):
obj = value[0]
seq_value = True
else:
obj = value
seq_value = False
# When the input is not a geometry or raster, attempt to construct one
# from the given string input.
if isinstance(obj, (Geometry, GDALRaster)):
pass
elif isinstance(obj, (bytes, six.string_types)) or hasattr(obj, '__geo_interface__'):
try:
obj = Geometry(obj)
except (GeometryException, GDALException):
try:
obj = GDALRaster(obj)
except GDALException:
raise ValueError("Couldn't create spatial object from lookup value '%s'." % obj)
elif isinstance(obj, dict):
try:
obj = GDALRaster(obj)
except GDALException:
raise ValueError("Couldn't create spatial object from lookup value '%s'." % obj)
else:
raise ValueError('Cannot use object with type %s for a spatial lookup parameter.' % type(obj).__name__)
# Assigning the SRID value.
obj.srid = self.get_srid(obj)
if seq_value:
lookup_val = [obj]
lookup_val.extend(value[1:])
return tuple(lookup_val)
else:
return obj
for klass in gis_lookups.values():
BaseSpatialField.register_lookup(klass)
class GeometryField(GeoSelectFormatMixin, BaseSpatialField): class GeometryField(GeoSelectFormatMixin, BaseSpatialField):
""" """
...@@ -224,6 +303,8 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField): ...@@ -224,6 +303,8 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField):
value properly, and preserve any other lookup parameters before value properly, and preserve any other lookup parameters before
returning to the caller. returning to the caller.
""" """
from django.contrib.gis.gdal import GDALRaster
value = super(GeometryField, self).get_prep_value(value) value = super(GeometryField, self).get_prep_value(value)
if isinstance(value, (Expression, bool)): if isinstance(value, (Expression, bool)):
return value return value
...@@ -236,7 +317,7 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField): ...@@ -236,7 +317,7 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField):
# When the input is not a GEOS geometry, attempt to construct one # When the input is not a GEOS geometry, attempt to construct one
# from the given string input. # from the given string input.
if isinstance(geom, Geometry): if isinstance(geom, (Geometry, GDALRaster)):
pass pass
elif isinstance(geom, (bytes, six.string_types)) or hasattr(geom, '__geo_interface__'): elif isinstance(geom, (bytes, six.string_types)) or hasattr(geom, '__geo_interface__'):
try: try:
...@@ -265,18 +346,6 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField): ...@@ -265,18 +346,6 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField):
value.srid = self.srid value.srid = self.srid
return value return value
def get_srid(self, geom):
"""
Returns the default SRID for the given geometry, taking into account
the SRID set for the field. For example, if the input geometry
has no SRID, then that of the field will be returned.
"""
gsrid = geom.srid # SRID of given geometry.
if gsrid is None or self.srid == -1 or (gsrid == -1 and self.srid != -1):
return self.srid
else:
return gsrid
# ### Routines overloaded from Field ### # ### Routines overloaded from Field ###
def contribute_to_class(self, cls, name, **kwargs): def contribute_to_class(self, cls, name, **kwargs):
super(GeometryField, self).contribute_to_class(cls, name, **kwargs) super(GeometryField, self).contribute_to_class(cls, name, **kwargs)
...@@ -316,17 +385,6 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField): ...@@ -316,17 +385,6 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField):
params = [connection.ops.Adapter(value)] params = [connection.ops.Adapter(value)]
return params return params
def get_db_prep_save(self, value, connection):
"Prepares the value for saving in the database."
if not value:
return None
else:
return connection.ops.Adapter(self.get_prep_value(value))
for klass in gis_lookups.values():
GeometryField.register_lookup(klass)
# The OpenGIS Geometry Type Fields # The OpenGIS Geometry Type Fields
class PointField(GeometryField): class PointField(GeometryField):
...@@ -387,6 +445,7 @@ class RasterField(BaseSpatialField): ...@@ -387,6 +445,7 @@ class RasterField(BaseSpatialField):
description = _("Raster Field") description = _("Raster Field")
geom_type = 'RASTER' geom_type = 'RASTER'
geography = False
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if not HAS_GDAL: if not HAS_GDAL:
...@@ -421,3 +480,15 @@ class RasterField(BaseSpatialField): ...@@ -421,3 +480,15 @@ class RasterField(BaseSpatialField):
# delays the instantiation of the objects to the moment of evaluation # delays the instantiation of the objects to the moment of evaluation
# of the raster attribute. # of the raster attribute.
setattr(cls, self.attname, SpatialProxy(GDALRaster, self)) setattr(cls, self.attname, SpatialProxy(GDALRaster, self))
def get_transform(self, name):
try:
band_index = int(name)
return type(
'SpecificRasterBandTransform',
(RasterBandTransform, ),
{'band_index': band_index}
)
except ValueError:
pass
return super(RasterField, self).get_transform(name)
...@@ -5,16 +5,23 @@ import re ...@@ -5,16 +5,23 @@ import re
from django.core.exceptions import FieldDoesNotExist from django.core.exceptions import FieldDoesNotExist
from django.db.models.constants import LOOKUP_SEP from django.db.models.constants import LOOKUP_SEP
from django.db.models.expressions import Col, Expression from django.db.models.expressions import Col, Expression
from django.db.models.lookups import BuiltinLookup, Lookup from django.db.models.lookups import BuiltinLookup, Lookup, Transform
from django.utils import six from django.utils import six
gis_lookups = {} gis_lookups = {}
class RasterBandTransform(Transform):
def as_sql(self, compiler, connection):
return compiler.compile(self.lhs)
class GISLookup(Lookup): class GISLookup(Lookup):
sql_template = None sql_template = None
transform_func = None transform_func = None
distance = False distance = False
band_rhs = None
band_lhs = None
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(GISLookup, self).__init__(*args, **kwargs) super(GISLookup, self).__init__(*args, **kwargs)
...@@ -28,10 +35,10 @@ class GISLookup(Lookup): ...@@ -28,10 +35,10 @@ class GISLookup(Lookup):
'point, 'the_geom', or a related lookup on a geographic field like 'point, 'the_geom', or a related lookup on a geographic field like
'address__point'. 'address__point'.
If a GeometryField exists according to the given lookup on the model If a BaseSpatialField exists according to the given lookup on the model
options, it will be returned. Otherwise returns None. options, it will be returned. Otherwise return None.
""" """
from django.contrib.gis.db.models.fields import GeometryField from django.contrib.gis.db.models.fields import BaseSpatialField
# This takes into account the situation where the lookup is a # This takes into account the situation where the lookup is a
# lookup to a related geographic field, e.g., 'address__point'. # lookup to a related geographic field, e.g., 'address__point'.
field_list = lookup.split(LOOKUP_SEP) field_list = lookup.split(LOOKUP_SEP)
...@@ -55,11 +62,34 @@ class GISLookup(Lookup): ...@@ -55,11 +62,34 @@ class GISLookup(Lookup):
return False return False
# Finally, make sure we got a Geographic field and return. # Finally, make sure we got a Geographic field and return.
if isinstance(geo_fld, GeometryField): if isinstance(geo_fld, BaseSpatialField):
return geo_fld return geo_fld
else: else:
return False return False
def process_band_indices(self, only_lhs=False):
"""
Extract the lhs band index from the band transform class and the rhs
band index from the input tuple.
"""
# PostGIS band indices are 1-based, so the band index needs to be
# increased to be consistent with the GDALRaster band indices.
if only_lhs:
self.band_rhs = 1
self.band_lhs = self.lhs.band_index + 1
return
if isinstance(self.lhs, RasterBandTransform):
self.band_lhs = self.lhs.band_index + 1
else:
self.band_lhs = 1
self.band_rhs = self.rhs[1]
if len(self.rhs) == 1:
self.rhs = self.rhs[0]
else:
self.rhs = (self.rhs[0], ) + self.rhs[2:]
def get_db_prep_lookup(self, value, connection): def get_db_prep_lookup(self, value, connection):
# get_db_prep_lookup is called by process_rhs from super class # get_db_prep_lookup is called by process_rhs from super class
if isinstance(value, (tuple, list)): if isinstance(value, (tuple, list)):
...@@ -70,10 +100,9 @@ class GISLookup(Lookup): ...@@ -70,10 +100,9 @@ class GISLookup(Lookup):
return ('%s', params) return ('%s', params)
def process_rhs(self, compiler, connection): def process_rhs(self, compiler, connection):
rhs, rhs_params = super(GISLookup, self).process_rhs(compiler, connection)
if hasattr(self.rhs, '_as_sql'): if hasattr(self.rhs, '_as_sql'):
# If rhs is some QuerySet, don't touch it # If rhs is some QuerySet, don't touch it
return rhs, rhs_params return super(GISLookup, self).process_rhs(compiler, connection)
geom = self.rhs geom = self.rhs
if isinstance(self.rhs, Col): if isinstance(self.rhs, Col):
...@@ -85,9 +114,19 @@ class GISLookup(Lookup): ...@@ -85,9 +114,19 @@ class GISLookup(Lookup):
raise ValueError('No geographic field found in expression.') raise ValueError('No geographic field found in expression.')
self.rhs.srid = geo_fld.srid self.rhs.srid = geo_fld.srid
elif isinstance(self.rhs, Expression): elif isinstance(self.rhs, Expression):
raise ValueError('Complex expressions not supported for GeometryField') raise ValueError('Complex expressions not supported for spatial fields.')
elif isinstance(self.rhs, (list, tuple)): elif isinstance(self.rhs, (list, tuple)):
geom = self.rhs[0] geom = self.rhs[0]
# Check if a band index was passed in the query argument.
if ((len(self.rhs) == 2 and not self.lookup_name == 'relate') or
(len(self.rhs) == 3 and self.lookup_name == 'relate')):
self.process_band_indices()
elif len(self.rhs) > 2:
raise ValueError('Tuple too long for lookup %s.' % self.lookup_name)
elif isinstance(self.lhs, RasterBandTransform):
self.process_band_indices(only_lhs=True)
rhs, rhs_params = super(GISLookup, self).process_rhs(compiler, connection)
rhs = connection.ops.get_geom_placeholder(self.lhs.output_field, geom, compiler) rhs = connection.ops.get_geom_placeholder(self.lhs.output_field, geom, compiler)
return rhs, rhs_params return rhs, rhs_params
...@@ -274,6 +313,8 @@ class IsValidLookup(BuiltinLookup): ...@@ -274,6 +313,8 @@ class IsValidLookup(BuiltinLookup):
lookup_name = 'isvalid' lookup_name = 'isvalid'
def as_sql(self, compiler, connection): def as_sql(self, compiler, connection):
if self.lhs.field.geom_type == 'RASTER':
raise ValueError('The isvalid lookup is only available on geometry fields.')
gis_op = connection.ops.gis_operators[self.lookup_name] gis_op = connection.ops.gis_operators[self.lookup_name]
sql, params = self.process_lhs(compiler, connection) sql, params = self.process_lhs(compiler, connection)
sql = '%(func)s(%(lhs)s)' % {'func': gis_op.func, 'lhs': sql} sql = '%(func)s(%(lhs)s)' % {'func': gis_op.func, 'lhs': sql}
...@@ -323,9 +364,17 @@ class DistanceLookupBase(GISLookup): ...@@ -323,9 +364,17 @@ class DistanceLookupBase(GISLookup):
sql_template = '%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)s' sql_template = '%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)s'
def process_rhs(self, compiler, connection): def process_rhs(self, compiler, connection):
if not isinstance(self.rhs, (tuple, list)) or not 2 <= len(self.rhs) <= 3: if not isinstance(self.rhs, (tuple, list)) or not 2 <= len(self.rhs) <= 4:
raise ValueError("2 or 3-element tuple required for '%s' lookup." % self.lookup_name) raise ValueError("2, 3, or 4-element tuple required for '%s' lookup." % self.lookup_name)
elif len(self.rhs) == 4 and not self.rhs[3] == 'spheroid':
raise ValueError("For 4-element tuples the last argument must be the 'speroid' directive.")
# Check if the second parameter is a band index.
if len(self.rhs) > 2 and not self.rhs[2] == 'spheroid':
self.process_band_indices()
params = [connection.ops.Adapter(self.rhs[0])] params = [connection.ops.Adapter(self.rhs[0])]
# Getting the distance parameter in the units of the field. # Getting the distance parameter in the units of the field.
dist_param = self.rhs[1] dist_param = self.rhs[1]
if hasattr(dist_param, 'resolve_expression'): if hasattr(dist_param, 'resolve_expression'):
......
This diff is collapsed.
...@@ -160,6 +160,9 @@ Minor features ...@@ -160,6 +160,9 @@ Minor features
:lookup:`isvalid` lookup, all for PostGIS. This allows filtering and :lookup:`isvalid` lookup, all for PostGIS. This allows filtering and
repairing invalid geometries on the database side. repairing invalid geometries on the database side.
* Added raster support for all :doc:`spatial lookups
</ref/contrib/gis/geoquerysets>`.
:mod:`django.contrib.messages` :mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
...@@ -558,6 +558,7 @@ pessimization ...@@ -558,6 +558,7 @@ pessimization
Petri Petri
Peucker Peucker
pgAdmin pgAdmin
PGRaster
phishing phishing
php php
picklable picklable
......
...@@ -82,9 +82,9 @@ class OperationTests(TransactionTestCase): ...@@ -82,9 +82,9 @@ class OperationTests(TransactionTestCase):
operation = migration_class(*args) operation = migration_class(*args)
new_state = project_state.clone() new_state = project_state.clone()
operation.state_forwards('gis', new_state) operation.state_forwards('gis', new_state)
self.current_state = new_state
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_forwards('gis', editor, project_state, new_state) operation.database_forwards('gis', editor, project_state, new_state)
self.current_state = new_state
def test_add_geom_field(self): def test_add_geom_field(self):
""" """
......
...@@ -3,6 +3,18 @@ from ..models import models ...@@ -3,6 +3,18 @@ from ..models import models
class RasterModel(models.Model): class RasterModel(models.Model):
rast = models.RasterField('A Verbose Raster Name', null=True, srid=4326, spatial_index=True, blank=True) rast = models.RasterField('A Verbose Raster Name', null=True, srid=4326, spatial_index=True, blank=True)
rastprojected = models.RasterField('A Projected Raster Table', srid=3086, null=True)
geom = models.PointField(null=True)
class Meta:
required_db_features = ['supports_raster']
def __str__(self):
return str(self.id)
class RasterRelatedModel(models.Model):
rastermodel = models.ForeignKey(RasterModel, models.CASCADE)
class Meta: class Meta:
required_db_features = ['supports_raster'] required_db_features = ['supports_raster']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment