在 GeoDjango 中映射多边形的最简单方法
Easiest method for mapping polygons in GeoDjango
我有一个带有 PosGIS 后端的简单应用程序。我想在数据库中为每个功能创建一个页面。我使用了 slug 和 URL 路由。
PostGIS 要素有一个存储在 geom 列中的多边形,使用 geodjango 将其建模为 MultiPolygonField。
将此 geom 列解析为 geojson 以便我可以将其添加到传单地图的最简单方法是什么?
下面是我的代码,它试图使用 geojson 序列化程序,但我在我的页面上收到了这个错误代码。
Views.py
from django.shortcuts import render
from Countries_App.models import Countries
from django.core.serializers import serialize
# Create your views here.
def show_country(request, country_slug):
# Create a context dictionary which we can pass
# to the template rendering engine.
context_dict = {}
try:
# Can we find a category name slug with the given name?
# If we can't, the .get() method raises a DoesNotExist exception.
# So the .get() method returns one model instance or raises an exception.
country = Countries.objects.get(slug=country_slug)
Country_geojson = serialize('geojson',
Countries.objects.get(slug=country_slug))
# We also add the category object from
# the database to the context dictionary.
# We'll use this in the template to verify that the category exists.
context_dict['Countries'] = country
except KeyError:
country = []
context_dict['Countries'] = Countries
country_slug = none
Country_geojson = serialize('geojson',
Country.objects.filter(name=country_slug))
# Go render the response and return it to the client.
return render(request, 'Countries_App/country.html', {'context_dict':
context_dict, 'Country_geojson': Country_geojson})
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^country/(?P<country_slug>[\w\-]+)/$',
GeoJSONLayerView.as_view(model=Countries), name='data'),
url(r'^country/(?P<country_slug>[\w\-]+)/$', views.show_country,
name='show_country'),
]
摘自我尝试将 geojson 添加到传单地图的模板
var geojsonfeature = '{{ Countries_geojson }}'
L.geoJSON(geojsonFeature).addTo(map);
我的处理方式是否正确?将 PostGIS 多边形几何简单地添加到模板的最简单方法是什么?请记住,我只想添加与 slug 匹配的特征的几何形状。
在django-geojson's documentation中有关于如何在django模板中使用模块标签的参考:
Mainly useful to dump features in HTML output and bypass AJAX call :
// Leaflet JS L.geoJson({{
object_list|geojsonfeature|safe}}).addTo(map);
Will work either for a model, a geometry field or a queryset.
{% load geojson_tags %}
var feature = {{ object|geojsonfeature|safe }};
var geom = {{ object.geom|geojsonfeature|safe }};
var collection = {{ object_list|geojsonfeature|safe }};
Properties and custom geometry field name can be provided.
{{ object|geojsonfeature:"name,age" }}
{{ object|geojsonfeature:"name,age:the_geom" }}
{{ object|geojsonfeature:":geofield" }}
我有一个带有 PosGIS 后端的简单应用程序。我想在数据库中为每个功能创建一个页面。我使用了 slug 和 URL 路由。
PostGIS 要素有一个存储在 geom 列中的多边形,使用 geodjango 将其建模为 MultiPolygonField。
将此 geom 列解析为 geojson 以便我可以将其添加到传单地图的最简单方法是什么?
下面是我的代码,它试图使用 geojson 序列化程序,但我在我的页面上收到了这个错误代码。
from django.shortcuts import render
from Countries_App.models import Countries
from django.core.serializers import serialize
# Create your views here.
def show_country(request, country_slug):
# Create a context dictionary which we can pass
# to the template rendering engine.
context_dict = {}
try:
# Can we find a category name slug with the given name?
# If we can't, the .get() method raises a DoesNotExist exception.
# So the .get() method returns one model instance or raises an exception.
country = Countries.objects.get(slug=country_slug)
Country_geojson = serialize('geojson',
Countries.objects.get(slug=country_slug))
# We also add the category object from
# the database to the context dictionary.
# We'll use this in the template to verify that the category exists.
context_dict['Countries'] = country
except KeyError:
country = []
context_dict['Countries'] = Countries
country_slug = none
Country_geojson = serialize('geojson',
Country.objects.filter(name=country_slug))
# Go render the response and return it to the client.
return render(request, 'Countries_App/country.html', {'context_dict':
context_dict, 'Country_geojson': Country_geojson})
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^country/(?P<country_slug>[\w\-]+)/$',
GeoJSONLayerView.as_view(model=Countries), name='data'),
url(r'^country/(?P<country_slug>[\w\-]+)/$', views.show_country,
name='show_country'),
]
摘自我尝试将 geojson 添加到传单地图的模板
var geojsonfeature = '{{ Countries_geojson }}'
L.geoJSON(geojsonFeature).addTo(map);
我的处理方式是否正确?将 PostGIS 多边形几何简单地添加到模板的最简单方法是什么?请记住,我只想添加与 slug 匹配的特征的几何形状。
在django-geojson's documentation中有关于如何在django模板中使用模块标签的参考:
Mainly useful to dump features in HTML output and bypass AJAX call :
// Leaflet JS L.geoJson({{ object_list|geojsonfeature|safe}}).addTo(map);
Will work either for a model, a geometry field or a queryset.
{% load geojson_tags %} var feature = {{ object|geojsonfeature|safe }}; var geom = {{ object.geom|geojsonfeature|safe }}; var collection = {{ object_list|geojsonfeature|safe }};
Properties and custom geometry field name can be provided.
{{ object|geojsonfeature:"name,age" }} {{ object|geojsonfeature:"name,age:the_geom" }} {{ object|geojsonfeature:":geofield" }}