I'm trying to interact with a SpatiaLite database via Python on my Windows machine.
I've managed to connect to the database and load the SpatiaLite extension, which means that I can pass SQL queries in from my Python code e.g:
from pysqlite2 import dbapi2 as sqlite
myPath = r"C:\SpatiaLite\test-2.3.sqlite"
conn = sqlite.connect(myPath)
conn.enable_load_extension(True)
curs = conn.cursor()
curs.execute("select load_extension('libspatialite-1.dll')")
curs.execute("""SELECT name, peoples, AsText(Geometry) from Towns where
peoples > 350000 order by peoples DESC""")
# Print the selected records
for row in curs:
print row
So far so good. I'd now like to know how to export a table as a shapefile. Using the command prompt in spatialite.exe I can do this using .dumpshp
, but I can't figure out how to access this command from Python. Any ideas, please?
I was hoping for something like this:
curs.execute("select dumpshp('Towns Geometry shape_towns CP1252 POINT')")
but perhaps it's more complicated than that?
Thanks very much!
Best Answer
You can use the command line spatialite_tool and embed it in python with subprocess module.
In command line
In Python