Geodjango GeoJSON 직렬 변환기 기하학은 항상 'null'입니다. (Geodjango GeoJSON Serializer geometry always 'null')


문제 설명

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 MenschMarcusartsimMenschMarcus)

참조 문서

  1. Geodjango GeoJSON Serializer geometry always 'null' (CC BY‑SA 2.5/3.0/4.0)

#geojson #serialization #geodjango #postgis






관련 질문

Geodjango GeoJSON 직렬 변환기 기하학은 항상 'null'입니다. (Geodjango GeoJSON Serializer geometry always 'null')

전단지에서 레이어 켜기/끄기(더 복잡한 시나리오) (Toggle layers on and off in Leaflet (more complex scenario))

RethinkDB r.polygon() - GeoJSON LinearRing에는 최소 4개의 위치가 있어야 합니까? (RethinkDB r.polygon() - GeoJSON LinearRing must have at least four positions?)

Leaflet : GeoJSON 속성에서 GeoJSON 레이어 설정 아이콘 (Leaflet : setting icon for GeoJSON layer from GeoJSON property)

'GeoJsonLayer' 기호를 확인할 수 없습니다. (Cannot resolve symbol 'GeoJsonLayer ')

스키마의 mongoose geojson, "지역 키를 추출할 수 없습니다" 오류 (mongoose geojson in schema, "Can't extract geo keys" error)

Android Google 지도는 GeoJSON을 사용하여 마커를 설정합니다. (Android Google Maps set marker using GeoJSON)

GraphQl로 geojson 포인트를 쿼리하는 방법은 무엇입니까? (How to query a geojson point with GraphQl?)

geojson 포인트 데이터 마커가 전단지 맵에서 클러스터링되지 않습니다. (The geojson point data markers are not clustering in leaflet map)

전단지 geoJSON.onEachFeature는 함수가 아닌가요? (leaflet geoJSON.onEachFeature is not a function?)

Folium에서 특정 국가 강조 표시 (Highlight one specific country in Folium)

RGeo 및 Geojson으로 면적 계산 (Calculating area with RGeo and Geojson)







코멘트