本文介绍了在 GeoDjango 的自定义视图中渲染 GeoQuerySet 的空间数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了我关于 GeoDjango 的第一个项目.

事实上,使用由 GeoDjango 驱动的管理应用程序,我们都可以查看/编辑与当前对象关联的空间数据.

问题是在填充对象之后我需要在单个地图上一次渲染多个对象的关联几何.我可能会将它实现为模型操作,重定向到自定义视图.我只是不知道如何在视图中包含 OpenLayers 小部件以及如何从我的 GeoQuerySet 中呈现我的复合几何体.

我将非常感谢有经验的 GeoDjango 程序员的任何提示.

解决方案

这个问题的两部分:

  • 如何生成 OpenLayers 可以通过 Django 读取的地理数据?
  • 如何使用 OpenLayers 使用这些数据?

生成地理数据

在 Django 中有几种不同的生成地理数据的方法.内置,您可以在查询集上使用 .kml() 或 .json() 方法;这样做会导致每个返回的实例都有一个 .json 或 .kml 属性,其中包含以字符串形式生成的 Geometry 的 KML 或 JSON.

然后您可以在使用 {{feature.kml}} 或 {{feature.json}} 的模板中使用此输出.(后者有点困难,因为你必须在它命中模板之前手动进行 JSON 编码,有点奇怪的情况.)

另一种选择是使用库来帮助您:特别是矢量格式.(谷歌featureserver vectorformats"获取信息,因为我只能包含一个超链接.)通过 PyPI/easy_install vectorformats 安装,你可以使用 Django 格式:

>>>从 vectorformats.Formats 导入 Django,GeoJSON>>>qs = Model.objects.filter(city="Cambridge")>>>djf = Django.Django(geodjango="geometry", properties=['city', 'state'])>>>geoj = GeoJSON.GeoJSON()>>>s = geoj.encode(djf.decode(qs))>>>印刷

可以通过 HTTPResponse 返回此字符串以返回 GeoJSON 对象.因此,您的视图会将这 4 行包装起来,生成一个查询集(这里是 qs),然后返回一个带有字符串的 HttpResponse.

消费数据

OpenLayers 具有可以读取数据的格式"对象:GeoJSON 和 KML 以及其他格式都有.

您可以使用标准的 XMLHttpRequest 机制加载数据,然后使用某种格式解析它们:

var f = new OpenLayers.Format.GeoJSON();var features = f.read(req.responseText);layer.addFeatures(特征);

或者,您可以使用内置的协议支持来加载远程数据:

 map = new OpenLayers.Map('map');var wms = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0",{层:'基本'});var layer = new OpenLayers.Layer.Vector("GML", {策略:[新的 OpenLayers.Strategy.Fixed()],协议:新的 OpenLayers.Protocol.HTTP({url: "/django/view/json/",格式:新的 OpenLayers.Format.GeoJSON()})});map.addLayers([wms, layer]);map.zoomToExtent(新的OpenLayers.Bounds(-3.92, 44.34, 4.87, 49.55));

您可以在此示例中看到,url"指向您的 Django 视图;包括所有数据加载和根据提供的格式解析数据.(您可以在 OpenLayers 固定行为示例中看到类似的示例/http协议.)

组合起来

  1. 创建一个 Django 视图,使用矢量格式将数据作为 GeoJSON 返回
  2. 创建一个单独的视图,该视图返回一个 HTML 页面,如链接的 OpenLayers 示例,修改内容如代码示例所示.
  3. 该视图为 HTML 页面提供服务,用于加载您的 GeoJSON 数据并对其进行解析.

I have just started my first project on GeoDjango.

As a matter of fact, with GeoDjango powered Admin application we all have a great possibility to view/edit spatial data, associated with the current object.

The problem is that after the objects having been populated I need to render several objects' associated geometry at once on a single map. I might implement it as a model action, redirecting to a custom view. I just don't know, how to include the OpenLayers widget in the view and how to render there my compound geometry from my GeoQuerySet.

I would be very thankful for any hint from an experienced GeoDjango programmer.

解决方案

Two halves of this question:

  • How do I generate Geographic data that OpenLayers can read via Django?
  • How do I consume this data with OpenLayers?

Generating Geographic Data

There are several different ways to generate geographic data in Django. Built in, you can use the .kml() or .json() methods on a queryset; doing so causes each returned instance to have a .json or .kml property which has the KML or JSON of the Geometry generated as a string.

You can then use this output in templates that use the {{feature.kml}} or {{feature.json}}. (The latter is somewhat difficult, because you would have to manually do the JSON encoding before it hit the template, a bit of an odd situation.)

Another option is to use a library to help you: specifically, vectorformats. (Google "featureserver vectorformats" for information, since I can only include one hyperlink.) Installed via PyPI/easy_install vectorformats, you can use the Django format:

>>> from vectorformats.Formats import Django, GeoJSON
>>> qs = Model.objects.filter(city="Cambridge")
>>> djf = Django.Django(geodjango="geometry", properties=['city', 'state'])
>>> geoj = GeoJSON.GeoJSON()
>>> s = geoj.encode(djf.decode(qs))
>>> print s

This string can be returned via an HTTPResponse to return a GeoJSON object. So, your view would wrap these 4 lines in a bit that generated a queryset (qs, here), and then returned an HttpResponse with the string.

Consuming Data

OpenLayers has 'format' objects which can read data: There are formats for GeoJSON and KML, as well as others.

You can load the data using standard XMLHttpRequest mechanisms then parse them with a format:

var f = new OpenLayers.Format.GeoJSON();
var features = f.read(req.responseText);
layer.addFeatures(features);

Alternatively, you can use the built in Protocol support to load remote data:

     map = new OpenLayers.Map('map');
     var wms = new OpenLayers.Layer.WMS(
         "OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0",
         {layers: 'basic'}
     );

     var layer = new OpenLayers.Layer.Vector("GML", {
         strategies: [new OpenLayers.Strategy.Fixed()],
         protocol: new OpenLayers.Protocol.HTTP({
            url: "/django/view/json/",
            format: new OpenLayers.Format.GeoJSON()
         })
     });

     map.addLayers([wms, layer]);
     map.zoomToExtent(new OpenLayers.Bounds(
         -3.92, 44.34, 4.87, 49.55
     ));

You can see in this example, that the 'url' points to your Django view; all the loading of data and parsing it according to the provided format is included. (You can see a similar example in the OpenLayers example for fixed behavior/http protocol.)

Putting it Together

  1. Create a Django view, using vectorformats to return your data as GeoJSON
  2. Create a separate view, which returns an HTML page like the OpenLayers example linked, with the modifications shown in the code sample.
  3. That view serves the HTML page that loads your GeoJSON data and parses it.

这篇关于在 GeoDjango 的自定义视图中渲染 GeoQuerySet 的空间数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:26