GeoJSON
串行化器¶geodjango为 `GeoJSON`_ 格式。见 序列化Django对象 有关序列化的详细信息。
这个 geojson
序列化程序不适用于往返数据,因为它没有等效的反序列化程序。例如,您不能使用 loaddata
重新加载此序列化程序生成的输出。如果计划重新加载输出的数据,请使用plain json serializer 相反。
除了 json
串行化器 geojson
序列化程序在被调用时接受以下附加选项 serializers.serialize()
:
geometry_field
:包含要用于 geometry
geojson特性的键。只有当模型具有多个几何字段,并且不想使用第一个定义的几何字段(默认情况下,将拾取第一个几何字段)时,才需要这样做。
id_field
:一个字符串,包含要用于 id
GeoJSON功能的密钥。默认情况下,使用对象的主键。
srid
:要用于 geometry
内容。默认为4326(wgs 84)。
这个 fields 选项可用于限制将出现在 properties
键,因为它与所有其他序列化程序一起工作。
例子::
from django.core.serializers import serialize
from my_app.models import City
serialize("geojson", City.objects.all(), geometry_field="point", fields=["name"])
输出:
{
"type": "FeatureCollection",
"crs": {"type": "name", "properties": {"name": "EPSG:4326"}},
"features": [
{
"type": "Feature",
"id": 1,
"geometry": {"type": "Point", "coordinates": [-87.650175, 41.850385]},
"properties": {"name": "Chicago"},
}
],
}
当 fields
未指定参数, geojson
序列化程序添加 pk
关键 properties
以对象的主键为值的字典。
7月 22, 2024