hi again. .
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. .
BTW 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 (example)
[<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. . by the way, thank you for entertaining my post. . have a good day!