GeoDjango: Add MultiLineStringField geometries to OpenLayers slippy map

المشرف العام

Administrator
طاقم الإدارة
I have successfully added point features from PostGIS to a simple OpenLayers map like so:

model.py

class Waypoint(models.Model): name = models.CharField(max_length=32) geometry = models.PointField(srid=4326) objects = models.GeoManager() views.py

def index(request): waypoints = Waypoint.objects.order_by('name') template = loader.get_template('waypoints/index.html') context = RequestContext(request, { 'waypoints': waypoints, }) return HttpResponse(template.render(context)) index.html snippet:

(function() { var vectorSource = new ol.source.Vector({ // create empty vector source }); //create a feature for each waypoint {% for waypoint in waypoints %} var iconFeature = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.transform([ {{waypoint.geometry.x}}, {{waypoint.geometry.y}} ], 'EPSG:4326', 'EPSG:3857')), name: '{{waypoint.name}}', }); vectorSource.addFeature(iconFeature); {% endfor %} // attach the vector source to the layer var vectorLayer = new ol.layer.Vector({ source: vectorSource, }); var map = new ol.Map({ layers: [ new ol.layer.Tile({source: new ol.source.OSM()}), vectorLayer ], target: document.getElementById('map'), view: new ol.View({ center: [-381898.600, 7556003.294], zoom: 9 }) }); I have been trying to use the similar logic to add MultiLineStringField geometries to the map. However, I cannot work out how to access the line coordinates from the model in my template.

I would have thought it would be something like this in my template:

{{my_line_field.geometry.coords}}

This doesn't work and I cannot find any good documentation for the MultiLineStringField class.

If someone could point me to some documentation for this or provide an example, that would be appreciated.

Thanks



أكثر...
 
أعلى