2

. i am trying to display a feature queried from postgis. . i also designed a user input field to enter the id of the desired feature to be displayed. . i have already queried the attributes first before displaying the feature polygon. . and the attributes were already displayed on the template. .however, while fetching the polygon, i got error. .

i do not know why this happens. .i have already displayed it before but not querying it by id, i have already displayed the whole shapefile from the postgis. .

This is my views.py

from django.shortcuts import render, redirect
from vectorformats.Formats import Django, GeoJSON
from Apps.models import Butuan_Parcel, AGAO
def search(request):

query = request.GET.get('q')
try:
    query = int(query)

except ValueError:
    query = None
    results = None


if query:
    results = Butuan_Parcel.objects.get(id=query)
   # obs = Butuan_Parcel.objects.all()
    context = RequestContext(request)

s_layer = Butuan_Parcel.objects.filter(id=query)
s_feat = Django.Django(geodjango='geometry', properties=['barangay'])
s_geo = GeoJSON.GeoJSON()
s_parcel = s_geo.encode(s_feat.decode(s_layer.transform(3857)))

# for ob in obs:
#     if query is not ob:
#        return render(request, 'noresults.html')
#    else:
return render(request, 'results.html', 'results':results,'s_parcel':s_parcel, 's_layer':s_layer}, context_instance=context)

and this is my results.html

<head>
<script type="text/javascript">
    var map;

    function init(){

    map = new OpenLayers.Map('map',{
            });


       base_layer =  new OpenLayers.Layer.OSM("OpenStreetMap");

       map.addLayer(base_layer);
/****************************map style********************************************/
    var styleMap = new OpenLayers.StyleMap(OpenLayers.Util.applyDefaults
    (
            {
                fillColor: "yellow", fillOpacity:0.25, strokeColor: "red"
            },
    OpenLayers.Feature.Vector.style["default"])
    );
/****************************Vector layers********************************************/
    var layer = new OpenLayers.Layer.Vector("Butuan",{
        styleMap:styleMap
        }
    );

    map.addLayer(layer);
    var format = new OpenLayers.Format.GeoJSON();

    {% for i in s_layer %}
    var feat = format.read({{i|safe}});
    {% endfor %}

    layer.addFeatures(feat);

/****************************map properties********************************************/
    map.setCenter(new OpenLayers.LonLat(13976068.3513, 995715.692078),11);
    map.addControl(new OpenLayers.Control.LayerSwitcher());
   }


</script>
</head>

<body onload = "init()">

    <div id="map"></div>

{% block content %}

<form method="get" action="/search/">
<table>
    <tr>

        <td>Search Barangay:</td>
        <td><input type="text" name="q" id="id_q" value="{{ query }}"/></td>
        <td><input type="submit" value="Search" /></td>

    </tr>
</table>
<br/>


<table>
    <tr>
        <td>ID</td>
        <td>:</td>

        <td>{{ results.id|safe }}</td>

    </tr>
    <tr>
        <td>Barangay</td>
        <td>:</td>
        <td>{{ results.barangay|safe }}</td>
    </tr>

{% for i in s_layer %}
  {{ i }}

{% endfor %}
    </table>
</form>




{% endblock %}

</body>

i tried to display whether my s_layer has data or not, that is why i have

{% for i in s_layer %}
  {{ i }}
{% endfor %}

and this displays a right data. . MAUG and when i tried to display {{ s_layer }} this gives me [< Butuan_Parcel: MAUG >]

when i code {{s_layer|safe}} i just have [] as the output.. .

on the other hand, i used

{% for i in s_layer %}
    var feat = format.read({{i|safe}});
    {% endfor %}

so that real data could be parsed into geojson and be displayed in the map. . however, there was no vector layer displayed on the map. . these code chunks I have are seen on the whole code above. . i cannot display this vector layer.

Can someone give me hints on how to display this on the map?

I got a screenshot and this is the output enter image description here

I wanted to display the output such as this.. enter image description here

  • You said you got an error "while fetching the polygon." What is the error? Please copy/paste the output. – Arthur Mar 11 '14 at 16:47
  • when i entered "24" on the search textbox, the output displays as ID:24, Barangay:MAUG, this data is queried thru results = Butuan_Parcel.objects.get(id=query) and I tried to query its corresponding polygon thru s_layer = Butuan_Parcel.objects.filter(id=query) . so when i tried to display {{s_layer}} to know if have fetch data, it prints [<Butuan_Parcel: NONG-NONG>] only, when i put {{s_layer|safe}} it just prints []. – vklopt Mar 12 '14 at 0:28
  • when i code {%for i in s_layer %} {{ i }} {% endfor %} I got an output MAUG. . therefore when i parse this data into geojson i put '{% for i in s_layer %} var feat = format.read({{i|safe}}); {% endfor %}' but there was no polygon displayed on the map. . how would i do that? – vklopt Mar 12 '14 at 0:33
  • [<Butuan_Parcel: NONG-NONG>] is the object representation in Python of a GeoQuerySet. Django doesn't have a way of represent this object in the template language. You need to access the underlying attributes (geometry, in this case) in formats that can be serialized to text; here is a list of the built-in output formats with GeoDjango. – Arthur Mar 12 '14 at 12:38
  • hi @Arthur actually i did s_layer = Butuan_Parcel.objects.filter(id=query) s_feat = Django.Django(geodjango='geometry', properties=['barangay']) s_geo = GeoJSON.GeoJSON() s_parcel = s_geo.encode(s_feat.decode(s_layer.transform(3857))) this code is to retrieve that polygon from the postgis using the parameter (actually, it's a user input). i used vectorformats to do the geojson conversion. however, i did not display the polygon . . what could be the problem? – vklopt Mar 13 '14 at 2:21
1

I see a typo in your code on the return render ... you have a closing } that does not open anywhere (missing { ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.