我最近一直在学习使用GeoDjango,并且我一直在通过一些教程来尝试了解如何将所有内容放在一起。我也是GIS新手,但对Django比较舒服。GeoDjango查询集对象
我已明确了沿着这个教程,如果不是有点过时,这是伟大如下:https://code.google.com/p/geodjango-basic-apps/wiki/FOSS4GWorkshop
我已到第8,更新Django的东西在那里我可以,也试图让周围的弃用OpenLayers的东西,但我碰到了一堵墙。
本教程使用了下面的代码生成一个查询集,并将其发送到它是由OpenLayers.js消耗模板:
教程的view.py:
def ward(request, id):
ward = get_object_or_404(Ward, pk=id)
interesting_points = InterestingLocation.objects.filter(
geometry__intersects=(ward.geometry))
return render_to_response("ward.html", {
'ward': ward, 'interesting_points': interesting_points })
教程的的OpenLayers代码(不完整):
geojson_format = new OpenLayers.Format.GeoJSON()
ward = geojson_format.read({{ ward.geometry.geojson|safe}})[0];
// We mark it 'safe' so that Django doesn't escape the quotes.
ward.attributes = { 'name': "{{ward.name}}", 'type': 'ward'};
vectors = new OpenLayers.Layer.Vector("Data");
vectors.addFeatures(ward);
我写了以下ng代码,但我不断收到错误信息(js console)“Object has not method'replace'”。
我view.py
def interesting_area(request, iso3_id):
iso3_id = iso3_id.upper()
country = get_object_or_404(WorldBorder, iso3=iso3_id)
interesting_points = InterestingLocation.objects.filter(
geometry__intersects=(country.mpoly))
return render_to_response("some_places.html", {
'country': country,
'interesting_points': interesting_points})
我openlayers.js试图
function map_init() {
json_format = new OpenLayers.Format.GeoJSON();
countryson = json_format.read({{country.mpoly.geojson|safe}})[0];
countryson.attributes = {'name': "{{country.name}}",
'area': "{{country.area}}",
'population': "{{country.pop2005}}",
'type': 'country'};
vectors = new OpenLayers.Layer.Vector("Data");
vectors.addFeatures(countryson);
var map = new OpenLayers.Map('map-container');
var base_layer = new OpenLayers.Layer.OSM("Base Map", {
"tileOptions": { "crossOriginKeyword": null }
});
map.addLayers([base_layer, vectors]);
map.zoomToMaxExtent(countryson.geometry.getBounds());
}
我相信错误是在该行countryson = json_format.read({{country.mpoly.geojson|safe}})[0];
有谁知道如何发送出模型对象,并能够在模板端读取其geometry.geojson属性?我已经看到了如何通过使用返回静态文件的视图/ url来实现这一点,但我希望能够通过直接向模板返回数据来实现这一点。
脚注:我已经看到一些其他答案,说使用vectorformats,但它似乎应该有一种方式来在GeoDjango本地做到这一点,但与我的谷歌搜索和寻找答案,我似乎无法找到了解人们通常如何做到这一点。
感谢您的帮助。
编辑:
我觉得有点愚蠢,但@ sk1p问我关于JS回溯,当我看着它,它告诉我,负责该错误的行为以下几点:
map.zoomToMaxExtent(countryson.geometry.getBounds());
所以我删除它,错误消失,但我仍然无法让我的地图渲染。我将继续查看返回的对象。
究竟是什么问题? – sk1p
'输入'键使它快速发布。编辑完整的问题。 – erewok
你能获得该js错误的回溯吗?另外,看看生成的html/js:IIRC'.geojson'不会返回完整的GeoJSON对象,您可能需要查看OpenLayers API以查看它是否接受您传递的内容 – sk1p