如何使用geojson从django到传单正确获取坐标



我将一些Shapefile多极子加载到Geodjango中,将它们显示为传单地图上的一个层。但在网站上,没有出现任何图层,只有地图本身。

几何数据存储在Geodjango数据库中,如下所示:

from django.contrib.gis.db import models
class wgo(models.Model):
(some more variables)
poly = models.MultiPolygonField(srid=4326)

我用geojson传递多极子,并像这样序列化:

wcrds = wgo.objects.filter(id=wid)
gridone = serialize('geojson', wcrds.all())
return render(request, 'result.html', {'gridone': gridone})

当我检查页面时,我可以看到geojson数据确实进入了html:

var Hlayer = new L.GeoJSON(
{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "properties": {"wijkcode": "WK036356", "wijknaam": "Middenmeer", "poly": "SRID=4326;MULTIPOLYGON (((4.93714288723798 52.3576900936898, 4.93742729807085 52.3577390397678,)))", "pk": "909"}, "geometry": null}]} 
, {
style: Hstyle
}
);
var mymap = L.map('mapid').setView([52.3701, 4.8967], 13);
var OpenStreetMap_Mapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(mymap);

(为了简洁起见,我去掉了大部分坐标(。geojson似乎没有意识到poly变量就是具有几何体的变量。

我在示例网站上看到了带有方括号的geojson坐标,而在我的网站上看到的是圆形坐标。

我直接通过Geodjango的图层映射导入了shapefile,遵循了Geodjango-tutorial。此外,我使用ogrinfo、ogr2ogr以及Python中的GDAL osgeo查看和操作了这个Shapefile,没有任何问题。

知道我如何让Geodjango和geojson以正确的格式将坐标传递给传单吗?提前谢谢。

这里是我的错误。在序列化geojson时,显然应该告诉它哪个变量包含几何体。代替

gridone = serialize('geojson', wcrds.all())

成功:

gridone = serialize('geojson', wcrds.all(), geometry_field='poly')

它是有效的。阅读文档,正如他们所说。。。

最新更新