1. ======================
    
  2. ``GeoJSON`` Serializer
    
  3. ======================
    
  4. 
    
  5. .. module:: django.contrib.gis.serializers.geojson
    
  6.     :synopsis: Serialization of GeoDjango models in the GeoJSON format.
    
  7. 
    
  8. GeoDjango provides a specific serializer for the `GeoJSON`__ format. See
    
  9. :doc:`/topics/serialization` for more information on serialization.
    
  10. 
    
  11. __ https://geojson.org/
    
  12. 
    
  13. The ``geojson`` serializer is not meant for round-tripping data, as it has no
    
  14. deserializer equivalent. For example, you cannot use :djadmin:`loaddata` to
    
  15. reload the output produced by this serializer. If you plan to reload the
    
  16. outputted data, use the plain :ref:`json serializer <serialization-formats-json>`
    
  17. instead.
    
  18. 
    
  19. In addition to the options of the ``json`` serializer, the ``geojson``
    
  20. serializer accepts the following additional option when it is called by
    
  21. ``serializers.serialize()``:
    
  22. 
    
  23. * ``geometry_field``: A string containing the name of a geometry field to use
    
  24.   for the ``geometry`` key of the GeoJSON feature. This is only needed when you
    
  25.   have a model with more than one geometry field and you don't want to use the
    
  26.   first defined geometry field (by default, the first geometry field is picked).
    
  27. 
    
  28. * ``srid``: The SRID to use for the ``geometry`` content. Defaults to 4326
    
  29.   (WGS 84).
    
  30. 
    
  31. The :ref:`fields <subset-of-fields>` option can be used to limit fields that
    
  32. will be present in the ``properties`` key, as it works with all other
    
  33. serializers.
    
  34. 
    
  35. Example::
    
  36. 
    
  37.     from django.core.serializers import serialize
    
  38.     from my_app.models import City
    
  39. 
    
  40.     serialize('geojson', City.objects.all(),
    
  41.               geometry_field='point',
    
  42.               fields=('name',))
    
  43. 
    
  44. Would output::
    
  45. 
    
  46.     {
    
  47.       'type': 'FeatureCollection',
    
  48.       'crs': {
    
  49.         'type': 'name',
    
  50.         'properties': {'name': 'EPSG:4326'}
    
  51.       },
    
  52.       'features': [
    
  53.         {
    
  54.           'type': 'Feature',
    
  55.           'geometry': {
    
  56.             'type': 'Point',
    
  57.             'coordinates': [-87.650175, 41.850385]
    
  58.           },
    
  59.           'properties': {
    
  60.             'name': 'Chicago'
    
  61.           }
    
  62.         }
    
  63.       ]
    
  64.     }
    
  65. 
    
  66. When the ``fields`` parameter is not specified, the ``geojson`` serializer adds
    
  67. a ``pk`` key to the ``properties`` dictionary with the primary key of the
    
  68. object as the value.