serializers.txt 2.4 KB

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