Install Spatialite for python (GeoDjango) on OS X

4.2k views Asked by At

I am tearing my hair out trying to install Spatialite for GeoDjango!

I am already using Homebrew, it's generally easy and convenient so I initially tried to follow the Homebrew instructions for GeoDjango.

But this stops short of installing any database, i.e. Spatialite. The next step is to try and install Spatialite itself, but there are no Homebrew-specific instructions provided by Django docs.

I found this tutorial which looks perfect - a Homebrew and virtualenv-friendly install of Spatialite for GeoDjango.

But it doesn't work... it appears that my pysqlite is linked against the non-spatial-enabled version of SQLite that comes with OS X, rather than the Spatial-ised one I installed from Homebrew, I get this error when Django tried to connect to the db:

"The pysqlite library does not support C extension loading. Both SQLite and pysqlite must be configured to allow the loading of extensions to use SpatiaLite."

The author of pysqlite hasn't responded to my pleas for help on Github and I haven't found anything via Google.

So I went back to the drawing board and decided to follow the "Mac OS X-specific instructions" in the GeoDjango docs... by installing the various geo libs from the KyngChaos binary packages.

The docs say "Install the packages in the order they are listed above" but I found I couldn't install UnixImageIO without installing PROJ first. The link in the docs to download Spatialite binaries (http://www.gaia-gis.it/spatialite-2.3.1/binaries.html) is broken so I used the "Spatialite Tools v4.1" from KyngChaos instead.

Proceeding to the next step I get this error:

$ spatialite geodjango.db "SELECT InitSpatialMetaData();"  
SQLite header and source version mismatch  
2013-10-17 12:57:35 c78be6d786c19073b3a6730dfe3fb1be54f5657a  
2013-09-03 17:11:13 7dd4968f235d6e1ca9547cda9cf3bd570e1609ef

Not really sure what's wrong at this point.

There is someone else here on SO who has gone the KyngChaos route and just ends up with the same "Both SQLite and pysqlite must be configured to allow the loading of extensions" error I got from the Homebrew route anyway.

I found this ticket #17756 for adding pyspatialite support to Django - pyspatialite is supposed to be an easier way to pip install everything but unfortunately it doesn't work either (see comments towards bottom of ticket).

I'm a bit reluctant to start trying to build everything from source by hand as it seems likely I'll just run into the same problems again, but spending hours Googling for info about cryptic compiler errors, magic flags and paths etc along the way.

I'm about ready to give up and just use Postgres/PostGIS.

3

There are 3 answers

0
Anentropic On BEST ANSWER

I was able to get this working now, using the tip here:
https://github.com/ghaering/pysqlite/issues/60#issuecomment-50345210

I'm not sure if it was using the real paths that fixed it, or just the Homebrew kegs or underlying packages have been updated and now install cleanly. Still, it works now.

I reproduce below the steps I took:

brew update
brew install sqlite  # 3.8.5
brew install libspatialite  # 4.2.0
brew install spatialite-tools  # 4.1.1

git clone https://github.com/ghaering/pysqlite.git
cd pysqlite

(where brew reported I had existing versions I unlinked them and installed the latest as commented above)

then edited setup.cfg to comment out #define=SQLITE_OMIT_LOAD_EXTENSION and specify the paths:

include_dirs=/usr/local/opt/sqlite/include
library_dirs=/usr/local/opt/sqlite/lib

activated the virtualenv where I want it installed, then

python setup.py build
python setup.py install

(build_static still fails with clang: error: no such file or directory: 'sqlite3.c')

(maybe I should have done pip install . as suggested in the github issue)

now the spatialite geodjango.db "SELECT InitSpatialMetaData();" succeeds, albeit with an ignorable error:

InitSpatiaMetaData() error:"table spatial_ref_sys already exists"

i.e. it's probably not even necessary to run that command

3
MZaragoza On

When I was istalling this i follow this instructions https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/spatialite/#pysqlite2

pysqlite2

If you’ve decided to use a newer version of pysqlite2 instead of the sqlite3 Python stdlib module, then you need to make sure it can load external extensions (i.e. the required enable_load_extension method is available so SpatiaLite can be loaded).

This might involve building it yourself. For this, download pysqlite2 2.6, and untar:

$ wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz
$ tar xzf pysqlite-2.6.3.tar.gz
$ cd pysqlite-2.6.3

Next, use a text editor (e.g., emacs or vi) to edit the setup.cfg file to look like the following:

[build_ext]
#define=
include_dirs=/usr/local/include
library_dirs=/usr/local/lib
libraries=sqlite3
#define=SQLITE_OMIT_LOAD_EXTENSION
0
laszchamachla On

I had the same error: SQLite header and source version mismatch.

For me it was enough to update libsqlite3-dev.

After that invoking $ spatialite geo.db "SELECT InitSpatialMetaData();" creates proper database.