attribute: Phillie Casablanca

Using Django 1.4 with GDAL 1.9 and PostGIS-2.0

Recently both Django 1.4 and PostGIS-2.0 were released and I needed to setup a new development environment.

However, since the release dates of both were pretty close, it looks like django-1.4 didn't manage to get support for Postgis-2.0 into the 1.4 release and the following error occures:

ERROR: operator class "gist_geometry_ops" does not accept access method "gist"

Luckily this is already identified as an issue. (See Ticket #16455 )

While the patch hasn't been accepted yet, it does appear to work for me, so at least you can use geometry columns in PostGIS-2.0 when you apply the patch.

Enable PostGIS-2.0

  1. Install Postgres 9.1 then install PostGIS-2.0

  2. Apply the patch to django.contrib.gis.db.backends.postgis.creation described in Ticket #16455

  3. Check/Set the GDAL_DATA environment variable is set to C:\Program Files (x86)\PostgreSQL\9.1\gdal-data

  4. Check/Set the PROJ_LIB environment variable is set to C:\Program Files (x86)\PostgreSQL\9.1\share\contrib\postgis\proj

  5. Check/Set the PROJ_SO environment variable is set to C:\Program Files (x86)\PostgreSQL\9.1\bin\libproj-0.dll


The gdal-data folder is ONLY installed with postgis-2.0, and is not found in postgis-1.5.
This folder is needed to perform projection translations and the python GDAL library also needs this information.

Once this is done you should be able to create/syncdb for models that contain geometry fields.

Enable GDAL-1.9.0:

Almost every time I setup or someone in my team sets up a new development environment someone has an issue getting GDAL to work. In my case I need some of the raster features available in the stock gdal package which are NOT available in the django version (see note). So I nned to install the standard python GDAL package in pypi.

Getting this to work with django was a long process of trial and error. Here's what I did (on win7):

  1. Install GDAL-1.9

  2. Next, create the directory C:\gdallibs.

  3. Then copy all the DLLs that were installed in C:\Python27\Lib\site-packages\osgeo to C:\gdallibs

  4. Add C:\gdallibs to the PATH environment variable.

    • Once added to your PATH django should find the libraries.

  5. django-1.4 does not yet have support for gdal-1.9.0 and cannot find the 1.9 library. Overwrite the local django.contrib.gis.gdal.libgdal with this updated version

    • This adds gdal1.9 to the library search and also allows configuration of GDAL_LIBRARY_PATH as an environment variable. (Although, I found it was not needed once C:\gdallibs was created and added to the PATH.

  6. Confirm that gdal.HAS_GDAL+ and *gdal.GEOJSON are True:

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


You may need to open a NEW console for the updated PATH environment variable to take effect.

monkut // May 2, 2012 // 4:45 p.m.