2016-01-24 21:26:11 +00:00
|
|
|
======================
|
|
|
|
``GeoJSON`` Serializer
|
|
|
|
======================
|
2014-11-08 16:08:12 +00:00
|
|
|
|
|
|
|
.. module:: django.contrib.gis.serializers.geojson
|
2016-02-19 07:31:25 +00:00
|
|
|
:synopsis: Serialization of GeoDjango models in the GeoJSON format.
|
2014-11-08 16:08:12 +00:00
|
|
|
|
2015-07-18 12:50:08 +00:00
|
|
|
GeoDjango provides a specific serializer for the `GeoJSON`__ format. See
|
|
|
|
:doc:`/topics/serialization` for more information on serialization.
|
|
|
|
|
2020-01-29 08:34:37 +00:00
|
|
|
__ https://geojson.org/
|
2014-11-08 16:08:12 +00:00
|
|
|
|
|
|
|
The ``geojson`` serializer is not meant for round-tripping data, as it has no
|
|
|
|
deserializer equivalent. For example, you cannot use :djadmin:`loaddata` to
|
|
|
|
reload the output produced by this serializer. If you plan to reload the
|
|
|
|
outputted data, use the plain :ref:`json serializer <serialization-formats-json>`
|
|
|
|
instead.
|
|
|
|
|
|
|
|
In addition to the options of the ``json`` serializer, the ``geojson``
|
|
|
|
serializer accepts the following additional option when it is called by
|
|
|
|
``serializers.serialize()``:
|
|
|
|
|
|
|
|
* ``geometry_field``: A string containing the name of a geometry field to use
|
|
|
|
for the ``geometry`` key of the GeoJSON feature. This is only needed when you
|
|
|
|
have a model with more than one geometry field and you don't want to use the
|
|
|
|
first defined geometry field (by default, the first geometry field is picked).
|
|
|
|
|
2022-05-28 09:33:15 +00:00
|
|
|
* ``id_field``: A string containing the name of a field to use for the ``id``
|
|
|
|
key of the GeoJSON feature. By default, the primary key of objects is used.
|
|
|
|
|
2014-11-08 16:08:12 +00:00
|
|
|
* ``srid``: The SRID to use for the ``geometry`` content. Defaults to 4326
|
|
|
|
(WGS 84).
|
|
|
|
|
|
|
|
The :ref:`fields <subset-of-fields>` option can be used to limit fields that
|
|
|
|
will be present in the ``properties`` key, as it works with all other
|
|
|
|
serializers.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
from django.core.serializers import serialize
|
|
|
|
from my_app.models import City
|
|
|
|
|
|
|
|
serialize("geojson", City.objects.all(), geometry_field="point", fields=["name"])
|
|
|
|
|
|
|
|
Would output::
|
|
|
|
|
|
|
|
{
|
|
|
|
"type": "FeatureCollection",
|
|
|
|
"crs": {"type": "name", "properties": {"name": "EPSG:4326"}},
|
|
|
|
"features": [
|
|
|
|
{
|
|
|
|
"type": "Feature",
|
2022-05-28 09:33:15 +00:00
|
|
|
"id": 1,
|
2014-11-08 16:08:12 +00:00
|
|
|
"geometry": {"type": "Point", "coordinates": [-87.650175, 41.850385]},
|
|
|
|
"properties": {"name": "Chicago"},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
2016-02-23 18:57:53 +00:00
|
|
|
|
|
|
|
When the ``fields`` parameter is not specified, the ``geojson`` serializer adds
|
|
|
|
a ``pk`` key to the ``properties`` dictionary with the primary key of the
|
|
|
|
object as the value.
|