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
|
2015-01-15 18:35:35 +08:00
|
|
|
from django.contrib.gis.db.models import 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'
|
|
|
|
select = 'AsText(%s)'
|
|
|
|
from_wkb = 'GeomFromWKB'
|
|
|
|
from_text = 'GeomFromText'
|
|
|
|
|
|
|
|
Adapter = WKTAdapter
|
2013-11-03 05:02:56 +08:00
|
|
|
Adaptor = Adapter # Backwards-compatibility alias.
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2014-05-23 03:51:30 +08:00
|
|
|
gis_operators = {
|
|
|
|
'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='MBREqual'),
|
|
|
|
'exact': SpatialOperator(func='MBREqual'),
|
|
|
|
'intersects': SpatialOperator(func='MBRIntersects'),
|
|
|
|
'overlaps': SpatialOperator(func='MBROverlaps'),
|
|
|
|
'same_as': SpatialOperator(func='MBREqual'),
|
|
|
|
'touches': SpatialOperator(func='MBRTouches'),
|
|
|
|
'within': SpatialOperator(func='MBRWithin'),
|
2013-05-09 23:13:13 +08:00
|
|
|
}
|
|
|
|
|
2015-01-25 07:03:02 +08:00
|
|
|
function_names = {
|
|
|
|
'Distance': 'ST_Distance',
|
|
|
|
'Length': 'GLength',
|
|
|
|
'Union': 'ST_Union',
|
|
|
|
}
|
|
|
|
|
|
|
|
disallowed_aggregates = (
|
|
|
|
aggregates.Collect, aggregates.Extent, aggregates.Extent3D,
|
|
|
|
aggregates.MakeLine, aggregates.Union,
|
|
|
|
)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def unsupported_functions(self):
|
|
|
|
unsupported = {
|
|
|
|
'AsGeoJSON', 'AsGML', 'AsKML', 'AsSVG', 'BoundingCircle',
|
|
|
|
'Difference', 'ForceRHR', 'GeoHash', 'Intersection', 'MemSize',
|
|
|
|
'Perimeter', 'PointOnSurface', 'Reverse', 'Scale', 'SnapToGrid',
|
|
|
|
'SymDifference', 'Transform', 'Translate',
|
|
|
|
}
|
|
|
|
if self.connection.mysql_version < (5, 6, 1):
|
|
|
|
unsupported.update({'Distance', 'Union'})
|
|
|
|
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
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def get_geom_placeholder(self, f, value, compiler):
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
|
|
|
The placeholder here has to include MySQL's WKT constructor. Because
|
|
|
|
MySQL does not support spatial transformations, there is no need to
|
|
|
|
modify the placeholder based on the contents of the given value.
|
|
|
|
"""
|
2013-12-25 21:13:18 +08:00
|
|
|
if hasattr(value, 'as_sql'):
|
2014-11-16 09:56:42 +08:00
|
|
|
placeholder, _ = compiler.compile(value)
|
2009-12-22 23:18:51 +08:00
|
|
|
else:
|
|
|
|
placeholder = '%s(%%s)' % self.from_text
|
|
|
|
return placeholder
|