在 GeoDjango 的自定义视图中渲染 GeoQuerySet 的空间数据

发布于 2024-09-05 03:27:12 字数 272 浏览 11 评论 0原文

我刚刚开始我的第一个 GeoDjango 项目。

事实上,通过 GeoDjango 支持的管理应用程序,我们都可以很好地查看/编辑与当前对象关联的空间数据。

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

我将非常感谢经验丰富的 GeoDjango 程序员的任何提示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光沙漏 2024-09-12 03:27:12

这个问题分为两部分:

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

生成地理数据

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

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

另一种选择是使用库来帮助您:具体来说,是矢量格式。 (Google“featureserver 矢量格式”以获取信息,因为我只能包含一个超链接。)通过 PyPI/easy_install 矢量格式安装,您可以使用 Django 格式:

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

可以通过 HTTPResponse 返回此字符串以返回 GeoJSON 对象。因此,您的视图会将这 4 行包装在生成查询集(此处为 qs)的位中,然后返回带有字符串的 HttpResponse。

使用数据

OpenLayers 有可以读取数据的“格式”对象:有 GeoJSON 和 KML 以及其他格式。

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

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

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

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

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

放在一起

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

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文