在 GeoDjango 的自定义视图中渲染 GeoQuerySet 的空间数据
我刚刚开始我的第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题分为两部分:
生成地理数据
在 Django 中生成地理数据有多种不同的方法。内置的,您可以在查询集上使用 .kml() 或 .json() 方法;这样做会导致每个返回的实例都有一个 .json 或 .kml 属性,其中包含生成为字符串的几何图形的 KML 或 JSON。
然后,您可以在使用 {{feature.kml}} 或 {{feature.json}} 的模板中使用此输出。 (后者有点困难,因为您必须在到达模板之前手动进行 JSON 编码,这有点奇怪。)
另一种选择是使用库来帮助您:具体来说,是矢量格式。 (Google“featureserver 矢量格式”以获取信息,因为我只能包含一个超链接。)通过 PyPI/easy_install 矢量格式安装,您可以使用 Django 格式:
可以通过 HTTPResponse 返回此字符串以返回 GeoJSON 对象。因此,您的视图会将这 4 行包装在生成查询集(此处为 qs)的位中,然后返回带有字符串的 HttpResponse。
使用数据
OpenLayers 有可以读取数据的“格式”对象:有 GeoJSON 和 KML 以及其他格式。
您可以使用标准 XMLHttpRequest 机制加载数据,然后使用某种格式解析它们:
或者,您可以使用内置的协议支持来加载远程数据:
您可以在这个示例中看到,“url”指向您的 Django 视图;包括所有数据加载并根据提供的格式解析数据。 (您可以在 OpenLayers 固定行为/http 示例中看到类似的示例协议。)
放在一起
Two halves of this question:
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:
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:
Alternatively, you can use the built in Protocol support to load remote data:
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