Tuesday, 21 January 2020

special python library needed for spatialite?


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?



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...