Spatialite Python – Special Python Library Needed for Spatialite

pythonspatialitesqlite

It appears that pyspatialite is very outdated. Do I need a special library like pyspatialite in order to use the spatial functions of spatialite? Or can I just connect to the database via the normal python driver and execute the spatial functions? If so, is there a more updated library that I am not aware of?

Best Answer

You can connect to Spatialite via Python using the latest version of pysqlite instead of pyspatialite. Spatialite is just the spatial enablement of SQLite so this works, but if you are connecting in this way (via pysqlite) you need to load the libspatialite extension to be able to use the spatial functionality of Spatialite.

from pysqlite2 import dbapi as sqlite
conn = sqlite.connect("myDB")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("libspatialite-2.dll")')
curs = conn.cursor()
... do something interesting

Make sure you get the correct name for the libspatialite dll which may vary depending on your version, otherwise this won't work. Scroll down on the link above and you'll find the documentation for load_extension.