| 1 |
""" |
| 2 |
This module contains the spatial lookup types, and the `get_geo_where_clause` |
| 3 |
routine for Oracle Spatial. |
| 4 |
|
| 5 |
Please note that WKT support is broken on the XE version, and thus |
| 6 |
this backend will not work on such platforms. Specifically, XE lacks |
| 7 |
support for an internal JVM, and Java libraries are required to use |
| 8 |
the WKT constructors. |
| 9 |
""" |
| 10 |
|
| 11 |
import re |
| 12 |
|
| 13 |
from django.contrib.gis.db import models |
| 14 |
from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations |
| 15 |
from django.contrib.gis.db.backends.oracle.adapter import OracleSpatialAdapter |
| 16 |
from django.contrib.gis.db.backends.utils import SpatialOperator |
| 17 |
from django.contrib.gis.geos.geometry import GEOSGeometry, GEOSGeometryBase |
| 18 |
from django.contrib.gis.geos.prototypes.io import wkb_r |
| 19 |
from django.contrib.gis.measure import Distance |
| 20 |
from django.db.backends.oracle.operations import DatabaseOperations |
| 21 |
|
| 22 |
DEFAULT_TOLERANCE = "0.05" |
| 23 |
|
| 24 |
|
| 25 |
class SDOOperator(SpatialOperator): |
| 26 |
sql_template = "%(func)s(%(lhs)s, %(rhs)s) = 'TRUE'" |
| 27 |
|
| 28 |
|
| 29 |
class SDODWithin(SpatialOperator): |
| 30 |
sql_template = "SDO_WITHIN_DISTANCE(%(lhs)s, %(rhs)s, %%s) = 'TRUE'" |
| 31 |
|
| 32 |
|
| 33 |
class SDODisjoint(SpatialOperator): |
| 34 |
sql_template = ( |
| 35 |
"SDO_GEOM.RELATE(%%(lhs)s, 'DISJOINT', %%(rhs)s, %s) = 'DISJOINT'" |
| 36 |
% DEFAULT_TOLERANCE |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
class SDORelate(SpatialOperator): |
| 41 |
sql_template = "SDO_RELATE(%(lhs)s, %(rhs)s, 'mask=%(mask)s') = 'TRUE'" |
| 42 |
|
| 43 |
def check_relate_argument(self, arg): |
| 44 |
masks = ( |
| 45 |
"TOUCH|OVERLAPBDYDISJOINT|OVERLAPBDYINTERSECT|EQUAL|INSIDE|COVEREDBY|" |
| 46 |
"CONTAINS|COVERS|ANYINTERACT|ON" |
| 47 |
) |
| 48 |
mask_regex = re.compile(r"^(%s)(\+(%s))*$" % (masks, masks), re.I) |
| 49 |
if not isinstance(arg, str) or not mask_regex.match(arg): |
| 50 |
raise ValueError('Invalid SDO_RELATE mask: "%s"' % arg) |
| 51 |
|
| 52 |
def as_sql(self, connection, lookup, template_params, sql_params): |
| 53 |
template_params["mask"] = sql_params[-1] |
| 54 |
return super().as_sql(connection, lookup, template_params, sql_params[:-1]) |
| 55 |
|
| 56 |
|
| 57 |
class OracleOperations(BaseSpatialOperations, DatabaseOperations): |
| 58 |
name = "oracle" |
| 59 |
oracle = True |
| 60 |
disallowed_aggregates = (models.Collect, models.Extent3D, models.MakeLine) |
| 61 |
|
| 62 |
Adapter = OracleSpatialAdapter |
| 63 |
|
| 64 |
extent = "SDO_AGGR_MBR" |
| 65 |
unionagg = "SDO_AGGR_UNION" |
| 66 |
|
| 67 |
from_text = "SDO_GEOMETRY" |
| 68 |
|
| 69 |
function_names = { |
| 70 |
"Area": "SDO_GEOM.SDO_AREA", |
| 71 |
"AsGeoJSON": "SDO_UTIL.TO_GEOJSON", |
| 72 |
"AsWKB": "SDO_UTIL.TO_WKBGEOMETRY", |
| 73 |
"AsWKT": "SDO_UTIL.TO_WKTGEOMETRY", |
| 74 |
"BoundingCircle": "SDO_GEOM.SDO_MBC", |
| 75 |
"Centroid": "SDO_GEOM.SDO_CENTROID", |
| 76 |
"Difference": "SDO_GEOM.SDO_DIFFERENCE", |
| 77 |
"Distance": "SDO_GEOM.SDO_DISTANCE", |
| 78 |
"Envelope": "SDO_GEOM_MBR", |
| 79 |
"FromWKB": "SDO_UTIL.FROM_WKBGEOMETRY", |
| 80 |
"FromWKT": "SDO_UTIL.FROM_WKTGEOMETRY", |
| 81 |
"Intersection": "SDO_GEOM.SDO_INTERSECTION", |
| 82 |
"IsValid": "SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT", |
| 83 |
"Length": "SDO_GEOM.SDO_LENGTH", |
| 84 |
"NumGeometries": "SDO_UTIL.GETNUMELEM", |
| 85 |
"NumPoints": "SDO_UTIL.GETNUMVERTICES", |
| 86 |
"Perimeter": "SDO_GEOM.SDO_LENGTH", |
| 87 |
"PointOnSurface": "SDO_GEOM.SDO_POINTONSURFACE", |
| 88 |
"Reverse": "SDO_UTIL.REVERSE_LINESTRING", |
| 89 |
"SymDifference": "SDO_GEOM.SDO_XOR", |
| 90 |
"Transform": "SDO_CS.TRANSFORM", |
| 91 |
"Union": "SDO_GEOM.SDO_UNION", |
| 92 |
} |
| 93 |
|
| 94 |
# We want to get SDO Geometries as WKT because it is much easier to |
| 95 |
# instantiate GEOS proxies from WKT than SDO_GEOMETRY(...) strings. |
| 96 |
# However, this adversely affects performance (i.e., Java is called |
| 97 |
# to convert to WKT on every query). If someone wishes to write a |
| 98 |
# SDO_GEOMETRY(...) parser in Python, let me know =) |
| 99 |
select = "SDO_UTIL.TO_WKBGEOMETRY(%s)" |
| 100 |
|
| 101 |
gis_operators = { |
| 102 |
"contains": SDOOperator(func="SDO_CONTAINS"), |
| 103 |
"coveredby": SDOOperator(func="SDO_COVEREDBY"), |
| 104 |
"covers": SDOOperator(func="SDO_COVERS"), |
| 105 |
"disjoint": SDODisjoint(), |
| 106 |
"intersects": SDOOperator( |
| 107 |
func="SDO_OVERLAPBDYINTERSECT" |
| 108 |
), # TODO: Is this really the same as ST_Intersects()? |
| 109 |
"equals": SDOOperator(func="SDO_EQUAL"), |
| 110 |
"exact": SDOOperator(func="SDO_EQUAL"), |
| 111 |
"overlaps": SDOOperator(func="SDO_OVERLAPS"), |
| 112 |
"same_as": SDOOperator(func="SDO_EQUAL"), |
| 113 |
# Oracle uses a different syntax, e.g., 'mask=inside+touch' |
| 114 |
"relate": SDORelate(), |
| 115 |
"touches": SDOOperator(func="SDO_TOUCH"), |
| 116 |
"within": SDOOperator(func="SDO_INSIDE"), |
| 117 |
"dwithin": SDODWithin(), |
| 118 |
} |
| 119 |
|
| 120 |
unsupported_functions = { |
| 121 |
"AsKML", |
| 122 |
"AsSVG", |
| 123 |
"Azimuth", |
| 124 |
"ClosestPoint", |
| 125 |
"ForcePolygonCW", |
| 126 |
"GeoHash", |
| 127 |
"GeometryDistance", |
| 128 |
"IsEmpty", |
| 129 |
"LineLocatePoint", |
| 130 |
"MakeValid", |
| 131 |
"MemSize", |
| 132 |
"Scale", |
| 133 |
"SnapToGrid", |
| 134 |
"Translate", |
| 135 |
} |
| 136 |
|
| 137 |
def geo_quote_name(self, name): |
| 138 |
return super().geo_quote_name(name).upper() |
| 139 |
|
| 140 |
def convert_extent(self, clob): |
| 141 |
if clob: |
| 142 |
# Generally, Oracle returns a polygon for the extent -- however, |
| 143 |
# it can return a single point if there's only one Point in the |
| 144 |
# table. |
| 145 |
ext_geom = GEOSGeometry(memoryview(clob.read())) |
| 146 |
gtype = str(ext_geom.geom_type) |
| 147 |
if gtype == "Polygon": |
| 148 |
# Construct the 4-tuple from the coordinates in the polygon. |
| 149 |
shell = ext_geom.shell |
| 150 |
ll, ur = shell[0][:2], shell[2][:2] |
| 151 |
elif gtype == "Point": |
| 152 |
ll = ext_geom.coords[:2] |
| 153 |
ur = ll |
| 154 |
else: |
| 155 |
raise Exception( |
| 156 |
"Unexpected geometry type returned for extent: %s" % gtype |
| 157 |
) |
| 158 |
xmin, ymin = ll |
| 159 |
xmax, ymax = ur |
| 160 |
return (xmin, ymin, xmax, ymax) |
| 161 |
else: |
| 162 |
return None |
| 163 |
|
| 164 |
def geo_db_type(self, f): |
| 165 |
""" |
| 166 |
Return the geometry database type for Oracle. Unlike other spatial |
| 167 |
backends, no stored procedure is necessary and it's the same for all |
| 168 |
geometry types. |
| 169 |
""" |
| 170 |
return "MDSYS.SDO_GEOMETRY" |
| 171 |
|
| 172 |
def get_distance(self, f, value, lookup_type): |
| 173 |
""" |
| 174 |
Return the distance parameters given the value and the lookup type. |
| 175 |
On Oracle, geometry columns with a geodetic coordinate system behave |
| 176 |
implicitly like a geography column, and thus meters will be used as |
| 177 |
the distance parameter on them. |
| 178 |
""" |
| 179 |
if not value: |
| 180 |
return [] |
| 181 |
value = value[0] |
| 182 |
if isinstance(value, Distance): |
| 183 |
if f.geodetic(self.connection): |
| 184 |
dist_param = value.m |
| 185 |
else: |
| 186 |
dist_param = getattr( |
| 187 |
value, Distance.unit_attname(f.units_name(self.connection)) |
| 188 |
) |
| 189 |
else: |
| 190 |
dist_param = value |
| 191 |
|
| 192 |
# dwithin lookups on Oracle require a special string parameter |
| 193 |
# that starts with "distance=". |
| 194 |
if lookup_type == "dwithin": |
| 195 |
dist_param = "distance=%s" % dist_param |
| 196 |
|
| 197 |
return [dist_param] |
| 198 |
|
| 199 |
def get_geom_placeholder(self, f, value, compiler): |
| 200 |
if value is None: |
| 201 |
return "NULL" |
| 202 |
return super().get_geom_placeholder(f, value, compiler) |
| 203 |
|
| 204 |
def spatial_aggregate_name(self, agg_name): |
| 205 |
""" |
| 206 |
Return the spatial aggregate SQL name. |
| 207 |
""" |
| 208 |
agg_name = "unionagg" if agg_name.lower() == "union" else agg_name.lower() |
| 209 |
return getattr(self, agg_name) |
| 210 |
|
| 211 |
# Routines for getting the OGC-compliant models. |
| 212 |
def geometry_columns(self): |
| 213 |
from django.contrib.gis.db.backends.oracle.models import OracleGeometryColumns |
| 214 |
|
| 215 |
return OracleGeometryColumns |
| 216 |
|
| 217 |
def spatial_ref_sys(self): |
| 218 |
from django.contrib.gis.db.backends.oracle.models import OracleSpatialRefSys |
| 219 |
|
| 220 |
return OracleSpatialRefSys |
| 221 |
|
| 222 |
def modify_insert_params(self, placeholder, params): |
| 223 |
"""Drop out insert parameters for NULL placeholder. Needed for Oracle Spatial |
| 224 |
backend due to #10888. |
| 225 |
""" |
| 226 |
if placeholder == "NULL": |
| 227 |
return [] |
| 228 |
return super().modify_insert_params(placeholder, params) |
| 229 |
|
| 230 |
def get_geometry_converter(self, expression): |
| 231 |
read = wkb_r().read |
| 232 |
srid = expression.output_field.srid |
| 233 |
if srid == -1: |
| 234 |
srid = None |
| 235 |
geom_class = expression.output_field.geom_class |
| 236 |
|
| 237 |
def converter(value, expression, connection): |
| 238 |
if value is not None: |
| 239 |
geom = GEOSGeometryBase(read(memoryview(value.read())), geom_class) |
| 240 |
if srid: |
| 241 |
geom.srid = srid |
| 242 |
return geom |
| 243 |
|
| 244 |
return converter |
| 245 |
|
| 246 |
def get_area_att_for_field(self, field): |
| 247 |
return "sq_m" |