Experiments with GeoDjango – Using CSO boundary data

I came across this video about Geodjango on youtube the other day. The functionality looked really impressive so I decided to give it a go. For the most part I followed the instructions on the django site but I wanted to use some data relevant to Ireland:

  1. The first variation from the tutorial came when I  tried to install a postgres DB. I am on Ubuntu 10.04 which supports postgres-8.4 but it seems postgres-8.3 is required to get the GIS functionality enabled (At least to work with geodjango). So I unistalled postgres-8.4
    sudo apt-get purge postgresql-8.4

    and added these lines

    deb http://archive.ubuntu.com/ubuntu/ karmic main restricted universe
    deb-src http://archive.ubuntu.com/ubuntu/ karmic main restricted universe

    to my /etc/apt/sources.list. Then just run the apt-get install command as described in the tutorial.

  2. Rather than use the dataset from the tutorial I wanted to something more relevant to my home country Ireland. The OSI have a lot of data available on their site but its all very expensive so I started with the city_towns data from the CSO. Its not exactly fit for purpose but at least its free for non commerical use. It comes in the same shp format as described in tutorial so I just had to modify the model to match the fields from the CSO file.
    1 from django.contrib.gis.db import models
    2
    3 class District(models.Model):
    4     edid = models.CharField(max_length=254)
    5     label = models.CharField(max_length=254)
    6     mploy = models.MultiPolygonField()
    7     objects = models.GeoManager()
    8
    9     def __unicode__(self):
    10         return self.label
  3. Finally the most time consuming part was to guess what SRS (Spatial Reference System) was used by the CSO/OSI in the city_towns.shp file. It wasn’t listed in the fields so I had to experiment. After a few attempts where all the towns ended up in the middle of the Atlantic I discovered the correct SRSID was 29902. The ESPG site isn’t exactly user friendly but it can help to find the correct ID. I adapted load.py like so
    1 import os
    2 from django.contrib.gis.utils import LayerMapping
    3 from models import District
    4
    5 world_mapping = {
    6     'edid' : 'USERID',
    7     'label' : 'label',
    8     'mploy' : 'MULTIPOLYGON',
    9 }
    10
    11 world_shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/city_towns.shp'))
    12
    13 def run(verbose=True):
    14     lm = LayerMapping(District, world_shp, world_mapping,
    15                           transform=True, encoding='iso-8859-1',source_srs=29902)
    16
    17     lm.save(strict=True, verbose=verbose)
    18

Results:

I was able to create a django model of the Irish cities and towns listed by the CSO. Making it possible to do interesting queries such as

District.objects.filter(mploy__contains=pnt)

i.e. in which district (town/city) is this pnt. Below is a screenshot from the admin site showing the region for Oldcastle, Co.Meath:

Geodjango Admin for Oldcastle, Co.Meath

Geodjango Admin for Oldcastle, Co.Meath

Advertisements

About bebblebrox

I am an experienced software developer with over five year’s commercial experience with strong software design and development skills. I am searching for an ambitious start-up company with a shared passion for delivering quality software products.
This entry was posted in Geographic. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s