db-api.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. ======================
  2. GeoDjango Database API
  3. ======================
  4. .. _spatial-backends:
  5. Spatial Backends
  6. ================
  7. .. module:: django.contrib.gis.db.backends
  8. :synopsis: GeoDjango's spatial database backends.
  9. GeoDjango currently provides the following spatial database backends:
  10. * ``django.contrib.gis.db.backends.postgis``
  11. * ``django.contrib.gis.db.backends.mysql``
  12. * ``django.contrib.gis.db.backends.oracle``
  13. * ``django.contrib.gis.db.backends.spatialite``
  14. .. _mysql-spatial-limitations:
  15. MySQL Spatial Limitations
  16. -------------------------
  17. Before MySQL 5.6.1, spatial extensions only support bounding box operations
  18. (what MySQL calls minimum bounding rectangles, or MBR). Specifically, MySQL did
  19. not conform to the OGC standard. Django supports spatial functions operating on
  20. real geometries available in modern MySQL versions. However, the spatial
  21. functions are not as rich as other backends like PostGIS.
  22. .. warning::
  23. True spatial indexes (R-trees) are only supported with
  24. MyISAM tables on MySQL. [#fnmysqlidx]_ In other words, when using
  25. MySQL spatial extensions you have to choose between fast spatial
  26. lookups and the integrity of your data -- MyISAM tables do
  27. not support transactions or foreign key constraints.
  28. Raster Support
  29. --------------
  30. ``RasterField`` is currently only implemented for the PostGIS backend. Spatial
  31. lookups are available for raster fields, but spatial database functions and
  32. aggregates aren't implemented for raster fields.
  33. Creating and Saving Models with Geometry Fields
  34. ===============================================
  35. Here is an example of how to create a geometry object (assuming the ``Zipcode``
  36. model)::
  37. >>> from zipcode.models import Zipcode
  38. >>> z = Zipcode(code=77096, poly='POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))')
  39. >>> z.save()
  40. :class:`~django.contrib.gis.geos.GEOSGeometry` objects may also be used to save geometric models::
  41. >>> from django.contrib.gis.geos import GEOSGeometry
  42. >>> poly = GEOSGeometry('POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))')
  43. >>> z = Zipcode(code=77096, poly=poly)
  44. >>> z.save()
  45. Moreover, if the ``GEOSGeometry`` is in a different coordinate system (has a
  46. different SRID value) than that of the field, then it will be implicitly
  47. transformed into the SRID of the model's field, using the spatial database's
  48. transform procedure::
  49. >>> poly_3084 = GEOSGeometry('POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))', srid=3084) # SRID 3084 is 'NAD83(HARN) / Texas Centric Lambert Conformal'
  50. >>> z = Zipcode(code=78212, poly=poly_3084)
  51. >>> z.save()
  52. >>> from django.db import connection
  53. >>> print(connection.queries[-1]['sql']) # printing the last SQL statement executed (requires DEBUG=True)
  54. INSERT INTO "geoapp_zipcode" ("code", "poly") VALUES (78212, ST_Transform(ST_GeomFromWKB('\\001 ... ', 3084), 4326))
  55. Thus, geometry parameters may be passed in using the ``GEOSGeometry`` object, WKT
  56. (Well Known Text [#fnwkt]_), HEXEWKB (PostGIS specific -- a WKB geometry in
  57. hexadecimal [#fnewkb]_), and GeoJSON (see :rfc:`7946`). Essentially, if the
  58. input is not a ``GEOSGeometry`` object, the geometry field will attempt to
  59. create a ``GEOSGeometry`` instance from the input.
  60. For more information creating :class:`~django.contrib.gis.geos.GEOSGeometry`
  61. objects, refer to the :ref:`GEOS tutorial <geos-tutorial>`.
  62. .. _creating-and-saving-raster-models:
  63. Creating and Saving Models with Raster Fields
  64. =============================================
  65. When creating raster models, the raster field will implicitly convert the input
  66. into a :class:`~django.contrib.gis.gdal.GDALRaster` using lazy-evaluation.
  67. The raster field will therefore accept any input that is accepted by the
  68. :class:`~django.contrib.gis.gdal.GDALRaster` constructor.
  69. Here is an example of how to create a raster object from a raster file
  70. ``volcano.tif`` (assuming the ``Elevation`` model)::
  71. >>> from elevation.models import Elevation
  72. >>> dem = Elevation(name='Volcano', rast='/path/to/raster/volcano.tif')
  73. >>> dem.save()
  74. :class:`~django.contrib.gis.gdal.GDALRaster` objects may also be used to save
  75. raster models::
  76. >>> from django.contrib.gis.gdal import GDALRaster
  77. >>> rast = GDALRaster({'width': 10, 'height': 10, 'name': 'Canyon', 'srid': 4326,
  78. ... 'scale': [0.1, -0.1], 'bands': [{"data": range(100)}]})
  79. >>> dem = Elevation(name='Canyon', rast=rast)
  80. >>> dem.save()
  81. Note that this equivalent to::
  82. >>> dem = Elevation.objects.create(
  83. ... name='Canyon',
  84. ... rast={'width': 10, 'height': 10, 'name': 'Canyon', 'srid': 4326,
  85. ... 'scale': [0.1, -0.1], 'bands': [{"data": range(100)}]},
  86. ... )
  87. .. _spatial-lookups-intro:
  88. Spatial Lookups
  89. ===============
  90. GeoDjango's lookup types may be used with any manager method like
  91. ``filter()``, ``exclude()``, etc. However, the lookup types unique to
  92. GeoDjango are only available on spatial fields.
  93. Filters on 'normal' fields (e.g. :class:`~django.db.models.CharField`)
  94. may be chained with those on geographic fields. Geographic lookups accept
  95. geometry and raster input on both sides and input types can be mixed freely.
  96. The general structure of geographic lookups is described below. A complete
  97. reference can be found in the :ref:`spatial lookup reference<spatial-lookups>`.
  98. Geometry Lookups
  99. ----------------
  100. Geographic queries with geometries take the following general form (assuming
  101. the ``Zipcode`` model used in the :doc:`model-api`)::
  102. >>> qs = Zipcode.objects.filter(<field>__<lookup_type>=<parameter>)
  103. >>> qs = Zipcode.objects.exclude(...)
  104. For example::
  105. >>> qs = Zipcode.objects.filter(poly__contains=pnt)
  106. >>> qs = Elevation.objects.filter(poly__contains=rst)
  107. In this case, ``poly`` is the geographic field, :lookup:`contains <gis-contains>`
  108. is the spatial lookup type, ``pnt`` is the parameter (which may be a
  109. :class:`~django.contrib.gis.geos.GEOSGeometry` object or a string of
  110. GeoJSON , WKT, or HEXEWKB), and ``rst`` is a
  111. :class:`~django.contrib.gis.gdal.GDALRaster` object.
  112. .. _spatial-lookup-raster:
  113. Raster Lookups
  114. --------------
  115. The raster lookup syntax is similar to the syntax for geometries. The only
  116. difference is that a band index can be specified as additional input. If no band
  117. index is specified, the first band is used by default (index ``0``). In that
  118. case the syntax is identical to the syntax for geometry lookups.
  119. To specify the band index, an additional parameter can be specified on both
  120. sides of the lookup. On the left hand side, the double underscore syntax is
  121. used to pass a band index. On the right hand side, a tuple of the raster and
  122. band index can be specified.
  123. This results in the following general form for lookups involving rasters
  124. (assuming the ``Elevation`` model used in the :doc:`model-api`)::
  125. >>> qs = Elevation.objects.filter(<field>__<lookup_type>=<parameter>)
  126. >>> qs = Elevation.objects.filter(<field>__<band_index>__<lookup_type>=<parameter>)
  127. >>> qs = Elevation.objects.filter(<field>__<lookup_type>=(<raster_input, <band_index>)
  128. For example::
  129. >>> qs = Elevation.objects.filter(rast__contains=geom)
  130. >>> qs = Elevation.objects.filter(rast__contains=rst)
  131. >>> qs = Elevation.objects.filter(rast__1__contains=geom)
  132. >>> qs = Elevation.objects.filter(rast__contains=(rst, 1))
  133. >>> qs = Elevation.objects.filter(rast__1__contains=(rst, 1))
  134. On the left hand side of the example, ``rast`` is the geographic raster field
  135. and :lookup:`contains <gis-contains>` is the spatial lookup type. On the right
  136. hand side, ``geom`` is a geometry input and ``rst`` is a
  137. :class:`~django.contrib.gis.gdal.GDALRaster` object. The band index defaults to
  138. ``0`` in the first two queries and is set to ``1`` on the others.
  139. While all spatial lookups can be used with raster objects on both sides, not all
  140. underlying operators natively accept raster input. For cases where the operator
  141. expects geometry input, the raster is automatically converted to a geometry.
  142. It's important to keep this in mind when interpreting the lookup results.
  143. The type of raster support is listed for all lookups in the :ref:`compatibility
  144. table <spatial-lookup-compatibility>`. Lookups involving rasters are currently
  145. only available for the PostGIS backend.
  146. .. _distance-queries:
  147. Distance Queries
  148. ================
  149. Introduction
  150. ------------
  151. Distance calculations with spatial data is tricky because, unfortunately,
  152. the Earth is not flat. Some distance queries with fields in a geographic
  153. coordinate system may have to be expressed differently because of
  154. limitations in PostGIS. Please see the :ref:`selecting-an-srid` section
  155. in the :doc:`model-api` documentation for more details.
  156. .. _distance-lookups-intro:
  157. Distance Lookups
  158. ----------------
  159. *Availability*: PostGIS, MariaDB, MySQL, Oracle, SpatiaLite, PGRaster (Native)
  160. The following distance lookups are available:
  161. * :lookup:`distance_lt`
  162. * :lookup:`distance_lte`
  163. * :lookup:`distance_gt`
  164. * :lookup:`distance_gte`
  165. * :lookup:`dwithin` (except MariaDB and MySQL)
  166. .. note::
  167. For *measuring*, rather than querying on distances, use the
  168. :class:`~django.contrib.gis.db.models.functions.Distance` function.
  169. Distance lookups take a tuple parameter comprising:
  170. #. A geometry or raster to base calculations from; and
  171. #. A number or :class:`~django.contrib.gis.measure.Distance` object containing the distance.
  172. If a :class:`~django.contrib.gis.measure.Distance` object is used,
  173. it may be expressed in any units (the SQL generated will use units
  174. converted to those of the field); otherwise, numeric parameters are assumed
  175. to be in the units of the field.
  176. .. note::
  177. In PostGIS, ``ST_Distance_Sphere`` does *not* limit the geometry types
  178. geographic distance queries are performed with. [#fndistsphere15]_ However,
  179. these queries may take a long time, as great-circle distances must be
  180. calculated on the fly for *every* row in the query. This is because the
  181. spatial index on traditional geometry fields cannot be used.
  182. For much better performance on WGS84 distance queries, consider using
  183. :ref:`geography columns <geography-type>` in your database instead because
  184. they are able to use their spatial index in distance queries.
  185. You can tell GeoDjango to use a geography column by setting ``geography=True``
  186. in your field definition.
  187. For example, let's say we have a ``SouthTexasCity`` model (from the
  188. :source:`GeoDjango distance tests <tests/gis_tests/distapp/models.py>` ) on a
  189. *projected* coordinate system valid for cities in southern Texas::
  190. from django.contrib.gis.db import models
  191. class SouthTexasCity(models.Model):
  192. name = models.CharField(max_length=30)
  193. # A projected coordinate system (only valid for South Texas!)
  194. # is used, units are in meters.
  195. point = models.PointField(srid=32140)
  196. Then distance queries may be performed as follows::
  197. >>> from django.contrib.gis.geos import GEOSGeometry
  198. >>> from django.contrib.gis.measure import D # ``D`` is a shortcut for ``Distance``
  199. >>> from geoapp.models import SouthTexasCity
  200. # Distances will be calculated from this point, which does not have to be projected.
  201. >>> pnt = GEOSGeometry('POINT(-96.876369 29.905320)', srid=4326)
  202. # If numeric parameter, units of field (meters in this case) are assumed.
  203. >>> qs = SouthTexasCity.objects.filter(point__distance_lte=(pnt, 7000))
  204. # Find all Cities within 7 km, > 20 miles away, and > 100 chains away (an obscure unit)
  205. >>> qs = SouthTexasCity.objects.filter(point__distance_lte=(pnt, D(km=7)))
  206. >>> qs = SouthTexasCity.objects.filter(point__distance_gte=(pnt, D(mi=20)))
  207. >>> qs = SouthTexasCity.objects.filter(point__distance_gte=(pnt, D(chain=100)))
  208. Raster queries work the same way by replacing the geometry field ``point`` with
  209. a raster field, or the ``pnt`` object with a raster object, or both. To specify
  210. the band index of a raster input on the right hand side, a 3-tuple can be
  211. passed to the lookup as follows::
  212. >>> qs = SouthTexasCity.objects.filter(point__distance_gte=(rst, 2, D(km=7)))
  213. Where the band with index 2 (the third band) of the raster ``rst`` would be
  214. used for the lookup.
  215. .. _compatibility-table:
  216. Compatibility Tables
  217. ====================
  218. .. _spatial-lookup-compatibility:
  219. Spatial Lookups
  220. ---------------
  221. The following table provides a summary of what spatial lookups are available
  222. for each spatial database backend. The PostGIS Raster (PGRaster) lookups are
  223. divided into the three categories described in the :ref:`raster lookup details
  224. <spatial-lookup-raster>`: native support ``N``, bilateral native support ``B``,
  225. and geometry conversion support ``C``.
  226. ================================= ========= ======== ========= ============ ========== ========
  227. Lookup Type PostGIS Oracle MariaDB MySQL [#]_ SpatiaLite PGRaster
  228. ================================= ========= ======== ========= ============ ========== ========
  229. :lookup:`bbcontains` X X X X N
  230. :lookup:`bboverlaps` X X X X N
  231. :lookup:`contained` X X X X N
  232. :lookup:`contains <gis-contains>` X X X X X B
  233. :lookup:`contains_properly` X B
  234. :lookup:`coveredby` X X X B
  235. :lookup:`covers` X X X B
  236. :lookup:`crosses` X X X X C
  237. :lookup:`disjoint` X X X X X B
  238. :lookup:`distance_gt` X X X X X N
  239. :lookup:`distance_gte` X X X X X N
  240. :lookup:`distance_lt` X X X X X N
  241. :lookup:`distance_lte` X X X X X N
  242. :lookup:`dwithin` X X X B
  243. :lookup:`equals` X X X X X C
  244. :lookup:`exact <same_as>` X X X X X B
  245. :lookup:`intersects` X X X X X B
  246. :lookup:`isvalid` X X X (≥ 5.7.5) X
  247. :lookup:`overlaps` X X X X X B
  248. :lookup:`relate` X X X X C
  249. :lookup:`same_as` X X X X X B
  250. :lookup:`touches` X X X X X B
  251. :lookup:`within` X X X X X B
  252. :lookup:`left` X C
  253. :lookup:`right` X C
  254. :lookup:`overlaps_left` X B
  255. :lookup:`overlaps_right` X B
  256. :lookup:`overlaps_above` X C
  257. :lookup:`overlaps_below` X C
  258. :lookup:`strictly_above` X C
  259. :lookup:`strictly_below` X C
  260. ================================= ========= ======== ========= ============ ========== ========
  261. .. _database-functions-compatibility:
  262. Database functions
  263. ------------------
  264. The following table provides a summary of what geography-specific database
  265. functions are available on each spatial backend.
  266. .. currentmodule:: django.contrib.gis.db.models.functions
  267. ==================================== ======= ============== ============ =========== ==========
  268. Function PostGIS Oracle MariaDB MySQL SpatiaLite
  269. ==================================== ======= ============== ============ =========== ==========
  270. :class:`Area` X X X X X
  271. :class:`AsGeoJSON` X X X (≥ 10.2.4) X (≥ 5.7.5) X
  272. :class:`AsGML` X X X
  273. :class:`AsKML` X X
  274. :class:`AsSVG` X X
  275. :class:`AsWKB` X X X X X
  276. :class:`AsWKT` X X X X X
  277. :class:`Azimuth` X X (LWGEOM)
  278. :class:`BoundingCircle` X X
  279. :class:`Centroid` X X X X X
  280. :class:`Difference` X X X X X
  281. :class:`Distance` X X X X X
  282. :class:`Envelope` X X X X X
  283. :class:`ForcePolygonCW` X X
  284. :class:`GeoHash` X X (≥ 5.7.5) X (LWGEOM)
  285. :class:`Intersection` X X X X X
  286. :class:`IsValid` X X X (≥ 5.7.5) X
  287. :class:`Length` X X X X X
  288. :class:`LineLocatePoint` X X
  289. :class:`MakeValid` X X (LWGEOM)
  290. :class:`MemSize` X
  291. :class:`NumGeometries` X X X X X
  292. :class:`NumPoints` X X X X X
  293. :class:`Perimeter` X X X
  294. :class:`PointOnSurface` X X X X
  295. :class:`Reverse` X X X
  296. :class:`Scale` X X
  297. :class:`SnapToGrid` X X
  298. :class:`SymDifference` X X X X X
  299. :class:`Transform` X X X
  300. :class:`Translate` X X
  301. :class:`Union` X X X X X
  302. ==================================== ======= ============== ============ =========== ==========
  303. Aggregate Functions
  304. -------------------
  305. The following table provides a summary of what GIS-specific aggregate functions
  306. are available on each spatial backend. Please note that MySQL does not
  307. support any of these aggregates, and is thus excluded from the table.
  308. .. currentmodule:: django.contrib.gis.db.models
  309. ======================= ======= ====== ==========
  310. Aggregate PostGIS Oracle SpatiaLite
  311. ======================= ======= ====== ==========
  312. :class:`Collect` X X
  313. :class:`Extent` X X X
  314. :class:`Extent3D` X
  315. :class:`MakeLine` X X
  316. :class:`Union` X X X
  317. ======================= ======= ====== ==========
  318. .. rubric:: Footnotes
  319. .. [#fnwkt] *See* Open Geospatial Consortium, Inc., `OpenGIS Simple Feature Specification For SQL <http://www.opengis.org/docs/99-049.pdf>`_, Document 99-049 (May 5, 1999), at Ch. 3.2.5, p. 3-11 (SQL Textual Representation of Geometry).
  320. .. [#fnewkb] *See* `PostGIS EWKB, EWKT and Canonical Forms <https://postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT>`_, PostGIS documentation at Ch. 4.1.2.
  321. .. [#fndistsphere15] *See* `PostGIS documentation <https://postgis.net/docs/ST_DistanceSphere.html>`_ on ``ST_DistanceSphere``.
  322. .. [#fnmysqlidx] *See* `Creating Spatial Indexes <https://dev.mysql.com/doc/refman/en/creating-spatial-indexes.html>`_
  323. in the MySQL Reference Manual:
  324. For MyISAM tables, ``SPATIAL INDEX`` creates an R-tree index. For storage
  325. engines that support nonspatial indexing of spatial columns, the engine
  326. creates a B-tree index. A B-tree index on spatial values will be useful
  327. for exact-value lookups, but not for range scans.
  328. .. [#] Refer :ref:`mysql-spatial-limitations` section for more details.