Index ¦ Archives ¦ Atom

Fixing GDAL and GEOS for Django on macOS

As a user of MacPorts for all the additional packages needed when working with Django on macOS, a recent upgrade to GEOS managed to break all my projects which used GeoDjango:

$ ./manage.py help
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)

  ...

  File "/Users/tomkins/.virtualenvs/greendale/lib/python3.5/site-packages/django/contrib/gis/geos/libgeos.py", line 147, in geos_version_info
    raise GEOSException('Could not parse version info string "%s"' % ver)
django.contrib.gis.geos.error.GEOSException: Could not parse version info string "3.6.2-CAPI-1.10.2 4d2925d6"

$ port installed geos
The following ports are currently installed:
  geos @3.6.2_0 (active)

Highly annoying!

This will be fixed in PR #8817 for Django master, which will be released in Django 2.0 later this year, and PR #8841 for the upcoming Django 1.11.5 release. However the fix won't be backported to older versions of Django, such as the Django 1.8 LTS branch. So to continue using GeoDjango on macOS, we need to use an older working version of GEOS.

KyngChaos packages

Fortunately KyngChaos has a variety of Unix Compatibility Frameworks available for download, including GDAL and GEOS. Fortunately it's an older version of GEOS (3.6.1) which will still work with older versions of Django. Also the older version of GDAL (1.11) works with Django 1.8, as newer versions of GDAL also cause problems with Django 1.8.

Download and install:

  • GDAL 1.11 Complete
  • GDAL 2.1 Complete

Although we won't be using GDAL 2.1 in this example, you can easily switch to it if you're only running Django 1.11.

Add the following to your .bash_profile:

export GDAL_LIBRARY_PATH="/Library/Frameworks/GDAL.framework/Versions/1.11/GDAL"
export GEOS_LIBRARY_PATH="/Library/Frameworks/GEOS.framework/Versions/3/GEOS"

Then add the following to your Django settings file:

# GeoDjango fixes
GDAL_LIBRARY_PATH = os.environ.get('GDAL_LIBRARY_PATH')
GEOS_LIBRARY_PATH = os.environ.get('GEOS_LIBRARY_PATH')

On the server you're deploying these environment variables won't be set, so the setting will default to None - in which case Django will automatically find the installed versions of GDAL and GEOS.

Now you should have a fully functioning GeoDjango project on macOS!

© Alex Tomkins.