| 1 |
from django.contrib.gis.db import models |
| 2 |
from django.contrib.gis.db.backends.base.adapter import WKTAdapter |
| 3 |
from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations |
| 4 |
from django.contrib.gis.db.backends.utils import SpatialOperator |
| 5 |
from django.contrib.gis.geos.geometry import GEOSGeometryBase |
| 6 |
from django.contrib.gis.geos.prototypes.io import wkb_r |
| 7 |
from django.contrib.gis.measure import Distance |
| 8 |
from django.db.backends.mysql.operations import DatabaseOperations |
| 9 |
from django.utils.functional import cached_property |
| 10 |
|
| 11 |
|
| 12 |
class MySQLOperations(BaseSpatialOperations, DatabaseOperations): |
| 13 |
name = "mysql" |
| 14 |
geom_func_prefix = "ST_" |
| 15 |
|
| 16 |
Adapter = WKTAdapter |
| 17 |
|
| 18 |
@cached_property |
| 19 |
def mariadb(self): |
| 20 |
return self.connection.mysql_is_mariadb |
| 21 |
|
| 22 |
@cached_property |
| 23 |
def mysql(self): |
| 24 |
return not self.connection.mysql_is_mariadb |
| 25 |
|
| 26 |
@cached_property |
| 27 |
def select(self): |
| 28 |
return self.geom_func_prefix + "AsBinary(%s)" |
| 29 |
|
| 30 |
@cached_property |
| 31 |
def from_text(self): |
| 32 |
return self.geom_func_prefix + "GeomFromText" |
| 33 |
|
| 34 |
@cached_property |
| 35 |
def collect(self): |
| 36 |
if self.connection.features.supports_collect_aggr: |
| 37 |
return self.geom_func_prefix + "Collect" |
| 38 |
|
| 39 |
@cached_property |
| 40 |
def gis_operators(self): |
| 41 |
operators = { |
| 42 |
"bbcontains": SpatialOperator( |
| 43 |
func="MBRContains" |
| 44 |
), # For consistency w/PostGIS API |
| 45 |
"bboverlaps": SpatialOperator(func="MBROverlaps"), # ... |
| 46 |
"contained": SpatialOperator(func="MBRWithin"), # ... |
| 47 |
"contains": SpatialOperator(func="ST_Contains"), |
| 48 |
"crosses": SpatialOperator(func="ST_Crosses"), |
| 49 |
"disjoint": SpatialOperator(func="ST_Disjoint"), |
| 50 |
"equals": SpatialOperator(func="ST_Equals"), |
| 51 |
"exact": SpatialOperator(func="ST_Equals"), |
| 52 |
"intersects": SpatialOperator(func="ST_Intersects"), |
| 53 |
"overlaps": SpatialOperator(func="ST_Overlaps"), |
| 54 |
"same_as": SpatialOperator(func="ST_Equals"), |
| 55 |
"touches": SpatialOperator(func="ST_Touches"), |
| 56 |
"within": SpatialOperator(func="ST_Within"), |
| 57 |
} |
| 58 |
if self.connection.mysql_is_mariadb: |
| 59 |
operators["relate"] = SpatialOperator(func="ST_Relate") |
| 60 |
else: |
| 61 |
operators["covers"] = SpatialOperator(func="MBRCovers") |
| 62 |
operators["coveredby"] = SpatialOperator(func="MBRCoveredBy") |
| 63 |
return operators |
| 64 |
|
| 65 |
@cached_property |
| 66 |
def disallowed_aggregates(self): |
| 67 |
disallowed_aggregates = [ |
| 68 |
models.Extent, |
| 69 |
models.Extent3D, |
| 70 |
models.MakeLine, |
| 71 |
models.Union, |
| 72 |
] |
| 73 |
is_mariadb = self.connection.mysql_is_mariadb |
| 74 |
if is_mariadb or self.connection.mysql_version < (8, 0, 24): |
| 75 |
disallowed_aggregates.insert(0, models.Collect) |
| 76 |
return tuple(disallowed_aggregates) |
| 77 |
|
| 78 |
function_names = { |
| 79 |
"FromWKB": "ST_GeomFromWKB", |
| 80 |
"FromWKT": "ST_GeomFromText", |
| 81 |
} |
| 82 |
|
| 83 |
@cached_property |
| 84 |
def unsupported_functions(self): |
| 85 |
unsupported = { |
| 86 |
"AsGML", |
| 87 |
"AsKML", |
| 88 |
"AsSVG", |
| 89 |
"Azimuth", |
| 90 |
"BoundingCircle", |
| 91 |
"ClosestPoint", |
| 92 |
"ForcePolygonCW", |
| 93 |
"GeometryDistance", |
| 94 |
"IsEmpty", |
| 95 |
"LineLocatePoint", |
| 96 |
"MakeValid", |
| 97 |
"MemSize", |
| 98 |
"Perimeter", |
| 99 |
"PointOnSurface", |
| 100 |
"Reverse", |
| 101 |
"Scale", |
| 102 |
"SnapToGrid", |
| 103 |
"Transform", |
| 104 |
"Translate", |
| 105 |
} |
| 106 |
if self.connection.mysql_is_mariadb: |
| 107 |
unsupported.remove("PointOnSurface") |
| 108 |
unsupported.update({"GeoHash", "IsValid"}) |
| 109 |
return unsupported |
| 110 |
|
| 111 |
def geo_db_type(self, f): |
| 112 |
return f.geom_type |
| 113 |
|
| 114 |
def get_distance(self, f, value, lookup_type): |
| 115 |
value = value[0] |
| 116 |
if isinstance(value, Distance): |
| 117 |
if f.geodetic(self.connection): |
| 118 |
raise ValueError( |
| 119 |
"Only numeric values of degree units are allowed on " |
| 120 |
"geodetic distance queries." |
| 121 |
) |
| 122 |
dist_param = getattr( |
| 123 |
value, Distance.unit_attname(f.units_name(self.connection)) |
| 124 |
) |
| 125 |
else: |
| 126 |
dist_param = value |
| 127 |
return [dist_param] |
| 128 |
|
| 129 |
def get_geometry_converter(self, expression): |
| 130 |
read = wkb_r().read |
| 131 |
srid = expression.output_field.srid |
| 132 |
if srid == -1: |
| 133 |
srid = None |
| 134 |
geom_class = expression.output_field.geom_class |
| 135 |
|
| 136 |
def converter(value, expression, connection): |
| 137 |
if value is not None: |
| 138 |
geom = GEOSGeometryBase(read(memoryview(value)), geom_class) |
| 139 |
if srid: |
| 140 |
geom.srid = srid |
| 141 |
return geom |
| 142 |
|
| 143 |
return converter |
| 144 |
|
| 145 |
def spatial_aggregate_name(self, agg_name): |
| 146 |
return getattr(self, agg_name.lower()) |