1. ===================
    
  2. GeoDjango Forms API
    
  3. ===================
    
  4. 
    
  5. .. module:: django.contrib.gis.forms
    
  6.     :synopsis: GeoDjango forms API.
    
  7. 
    
  8. GeoDjango provides some specialized form fields and widgets in order to visually
    
  9. display and edit geolocalized data on a map. By default, they use
    
  10. `OpenLayers`_-powered maps, with a base WMS layer provided by `NASA`_.
    
  11. 
    
  12. .. _OpenLayers: https://openlayers.org/
    
  13. .. _NASA: https://www.earthdata.nasa.gov/
    
  14. 
    
  15. Field arguments
    
  16. ===============
    
  17. In addition to the regular :ref:`form field arguments <core-field-arguments>`,
    
  18. GeoDjango form fields take the following optional arguments.
    
  19. 
    
  20. ``srid``
    
  21. --------
    
  22. 
    
  23. .. attribute:: Field.srid
    
  24. 
    
  25.     This is the SRID code that the field value should be transformed to. For
    
  26.     example, if the map widget SRID is different from the SRID more generally
    
  27.     used by your application or database, the field will automatically convert
    
  28.     input values into that SRID.
    
  29. 
    
  30. ``geom_type``
    
  31. -------------
    
  32. 
    
  33. .. attribute:: Field.geom_type
    
  34. 
    
  35.     You generally shouldn't have to set or change that attribute which should
    
  36.     be set up depending on the field class. It matches the OpenGIS standard
    
  37.     geometry name.
    
  38. 
    
  39. Form field classes
    
  40. ==================
    
  41. 
    
  42. ``GeometryField``
    
  43. -----------------
    
  44. 
    
  45. .. class:: GeometryField
    
  46. 
    
  47. ``PointField``
    
  48. --------------
    
  49. 
    
  50. .. class:: PointField
    
  51. 
    
  52. ``LineStringField``
    
  53. -------------------
    
  54. 
    
  55. .. class:: LineStringField
    
  56. 
    
  57. ``PolygonField``
    
  58. ----------------
    
  59. 
    
  60. .. class:: PolygonField
    
  61. 
    
  62. ``MultiPointField``
    
  63. -------------------
    
  64. 
    
  65. .. class:: MultiPointField
    
  66. 
    
  67. ``MultiLineStringField``
    
  68. ------------------------
    
  69. 
    
  70. .. class:: MultiLineStringField
    
  71. 
    
  72. ``MultiPolygonField``
    
  73. ---------------------
    
  74. 
    
  75. .. class:: MultiPolygonField
    
  76. 
    
  77. ``GeometryCollectionField``
    
  78. ---------------------------
    
  79. 
    
  80. .. class:: GeometryCollectionField
    
  81. 
    
  82. Form widgets
    
  83. ============
    
  84. 
    
  85. .. module:: django.contrib.gis.forms.widgets
    
  86.     :synopsis: GeoDjango widgets API.
    
  87. 
    
  88. GeoDjango form widgets allow you to display and edit geographic data on a
    
  89. visual map.
    
  90. Note that none of the currently available widgets supports 3D geometries, hence
    
  91. geometry fields will fallback using a ``Textarea`` widget for such data.
    
  92. 
    
  93. Widget attributes
    
  94. -----------------
    
  95. 
    
  96. GeoDjango widgets are template-based, so their attributes are mostly different
    
  97. from other Django widget attributes.
    
  98. 
    
  99. 
    
  100. .. attribute:: BaseGeometryWidget.geom_type
    
  101. 
    
  102.     The OpenGIS geometry type, generally set by the form field.
    
  103. 
    
  104. .. attribute:: BaseGeometryWidget.map_height
    
  105. .. attribute:: BaseGeometryWidget.map_width
    
  106. 
    
  107.     Height and width of the widget map (default is 400x600).
    
  108. 
    
  109. .. attribute:: BaseGeometryWidget.map_srid
    
  110. 
    
  111.     SRID code used by the map (default is 4326).
    
  112. 
    
  113. .. attribute:: BaseGeometryWidget.display_raw
    
  114. 
    
  115.     Boolean value specifying if a textarea input showing the serialized
    
  116.     representation of the current geometry is visible, mainly for debugging
    
  117.     purposes (default is ``False``).
    
  118. 
    
  119. .. attribute:: BaseGeometryWidget.supports_3d
    
  120. 
    
  121.     Indicates if the widget supports edition of 3D data (default is ``False``).
    
  122. 
    
  123. .. attribute:: BaseGeometryWidget.template_name
    
  124. 
    
  125.     The template used to render the map widget.
    
  126. 
    
  127. You can pass widget attributes in the same manner that for any other Django
    
  128. widget. For example::
    
  129. 
    
  130.     from django.contrib.gis import forms
    
  131. 
    
  132.     class MyGeoForm(forms.Form):
    
  133.         point = forms.PointField(widget=
    
  134.             forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500}))
    
  135. 
    
  136. Widget classes
    
  137. --------------
    
  138. 
    
  139. ``BaseGeometryWidget``
    
  140. 
    
  141. .. class:: BaseGeometryWidget
    
  142. 
    
  143.     This is an abstract base widget containing the logic needed by subclasses.
    
  144.     You cannot directly use this widget for a geometry field.
    
  145.     Note that the rendering of GeoDjango widgets is based on a template,
    
  146.     identified by the :attr:`template_name` class attribute.
    
  147. 
    
  148. ``OpenLayersWidget``
    
  149. 
    
  150. .. class:: OpenLayersWidget
    
  151. 
    
  152.     This is the default widget used by all GeoDjango form fields.
    
  153.     ``template_name`` is ``gis/openlayers.html``.
    
  154. 
    
  155.     ``OpenLayersWidget`` and :class:`OSMWidget` use the ``openlayers.js`` file
    
  156.     hosted on the ``cdnjs.cloudflare.com`` content-delivery network. You can
    
  157.     subclass these widgets in order to specify your own version of the
    
  158.     ``OpenLayers.js`` file in the ``js`` property of the inner ``Media`` class
    
  159.     (see :ref:`assets-as-a-static-definition`).
    
  160. 
    
  161. ``OSMWidget``
    
  162. 
    
  163. .. class:: OSMWidget
    
  164. 
    
  165.     This widget uses an OpenStreetMap base layer to display geographic objects
    
  166.     on. Attributes are:
    
  167. 
    
  168.     .. attribute:: template_name
    
  169. 
    
  170.         ``gis/openlayers-osm.html``
    
  171. 
    
  172.     .. attribute:: default_lat
    
  173.     .. attribute:: default_lon
    
  174. 
    
  175.         The default center latitude and longitude are ``47`` and ``5``,
    
  176.         respectively, which is a location in eastern France.
    
  177. 
    
  178.     .. attribute:: default_zoom
    
  179. 
    
  180.         The default map zoom is ``12``.
    
  181. 
    
  182.     The :class:`OpenLayersWidget` note about JavaScript file hosting above also
    
  183.     applies here. See also this `FAQ answer`_ about ``https`` access to map
    
  184.     tiles.
    
  185. 
    
  186.     .. _FAQ answer: https://help.openstreetmap.org/questions/10920/how-to-embed-a-map-in-my-https-site