2015-01-13 04:20:40 +08:00
|
|
|
from django.contrib.gis.db.backends.base.adapter import WKTAdapter
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.gis.db.backends.base.operations import \
|
|
|
|
BaseSpatialOperations
|
2014-05-23 03:51:30 +08:00
|
|
|
from django.contrib.gis.db.backends.utils import SpatialOperator
|
2016-06-22 03:57:45 +08:00
|
|
|
from django.contrib.gis.db.models import GeometryField, aggregates
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.mysql.operations import DatabaseOperations
|
2015-01-25 07:03:02 +08:00
|
|
|
from django.utils.functional import cached_property
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2012-07-21 03:14:27 +08:00
|
|
|
|
2015-01-14 20:36:32 +08:00
|
|
|
class MySQLOperations(BaseSpatialOperations, DatabaseOperations):
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
mysql = True
|
|
|
|
name = 'mysql'
|
|
|
|
|
|
|
|
Adapter = WKTAdapter
|
|
|
|
|
2016-10-24 23:12:47 +08:00
|
|
|
@cached_property
|
|
|
|
def geom_func_prefix(self):
|
|
|
|
return '' if self.is_mysql_5_5 else 'ST_'
|
|
|
|
|
2016-05-24 21:53:50 +08:00
|
|
|
@cached_property
|
|
|
|
def is_mysql_5_5(self):
|
|
|
|
return self.connection.mysql_version < (5, 6, 1)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def is_mysql_5_6(self):
|
|
|
|
return self.connection.mysql_version < (5, 7, 6)
|
|
|
|
|
2016-06-22 03:57:45 +08:00
|
|
|
@cached_property
|
|
|
|
def uses_invalid_empty_geometry_collection(self):
|
|
|
|
return self.connection.mysql_version >= (5, 7, 5)
|
|
|
|
|
2016-01-30 06:25:15 +08:00
|
|
|
@cached_property
|
|
|
|
def select(self):
|
2016-10-24 23:12:47 +08:00
|
|
|
return self.geom_func_prefix + 'AsText(%s)'
|
2016-01-30 06:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def from_wkb(self):
|
2016-10-24 23:12:47 +08:00
|
|
|
return self.geom_func_prefix + 'GeomFromWKB'
|
2016-01-30 06:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def from_text(self):
|
2016-10-24 23:12:47 +08:00
|
|
|
return self.geom_func_prefix + 'GeomFromText'
|
2016-01-30 06:25:15 +08:00
|
|
|
|
2016-05-24 21:53:50 +08:00
|
|
|
@cached_property
|
|
|
|
def gis_operators(self):
|
|
|
|
MBREquals = 'MBREqual' if self.is_mysql_5_6 else 'MBREquals'
|
|
|
|
return {
|
|
|
|
'bbcontains': SpatialOperator(func='MBRContains'), # For consistency w/PostGIS API
|
|
|
|
'bboverlaps': SpatialOperator(func='MBROverlaps'), # ...
|
|
|
|
'contained': SpatialOperator(func='MBRWithin'), # ...
|
|
|
|
'contains': SpatialOperator(func='MBRContains'),
|
|
|
|
'disjoint': SpatialOperator(func='MBRDisjoint'),
|
|
|
|
'equals': SpatialOperator(func=MBREquals),
|
|
|
|
'exact': SpatialOperator(func=MBREquals),
|
|
|
|
'intersects': SpatialOperator(func='MBRIntersects'),
|
|
|
|
'overlaps': SpatialOperator(func='MBROverlaps'),
|
|
|
|
'same_as': SpatialOperator(func=MBREquals),
|
|
|
|
'touches': SpatialOperator(func='MBRTouches'),
|
|
|
|
'within': SpatialOperator(func='MBRWithin'),
|
|
|
|
}
|
2013-05-09 23:13:13 +08:00
|
|
|
|
2016-01-30 06:25:15 +08:00
|
|
|
@cached_property
|
|
|
|
def function_names(self):
|
2016-10-24 23:12:47 +08:00
|
|
|
return {'Length': 'GLength'} if self.is_mysql_5_5 else {}
|
2015-01-25 07:03:02 +08:00
|
|
|
|
|
|
|
disallowed_aggregates = (
|
|
|
|
aggregates.Collect, aggregates.Extent, aggregates.Extent3D,
|
|
|
|
aggregates.MakeLine, aggregates.Union,
|
|
|
|
)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def unsupported_functions(self):
|
|
|
|
unsupported = {
|
2017-04-03 02:24:06 +08:00
|
|
|
'AsGML', 'AsKML', 'AsSVG', 'Azimuth', 'BoundingCircle', 'ForceRHR',
|
2017-04-02 03:02:09 +08:00
|
|
|
'LineLocatePoint', 'MakeValid', 'MemSize', 'Perimeter',
|
|
|
|
'PointOnSurface', 'Reverse', 'Scale', 'SnapToGrid', 'Transform',
|
|
|
|
'Translate',
|
2015-01-25 07:03:02 +08:00
|
|
|
}
|
2017-04-02 01:43:53 +08:00
|
|
|
if self.connection.mysql_version < (5, 7, 5):
|
|
|
|
unsupported.update({'AsGeoJSON', 'GeoHash', 'IsValid'})
|
2016-05-24 21:53:50 +08:00
|
|
|
if self.is_mysql_5_5:
|
2015-10-31 11:02:19 +08:00
|
|
|
unsupported.update({'Difference', 'Distance', 'Intersection', 'SymDifference', 'Union'})
|
2015-01-25 07:03:02 +08:00
|
|
|
return unsupported
|
2015-01-15 18:35:35 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def geo_db_type(self, f):
|
|
|
|
return f.geom_type
|
|
|
|
|
2016-06-22 03:57:45 +08:00
|
|
|
def get_db_converters(self, expression):
|
2017-01-21 21:13:44 +08:00
|
|
|
converters = super().get_db_converters(expression)
|
2016-06-22 03:57:45 +08:00
|
|
|
if isinstance(expression.output_field, GeometryField) and self.uses_invalid_empty_geometry_collection:
|
|
|
|
converters.append(self.convert_invalid_empty_geometry_collection)
|
|
|
|
return converters
|
|
|
|
|
2016-09-30 20:58:59 +08:00
|
|
|
# https://dev.mysql.com/doc/refman/en/spatial-function-argument-handling.html
|
2016-06-22 03:57:45 +08:00
|
|
|
# MySQL 5.7.5 adds support for the empty geometry collections, but they are represented with invalid WKT.
|
|
|
|
def convert_invalid_empty_geometry_collection(self, value, expression, connection, context):
|
|
|
|
if value == b'GEOMETRYCOLLECTION()':
|
|
|
|
return b'GEOMETRYCOLLECTION EMPTY'
|
|
|
|
return value
|