Adventures in GeoDjango

March 27, 2010

I've been meaning to play with GeoDjango for a while, but my server was stuck on Debian Etch. Now I've upgraded to Lenny I can easily install the required libraries using apt.


I'm running this on a Debian Lenny system, here's the command to install the needed packages:

apt-get install libgeos-3.0.0 proj postgis gdal-bin postgresql-8.3-postgis

Database Setup

$ su postgres
$ createdb -E UTF8 template_postgis
$ createlang -d template_postgis plpgsql
$ psql -d template_postgis -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql
$ psql -d template_postgis -f /usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql
$ psql -d template_postgis
template_postgis=# GRANT ALL ON geometry_columns TO PUBLIC;
template_postgis=# GRANT ALL ON spatial_ref_sys TO PUBLIC;

I tried the tests from the Debian install instructions at but they failed. I think these are no longer supported with Django 1.2 (I'm doing this test using Django 1.2 Alpha). However, a good test to do is:

>>> from django.contrib.gis.gdal import HAS_GDAL
>>> print HAS_GDAL

This proves it can find the specified libraries.


There's an introductory tutorial at which involves importing some shapes for all the countries of the world into a Django model. Once this is done, you can take a point (specified using latitude and longitude) and ask the Django code to tell you which country it is in.

Here's an example taken from the tutorial:

from django.contrib.gis.geos import Point
pnt = Point(12.4604, 43.9420)
sm = WorldBorders.objects.get(mpoly__intersects=pnt)
<WorldBorders: San Marino>