Skip to content

Instantly share code, notes, and snippets.

@filipkral
Created March 15, 2016 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipkral/00d38ba761f72289b0df to your computer and use it in GitHub Desktop.
Save filipkral/00d38ba761f72289b0df to your computer and use it in GitHub Desktop.
Read and Write Spatialite with Python
from osgeo import ogr, osr
def read_splt(db):
drvr = ogr.GetDriverByName('SQLite')
ds = drvr.Open(db, 0)
layer=ds.GetLayer('pts')
for feature in layer:
print(feature)
break
ds.Destroy()
def write_splt(db):
drvr = ogr.GetDriverByName('SQLite')
ds = drvr.CreateDataSource(db, options=['SPATIALITE=yes'])
ds = drvr.Open(db, 1)
srs = osr.SpatialReference()
srs.ImportFromEPSG(27700)
lr = ds.CreateLayer("pts", srs, geom_type=ogr.wkbPoint)
# Add an ID field
lr.CreateField(ogr.FieldDefn("id", ogr.OFTInteger))
# Create the feature and set values
featureDefn = lr.GetLayerDefn()
feature = ogr.Feature(featureDefn)
feature.SetGeometry(ogr.CreateGeometryFromWkt('POINT (200000 300000)'))
feature.SetField("id", 1)
lr.CreateFeature(feature)
del lr
ds.Destroy()
if __name__ == "__main__":
db = '/temp/mydb.sqlite'
write_splt(db)
read_splt(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment