| 1 |
from django.contrib.gis.db.models import GeometryField |
| 2 |
from django.contrib.gis.db.models.functions import Distance |
| 3 |
from django.contrib.gis.measure import Area as AreaMeasure |
| 4 |
from django.contrib.gis.measure import Distance as DistanceMeasure |
| 5 |
from django.db import NotSupportedError |
| 6 |
from django.utils.functional import cached_property |
| 7 |
|
| 8 |
|
| 9 |
class BaseSpatialOperations: |
| 10 |
# Quick booleans for the type of this spatial backend, and |
| 11 |
# an attribute for the spatial database version tuple (if applicable) |
| 12 |
postgis = False |
| 13 |
spatialite = False |
| 14 |
mariadb = False |
| 15 |
mysql = False |
| 16 |
oracle = False |
| 17 |
spatial_version = None |
| 18 |
|
| 19 |
# How the geometry column should be selected. |
| 20 |
select = "%s" |
| 21 |
|
| 22 |
@cached_property |
| 23 |
def select_extent(self): |
| 24 |
return self.select |
| 25 |
|
| 26 |
# Aggregates |
| 27 |
disallowed_aggregates = () |
| 28 |
|
| 29 |
geom_func_prefix = "" |
| 30 |
|
| 31 |
# Mapping between Django function names and backend names, when names do not |
| 32 |
# match; used in spatial_function_name(). |
| 33 |
function_names = {} |
| 34 |
|
| 35 |
# Set of known unsupported functions of the backend |
| 36 |
unsupported_functions = { |
| 37 |
"Area", |
| 38 |
"AsGeoJSON", |
| 39 |
"AsGML", |
| 40 |
"AsKML", |
| 41 |
"AsSVG", |
| 42 |
"Azimuth", |
| 43 |
"BoundingCircle", |
| 44 |
"Centroid", |
| 45 |
"ClosestPoint", |
| 46 |
"Difference", |
| 47 |
"Distance", |
| 48 |
"Envelope", |
| 49 |
"FromWKB", |
| 50 |
"FromWKT", |
| 51 |
"GeoHash", |
| 52 |
"GeometryDistance", |
| 53 |
"Intersection", |
| 54 |
"IsEmpty", |
| 55 |
"IsValid", |
| 56 |
"Length", |
| 57 |
"LineLocatePoint", |
| 58 |
"MakeValid", |
| 59 |
"MemSize", |
| 60 |
"NumGeometries", |
| 61 |
"NumPoints", |
| 62 |
"Perimeter", |
| 63 |
"PointOnSurface", |
| 64 |
"Reverse", |
| 65 |
"Scale", |
| 66 |
"SnapToGrid", |
| 67 |
"SymDifference", |
| 68 |
"Transform", |
| 69 |
"Translate", |
| 70 |
"Union", |
| 71 |
} |
| 72 |
|
| 73 |
# Constructors |
| 74 |
from_text = False |
| 75 |
|
| 76 |
# Default conversion functions for aggregates; will be overridden if implemented |
| 77 |
# for the spatial backend. |
| 78 |
def convert_extent(self, box, srid): |
| 79 |
raise NotImplementedError( |
| 80 |
"Aggregate extent not implemented for this spatial backend." |
| 81 |
) |
| 82 |
|
| 83 |
def convert_extent3d(self, box, srid): |
| 84 |
raise NotImplementedError( |
| 85 |
"Aggregate 3D extent not implemented for this spatial backend." |
| 86 |
) |
| 87 |
|
| 88 |
# For quoting column values, rather than columns. |
| 89 |
def geo_quote_name(self, name): |
| 90 |
return "'%s'" % name |
| 91 |
|
| 92 |
# GeometryField operations |
| 93 |
def geo_db_type(self, f): |
| 94 |
""" |
| 95 |
Return the database column type for the geometry field on |
| 96 |
the spatial backend. |
| 97 |
""" |
| 98 |
raise NotImplementedError( |
| 99 |
"subclasses of BaseSpatialOperations must provide a geo_db_type() method" |
| 100 |
) |
| 101 |
|
| 102 |
def get_distance(self, f, value, lookup_type): |
| 103 |
""" |
| 104 |
Return the distance parameters for the given geometry field, |
| 105 |
lookup value, and lookup type. |
| 106 |
""" |
| 107 |
raise NotImplementedError( |
| 108 |
"Distance operations not available on this spatial backend." |
| 109 |
) |
| 110 |
|
| 111 |
def get_geom_placeholder(self, f, value, compiler): |
| 112 |
""" |
| 113 |
Return the placeholder for the given geometry field with the given |
| 114 |
value. Depending on the spatial backend, the placeholder may contain a |
| 115 |
stored procedure call to the transformation function of the spatial |
| 116 |
backend. |
| 117 |
""" |
| 118 |
|
| 119 |
def transform_value(value, field): |
| 120 |
return value is not None and value.srid != field.srid |
| 121 |
|
| 122 |
if hasattr(value, "as_sql"): |
| 123 |
return ( |
| 124 |
"%s(%%s, %s)" % (self.spatial_function_name("Transform"), f.srid) |
| 125 |
if transform_value(value.output_field, f) |
| 126 |
else "%s" |
| 127 |
) |
| 128 |
if transform_value(value, f): |
| 129 |
# Add Transform() to the SQL placeholder. |
| 130 |
return "%s(%s(%%s,%s), %s)" % ( |
| 131 |
self.spatial_function_name("Transform"), |
| 132 |
self.from_text, |
| 133 |
value.srid, |
| 134 |
f.srid, |
| 135 |
) |
| 136 |
elif self.connection.features.has_spatialrefsys_table: |
| 137 |
return "%s(%%s,%s)" % (self.from_text, f.srid) |
| 138 |
else: |
| 139 |
# For backwards compatibility on MySQL (#27464). |
| 140 |
return "%s(%%s)" % self.from_text |
| 141 |
|
| 142 |
def check_expression_support(self, expression): |
| 143 |
if isinstance(expression, self.disallowed_aggregates): |
| 144 |
raise NotSupportedError( |
| 145 |
"%s spatial aggregation is not supported by this database backend." |
| 146 |
% expression.name |
| 147 |
) |
| 148 |
super().check_expression_support(expression) |
| 149 |
|
| 150 |
def spatial_aggregate_name(self, agg_name): |
| 151 |
raise NotImplementedError( |
| 152 |
"Aggregate support not implemented for this spatial backend." |
| 153 |
) |
| 154 |
|
| 155 |
def spatial_function_name(self, func_name): |
| 156 |
if func_name in self.unsupported_functions: |
| 157 |
raise NotSupportedError( |
| 158 |
"This backend doesn't support the %s function." % func_name |
| 159 |
) |
| 160 |
return self.function_names.get(func_name, self.geom_func_prefix + func_name) |
| 161 |
|
| 162 |
# Routines for getting the OGC-compliant models. |
| 163 |
def geometry_columns(self): |
| 164 |
raise NotImplementedError( |
| 165 |
"Subclasses of BaseSpatialOperations must provide a geometry_columns() " |
| 166 |
"method." |
| 167 |
) |
| 168 |
|
| 169 |
def spatial_ref_sys(self): |
| 170 |
raise NotImplementedError( |
| 171 |
"subclasses of BaseSpatialOperations must a provide spatial_ref_sys() " |
| 172 |
"method" |
| 173 |
) |
| 174 |
|
| 175 |
distance_expr_for_lookup = staticmethod(Distance) |
| 176 |
|
| 177 |
def get_db_converters(self, expression): |
| 178 |
converters = super().get_db_converters(expression) |
| 179 |
if isinstance(expression.output_field, GeometryField): |
| 180 |
converters.append(self.get_geometry_converter(expression)) |
| 181 |
return converters |
| 182 |
|
| 183 |
def get_geometry_converter(self, expression): |
| 184 |
raise NotImplementedError( |
| 185 |
"Subclasses of BaseSpatialOperations must provide a " |
| 186 |
"get_geometry_converter() method." |
| 187 |
) |
| 188 |
|
| 189 |
def get_area_att_for_field(self, field): |
| 190 |
if field.geodetic(self.connection): |
| 191 |
if self.connection.features.supports_area_geodetic: |
| 192 |
return "sq_m" |
| 193 |
raise NotImplementedError( |
| 194 |
"Area on geodetic coordinate systems not supported." |
| 195 |
) |
| 196 |
else: |
| 197 |
units_name = field.units_name(self.connection) |
| 198 |
if units_name: |
| 199 |
return AreaMeasure.unit_attname(units_name) |
| 200 |
|
| 201 |
def get_distance_att_for_field(self, field): |
| 202 |
dist_att = None |
| 203 |
if field.geodetic(self.connection): |
| 204 |
if self.connection.features.supports_distance_geodetic: |
| 205 |
dist_att = "m" |
| 206 |
else: |
| 207 |
units = field.units_name(self.connection) |
| 208 |
if units: |
| 209 |
dist_att = DistanceMeasure.unit_attname(units) |
| 210 |
return dist_att |