문제 설명
Geodjango GeoJSON 직렬 변환기 기하학은 항상 'null'입니다. (Geodjango GeoJSON Serializer geometry always 'null')
GeoDjango와 GeoJSON serlizer의 친구들입니다. 저는 공식 GeoDjango 튜토리얼을 따르고 있었습니다: https://docs.djangoproject. com/en/1.8/ref/contrib/gis/tutorial/
결국에는 name
의 국가로 가득 찬 PostgreSQL + PostGIS 데이터베이스가 있습니다. , iso3
코드 등이 있습니다. 특히 mpoly
의 지오메트리는 MultiPolygon(wkb에 저장됨)으로 표시됩니다. GeoDjango를 사용하여 데이터베이스에서 항목을 검색하고 싶습니다. 나는 그것을 위해 고군분투하고 있다.
나는 한 개체의 속성을 차례로 검색할 수 있다:
from django.http import HttpResponse
from django.shortcuts import render
from django.core.serializers import serialize
from AppName.models import WorldBorder
[...]
WorldBorder.objects.filter(name='Germany')[0].name # "Germany"
WorldBorder.objects.filter(name='Germany')[0].iso3 # "DEU"
WorldBorder.objects.filter(name='Germany')[0].mpoly.geojson # long & correct output
데이터가 데이터베이스에 올바르게 저장되고 개체 속성을 검색할 수 있습니다. 이제 해당 국가에 대한 전체 geojson 파일을 얻고 싶습니다. Django는 이를 위해 GeoJSON 직렬 변환기를 만들었습니다. https://docs.djangoproject .com/en/1.8/ref/contrib/gis/serializers/
설명된 방식으로 사용하는 경우:
serialize('geojson',
WorldBorder.objects.filter(name='Germany'),
geometry_field='mpoly',
fields=('name',)
)
이 출력을 얻습니다:
u'{"type": "FeatureCollection", "crs":{"type": "name", "properties": {"name": "EPSG:4326"}},
"features": [{"geometry": null,"type": "Feature",
"properties":{"name": "Germany" }}]}'
저를 미치게 만드는 것은 "geometry": null
그래서 기하학이 아닌 모든 것을 직렬화합니다. 왜 그런 겁니까? 내가 뭘 잘못하고 있죠? 특히? GeoDjango를 사용하여 GeoJSON 형식의 PostGIS 데이터베이스에서 지오메트리를 가져오려면 어떻게 해야 합니까? 도움을 주시면 감사하겠습니다.
감사합니다 :)
참조 솔루션
방법 1:
In case anyone else runs into this issue:
It seems the problem is in Django 1.8, geometry has to be passed in the fields for it be serialized.
More here https://code.djangoproject.com/ticket/26138
방법 2:
If anybody is still interested in the answer. After a Django update I could fix it by using the normal serializer from the django packages.
from django.core.serializers import serialize
and then serialized using the 'geojson'
option:
serialize('geojson',
WorldBorder.objects.filter(name='Germany'),
geometry_field='geom',
fields=('id', 'name', 'other_properties_you_want')
and it worked like a charm! Except for the fact that the id
did not get serialized.
(by MenschMarcus、artsim、MenschMarcus)