Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

operations.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import re
  2. from django.conf import settings
  3. from django.contrib.gis.db.backends.base.operations import (
  4. BaseSpatialOperations,
  5. )
  6. from django.contrib.gis.db.backends.utils import SpatialOperator
  7. from django.contrib.gis.db.models import GeometryField, RasterField
  8. from django.contrib.gis.gdal import GDALRaster
  9. from django.contrib.gis.geos.geometry import GEOSGeometryBase
  10. from django.contrib.gis.geos.prototypes.io import wkb_r
  11. from django.contrib.gis.measure import Distance
  12. from django.core.exceptions import ImproperlyConfigured
  13. from django.db.backends.postgresql.operations import DatabaseOperations
  14. from django.db.models import Func, Value
  15. from django.db.utils import NotSupportedError, ProgrammingError
  16. from django.utils.functional import cached_property
  17. from django.utils.version import get_version_tuple
  18. from .adapter import PostGISAdapter
  19. from .models import PostGISGeometryColumns, PostGISSpatialRefSys
  20. from .pgraster import from_pgraster
  21. # Identifier to mark raster lookups as bilateral.
  22. BILATERAL = 'bilateral'
  23. class PostGISOperator(SpatialOperator):
  24. def __init__(self, geography=False, raster=False, **kwargs):
  25. # Only a subset of the operators and functions are available for the
  26. # geography type.
  27. self.geography = geography
  28. # Only a subset of the operators and functions are available for the
  29. # raster type. Lookups that don't support raster will be converted to
  30. # polygons. If the raster argument is set to BILATERAL, then the
  31. # operator cannot handle mixed geom-raster lookups.
  32. self.raster = raster
  33. super().__init__(**kwargs)
  34. def as_sql(self, connection, lookup, template_params, *args):
  35. if lookup.lhs.output_field.geography and not self.geography:
  36. raise ValueError('PostGIS geography does not support the "%s" '
  37. 'function/operator.' % (self.func or self.op,))
  38. template_params = self.check_raster(lookup, template_params)
  39. return super().as_sql(connection, lookup, template_params, *args)
  40. def check_raster(self, lookup, template_params):
  41. spheroid = lookup.rhs_params and lookup.rhs_params[-1] == 'spheroid'
  42. # Check which input is a raster.
  43. lhs_is_raster = lookup.lhs.field.geom_type == 'RASTER'
  44. rhs_is_raster = isinstance(lookup.rhs, GDALRaster)
  45. # Look for band indices and inject them if provided.
  46. if lookup.band_lhs is not None and lhs_is_raster:
  47. if not self.func:
  48. raise ValueError('Band indices are not allowed for this operator, it works on bbox only.')
  49. template_params['lhs'] = '%s, %s' % (template_params['lhs'], lookup.band_lhs)
  50. if lookup.band_rhs is not None and rhs_is_raster:
  51. if not self.func:
  52. raise ValueError('Band indices are not allowed for this operator, it works on bbox only.')
  53. template_params['rhs'] = '%s, %s' % (template_params['rhs'], lookup.band_rhs)
  54. # Convert rasters to polygons if necessary.
  55. if not self.raster or spheroid:
  56. # Operators without raster support.
  57. if lhs_is_raster:
  58. template_params['lhs'] = 'ST_Polygon(%s)' % template_params['lhs']
  59. if rhs_is_raster:
  60. template_params['rhs'] = 'ST_Polygon(%s)' % template_params['rhs']
  61. elif self.raster == BILATERAL:
  62. # Operators with raster support but don't support mixed (rast-geom)
  63. # lookups.
  64. if lhs_is_raster and not rhs_is_raster:
  65. template_params['lhs'] = 'ST_Polygon(%s)' % template_params['lhs']
  66. elif rhs_is_raster and not lhs_is_raster:
  67. template_params['rhs'] = 'ST_Polygon(%s)' % template_params['rhs']
  68. return template_params
  69. class ST_Polygon(Func):
  70. function = 'ST_Polygon'
  71. def __init__(self, expr):
  72. super().__init__(expr)
  73. expr = self.source_expressions[0]
  74. if isinstance(expr, Value) and not expr._output_field_or_none:
  75. self.source_expressions[0] = Value(expr.value, output_field=RasterField(srid=expr.value.srid))
  76. @cached_property
  77. def output_field(self):
  78. return GeometryField(srid=self.source_expressions[0].field.srid)
  79. class PostGISOperations(BaseSpatialOperations, DatabaseOperations):
  80. name = 'postgis'
  81. postgis = True
  82. geography = True
  83. geom_func_prefix = 'ST_'
  84. Adapter = PostGISAdapter
  85. collect = geom_func_prefix + 'Collect'
  86. extent = geom_func_prefix + 'Extent'
  87. extent3d = geom_func_prefix + '3DExtent'
  88. length3d = geom_func_prefix + '3DLength'
  89. makeline = geom_func_prefix + 'MakeLine'
  90. perimeter3d = geom_func_prefix + '3DPerimeter'
  91. unionagg = geom_func_prefix + 'Union'
  92. gis_operators = {
  93. 'bbcontains': PostGISOperator(op='~', raster=True),
  94. 'bboverlaps': PostGISOperator(op='&&', geography=True, raster=True),
  95. 'contained': PostGISOperator(op='@', raster=True),
  96. 'overlaps_left': PostGISOperator(op='&<', raster=BILATERAL),
  97. 'overlaps_right': PostGISOperator(op='&>', raster=BILATERAL),
  98. 'overlaps_below': PostGISOperator(op='&<|'),
  99. 'overlaps_above': PostGISOperator(op='|&>'),
  100. 'left': PostGISOperator(op='<<'),
  101. 'right': PostGISOperator(op='>>'),
  102. 'strictly_below': PostGISOperator(op='<<|'),
  103. 'strictly_above': PostGISOperator(op='|>>'),
  104. 'same_as': PostGISOperator(op='~=', raster=BILATERAL),
  105. 'exact': PostGISOperator(op='~=', raster=BILATERAL), # alias of same_as
  106. 'contains': PostGISOperator(func='ST_Contains', raster=BILATERAL),
  107. 'contains_properly': PostGISOperator(func='ST_ContainsProperly', raster=BILATERAL),
  108. 'coveredby': PostGISOperator(func='ST_CoveredBy', geography=True, raster=BILATERAL),
  109. 'covers': PostGISOperator(func='ST_Covers', geography=True, raster=BILATERAL),
  110. 'crosses': PostGISOperator(func='ST_Crosses'),
  111. 'disjoint': PostGISOperator(func='ST_Disjoint', raster=BILATERAL),
  112. 'equals': PostGISOperator(func='ST_Equals'),
  113. 'intersects': PostGISOperator(func='ST_Intersects', geography=True, raster=BILATERAL),
  114. 'overlaps': PostGISOperator(func='ST_Overlaps', raster=BILATERAL),
  115. 'relate': PostGISOperator(func='ST_Relate'),
  116. 'touches': PostGISOperator(func='ST_Touches', raster=BILATERAL),
  117. 'within': PostGISOperator(func='ST_Within', raster=BILATERAL),
  118. 'dwithin': PostGISOperator(func='ST_DWithin', geography=True, raster=BILATERAL),
  119. }
  120. unsupported_functions = set()
  121. select = '%s::bytea'
  122. select_extent = None
  123. @cached_property
  124. def function_names(self):
  125. function_names = {
  126. 'BoundingCircle': 'ST_MinimumBoundingCircle',
  127. 'NumPoints': 'ST_NPoints',
  128. }
  129. if self.spatial_version < (2, 2, 0):
  130. function_names.update({
  131. 'DistanceSphere': 'ST_distance_sphere',
  132. 'DistanceSpheroid': 'ST_distance_spheroid',
  133. 'LengthSpheroid': 'ST_length_spheroid',
  134. 'MemSize': 'ST_mem_size',
  135. })
  136. if self.spatial_version < (2, 4, 0):
  137. function_names['ForcePolygonCW'] = 'ST_ForceRHR'
  138. return function_names
  139. @cached_property
  140. def spatial_version(self):
  141. """Determine the version of the PostGIS library."""
  142. # Trying to get the PostGIS version because the function
  143. # signatures will depend on the version used. The cost
  144. # here is a database query to determine the version, which
  145. # can be mitigated by setting `POSTGIS_VERSION` with a 3-tuple
  146. # comprising user-supplied values for the major, minor, and
  147. # subminor revision of PostGIS.
  148. if hasattr(settings, 'POSTGIS_VERSION'):
  149. version = settings.POSTGIS_VERSION
  150. else:
  151. # Run a basic query to check the status of the connection so we're
  152. # sure we only raise the error below if the problem comes from
  153. # PostGIS and not from PostgreSQL itself (see #24862).
  154. self._get_postgis_func('version')
  155. try:
  156. vtup = self.postgis_version_tuple()
  157. except ProgrammingError:
  158. raise ImproperlyConfigured(
  159. 'Cannot determine PostGIS version for database "%s" '
  160. 'using command "SELECT postgis_lib_version()". '
  161. 'GeoDjango requires at least PostGIS version 2.1. '
  162. 'Was the database created from a spatial database '
  163. 'template?' % self.connection.settings_dict['NAME']
  164. )
  165. version = vtup[1:]
  166. return version
  167. def convert_extent(self, box):
  168. """
  169. Return a 4-tuple extent for the `Extent` aggregate by converting
  170. the bounding box text returned by PostGIS (`box` argument), for
  171. example: "BOX(-90.0 30.0, -85.0 40.0)".
  172. """
  173. if box is None:
  174. return None
  175. ll, ur = box[4:-1].split(',')
  176. xmin, ymin = map(float, ll.split())
  177. xmax, ymax = map(float, ur.split())
  178. return (xmin, ymin, xmax, ymax)
  179. def convert_extent3d(self, box3d):
  180. """
  181. Return a 6-tuple extent for the `Extent3D` aggregate by converting
  182. the 3d bounding-box text returned by PostGIS (`box3d` argument), for
  183. example: "BOX3D(-90.0 30.0 1, -85.0 40.0 2)".
  184. """
  185. if box3d is None:
  186. return None
  187. ll, ur = box3d[6:-1].split(',')
  188. xmin, ymin, zmin = map(float, ll.split())
  189. xmax, ymax, zmax = map(float, ur.split())
  190. return (xmin, ymin, zmin, xmax, ymax, zmax)
  191. def geo_db_type(self, f):
  192. """
  193. Return the database field type for the given spatial field.
  194. """
  195. if f.geom_type == 'RASTER':
  196. return 'raster'
  197. # Type-based geometries.
  198. # TODO: Support 'M' extension.
  199. if f.dim == 3:
  200. geom_type = f.geom_type + 'Z'
  201. else:
  202. geom_type = f.geom_type
  203. if f.geography:
  204. if f.srid != 4326:
  205. raise NotSupportedError('PostGIS only supports geography columns with an SRID of 4326.')
  206. return 'geography(%s,%d)' % (geom_type, f.srid)
  207. else:
  208. return 'geometry(%s,%d)' % (geom_type, f.srid)
  209. def get_distance(self, f, dist_val, lookup_type):
  210. """
  211. Retrieve the distance parameters for the given geometry field,
  212. distance lookup value, and the distance lookup type.
  213. This is the most complex implementation of the spatial backends due to
  214. what is supported on geodetic geometry columns vs. what's available on
  215. projected geometry columns. In addition, it has to take into account
  216. the geography column type.
  217. """
  218. # Getting the distance parameter
  219. value = dist_val[0]
  220. # Shorthand boolean flags.
  221. geodetic = f.geodetic(self.connection)
  222. geography = f.geography
  223. if isinstance(value, Distance):
  224. if geography:
  225. dist_param = value.m
  226. elif geodetic:
  227. if lookup_type == 'dwithin':
  228. raise ValueError('Only numeric values of degree units are '
  229. 'allowed on geographic DWithin queries.')
  230. dist_param = value.m
  231. else:
  232. dist_param = getattr(value, Distance.unit_attname(f.units_name(self.connection)))
  233. else:
  234. # Assuming the distance is in the units of the field.
  235. dist_param = value
  236. return [dist_param]
  237. def get_geom_placeholder(self, f, value, compiler):
  238. """
  239. Provide a proper substitution value for Geometries or rasters that are
  240. not in the SRID of the field. Specifically, this routine will
  241. substitute in the ST_Transform() function call.
  242. """
  243. tranform_func = self.spatial_function_name('Transform')
  244. if hasattr(value, 'as_sql'):
  245. if value.field.srid == f.srid:
  246. placeholder = '%s'
  247. else:
  248. placeholder = '%s(%%s, %s)' % (tranform_func, f.srid)
  249. return placeholder
  250. # Get the srid for this object
  251. if value is None:
  252. value_srid = None
  253. else:
  254. value_srid = value.srid
  255. # Adding Transform() to the SQL placeholder if the value srid
  256. # is not equal to the field srid.
  257. if value_srid is None or value_srid == f.srid:
  258. placeholder = '%s'
  259. else:
  260. placeholder = '%s(%%s, %s)' % (tranform_func, f.srid)
  261. return placeholder
  262. def _get_postgis_func(self, func):
  263. """
  264. Helper routine for calling PostGIS functions and returning their result.
  265. """
  266. # Close out the connection. See #9437.
  267. with self.connection.temporary_connection() as cursor:
  268. cursor.execute('SELECT %s()' % func)
  269. return cursor.fetchone()[0]
  270. def postgis_geos_version(self):
  271. "Return the version of the GEOS library used with PostGIS."
  272. return self._get_postgis_func('postgis_geos_version')
  273. def postgis_lib_version(self):
  274. "Return the version number of the PostGIS library used with PostgreSQL."
  275. return self._get_postgis_func('postgis_lib_version')
  276. def postgis_proj_version(self):
  277. "Return the version of the PROJ.4 library used with PostGIS."
  278. return self._get_postgis_func('postgis_proj_version')
  279. def postgis_version(self):
  280. "Return PostGIS version number and compile-time options."
  281. return self._get_postgis_func('postgis_version')
  282. def postgis_full_version(self):
  283. "Return PostGIS version number and compile-time options."
  284. return self._get_postgis_func('postgis_full_version')
  285. def postgis_version_tuple(self):
  286. """
  287. Return the PostGIS version as a tuple (version string, major,
  288. minor, subminor).
  289. """
  290. version = self.postgis_lib_version()
  291. return (version,) + get_version_tuple(version)
  292. def proj_version_tuple(self):
  293. """
  294. Return the version of PROJ.4 used by PostGIS as a tuple of the
  295. major, minor, and subminor release numbers.
  296. """
  297. proj_regex = re.compile(r'(\d+)\.(\d+)\.(\d+)')
  298. proj_ver_str = self.postgis_proj_version()
  299. m = proj_regex.search(proj_ver_str)
  300. if m:
  301. return tuple(map(int, [m.group(1), m.group(2), m.group(3)]))
  302. else:
  303. raise Exception('Could not determine PROJ.4 version from PostGIS.')
  304. def spatial_aggregate_name(self, agg_name):
  305. if agg_name == 'Extent3D':
  306. return self.extent3d
  307. else:
  308. return self.geom_func_prefix + agg_name
  309. # Routines for getting the OGC-compliant models.
  310. def geometry_columns(self):
  311. return PostGISGeometryColumns
  312. def spatial_ref_sys(self):
  313. return PostGISSpatialRefSys
  314. def parse_raster(self, value):
  315. """Convert a PostGIS HEX String into a dict readable by GDALRaster."""
  316. return from_pgraster(value)
  317. def distance_expr_for_lookup(self, lhs, rhs, **kwargs):
  318. return super().distance_expr_for_lookup(
  319. self._normalize_distance_lookup_arg(lhs),
  320. self._normalize_distance_lookup_arg(rhs),
  321. **kwargs
  322. )
  323. @staticmethod
  324. def _normalize_distance_lookup_arg(arg):
  325. is_raster = (
  326. arg.field.geom_type == 'RASTER'
  327. if hasattr(arg, 'field') else
  328. isinstance(arg, GDALRaster)
  329. )
  330. return ST_Polygon(arg) if is_raster else arg
  331. def get_geometry_converter(self, expression):
  332. read = wkb_r().read
  333. geom_class = expression.output_field.geom_class
  334. def converter(value, expression, connection):
  335. return None if value is None else GEOSGeometryBase(read(value), geom_class)
  336. return converter
  337. def get_area_att_for_field(self, field):
  338. return 'sq_m'