GeoJSON
串行器¶GeoDjango为GeoJSON格式提供特定的序列化程序。 有关序列化的更多信息,请参见Serializing Django objects。
geojson
序列化程序不适用于往返数据,因为它没有解串器等效。 例如,您不能使用loaddata
重新加载此序列化产生的输出。 如果您计划重新加载输出的数据,请改用明文json serializer。
除了serializers.serialize()
序列化程序的选项,geojson
序列化程序在由json
调用时接受以下附加选项:
geometry_field
:包含要用于GeoJSON功能的geometry
键的几何字段名称的字符串。 仅当您具有包含多个几何字段的模型,并且不想使用第一个已定义的几何字段(默认情况下,选择第一个几何字段)时,才需要这样做。srid
:用于geometry
内容的SRID。 默认为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',
'geometry': {
'type': 'Point',
'coordinates': [-87.650175, 41.850385]
},
'properties': {
'name': 'Chicago'
}
}
]
}
当没有指定fields
参数时,geojson
串行器将pk
键添加到properties
字典中,其中主键的对象作为值。
The pk
key was added to the properties
dictionary.
2017年9月6日