fields.py 14 KB

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