GeoDjango and Friends

I gave a talk last night to the Portland Python User Group at Webtrends on the geospatial capabilities of the Django web application framework.  In addition to showing off GeoDjango, I took their basic WorldBorders sample and extended it to provide a GeoJSON web service that OpenLayers could consume.

Presentation Slides

World_Borders Tutorial_Plus (Source Code)

I essentially took a render_to_geojson utility developed by my friend Dane Springmeyer and put together a quick and dirty GeoJSON web service.  The output from this service is ready to be consumed by an OpenLayers GeoJSON protocol.  I then combined that with the GeoExt ‘Feature Store In An Ext Grid’ example and gave it a nice OpenStreetMap base layer.

Here’s an excerpt from the GeoJSON web service:

{
 "srid": 900913,
 "crs": {
  "type": "link",
  "properties": {
   "href": "http://spatialreference.org/ref/epsg/900913/",
   "type": "proj4"
  }
 },
 "type": "FeatureCollection",
 "features": [
  {
   "geometry": {
    "type": "MultiPolygon",
    "coordinates": [
     [
      [
       [
        -9774223.6553460006,
        1955094.8610100001

Here's the web service view:

def borders(request):
    borders_qs = WorldBorders.objects.filter(subregion=request.GET.get('subregion'))
    return render_to_geojson(
        borders_qs,
        geom_attribute='geom',
        mimetype = 'text/plain',
        proj_transform=900913,
        pretty_print=True
    )

and here's the client feature store that I hooked to it:

// create feature store, binding it to the vector layer
store = new GeoExt.data.FeatureStore({
    layer: vecLayer,
    fields: [
        {name: 'name', type: 'string'},
        {name: 'area', type: 'float'},
        {name: 'pop2005', type: 'float'}
    ],
    proxy: new GeoExt.data.ProtocolProxy({
        protocol: new OpenLayers.Protocol.HTTP({
            url: "/world/borders/json?subregion=13",
            format: new OpenLayers.Format.GeoJSON()
        })
    }),
    autoLoad: true
});

Leave a Reply