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.

fields.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. from collections import defaultdict, namedtuple
  2. from django.contrib.gis import forms, gdal
  3. from django.contrib.gis.db.models.proxy import SpatialProxy
  4. from django.contrib.gis.gdal.error import GDALException
  5. from django.contrib.gis.geos import (
  6. GeometryCollection, GEOSException, GEOSGeometry, LineString,
  7. MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
  8. )
  9. from django.core.exceptions import ImproperlyConfigured
  10. from django.db.models.fields import Field
  11. from django.utils.translation import gettext_lazy as _
  12. # Local cache of the spatial_ref_sys table, which holds SRID data for each
  13. # spatial database alias. This cache exists so that the database isn't queried
  14. # for SRID info each time a distance query is constructed.
  15. _srid_cache = defaultdict(dict)
  16. SRIDCacheEntry = namedtuple('SRIDCacheEntry', ['units', 'units_name', 'spheroid', 'geodetic'])
  17. def get_srid_info(srid, connection):
  18. """
  19. Return the units, unit name, and spheroid WKT associated with the
  20. given SRID from the `spatial_ref_sys` (or equivalent) spatial database
  21. table for the given database connection. These results are cached.
  22. """
  23. from django.contrib.gis.gdal import SpatialReference
  24. global _srid_cache
  25. try:
  26. # The SpatialRefSys model for the spatial backend.
  27. SpatialRefSys = connection.ops.spatial_ref_sys()
  28. except NotImplementedError:
  29. SpatialRefSys = None
  30. alias, get_srs = (
  31. (connection.alias, lambda srid: SpatialRefSys.objects.using(connection.alias).get(srid=srid).srs)
  32. if SpatialRefSys else
  33. (None, SpatialReference)
  34. )
  35. if srid not in _srid_cache[alias]:
  36. srs = get_srs(srid)
  37. units, units_name = srs.units
  38. _srid_cache[alias][srid] = SRIDCacheEntry(
  39. units=units,
  40. units_name=units_name,
  41. spheroid='SPHEROID["%s",%s,%s]' % (srs['spheroid'], srs.semi_major, srs.inverse_flattening),
  42. geodetic=srs.geographic,
  43. )
  44. return _srid_cache[alias][srid]
  45. class BaseSpatialField(Field):
  46. """
  47. The Base GIS Field.
  48. It's used as a base class for GeometryField and RasterField. Defines
  49. properties that are common to all GIS fields such as the characteristics
  50. of the spatial reference system of the field.
  51. """
  52. description = _("The base GIS field.")
  53. empty_strings_allowed = False
  54. def __init__(self, verbose_name=None, srid=4326, spatial_index=True, **kwargs):
  55. """
  56. The initialization function for base spatial fields. Takes the following
  57. as keyword arguments:
  58. srid:
  59. The spatial reference system identifier, an OGC standard.
  60. Defaults to 4326 (WGS84).
  61. spatial_index:
  62. Indicates whether to create a spatial index. Defaults to True.
  63. Set this instead of 'db_index' for geographic fields since index
  64. creation is different for geometry columns.
  65. """
  66. # Setting the index flag with the value of the `spatial_index` keyword.
  67. self.spatial_index = spatial_index
  68. # Setting the SRID and getting the units. Unit information must be
  69. # easily available in the field instance for distance queries.
  70. self.srid = srid
  71. # Setting the verbose_name keyword argument with the positional
  72. # first parameter, so this works like normal fields.
  73. kwargs['verbose_name'] = verbose_name
  74. super().__init__(**kwargs)
  75. def deconstruct(self):
  76. name, path, args, kwargs = super().deconstruct()
  77. # Always include SRID for less fragility; include spatial index if it's
  78. # not the default value.
  79. kwargs['srid'] = self.srid
  80. if self.spatial_index is not True:
  81. kwargs['spatial_index'] = self.spatial_index
  82. return name, path, args, kwargs
  83. def db_type(self, connection):
  84. return connection.ops.geo_db_type(self)
  85. def spheroid(self, connection):
  86. return get_srid_info(self.srid, connection).spheroid
  87. def units(self, connection):
  88. return get_srid_info(self.srid, connection).units
  89. def units_name(self, connection):
  90. return get_srid_info(self.srid, connection).units_name
  91. def geodetic(self, connection):
  92. """
  93. Return true if this field's SRID corresponds with a coordinate
  94. system that uses non-projected units (e.g., latitude/longitude).
  95. """
  96. return get_srid_info(self.srid, connection).geodetic
  97. def get_placeholder(self, value, compiler, connection):
  98. """
  99. Return the placeholder for the spatial column for the
  100. given value.
  101. """
  102. return connection.ops.get_geom_placeholder(self, value, compiler)
  103. def get_srid(self, obj):
  104. """
  105. Return the default SRID for the given geometry or raster, taking into
  106. account the SRID set for the field. For example, if the input geometry
  107. or raster doesn't have an SRID, then the SRID of the field will be
  108. returned.
  109. """
  110. srid = obj.srid # SRID of given geometry.
  111. if srid is None or self.srid == -1 or (srid == -1 and self.srid != -1):
  112. return self.srid
  113. else:
  114. return srid
  115. def get_db_prep_value(self, value, connection, *args, **kwargs):
  116. if value is None:
  117. return None
  118. return connection.ops.Adapter(
  119. super().get_db_prep_value(value, connection, *args, **kwargs),
  120. **({'geography': True} if self.geography and connection.ops.geography else {})
  121. )
  122. def get_raster_prep_value(self, value, is_candidate):
  123. """
  124. Return a GDALRaster if conversion is successful, otherwise return None.
  125. """
  126. if isinstance(value, gdal.GDALRaster):
  127. return value
  128. elif is_candidate:
  129. try:
  130. return gdal.GDALRaster(value)
  131. except GDALException:
  132. pass
  133. elif isinstance(value, dict):
  134. try:
  135. return gdal.GDALRaster(value)
  136. except GDALException:
  137. raise ValueError("Couldn't create spatial object from lookup value '%s'." % value)
  138. def get_prep_value(self, value):
  139. obj = super().get_prep_value(value)
  140. if obj is None:
  141. return None
  142. # When the input is not a geometry or raster, attempt to construct one
  143. # from the given string input.
  144. if isinstance(obj, GEOSGeometry):
  145. pass
  146. else:
  147. # Check if input is a candidate for conversion to raster or geometry.
  148. is_candidate = isinstance(obj, (bytes, str)) or hasattr(obj, '__geo_interface__')
  149. # Try to convert the input to raster.
  150. raster = self.get_raster_prep_value(obj, is_candidate)
  151. if raster:
  152. obj = raster
  153. elif is_candidate:
  154. try:
  155. obj = GEOSGeometry(obj)
  156. except (GEOSException, GDALException):
  157. raise ValueError("Couldn't create spatial object from lookup value '%s'." % obj)
  158. else:
  159. raise ValueError('Cannot use object with type %s for a spatial lookup parameter.' % type(obj).__name__)
  160. # Assigning the SRID value.
  161. obj.srid = self.get_srid(obj)
  162. return obj
  163. class GeometryField(BaseSpatialField):
  164. """
  165. The base Geometry field -- maps to the OpenGIS Specification Geometry type.
  166. """
  167. description = _("The base Geometry field -- maps to the OpenGIS Specification Geometry type.")
  168. form_class = forms.GeometryField
  169. # The OpenGIS Geometry name.
  170. geom_type = 'GEOMETRY'
  171. geom_class = None
  172. def __init__(self, verbose_name=None, dim=2, geography=False, *, extent=(-180.0, -90.0, 180.0, 90.0),
  173. tolerance=0.05, **kwargs):
  174. """
  175. The initialization function for geometry fields. In addition to the
  176. parameters from BaseSpatialField, it takes the following as keyword
  177. arguments:
  178. dim:
  179. The number of dimensions for this geometry. Defaults to 2.
  180. extent:
  181. Customize the extent, in a 4-tuple of WGS 84 coordinates, for the
  182. geometry field entry in the `USER_SDO_GEOM_METADATA` table. Defaults
  183. to (-180.0, -90.0, 180.0, 90.0).
  184. tolerance:
  185. Define the tolerance, in meters, to use for the geometry field
  186. entry in the `USER_SDO_GEOM_METADATA` table. Defaults to 0.05.
  187. """
  188. # Setting the dimension of the geometry field.
  189. self.dim = dim
  190. # Is this a geography rather than a geometry column?
  191. self.geography = geography
  192. # Oracle-specific private attributes for creating the entry in
  193. # `USER_SDO_GEOM_METADATA`
  194. self._extent = extent
  195. self._tolerance = tolerance
  196. super().__init__(verbose_name=verbose_name, **kwargs)
  197. def deconstruct(self):
  198. name, path, args, kwargs = super().deconstruct()
  199. # Include kwargs if they're not the default values.
  200. if self.dim != 2:
  201. kwargs['dim'] = self.dim
  202. if self.geography is not False:
  203. kwargs['geography'] = self.geography
  204. if self._extent != (-180.0, -90.0, 180.0, 90.0):
  205. kwargs['extent'] = self._extent
  206. if self._tolerance != 0.05:
  207. kwargs['tolerance'] = self._tolerance
  208. return name, path, args, kwargs
  209. def contribute_to_class(self, cls, name, **kwargs):
  210. super().contribute_to_class(cls, name, **kwargs)
  211. # Setup for lazy-instantiated Geometry object.
  212. setattr(cls, self.attname, SpatialProxy(self.geom_class or GEOSGeometry, self, load_func=GEOSGeometry))
  213. def formfield(self, **kwargs):
  214. defaults = {
  215. 'form_class': self.form_class,
  216. 'geom_type': self.geom_type,
  217. 'srid': self.srid,
  218. **kwargs,
  219. }
  220. if self.dim > 2 and not getattr(defaults['form_class'].widget, 'supports_3d', False):
  221. defaults.setdefault('widget', forms.Textarea)
  222. return super().formfield(**defaults)
  223. def select_format(self, compiler, sql, params):
  224. """
  225. Return the selection format string, depending on the requirements
  226. of the spatial backend. For example, Oracle and MySQL require custom
  227. selection formats in order to retrieve geometries in OGC WKB.
  228. """
  229. return compiler.connection.ops.select % sql, params
  230. # The OpenGIS Geometry Type Fields
  231. class PointField(GeometryField):
  232. geom_type = 'POINT'
  233. geom_class = Point
  234. form_class = forms.PointField
  235. description = _("Point")
  236. class LineStringField(GeometryField):
  237. geom_type = 'LINESTRING'
  238. geom_class = LineString
  239. form_class = forms.LineStringField
  240. description = _("Line string")
  241. class PolygonField(GeometryField):
  242. geom_type = 'POLYGON'
  243. geom_class = Polygon
  244. form_class = forms.PolygonField
  245. description = _("Polygon")
  246. class MultiPointField(GeometryField):
  247. geom_type = 'MULTIPOINT'
  248. geom_class = MultiPoint
  249. form_class = forms.MultiPointField
  250. description = _("Multi-point")
  251. class MultiLineStringField(GeometryField):
  252. geom_type = 'MULTILINESTRING'
  253. geom_class = MultiLineString
  254. form_class = forms.MultiLineStringField
  255. description = _("Multi-line string")
  256. class MultiPolygonField(GeometryField):
  257. geom_type = 'MULTIPOLYGON'
  258. geom_class = MultiPolygon
  259. form_class = forms.MultiPolygonField
  260. description = _("Multi polygon")
  261. class GeometryCollectionField(GeometryField):
  262. geom_type = 'GEOMETRYCOLLECTION'
  263. geom_class = GeometryCollection
  264. form_class = forms.GeometryCollectionField
  265. description = _("Geometry collection")
  266. class ExtentField(Field):
  267. "Used as a return value from an extent aggregate"
  268. description = _("Extent Aggregate Field")
  269. def get_internal_type(self):
  270. return "ExtentField"
  271. def select_format(self, compiler, sql, params):
  272. select = compiler.connection.ops.select_extent
  273. return select % sql if select else sql, params
  274. class RasterField(BaseSpatialField):
  275. """
  276. Raster field for GeoDjango -- evaluates into GDALRaster objects.
  277. """
  278. description = _("Raster Field")
  279. geom_type = 'RASTER'
  280. geography = False
  281. def _check_connection(self, connection):
  282. # Make sure raster fields are used only on backends with raster support.
  283. if not connection.features.gis_enabled or not connection.features.supports_raster:
  284. raise ImproperlyConfigured('Raster fields require backends with raster support.')
  285. def db_type(self, connection):
  286. self._check_connection(connection)
  287. return super().db_type(connection)
  288. def from_db_value(self, value, expression, connection):
  289. return connection.ops.parse_raster(value)
  290. def contribute_to_class(self, cls, name, **kwargs):
  291. super().contribute_to_class(cls, name, **kwargs)
  292. # Setup for lazy-instantiated Raster object. For large querysets, the
  293. # instantiation of all GDALRasters can potentially be expensive. This
  294. # delays the instantiation of the objects to the moment of evaluation
  295. # of the raster attribute.
  296. setattr(cls, self.attname, SpatialProxy(gdal.GDALRaster, self))
  297. def get_transform(self, name):
  298. from django.contrib.gis.db.models.lookups import RasterBandTransform
  299. try:
  300. band_index = int(name)
  301. return type(
  302. 'SpecificRasterBandTransform',
  303. (RasterBandTransform,),
  304. {'band_index': band_index}
  305. )
  306. except ValueError:
  307. pass
  308. return super().get_transform(name)