Rendering multipolygons to kml

1,202 views
Skip to first unread message

Flavio Coelho

unread,
Jan 21, 2009, 9:43:08 AM1/21/09
to geodjango
Hi,

I am new to Geodjango, and I am trying to use it to visualize some
maps I need to work with.

I want to overlay my maps (a polygon layer) on the base google maps
layer, and use the attributes of each polygon to color the maps etc.

I wonder if render_to_kml supports collections of multipolygon fields,
and what kml template I should use.


In case the above function can't handle multipolygons, I'd like to
know what is the recommended way to display a bunch of polygons on
google Maps using geodjango.

thanks in advance,

Flávio

Ariel Mauricio Nunez Gomez

unread,
Jan 21, 2009, 10:06:58 AM1/21/09
to geod...@googlegroups.com
I wonder if render_to_kml supports collections of multipolygon fields,
and what kml template I should use.
Render to kml is just render_to_string with the right mimetype  for Kml files
I don't remember where the kml templates live in contrib.gis, but they are pretty basic, I suggest you go make your own since you are gonna have to customize styles anyway to support your thematic needs.

In case the above function can't handle multipolygons, I'd like to
know what is the recommended way to display a bunch of polygons on
google Maps using geodjango.
Create a marker for each polygon.

for polygon in x.multipoligon:
    ... (add a marker here) 

Ariel.

Derek Willis

unread,
Jan 21, 2009, 10:29:25 AM1/21/09
to geodjango
We've used the .kml() function to render files of multi-polygons for
Google Maps in our app without trouble. The templates are here:

django/contrib/gis/templates/gis/kml

in case you need to modify them, but like Ariel said, you'll probably
end up creating your own should you want to include style changes.

Derek Willis

On Jan 21, 10:06 am, Ariel Mauricio Nunez Gomez

Flavio Coelho

unread,
Jan 21, 2009, 10:45:03 AM1/21/09
to geod...@googlegroups.com
On Wed, Jan 21, 2009 at 3:06 PM, Ariel Mauricio Nunez Gomez <ingenie...@gmail.com> wrote:
I wonder if render_to_kml supports collections of multipolygon fields,
and what kml template I should use.
Render to kml is just render_to_string with the right mimetype  for Kml files
I don't remember where the kml templates live in contrib.gis, but they are pretty basic, I suggest you go make your own since you are gonna have to customize styles anyway to support your thematic needs.

I already started to do this, but I stumbled on not knowing how to get the coordinate list from the objects (as returned by MyModel.objects.all()):

here is how a polygon should be represented in kml:

<Placemark>
<name>Some name</name>
<description>some description;</description>
<Style><PolyStyle><color>#80800000</color><fill>1</fill><outline>1</outline></PolyStyle></Style>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>-43.196591925838263,-22.897680139398773,0 -43.196362794169808,-22.89775202483413,0 -43.196125645784612,-22.898010711498866,0 -43.195790202127043,-22.897983609066173,
...
-43.198007179590775,-22.898279897020856,0 -43.197726669648162,-22.898000439337203,0 -43.1974422846057,-22.898050753172235,0 -43.196591925838263,-22.897680139398773,0</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
</Placemark>

In order to cook my own kml, I need to be able to fetch the coordinates list of the points defining the polygon.

Playing around in the django shell I found the the above KML code is stored in the attribute object.geom.kml:

for example:

listofpols = MyModel.objects.all()
listofpols[0].geom.kml #this gives you the kml above!!!!


thanks for the hints, I think this is all I needed to write a correct kml template for polygons.

Flávio

Ariel.





--
---
Flávio Codeço Coelho

Flavio Coelho

unread,
Jan 21, 2009, 11:14:46 AM1/21/09
to geod...@googlegroups.com
Thanks,

I created my own kml template, and now my url is generating valid kml!!

I have another problem though:

I am generating the google maps widget like this:

from django.contrib.gis.maps.google import GoogleMap

def map_page(request):
    '''render'''
    return render_to_response("map.html",{'google':GoogleMap(kml_urls=['/mapas/rio'])})

which is generating the following javascript:

function gmap_load(){
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(0, 0), 4);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());

var kml1 = new GGeoXml("/mapas/rio");
map.addOverlay(kml1);


However, my overlay is not showing up although the javascript seems to be correct.

any ideas?

thanks,

Flávio


Ariel Mauricio Nunez Gomez

unread,
Jan 21, 2009, 11:19:03 AM1/21/09
to geod...@googlegroups.com

var kml1 = new GGeoXml("/mapas/rio");
map.addOverlay(kml1);

Short version:
GGeoXml expects a public url so Google servers can hit it.

Long version:
Google it :)

Ariel

Flavio Coelho

unread,
Jan 21, 2009, 11:46:52 AM1/21/09
to geod...@googlegroups.com
Thanks for bothering to answer it when I could have googled it. It will hopefully serve others with a similar problem to find the complete answer in on place.

This kind of sucks, because then I cannot test my app without deploying it first.

Maybe openlayers does not have that limitation, I'll implement an openlayers version of my view to debug and then switch to Google after deploying.

thanks to everyone for the help!

Flávio
Reply all
Reply to author
Forward
0 new messages