1

I have a codes that will convert polygon shapes that is stored in the database into kml format and count all the rows in it and display it to the browser.

models.py #my model in creating fields in my table

from django.db import models
from django.contrib.gis.db import models as gismodels

class Butuan_Parcel(gismodels.Model): 
NewPin = models.CharField(max_length=254)
geometry = gismodels.MultiPolygonField(srid=32651)
objects = gismodels.GeoManager()

def __unicode__(self):
    return self.NewPin;

views.py

from django.shortcuts import render_to_response
from django.contrib.gis.shortcuts import render_to_kml
from butuan_parcel.models import Butuan_Parcel

def all_kml(request) : #functions that convert to kml format
locations = Butuan_Parcel.objects.kml()
return render_to_kml("gis/kml/placemarks.kml", {'places' : locations})

def map_page(request) : #functions that count rows stored in shapefile table
lcount = Butuan_Parcel.objects.all().count()
return render_to_response('butuan_parcel/map.html', { 'location_count' : lcount})

urls.py

from django.conf.urls import * 
from butuan_parcel.views import * 
from django.views.static import *
from django.conf import settings    
from django.contrib import admin 

admin.autodiscover() 
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^kml/', all_kml), #calling the all_kml functions from views.py 
(r'^$', map_page)) #calling map_page functions from views.py

map. hmtl

<html>
<body>
var map, base_layer, kml; 
function init() {
 map = new OpenLayers.Map ('map'); 
 base_layer = new OpenLayers.Layer.WMS ("Openlayers WMS", "http://labs.metacarta.com/wms/vmapo", { layers: 'basic'});
 kml = new Openlayers.Layer.GML("KML", "/kml/", { format: Openlayers.Format.KML }); map.addLayers([base_layer, kml]); map.zoomToMaxExtent(); }
 </script>

There should be a total of {{ location_count }} Locations. To see this data in Google Earth, OPen as KML

</body>
</html>

and this is the output in localhost:8000

There should be a total of 105 Locations. To see this data in Google Earth, OPen as KML

now, i want to query the polygon shapes and display it to the browser, not to count anymore. Is there anyone who knows how to retrieve the polygon and display it to the browser.. :) ?? Your help is very much appreciated.. ,..,

1

First off, you appear to be missing part of your HTML document, as there is no opening <script> tag. Besides that, there are two key things you are missing.

The first is somewhere to display the map. When you pass the parameter 'map' to the OpenLayers.Map constructor, you are actually specifying where on the page to create and display your map. After your script, you should have an empty div with this ID, for OpenLayers to add the map to:

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

The second thing you need is to actually have the browser run your your init() script. The most common method to do this is to add an onLoad tag to your <body> element:

<body onload="init()">

Your whole HTML file should then look something like this:

<html>
    <body onload="init()">
        <script type="text/javascript"> 
            var map, base_layer, kml; 
            function init() {
                map = new OpenLayers.Map ('map'); 
                base_layer = new OpenLayers.Layer.WMS (
                    "Openlayers WMS",
                    "http://labs.metacarta.com/wms/vmapo",
                    { layers: 'basic' }
                );
                kml = new Openlayers.Layer.GML(
                    "KML",
                    "/kml/",
                    { format: Openlayers.Format.KML }
                );
                map.addLayers([base_layer, kml]);
                map.zoomToMaxExtent();
            };
        </script>

        There should be a total of {{ location_count }} Locations.
        To see this data in Google Earth, open as KML.

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

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.