Created
April 6, 2018 16:33
-
-
Save kylefelipe/67e3329b8a1d268f385af9fed7461baf to your computer and use it in GitHub Desktop.
importing a SHP to a Spatialite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it work with python 3.6?