如何在geodjango中使用OpenStreetMap的openlayers显示数据?

时间:2009-02-18 01:03:41

标签: python mapping openlayers geodjango

我已经使用管理员应用openlayersOpenStreetMaps运行geodjango。

现在我想写一些视图来显示数据。基本上,我只是想在地图上添加一个点列表(在管理员中看到)。

Geodjango似乎使用特殊 openlayers.js文件在管理员中实现了它的魔力。有没有一种与此接口的好方法?

如何编写视图/模板以在打开的街道地图窗口中显示geodjango数据,如管理员所示?

目前,我正在挖掘openlayers.js文件和api寻找'简单'的解决方案。 (我没有经验,所以这需要一些时间。)

我能看到的当前方法是将以下内容添加为模板,并使用django添加显示点所需的代码。 (基于示例here

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Draw Feature Example</title>

        <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
      <script type="text/javascript">
            var map;

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

                /*
                 * Layer style
                 */
                // we want opaque external graphics and non-opaque internal graphics
                var layer_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
                layer_style.fillOpacity = 0.2;
                layer_style.graphicOpacity = 1;

                /*
                 * Blue style
                 */
                var style_blue = OpenLayers.Util.extend({}, layer_style);
                style_blue.strokeColor = "blue";
                style_blue.fillColor = "blue";
                style_blue.graphicName = "star";
                style_blue.pointRadius = 10;
                style_blue.strokeWidth = 3;
                style_blue.rotation = 45;
                style_blue.strokeLinecap = "butt";

                var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: layer_style});

                // create a point feature
                var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
                var pointFeature = new OpenLayers.Feature.Vector(point,null,style_blue);
                // Add additional points/features here via django

                map.addLayer(vectorLayer);
                map.setCenter(new OpenLayers.LonLat(point.x, point.y), 5);
                vectorLayer.addFeatures([pointFeature]);
            }
        </script>
    </head>
    <body onload="init()">
        <div id="map" class="smallmap"></div>
    </body>
</html>

它是如何完成的,还是有更好的方法?

5 个答案:

答案 0 :(得分:4)

另一种解决方案是创建一个使用GeoDjango Admin小部件的表单。

要做到这一点,我:

设置GeneratePolygonAdminClass:

class GeneratePolygonAdmin(admin.GeoModelAdmin):
    list_filter=('polygon',)
    list_display=('object', 'polygon')

表单的构建位置:

geoAdmin=GeneratePolygonAdmin(ModelWithPolygonField, admin.site)
PolygonFormField=GeneratePolygon._meta.get_field('Polygon')
PolygonWidget=geoAdmin.get_map_widget(PolygonFormField)
Dict['Polygon']=forms.CharField(widget=PolygonWidget())  #In this case, I am creating a Dict to use for a dynamic form

填充表单的小部件:

def SetupPolygonWidget(form, LayerName, MapFileName, DefaultPolygon=''):
    form.setData({'Polygon':DefaultPolygon})
    form.fields['Polygon'].widget.params['wms_layer']=LayerName
    form.fields['Polygon'].widget.params['wms_url']='/cgi-bin/mapserv?MAP=' + MapFileName
    form.fields['Polygon'].widget.params['default_lon']=-80.9
    form.fields['Polygon'].widget.params['default_lat']=33.7
    form.fields['Polygon'].widget.params['default_zoom']=11
    form.fields['Polygon'].widget.params['wms_name']=YOURWMSLayerName
    form.fields['Polygon'].widget.params['map_width']=800
    form.fields['Polygon'].widget.params['map_height']=600
    form.fields['Polygon'].widget.params['map_srid']=YOUR_SRID
    form.fields['Polygon'].widget.params['modifiable']=True
    form.fields['Polygon'].widget.params['map_options']={}
    form.fields['Polygon'].widget.params['map_options']['buffer'] = 0   
    return form

基于以下代码: http://code.djangoproject.com/browser/django/branches/gis/django/contrib/gis/admin/options.py?rev=7980

看起来你可以使用extra_js选项来包含OpenStreetMap(我还没有测试过这个)。

答案 1 :(得分:2)

我认为您的解决方案是可行的,可能是最简单的方法。只需模板化javascript并使用Django在渲染模板时注入数据点。

如果你想变得更加漂亮,你可以拥有一个Django视图,将数据点作为JSON(application / json)提供,然后使用AJAX根据浏览器中发生的事件回调和检索数据。如果您希望您的应用程序在OpenLayers提供的内容之外具有高度交互性,那么这可能会增加复杂性,但当然这一切都取决于您的应用程序的需求。

答案 2 :(得分:2)

这是相当古老的,我不会像我最初想的那样创建模板黑客。现在我将leaflet.js与ajax请求一起使用到django视图,该视图将geojson返回到传单geojson层。

这使得django方面非常容易。

示例Django视图:

# -*- coding: utf-8 -*-
'''
'''
import json
from django.http import HttpResponse, HttpResponseBadRequest
from django.contrib.gis.geos import Polygon

from models import ResultLayer, MyModel

def get_layer_polygons(request, layer_id):
    """
    Return the polygons for the given bbox (bounding box)
    """
    layer = ResultLayer.objects.get(id=layer_id)    
    bbox_raw = request.GET.get("bbox", None)

    # Make sure the incoming bounding box is correctly formed!
    bbox = None
    if bbox_raw and bbox_raw.count(",") == 3:        
        bbox = [float(v) for v in bbox_raw.split(",")]     
    if not bbox:
        msg = "Improperly formed or not given 'bbox' querystring option, should be in the format '?bbox=minlon,minlat,maxlon,maxlat'"
        return HttpResponseBadRequest(msg)

    bbox_poly = Polygon.from_bbox(bbox)
    bbox_poly.srid = 900913 # google
    bbox_poly.transform(layer.srid) # transform to the layer's srid for querying  

    bin_size = int(bin_size)
    # build vector polygons from bin
    results = MyModel.objects.filter(layer=layer, poly__intersects=bbox_poly).transform(900913, field_name="poly")
    geojson_data = []
    for r in results:
        # loading json in order to dump json list later
        gjson = r.poly.geojson
        py_gjson = json.loads(gjson)
        geojson_data.append(py_gjson)
    return HttpResponse(json.dumps(geojson_data), mimetype='application/json')

答案 3 :(得分:1)

您可以考虑使用FloppyForms。最后,我通常最终根据自己的需求定制解决方案,但这是一个很好的入门方式。

答案 4 :(得分:0)

从geodjango-basic-apps项目中查看本教程:
http://code.google.com/p/geodjango-basic-apps/wiki/FOSS4GWorkshop

也许你不必破解自己的javascript