import subprocess
import sqlite3
with sqlite3.connect('test-2.3.sqlite') as conn:
conn.enable_load_extension(True)
c = conn.cursor()
c.execute("SELECT load_extension('mod_spatialite')") # https://www.gaia-gis.it/fossil/libspatialite/wiki?name=mod_spatialite
c.execute("SELECT InitSpatialMetaData(1)")
os.putenv("SPATIALITE", "relaxed") # set SPATIALITE_SECURITY=relaxed
# testing if Spatialite works well
for row in c.execute("SELECT ST_AsText(MakePoint(11.5,42.5,4326))"):
print row
# checking Spatialite version
for vs in c.execute("SELECT spatialite_version()"):
print 'Version Spatialite: ',vs[0]
This works well - I get answers:
(u'POINT(11.5 42.5)',)
Version Spatialite: 4.3.0
And now I have difficulties in loading shapefiles. I've tried 3 options:
VERSION 1 Using ImportSHP:
filename = 'shape_towns'
table = 'NewTowns2'
charset = 'CP1252'
srid = 32632
c.execute("SELECT ImportSHP(filename,table,charset,srid);")
ERROR MESSAGE:
Traceback (most recent call last):
File "C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\sqliteIGI.py", line 63, in <module>
c.execute("SELECT ImportSHP(filename,table,charset,srid);")
OperationalError: near "table": syntax error
VERSION 2:
os.system('spatialite.exe test-2.3.sqlite < C:\sqlite\sql_statements\file*.sql')
*files content:
ERROR MESSAGE:
Error: incomplete SQL: ... (content)
VERSION 3:
subprocess.call(["spatialite_gui", "-e", "-shp", "shape_towns", "-d", "test-2.3.sqlite", "-t", "NewTowns2", "-g", "Geometry", "-c", "CP1252", "--type", "POINT"])
ERROR MESSAGE (window from spatialite_gui):
Failure while connecting to DB unable to open database file -e
After each version I add:
conn.commit()
Thank you for helping,
Justyna
Hi,I am a student and now writing MA thesis. I need to import shapefile to Spatialite via Python script. I am not an expert at programming. I have read some forums and found some options to do this task. However, none of this works for me. I prefer the fastest solution (I supose that using virtual table is slower in further spatial analysis since you cannot create spatial indexes?).
I would be gratefull if you could show me where I make mistake or provide better solution.I have installed: (my system is 64-bit but with these files I've manager to load Spatialite extension)
- Python 2.7.10 (win32)
- mod_spatialite-4.3.0a-win-x86 http://www.gaia-gis.it/gaia-sins/windows-bin-x86/
- spatialite_gui-4.3.0a-win-x86 http://www.gaia-gis.it/gaia-sins/windows-bin-x86/
- sqlite-dll-win32-x86-3220000 https://www.sqlite.org/download.html
- sqlite-tools-win32-x86-3220000 https://www.sqlite.org/download.html
I have replaced default Python file sqlite3.dll with file from sqlite-dll-win32-x86-3220000.I set environment path to C:\sqlite folder (shared via my gogle drive): https://drive.google.com/file/d/1YwlRsjAb5dv35ZdBgeo33o-00mGUTFnQ/view?usp=sharing
- test-2.3.sqlite - this is the database where I want import shapefile
- shape_towns - this is the shapefile
- sql_statements - folder with used in script sql statements
Python script:I am loading Spatialite extension:import osimport subprocess
import sqlite3
with sqlite3.connect('test-2.3.sqlite') as conn:
conn.enable_load_extension(True)
c = conn.cursor()
c.execute("SELECT load_extension('mod_spatialite')") # https://www.gaia-gis.it/fossil/libspatialite/wiki?name=mod_spatialite
c.execute("SELECT InitSpatialMetaData(1)")
os.putenv("SPATIALITE", "relaxed") # set SPATIALITE_SECURITY=relaxed
# testing if Spatialite works well
for row in c.execute("SELECT ST_AsText(MakePoint(11.5,42.5,4326))"):
print row
# checking Spatialite version
for vs in c.execute("SELECT spatialite_version()"):
print 'Version Spatialite: ',vs[0]
This works well - I get answers:
(u'POINT(11.5 42.5)',)
Version Spatialite: 4.3.0
And now I have difficulties in loading shapefiles. I've tried 3 options:
VERSION 1 Using ImportSHP:
filename = 'shape_towns'
table = 'NewTowns2'
charset = 'CP1252'
srid = 32632
c.execute("SELECT ImportSHP(filename,table,charset,srid);")
ERROR MESSAGE:
Traceback (most recent call last):
File "C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\sqliteIGI.py", line 63, in <module>
c.execute("SELECT ImportSHP(filename,table,charset,srid);")
OperationalError: near "table": syntax error
VERSION 2:
os.system('spatialite.exe test-2.3.sqlite < C:\sqlite\sql_statements\file*.sql')
*files content:
importSHP.sql: "SELECT ImportSHP('shape_towns','NewTowns2','CP1252',32632);" VT.sql: "CREATE VIRTUAL TABLE NewTowns2 USING VirtualShape(C:/sqlite/shape_towns, CP1252, 32632);" loadshp.sql: ".loadshp "C:\sqlite\shape_towns" NewTowns2 CP1252 32632 geom"
ERROR MESSAGE:
Error: incomplete SQL: ... (content)
VERSION 3:
subprocess.call(["spatialite_gui", "-e", "-shp", "shape_towns", "-d", "test-2.3.sqlite", "-t", "NewTowns2", "-g", "Geometry", "-c", "CP1252", "--type", "POINT"])
ERROR MESSAGE (window from spatialite_gui):
Failure while connecting to DB unable to open database file -e
After each version I add:
conn.commit()
Thank you for helping,
Justyna
import sqlite3
## LOADING EXTENSION SPATIALITE
with sqlite3.connect('test4.sqlite') as conn:
conn.enable_load_extension(True)
c = conn.cursor()
c.execute("SELECT load_extension('mod_spatialite')")
c.execute("SELECT InitSpatialMetaData(1)")
os.putenv("SPATIALITE", "relaxed")
## LOADING SHAPEFILE
filename = 'shape_towns'
table = 'NewTowns2'
charset = 'CP1252'
srid = 32632
c.execute("SELECT ImportSHP(" + filename + "," + table + "," + charset + "," + str(srid) + ");")
conn.commit()
File "C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\sqlite5.py", line 20, in <module>
c.execute("SELECT ImportSHP(" + filename + "," + table + "," + charset + "," + str(srid) + ");")
OperationalError: no such column: shape_towns
shape_towns is my shapefile I do not understand the message no such column
Justyna
Thank you Mark :)So if I only need the shapefile to make a buffer out of it I should use ImportSHP?
Now my script looks as below:import osimport sqlite3
## LOADING EXTENSION SPATIALITE
with sqlite3.connect('test4.sqlite') as conn:
conn.enable_load_extension(True)
c = conn.cursor()
c.execute("SELECT load_extension('mod_spatialite')")
c.execute("SELECT InitSpatialMetaData(1)")
os.putenv("SPATIALITE", "relaxed")
## LOADING SHAPEFILE
filename = 'shape_towns'
table = 'NewTowns2'
charset = 'CP1252'
srid = 32632
c.execute("SELECT ImportSHP(" + filename + "," + table + "," + charset + "," + str(srid) + ");")
conn.commit()
After running script I get such a message:Traceback (most recent call last):File "C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\sqlite5.py", line 20, in <module>
c.execute("SELECT ImportSHP(" + filename + "," + table + "," + charset + "," + str(srid) + ");")
OperationalError: no such column: shape_towns
Good morning...
Try thisc.execute("SELECT ImportSHP({filename}, {table}, {charset}, {srid});".format(filename=filename, table=table, charset=charset, srid=srid))
Remember: if your script is not placed in the same folder with shapefile you have to use full path to the file (C:....)
--
You received this message because you are subscribed to a topic in the Google Groups "SpatiaLite Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spatialite-users/AK8y8UUhLIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spatialite-use...@googlegroups.com.
To post to this group, send email to spatiali...@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.
File "C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\sqlite5.py", line 20, in <module>
c.execute("""SELECT ImportSHP('{filename}', '{table}', '{charset}', {srid});""".format(filename=filename, table=table, charset=charset, srid=srid))
OperationalError: no such function: ImportSHP
InitSpatiaMetaData() error:"table spatial_ref_sys already exists"
Thanks :) I've tried the code (delete one extra "), but I get such a message:Traceback (most recent call last):File "C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\sqlite5.py", line 20, in <module>
c.execute("""SELECT ImportSHP('{filename}', '{table}', '{charset}', {srid});""".format(filename=filename, table=table, charset=charset, srid=srid))
OperationalError: no such function: ImportSHP
InitSpatiaMetaData() error:"table spatial_ref_sys already exists"
Hi Sandro,I have such a line in my code:
os.putenv("SPATIALITE", "relaxed")
is it not correct?
os.putenv("SPATIALITE", "relaxed") # set SPATIALITE_SECURITY=relaxed
>>> print sql_statement
SELECT ImportSHP('shape_towns', 'NewTowns2', 'CP1252', 32632);
And after pasting into gui I get the correct result.
I have to add that Buffer function works via Python script.
Thank you Mark, Sandro for helping :)I have printed the statement - it looks ok:>>> print sql_statement
SELECT ImportSHP('shape_towns', 'NewTowns2', 'CP1252', 32632);
And after pasting into gui I get the correct result.
I have to add that Buffer function works via Python script.
As to SPATIALITE_SECURITY this does not work also:os.putenv("SPATIALITE_SECURITY", "relaxed")I have also tried using cmd:SET SPATIALITE_SECURITY=relaxedpython scrypt_name.pyand I got the same error message.
None
with sqlite3.connect('test.sqlite') as conn: ........
and printed in terminal:
>>> print os.environ["SPATIALITE_SECURITY"]
relaxed
but still the same error message
I've changed the code:os.environ['SPATIALITE_SECURITY']='relaxed'
On Friday, 6 April 2018 15:03:51 UTC+2, Justyna Dębicka wrote:I've changed the code:os.environ['SPATIALITE_SECURITY']='relaxed'This should be:os.putenv("SPATIALITE_SECURITY", "relaxed")-------> with 'put' <----------
>>> os.putenv("SPATIALITE_SECURITY", "relaxed")
>>> print os.environ.get("SPATIALITE_SECURITY")
None
But setting as below:
>>> import os
>>> os.environ['SPATIALITE_SECURITY']='relaxed'
>>> print os.environ.get("SPATIALITE_SECURITY")
relaxed
This is mu current code:
import os
import sqlite3
## LOADING EXTENSION SPATIALITE
os.environ['SPATIALITE_SECURITY']='relaxed'
with sqlite3.connect("testing.sqlite") as conn:
conn.enable_load_extension(True)
c = conn.cursor()
c.execute("SELECT load_extension('mod_spatialite')")
c.execute("SELECT InitSpatialMetaData(1)")
## LOADING SHAPEFILE
filename = r'C:\Users\justa\Documents\Magisterka2018\SkryptyPython\testy\shape'
table = 'NewTowns2'
charset = 'CP1252'
srid = 32632
sql_statement="""SELECT ImportSHP('{filename}', '{table}', '{charset}', {srid});""".format(filename=filename, table=table, charset=charset, srid=srid)
c.execute(sql_statement)
conn.commit()
--
You received this message because you are subscribed to a topic in the Google Groups "SpatiaLite Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spatialite-users/AK8y8UUhLIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spatialite-users+unsubscribe@googlegroups.com.
To post to this group, send email to spatialite-users@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to spatialite-use...@googlegroups.com.
To post to this group, send email to spatiali...@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to spatialite-users+unsubscribe@googlegroups.com.
To post to this group, send email to spatialite-users@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.