This little trick derives from the fact that the from_postgis
class method is not really specific to postgis at all;
it will work with sqlalchmey or dbapi2 connections.
However, there are some peculiarities with spatialite
that prevent this from being as simple as one might hope. There are two options:
-
The
pysqlite2
driver works great for vanilla sqlite3 databases but spatialite requires loading an extension. In order to load extensions, you need to install a patched version and do some manual loading of the shared library. (seethe_pysqlite2_way.py
) -
A better alternative is to use
pyspatialite
but installation is also a bit funky. I had to install from the current git master instead of the pypi version. The extension is loaded automatically. (seethe_pyspatialite_way.py
)
Either way, you have to explicitly wrap your geometry column(s) in Hex(ST_AsBinary(...))
in order to run the geometries through shapely.wkb.loads
which is used internally in from_postgis
.
fwiw, the native sqlite3 library is able to handle loading extensions and it works just fine with spatialite! 😃