1. ===================
    
  2. GeoDjango Model API
    
  3. ===================
    
  4. 
    
  5. .. module:: django.contrib.gis.db.models
    
  6.     :synopsis: GeoDjango model and field API.
    
  7. 
    
  8. This document explores the details of the GeoDjango Model API.  Throughout this
    
  9. section, we'll be using the following geographic model of a `ZIP code`__ and
    
  10. of a `Digital Elevation Model`__ as our examples::
    
  11. 
    
  12.     from django.contrib.gis.db import models
    
  13. 
    
  14.     class Zipcode(models.Model):
    
  15.         code = models.CharField(max_length=5)
    
  16.         poly = models.PolygonField()
    
  17. 
    
  18.     class Elevation(models.Model):
    
  19.         name = models.CharField(max_length=100)
    
  20.         rast = models.RasterField()
    
  21. 
    
  22. __ https://en.wikipedia.org/wiki/ZIP_code
    
  23. __ https://en.wikipedia.org/wiki/Digital_elevation_model
    
  24. 
    
  25. Spatial Field Types
    
  26. ===================
    
  27. 
    
  28. Spatial fields consist of a series of geometry field types and one raster field
    
  29. type. Each of the geometry field types correspond to the OpenGIS Simple
    
  30. Features specification [#fnogc]_. There is no such standard for raster data.
    
  31. 
    
  32. ``GeometryField``
    
  33. -----------------
    
  34. 
    
  35. .. class:: GeometryField
    
  36. 
    
  37. The base class for geometry fields.
    
  38. 
    
  39. ``PointField``
    
  40. --------------
    
  41. 
    
  42. .. class:: PointField
    
  43. 
    
  44. Stores a :class:`~django.contrib.gis.geos.Point`.
    
  45. 
    
  46. ``LineStringField``
    
  47. -------------------
    
  48. 
    
  49. .. class:: LineStringField
    
  50. 
    
  51. Stores a :class:`~django.contrib.gis.geos.LineString`.
    
  52. 
    
  53. ``PolygonField``
    
  54. ----------------
    
  55. 
    
  56. .. class:: PolygonField
    
  57. 
    
  58. Stores a :class:`~django.contrib.gis.geos.Polygon`.
    
  59. 
    
  60. ``MultiPointField``
    
  61. -------------------
    
  62. 
    
  63. .. class:: MultiPointField
    
  64. 
    
  65. Stores a :class:`~django.contrib.gis.geos.MultiPoint`.
    
  66. 
    
  67. ``MultiLineStringField``
    
  68. ------------------------
    
  69. 
    
  70. .. class:: MultiLineStringField
    
  71. 
    
  72. Stores a :class:`~django.contrib.gis.geos.MultiLineString`.
    
  73. 
    
  74. ``MultiPolygonField``
    
  75. ---------------------
    
  76. 
    
  77. .. class:: MultiPolygonField
    
  78. 
    
  79. Stores a :class:`~django.contrib.gis.geos.MultiPolygon`.
    
  80. 
    
  81. ``GeometryCollectionField``
    
  82. ---------------------------
    
  83. 
    
  84. .. class:: GeometryCollectionField
    
  85. 
    
  86. Stores a :class:`~django.contrib.gis.geos.GeometryCollection`.
    
  87. 
    
  88. ``RasterField``
    
  89. ---------------
    
  90. 
    
  91. .. class:: RasterField
    
  92. 
    
  93. Stores a :class:`~django.contrib.gis.gdal.GDALRaster`.
    
  94. 
    
  95. ``RasterField`` is currently only implemented for the PostGIS backend.
    
  96. 
    
  97. Spatial Field Options
    
  98. =====================
    
  99. 
    
  100. In addition to the regular :ref:`common-model-field-options` available for
    
  101. Django model fields, spatial fields have the following additional options.
    
  102. All are optional.
    
  103. 
    
  104. ``srid``
    
  105. --------
    
  106. 
    
  107. .. attribute:: BaseSpatialField.srid
    
  108. 
    
  109. Sets the SRID [#fnogcsrid]_ (Spatial Reference System Identity) of the geometry field to
    
  110. the given value. Defaults to 4326 (also known as `WGS84`__, units are in degrees
    
  111. of longitude and latitude).
    
  112. 
    
  113. __ https://en.wikipedia.org/wiki/WGS84
    
  114. 
    
  115. .. _selecting-an-srid:
    
  116. 
    
  117. Selecting an SRID
    
  118. ~~~~~~~~~~~~~~~~~
    
  119. 
    
  120. Choosing an appropriate SRID for your model is an important decision that the
    
  121. developer should consider carefully.  The SRID is an integer specifier that
    
  122. corresponds to the projection system that will be used to interpret the data
    
  123. in the spatial database. [#fnsrid]_  Projection systems give the context to the
    
  124. coordinates that specify a location.  Although the details of `geodesy`__ are
    
  125. beyond the scope of this documentation, the general problem is that the earth
    
  126. is spherical and representations of the earth (e.g., paper maps, web maps)
    
  127. are not.
    
  128. 
    
  129. Most people are familiar with using latitude and longitude to reference a
    
  130. location on the earth's surface.  However, latitude and longitude are angles,
    
  131. not distances. In other words, while the shortest path between two points on
    
  132. a flat surface is a straight line, the shortest path between two points on a curved
    
  133. surface (such as the earth) is an *arc* of a `great circle`__. [#fnthematic]_  Thus,
    
  134. additional computation is required to obtain distances in planar units (e.g.,
    
  135. kilometers and miles).  Using a geographic coordinate system may introduce
    
  136. complications for the developer later on. For example, SpatiaLite does not have
    
  137. the capability to perform distance calculations between geometries using
    
  138. geographic coordinate systems, e.g. constructing a query to  find all points
    
  139. within 5 miles of a county boundary stored as WGS84.
    
  140. [#fndist]_
    
  141. 
    
  142. Portions of the earth's surface may projected onto a two-dimensional, or
    
  143. Cartesian, plane.  Projected coordinate systems are especially convenient
    
  144. for region-specific applications, e.g., if you know that your database will
    
  145. only cover geometries in `North Kansas`__, then you may consider using projection
    
  146. system specific to that region.  Moreover, projected coordinate systems are
    
  147. defined in Cartesian units (such as meters or feet), easing distance
    
  148. calculations.
    
  149. 
    
  150. .. note::
    
  151. 
    
  152.     If you wish to perform arbitrary distance queries using non-point
    
  153.     geometries in WGS84 in PostGIS and you want decent performance, enable the
    
  154.     :attr:`GeometryField.geography` keyword so that :ref:`geography database
    
  155.     type <geography-type>` is used instead.
    
  156. 
    
  157. Additional Resources:
    
  158. 
    
  159. * `spatialreference.org`__: A Django-powered database of spatial reference
    
  160.   systems.
    
  161. * `The State Plane Coordinate System`__: A website covering the various
    
  162.   projection systems used in the United States.  Much of the U.S. spatial
    
  163.   data encountered will be in one of these coordinate systems rather than
    
  164.   in a geographic coordinate system such as WGS84.
    
  165. 
    
  166. __ https://en.wikipedia.org/wiki/Geodesy
    
  167. __ https://en.wikipedia.org/wiki/Great_circle
    
  168. __ https://spatialreference.org/ref/epsg/2796/
    
  169. __ https://spatialreference.org/
    
  170. __ https://web.archive.org/web/20080302095452/http://welcome.warnercnr.colostate.edu/class_info/nr502/lg3/datums_coordinates/spcs.html
    
  171. 
    
  172. ``spatial_index``
    
  173. -----------------
    
  174. 
    
  175. .. attribute:: BaseSpatialField.spatial_index
    
  176. 
    
  177. Defaults to ``True``.  Creates a spatial index for the given geometry
    
  178. field.
    
  179. 
    
  180. .. note::
    
  181. 
    
  182.     This is different from the ``db_index`` field option because spatial
    
  183.     indexes are created in a different manner than regular database
    
  184.     indexes.  Specifically, spatial indexes are typically created using
    
  185.     a variant of the R-Tree, while regular database indexes typically
    
  186.     use B-Trees.
    
  187. 
    
  188. .. _geometry-field-options:
    
  189. 
    
  190. Geometry Field Options
    
  191. ======================
    
  192. 
    
  193. There are additional options available for Geometry fields. All the following
    
  194. options are optional.
    
  195. 
    
  196. ``dim``
    
  197. -------
    
  198. 
    
  199. .. attribute:: GeometryField.dim
    
  200. 
    
  201. This option may be used for customizing the coordinate dimension of the
    
  202. geometry field.  By default, it is set to 2, for representing two-dimensional
    
  203. geometries.  For spatial backends that support it, it may be set to 3 for
    
  204. three-dimensional support.
    
  205. 
    
  206. .. note::
    
  207. 
    
  208.     At this time 3D support is limited to the PostGIS and SpatiaLite backends.
    
  209. 
    
  210. ``geography``
    
  211. -------------
    
  212. 
    
  213. .. attribute:: GeometryField.geography
    
  214. 
    
  215. If set to ``True``, this option will create a database column of
    
  216. type geography, rather than geometry.  Please refer to the
    
  217. :ref:`geography type <geography-type>` section below for more
    
  218. details.
    
  219. 
    
  220. .. note::
    
  221. 
    
  222.     Geography support is limited to PostGIS and will force the SRID to be 4326.
    
  223. 
    
  224. .. _geography-type:
    
  225. 
    
  226. Geography Type
    
  227. ~~~~~~~~~~~~~~
    
  228. 
    
  229. The geography type provides native support for spatial features represented
    
  230. with geographic coordinates (e.g., WGS84 longitude/latitude). [#fngeography]_
    
  231. Unlike the plane used by a geometry type, the geography type uses a spherical
    
  232. representation of its data.  Distance and measurement operations
    
  233. performed on a geography column automatically employ great circle arc
    
  234. calculations and return linear units.  In other words, when ``ST_Distance``
    
  235. is called on two geographies, a value in meters is returned (as opposed
    
  236. to degrees if called on a geometry column in WGS84).
    
  237. 
    
  238. Because geography calculations involve more mathematics, only a subset of the
    
  239. PostGIS spatial lookups are available for the geography type. Practically,
    
  240. this means that in addition to the :ref:`distance lookups <distance-lookups>`
    
  241. only the following additional :ref:`spatial lookups <spatial-lookups>` are
    
  242. available for geography columns:
    
  243. 
    
  244. * :lookup:`bboverlaps`
    
  245. * :lookup:`coveredby`
    
  246. * :lookup:`covers`
    
  247. * :lookup:`intersects`
    
  248. 
    
  249. If you need to use a spatial lookup or aggregate that doesn't support the
    
  250. geography type as input, you can use the
    
  251. :class:`~django.db.models.functions.Cast` database function to convert the
    
  252. geography column to a geometry type in the query::
    
  253. 
    
  254.     from django.contrib.gis.db.models import PointField
    
  255.     from django.db.models.functions import Cast
    
  256. 
    
  257.     Zipcode.objects.annotate(
    
  258.         geom=Cast('geography_field', PointField())
    
  259.     ).filter(geom__within=poly)
    
  260. 
    
  261. For more information, the PostGIS documentation contains a helpful section on
    
  262. determining `when to use geography data type over geometry data type
    
  263. <https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_GeographyVSGeometry>`_.
    
  264. 
    
  265. .. rubric:: Footnotes
    
  266. .. [#fnogc] OpenGIS Consortium, Inc., `Simple Feature Specification For SQL <https://www.ogc.org/standards/sfs>`_.
    
  267. .. [#fnogcsrid] *See id.* at Ch. 2.3.8, p. 39 (Geometry Values and Spatial Reference Systems).
    
  268. .. [#fnsrid] Typically, SRID integer corresponds to an EPSG (`European Petroleum Survey Group <https://epsg.org/>`_) identifier.  However, it may also be associated with custom projections defined in spatial database's spatial reference systems table.
    
  269. .. [#fnthematic] Terry A. Slocum, Robert B. McMaster, Fritz C. Kessler, & Hugh H. Howard, *Thematic Cartography and Geographic Visualization* (Prentice Hall, 2nd edition), at Ch. 7.1.3.
    
  270. .. [#fndist] This limitation does not apply to PostGIS.
    
  271. .. [#fngeography] Please refer to the `PostGIS Geography Type <https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_Geography>`_ documentation for more details.