Skip to content

Instantly share code, notes, and snippets.

@kylefelipe
Created April 6, 2018 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kylefelipe/67e3329b8a1d268f385af9fed7461baf to your computer and use it in GitHub Desktop.
Save kylefelipe/67e3329b8a1d268f385af9fed7461baf to your computer and use it in GitHub Desktop.
importing a SHP to a Spatialite
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python 2
import os
from sqlite3 import dbapi2 as db
db_folder = "/"
db_name = "test"
shp_folder = "" # without the extension .shp
table_name = ""
encoding = ""
srid = ""
"""Ceating Database"""
print "Creating Database..."
os.putenv("SPATIALITE_SECURITY", "relaxed")
conn = db.connect(db_folder + db_name + ".sqlite")
conn.enable_load_extension(True)
conn.execute("SELECT load_extension('mod_spatialite')")
# Init Spatial Metadata
conn.execute('SELECT InitSpatialMetadata(1)')
conn.commit()
"""Import a ESRI Shapefile to a SPATIALITE database
shp_folder = Full path to Esri Shapefile, don't use .shp on shapefile name
table_name = name to imported ESRI Shapefile
encoding = SPH atribute table encoding
srid = System Coordinate ID in proj4"""
cur = conn.cursor()
sql = """SELECT ImportSHP("{shp_folder}", "{table_name}", '{encoding}', {srid});"""\
.format(shp_folder=shp_folder, table_name=table_name, encoding=encoding, srid=srid)
cur.executescript(sql)
conn.commit()
conn.close()
@am2222
Copy link

am2222 commented May 10, 2018

Does it work with python 3.6?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment