[GIS] Linking OGR with spatialite

installationogrpythonspatialite

I'm trying to create a spatialite file with the code snippet below. However the outDataSource object gets assigned NoneType and all subsequent (unshown) code fails.

I'm thinking the problem is with the spatialite installation for two reasons:

  1. When I choose 'ESRI Shapefile' as the driver and leave out the options=['SPATIALITE=yes'] the outDataSource is created correctly and all subsequent codes works as anticipated.
  2. When I leave the driver as 'SQLite' but take out the options=['SPATIALITE=yes'], the non-spatial sqlite tables are indeed created. So, any ideas on what I am missing or might have done incorrectly to establish the link between OGR and spatialite?

I'm developing on Spyder 2.3.0rc, with Python 2.7.7 32bits, Qt 4.8.6,
PyQt4 (API v2) 4.10.4 on Windows, as delivered by the Anaconda-2.0.1-Windows-x86
executable. Additionally, the development comments at the end of the code describe the steps I took to install spatialite.

# standard modules
import os

# third party modules
from osgeo import ogr


##############################################################################
# create a new spatialite output file
##############################################################################

outputGridfn = os.path.join(os.getcwd(), 'grid.sqlite')
outDriver = ogr.GetDriverByName('SQLite')
outDataSource = outDriver.CreateDataSource(outputGridfn,
                                           options=['SPATIALITE=yes'])


##############################################################################
# Development notes
##############################################################################

# SPATIALITE functionality requires the following work
# Step 0: Download DLL's from
# latuviitta.org/documents/Spatialite_4.0_test_with_jre_1.6.zip
# Step 1: Unzip and copy the spatialite DLL into the Python DLL library

# Recompile the sqlite3 DLL from the amalgamation as follows
# Step 0: Add '#define SQLITE_ENABLE_RTREE' to the sqlite3.c file
# Step 1: gcc -c sqlite3.c
# Step 2: gcc -shared -o sqlite3.dll sqlite3.o
# Step 3: Place the DLL in the python DLL library

# Add libgeo-3-3-1.dll to python DLL library

Best Answer

as @BradHards mentioned - it may be that the install of GDAL with anaconda is lacking support for spatialite. this google group thread indicates GDAL is a recent addition to the anaconda repo, possibly still with some issues. could try another distro - or go a more manual route reinstalling GDAL without conda.

Related Question