operations.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from django.contrib.gis.db.backends.base.adapter import WKTAdapter
  2. from django.contrib.gis.db.backends.base.operations import (
  3. BaseSpatialOperations,
  4. )
  5. from django.contrib.gis.db.backends.utils import SpatialOperator
  6. from django.contrib.gis.db.models import aggregates
  7. from django.contrib.gis.geos.geometry import GEOSGeometryBase
  8. from django.contrib.gis.geos.prototypes.io import wkb_r
  9. from django.contrib.gis.measure import Distance
  10. from django.db.backends.mysql.operations import DatabaseOperations
  11. from django.utils.functional import cached_property
  12. class MySQLOperations(BaseSpatialOperations, DatabaseOperations):
  13. mysql = True
  14. name = 'mysql'
  15. geom_func_prefix = 'ST_'
  16. Adapter = WKTAdapter
  17. @cached_property
  18. def is_mysql_5_6(self):
  19. return self.connection.mysql_version < (5, 7, 6)
  20. @cached_property
  21. def select(self):
  22. return self.geom_func_prefix + 'AsBinary(%s)'
  23. @cached_property
  24. def from_text(self):
  25. return self.geom_func_prefix + 'GeomFromText'
  26. @cached_property
  27. def gis_operators(self):
  28. MBREquals = 'MBREqual' if self.is_mysql_5_6 else 'MBREquals'
  29. return {
  30. 'bbcontains': SpatialOperator(func='MBRContains'), # For consistency w/PostGIS API
  31. 'bboverlaps': SpatialOperator(func='MBROverlaps'), # ...
  32. 'contained': SpatialOperator(func='MBRWithin'), # ...
  33. 'contains': SpatialOperator(func='MBRContains'),
  34. 'disjoint': SpatialOperator(func='MBRDisjoint'),
  35. 'equals': SpatialOperator(func=MBREquals),
  36. 'exact': SpatialOperator(func=MBREquals),
  37. 'intersects': SpatialOperator(func='MBRIntersects'),
  38. 'overlaps': SpatialOperator(func='MBROverlaps'),
  39. 'same_as': SpatialOperator(func=MBREquals),
  40. 'touches': SpatialOperator(func='MBRTouches'),
  41. 'within': SpatialOperator(func='MBRWithin'),
  42. }
  43. disallowed_aggregates = (
  44. aggregates.Collect, aggregates.Extent, aggregates.Extent3D,
  45. aggregates.MakeLine, aggregates.Union,
  46. )
  47. @cached_property
  48. def unsupported_functions(self):
  49. unsupported = {
  50. 'AsGML', 'AsKML', 'AsSVG', 'Azimuth', 'BoundingCircle',
  51. 'ForcePolygonCW', 'ForceRHR', 'LineLocatePoint', 'MakeValid',
  52. 'MemSize', 'Perimeter', 'PointOnSurface', 'Reverse', 'Scale',
  53. 'SnapToGrid', 'Transform', 'Translate',
  54. }
  55. if self.connection.mysql_version < (5, 7, 5):
  56. unsupported.update({'AsGeoJSON', 'GeoHash', 'IsValid'})
  57. return unsupported
  58. def geo_db_type(self, f):
  59. return f.geom_type
  60. def get_distance(self, f, value, lookup_type):
  61. value = value[0]
  62. if isinstance(value, Distance):
  63. if f.geodetic(self.connection):
  64. raise ValueError(
  65. 'Only numeric values of degree units are allowed on '
  66. 'geodetic distance queries.'
  67. )
  68. dist_param = getattr(value, Distance.unit_attname(f.units_name(self.connection)))
  69. else:
  70. dist_param = value
  71. return [dist_param]
  72. def get_geometry_converter(self, expression):
  73. read = wkb_r().read
  74. srid = expression.output_field.srid
  75. if srid == -1:
  76. srid = None
  77. geom_class = expression.output_field.geom_class
  78. def converter(value, expression, connection):
  79. if value is not None:
  80. geom = GEOSGeometryBase(read(memoryview(value)), geom_class)
  81. if srid:
  82. geom.srid = srid
  83. return geom
  84. return converter