2009-12-22 23:18:51 +08:00
|
|
|
"""
|
|
|
|
This module contains the spatial lookup types, and the `get_geo_where_clause`
|
|
|
|
routine for Oracle Spatial.
|
|
|
|
|
|
|
|
Please note that WKT support is broken on the XE version, and thus
|
|
|
|
this backend will not work on such platforms. Specifically, XE lacks
|
|
|
|
support for an internal JVM, and Java libraries are required to use
|
|
|
|
the WKT constructors.
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.gis.db.backends.base.operations import \
|
|
|
|
BaseSpatialOperations
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.db.backends.oracle.adapter import OracleSpatialAdapter
|
2014-09-20 03:06:33 +08:00
|
|
|
from django.contrib.gis.db.backends.utils import SpatialOperator
|
2015-01-15 18:35:35 +08:00
|
|
|
from django.contrib.gis.db.models import aggregates
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry
|
|
|
|
from django.contrib.gis.measure import Distance
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.oracle.operations import DatabaseOperations
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
DEFAULT_TOLERANCE = '0.05'
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
class SDOOperator(SpatialOperator):
|
|
|
|
sql_template = "%(func)s(%(lhs)s, %(rhs)s) = 'TRUE'"
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-05-09 23:13:13 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
class SDODistance(SpatialOperator):
|
2015-10-10 02:08:08 +08:00
|
|
|
sql_template = "SDO_GEOM.SDO_DISTANCE(%%(lhs)s, %%(rhs)s, %s) %%(op)s %%(value)s" % DEFAULT_TOLERANCE
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
class SDODWithin(SpatialOperator):
|
|
|
|
sql_template = "SDO_WITHIN_DISTANCE(%(lhs)s, %(rhs)s, %%s) = 'TRUE'"
|
2013-05-09 23:13:13 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
class SDODisjoint(SpatialOperator):
|
|
|
|
sql_template = "SDO_GEOM.RELATE(%%(lhs)s, 'DISJOINT', %%(rhs)s, %s) = 'DISJOINT'" % DEFAULT_TOLERANCE
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-05-09 23:13:13 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
class SDORelate(SpatialOperator):
|
|
|
|
sql_template = "SDO_RELATE(%(lhs)s, %(rhs)s, 'mask=%(mask)s') = 'TRUE'"
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
def check_relate_argument(self, arg):
|
|
|
|
masks = 'TOUCH|OVERLAPBDYDISJOINT|OVERLAPBDYINTERSECT|EQUAL|INSIDE|COVEREDBY|CONTAINS|COVERS|ANYINTERACT|ON'
|
|
|
|
mask_regex = re.compile(r'^(%s)(\+(%s))*$' % (masks, masks), re.I)
|
2016-12-29 23:27:49 +08:00
|
|
|
if not isinstance(arg, str) or not mask_regex.match(arg):
|
2014-09-20 03:06:33 +08:00
|
|
|
raise ValueError('Invalid SDO_RELATE mask: "%s"' % arg)
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
def as_sql(self, connection, lookup, template_params, sql_params):
|
|
|
|
template_params['mask'] = sql_params.pop()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().as_sql(connection, lookup, template_params, sql_params)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2015-01-14 20:36:32 +08:00
|
|
|
class OracleOperations(BaseSpatialOperations, DatabaseOperations):
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
name = 'oracle'
|
|
|
|
oracle = True
|
2015-01-15 18:35:35 +08:00
|
|
|
disallowed_aggregates = (aggregates.Collect, aggregates.Extent3D, aggregates.MakeLine)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
Adapter = OracleSpatialAdapter
|
|
|
|
|
2013-05-09 23:13:13 +08:00
|
|
|
extent = 'SDO_AGGR_MBR'
|
2009-12-22 23:18:51 +08:00
|
|
|
unionagg = 'SDO_AGGR_UNION'
|
|
|
|
|
2016-06-13 23:27:52 +08:00
|
|
|
from_text = 'SDO_GEOMETRY'
|
|
|
|
|
2015-09-20 18:41:25 +08:00
|
|
|
function_names = {
|
|
|
|
'Area': 'SDO_GEOM.SDO_AREA',
|
2016-12-14 18:44:23 +08:00
|
|
|
'BoundingCircle': 'SDO_GEOM.SDO_MBC',
|
2015-09-20 18:41:25 +08:00
|
|
|
'Centroid': 'SDO_GEOM.SDO_CENTROID',
|
|
|
|
'Difference': 'SDO_GEOM.SDO_DIFFERENCE',
|
|
|
|
'Distance': 'SDO_GEOM.SDO_DISTANCE',
|
|
|
|
'Intersection': 'SDO_GEOM.SDO_INTERSECTION',
|
2016-12-01 00:22:56 +08:00
|
|
|
'IsValid': 'SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT',
|
2015-09-20 18:41:25 +08:00
|
|
|
'Length': 'SDO_GEOM.SDO_LENGTH',
|
|
|
|
'NumGeometries': 'SDO_UTIL.GETNUMELEM',
|
|
|
|
'NumPoints': 'SDO_UTIL.GETNUMVERTICES',
|
|
|
|
'Perimeter': 'SDO_GEOM.SDO_LENGTH',
|
|
|
|
'PointOnSurface': 'SDO_GEOM.SDO_POINTONSURFACE',
|
|
|
|
'Reverse': 'SDO_UTIL.REVERSE_LINESTRING',
|
|
|
|
'SymDifference': 'SDO_GEOM.SDO_XOR',
|
|
|
|
'Transform': 'SDO_CS.TRANSFORM',
|
|
|
|
'Union': 'SDO_GEOM.SDO_UNION',
|
|
|
|
}
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# We want to get SDO Geometries as WKT because it is much easier to
|
|
|
|
# instantiate GEOS proxies from WKT than SDO_GEOMETRY(...) strings.
|
|
|
|
# However, this adversely affects performance (i.e., Java is called
|
|
|
|
# to convert to WKT on every query). If someone wishes to write a
|
|
|
|
# SDO_GEOMETRY(...) parser in Python, let me know =)
|
|
|
|
select = 'SDO_UTIL.TO_WKTGEOMETRY(%s)'
|
|
|
|
|
2014-09-20 03:06:33 +08:00
|
|
|
gis_operators = {
|
|
|
|
'contains': SDOOperator(func='SDO_CONTAINS'),
|
|
|
|
'coveredby': SDOOperator(func='SDO_COVEREDBY'),
|
|
|
|
'covers': SDOOperator(func='SDO_COVERS'),
|
|
|
|
'disjoint': SDODisjoint(),
|
|
|
|
'intersects': SDOOperator(func='SDO_OVERLAPBDYINTERSECT'), # TODO: Is this really the same as ST_Intersects()?
|
|
|
|
'equals': SDOOperator(func='SDO_EQUAL'),
|
|
|
|
'exact': SDOOperator(func='SDO_EQUAL'),
|
|
|
|
'overlaps': SDOOperator(func='SDO_OVERLAPS'),
|
|
|
|
'same_as': SDOOperator(func='SDO_EQUAL'),
|
|
|
|
'relate': SDORelate(), # Oracle uses a different syntax, e.g., 'mask=inside+touch'
|
|
|
|
'touches': SDOOperator(func='SDO_TOUCH'),
|
|
|
|
'within': SDOOperator(func='SDO_INSIDE'),
|
|
|
|
'distance_gt': SDODistance(op='>'),
|
|
|
|
'distance_gte': SDODistance(op='>='),
|
|
|
|
'distance_lt': SDODistance(op='<'),
|
|
|
|
'distance_lte': SDODistance(op='<='),
|
|
|
|
'dwithin': SDODWithin(),
|
2013-10-18 17:02:43 +08:00
|
|
|
}
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2017-01-28 21:19:47 +08:00
|
|
|
unsupported_functions = {
|
|
|
|
'AsGeoJSON', 'AsKML', 'AsSVG', 'Envelope', 'ForceRHR', 'GeoHash',
|
2017-04-02 03:02:09 +08:00
|
|
|
'LineLocatePoint', 'MakeValid', 'MemSize', 'Scale', 'SnapToGrid',
|
|
|
|
'Translate',
|
2017-01-28 21:19:47 +08:00
|
|
|
}
|
2015-09-20 18:41:25 +08:00
|
|
|
|
2014-09-18 19:42:51 +08:00
|
|
|
def geo_quote_name(self, name):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().geo_quote_name(name).upper()
|
2014-09-18 19:42:51 +08:00
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
def get_db_converters(self, expression):
|
2017-01-21 21:13:44 +08:00
|
|
|
converters = super().get_db_converters(expression)
|
2014-12-01 15:28:01 +08:00
|
|
|
internal_type = expression.output_field.get_internal_type()
|
2014-09-19 03:44:37 +08:00
|
|
|
geometry_fields = (
|
|
|
|
'PointField', 'GeometryField', 'LineStringField',
|
|
|
|
'PolygonField', 'MultiPointField', 'MultiLineStringField',
|
|
|
|
'MultiPolygonField', 'GeometryCollectionField', 'GeomField',
|
2014-09-21 15:51:59 +08:00
|
|
|
'GMLField',
|
2014-09-19 03:44:37 +08:00
|
|
|
)
|
|
|
|
if internal_type in geometry_fields:
|
|
|
|
converters.append(self.convert_textfield_value)
|
|
|
|
return converters
|
|
|
|
|
2017-03-21 21:13:18 +08:00
|
|
|
def convert_extent(self, clob):
|
2009-12-22 23:18:51 +08:00
|
|
|
if clob:
|
|
|
|
# Generally, Oracle returns a polygon for the extent -- however,
|
|
|
|
# it can return a single point if there's only one Point in the
|
|
|
|
# table.
|
2017-03-21 21:13:18 +08:00
|
|
|
ext_geom = Geometry(clob.read())
|
2009-12-22 23:18:51 +08:00
|
|
|
gtype = str(ext_geom.geom_type)
|
|
|
|
if gtype == 'Polygon':
|
|
|
|
# Construct the 4-tuple from the coordinates in the polygon.
|
|
|
|
shell = ext_geom.shell
|
|
|
|
ll, ur = shell[0][:2], shell[2][:2]
|
|
|
|
elif gtype == 'Point':
|
|
|
|
ll = ext_geom.coords[:2]
|
|
|
|
ur = ll
|
|
|
|
else:
|
|
|
|
raise Exception('Unexpected geometry type returned for extent: %s' % gtype)
|
|
|
|
xmin, ymin = ll
|
|
|
|
xmax, ymax = ur
|
|
|
|
return (xmin, ymin, xmax, ymax)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def geo_db_type(self, f):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the geometry database type for Oracle. Unlike other spatial
|
2009-12-22 23:18:51 +08:00
|
|
|
backends, no stored procedure is necessary and it's the same for all
|
|
|
|
geometry types.
|
|
|
|
"""
|
|
|
|
return 'MDSYS.SDO_GEOMETRY'
|
|
|
|
|
2017-02-11 18:47:20 +08:00
|
|
|
def get_distance(self, f, value, lookup_type):
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the distance parameters given the value and the lookup type.
|
2009-12-22 23:18:51 +08:00
|
|
|
On Oracle, geometry columns with a geodetic coordinate system behave
|
|
|
|
implicitly like a geography column, and thus meters will be used as
|
|
|
|
the distance parameter on them.
|
|
|
|
"""
|
|
|
|
if not value:
|
|
|
|
return []
|
|
|
|
value = value[0]
|
|
|
|
if isinstance(value, Distance):
|
|
|
|
if f.geodetic(self.connection):
|
|
|
|
dist_param = value.m
|
|
|
|
else:
|
|
|
|
dist_param = getattr(value, Distance.unit_attname(f.units_name(self.connection)))
|
|
|
|
else:
|
|
|
|
dist_param = value
|
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
# dwithin lookups on Oracle require a special string parameter
|
2009-12-22 23:18:51 +08:00
|
|
|
# that starts with "distance=".
|
|
|
|
if lookup_type == 'dwithin':
|
|
|
|
dist_param = 'distance=%s' % dist_param
|
|
|
|
|
|
|
|
return [dist_param]
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def get_geom_placeholder(self, f, value, compiler):
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Provide a proper substitution value for Geometries that are not in the
|
2009-12-22 23:18:51 +08:00
|
|
|
SRID of the field. Specifically, this routine will substitute in the
|
|
|
|
SDO_CS.TRANSFORM() function call.
|
|
|
|
"""
|
2017-03-16 21:38:02 +08:00
|
|
|
tranform_func = self.spatial_function_name('Transform')
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if value is None:
|
|
|
|
return 'NULL'
|
|
|
|
|
|
|
|
def transform_value(val, srid):
|
|
|
|
return val.srid != srid
|
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
if hasattr(value, 'as_sql'):
|
2009-12-22 23:18:51 +08:00
|
|
|
if transform_value(value, f.srid):
|
2017-03-16 21:38:02 +08:00
|
|
|
placeholder = '%s(%%s, %s)' % (tranform_func, f.srid)
|
2009-12-22 23:18:51 +08:00
|
|
|
else:
|
|
|
|
placeholder = '%s'
|
2014-03-02 22:25:53 +08:00
|
|
|
# No geometry value used for F expression, substitute in
|
2009-12-22 23:18:51 +08:00
|
|
|
# the column name instead.
|
2014-11-16 09:56:42 +08:00
|
|
|
sql, _ = compiler.compile(value)
|
2013-12-25 21:13:18 +08:00
|
|
|
return placeholder % sql
|
2009-12-22 23:18:51 +08:00
|
|
|
else:
|
|
|
|
if transform_value(value, f.srid):
|
2017-03-16 21:38:02 +08:00
|
|
|
return '%s(SDO_GEOMETRY(%%s, %s), %s)' % (tranform_func, value.srid, f.srid)
|
2009-12-22 23:18:51 +08:00
|
|
|
else:
|
|
|
|
return 'SDO_GEOMETRY(%%s, %s)' % f.srid
|
|
|
|
|
2015-01-15 18:35:35 +08:00
|
|
|
def spatial_aggregate_name(self, agg_name):
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the spatial aggregate SQL name.
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
2015-01-15 18:35:35 +08:00
|
|
|
agg_name = 'unionagg' if agg_name.lower() == 'union' else agg_name.lower()
|
|
|
|
return getattr(self, agg_name)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
# Routines for getting the OGC-compliant models.
|
|
|
|
def geometry_columns(self):
|
2014-06-08 15:54:35 +08:00
|
|
|
from django.contrib.gis.db.backends.oracle.models import OracleGeometryColumns
|
|
|
|
return OracleGeometryColumns
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def spatial_ref_sys(self):
|
2014-06-08 15:54:35 +08:00
|
|
|
from django.contrib.gis.db.backends.oracle.models import OracleSpatialRefSys
|
|
|
|
return OracleSpatialRefSys
|
2013-02-10 23:15:49 +08:00
|
|
|
|
2015-08-03 22:34:19 +08:00
|
|
|
def modify_insert_params(self, placeholder, params):
|
2012-11-15 20:23:02 +08:00
|
|
|
"""Drop out insert parameters for NULL placeholder. Needed for Oracle Spatial
|
2015-08-03 22:34:19 +08:00
|
|
|
backend due to #10888.
|
2012-11-15 20:23:02 +08:00
|
|
|
"""
|
2015-08-03 22:34:19 +08:00
|
|
|
if placeholder == 'NULL':
|
|
|
|
return []
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().modify_insert_params(placeholder, params)
|