django - Rendering spatial data of GeoQuerySet in a custom view on GeoDjango -


I have just started my first project on Geodengo.

In fact, with GeoDjango powered, we all have a very good chance to view / edit the spatial data associated with the current object.

The problem is that after the arrival of objects, I have to submit many objects' relevant geometry once on a map I can apply it as a model action , I can redirect to a custom view. I do not know how to open the OpenLayers widget and how to render the geometry of my premises with my GeographicSat.

I would be very grateful to any of the experienced Geodang programmers. / P>

Two parts of this question:

  • How do I generate geographic data Can Open Layer Read Through Django?
  • How can I use this data with Open Layers?

Generate Geographic Data

Generate Geographic Data in Django Built In, you can use .kml () or .json () methods; By doing so each .json or .kml property is due to recycling, which has KML or JSON of Gemetry in the form of a string.

Then you can use this output in a template that {{feature .kml}} or {{feature.json}}. (There is some difficulty later, because you have to do Jason encoding manually before clicking on the template, a bit of a strange situation.)

Another option is to use a library to help you : In particular, vectorformats (Google "FEATURES Vector Formats" for information), because I can only include one hyperlink.) PPI / Easy-installed through the application vectorform , You can use the DJ format:

  & gt; & Gt; & Gt; From Vector Formats Import farms Déjengo, GOJSONA & gt; & Gt; & Gt; Qs = Model.objects.filter (city = "cambridge")> gt; & Gt; & Gt; Djf = Django.Django (geodjango = "geometry", property = ['city', 'state'])> gt; & Gt; & Gt; Geoj = GeoJSON.GeoJSON ()> & gt; & Gt; & Gt; S = geoj.encode (djf.decode (qs)) & gt; & Gt; & Gt; Print S  

This string can be returned back to a GeoJSON object through an HTTPResponse. Therefore, your view wraps these 4 lines in a bit which generates a query (QS, here), and then returns a HTTPRPS with the string.

Consumer Data

The 'Format' objects in the open layers, which can read data: There are formats for Geogeson and KML, as well as others.

If you can load the data using the standard XMLHttpRequest system, then you can parse it with the format:

  var f = New OpenLayer.format.Geogesan ( ); Var attribute = feed (req.responseText); Layer.addFeatures (features);  

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

  map = new OpenLayer. Map ('map'); Var wms = new open layer Layers MMS ("Open Layers WMS", "http://labs.metacarta.com/wms/vmap0", {Layers: 'Basic'}); Var Layer = New Open Layer Layers Vector ("GML", {Strategies: [New Open Layers. Strategy. (Fixed), Protocol: New Open Layer. Protocol. HTTP ({URL: "/ Diesjeno / View / Jason /", Format: New OpenLayer format.Googeson ()})}); Map.addLayers ([wms, layer]); Map.zoomToExtent (new openlayer bounce (-3.92, 44.34, 4.87, 49.55));  

In this example you can see that 'url' indicates your Django view; All of the data is included in parsing and parse according to the format provided. (You can see a similar example in.)

Putting it together

  1. Create a gigono view, use your vector format to return your data as GeoJogan To do this
  2. Create a different view, which links an HTML page to the link to the OpenLayer example, which returns the code with modifications shown in the sample.
  3. This view serves as the HTML page that loads your geographic data and parse it.

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -