Skip to content

Instantly share code, notes, and snippets.

@happysundar
Last active December 27, 2016 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save happysundar/6181707 to your computer and use it in GitHub Desktop.
Save happysundar/6181707 to your computer and use it in GitHub Desktop.
how to install pyspatialite

Install prerequisite package dependencies

From http://elgis.argeo.org/repos/6/, find the latest repo rpm, and install it as root:

`sudo rpm -Uvh http://elgis.argeo.org/repos/6/elgis-release-6-6_0.noarch.rpm`

Install Dependency Packages:


  1. yum install python-devel (needed by Shapely to compile C extensions)
  2. yum install bzip2 bzip2-devel (needed by Scrapy : remove this when this is no longer a dependency)
  3. yum install gdal gdal-devel
  4. yum install geos geos-devel
  5. yum install proj proj-devel
  6. yum install libxml2 libxml2-devel (needed to make xpath searching efficient)
  7. yum install libxslt libxslt-devel
  8. yum install sqlite sqlite-devel
  9. yum install expat expat-devel (this is needed by spatialite-tools)

Installing pythonz and virtualenv-burrito

TODO: Add instructions for doing this

Installing libspatialite, spatialite-tools and pysqlite

Why do this

  1. The default version of sqlite3 that is installed is not the current version (3.8.1 as of writing this), but an older version - 3.6.20 in my system. However, the current version of spatialite-tools will not compile with the older version. Hence, we have to compile/install this version. I did this from source (since my repos didn’t have the latest version).
  2. The default ‘make install’ installs all libraries/binaries on /usr/local/lib. This is usually not where the rpm packages install the libraries (in my system, this is /usr/lib64). When building, we have to make sure that the build system looks at this directory - something we will acheive using the export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig trick.

Installing Latest Version Of Sqlite (3.8.1)

First, let’s get the latest version of sqlite3 compiled and installed (to /usr/local/lib — so this shouldn’t mess with the yum installed version). For this, head over to http://www.sqlite.org/download.html and download the latest sqlite3-autoconf tarball. For me, that was http://www.sqlite.org/2013/sqlite-autoconf-3080100.tar.gz as of this time of writing.

  • Download to a directory using wget http://www.sqlite.org/2013/sqlite-autoconf-3080100.tar.gz or something similar.
  • Extract and cd to that directory
  • Do ./configure
  • Do sudo make install if you are not running as root (ignore ‘sudo’ if you are running as root).

Installing libspatialite

Installing ReadOSM

Installing spatialite-tools

Installing Pysqlite with enable_load_extension method available

Why do this

The default sqlite3 module or the external pysqlite modules don't come with the enable_load_extension method enabled (since some platforms have SQLite libraries which are compiled without this feature).

So, we have to compile the pysqlite3 extension with this feature enabled (since its easier to do this than to compile python)

Installing pysqlite

This is the simple part. Do:

`pip install pysqlite --no-install`

Change into the package source directory:

`cd <virtual_env_dir>/build/pysqlite`

Assuming that you have done the previous dependency install steps, you should have the sqlite 3.8.1 version installed in /usr/local/lib. Prepend that to $LD_LIBRARY_PATH:

`export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib`		

Edit setup.cfg and comment out the line containing this define:

`#define SQLITE_OMIT_LOAD_EXTENSION`

Now, do:

`pip install --no-download -I pysqlite`

This will compile sqlite3, and install the pysqlite package containing the latest version of sqlite3 (3.8.1 as of this writing)

Verify that this is indeed the case by doing this from the python prompt:

	from pysqlite2 import dbapi2 as sqlite3	
	sqlite3.sqlite_version_info

This should show:

`(3, 8, 1)`

Not only this, now you can load the spatialite library by doing this:

	from pysqlite2 import dbapi2 as sqlite3

	con = sqlite3.connect(":memory:") #obviously, use your actual database filename here

	con.enable_load_extension(True)				
	 
	con.execute('SELECT load_extension("/usr/local/lib/libspatialite.so")')
@impressarix
Copy link

I was greatly helped by the above explanation. I've installed QGIS in Fedora 25, but hit with the issue of the absence of pyspatialite package. By order of the steps above I've been able to enjoy QGIS application for the purposes of research on my Fedora. thanks.

Dwicahya Sulistyawan,
from a country full of beautiful natural scenery on the equator
Indonesia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment