None

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.

Install

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 http://code.djangoproject.com/wiki/GeoDjangoDebianLennyInstall 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
True

This proves it can find the specified libraries.

Tutorial

There's an introductory tutorial at http://geodjango.org/docs/tutorial.html 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)
sm
<WorldBorders: San Marino>

References

http://code.djangoproject.com/wiki/GeoDjangoDebianLennyInstall