0

the python code below produces the expected line shapefile viewable in QGIS

import os
from osgeo import ogr

drv=ogr.GetDriverByName('ESRI Shapefile')
if os.path.exists('test.shp'):
    os.remove('test.shp')
ds=drv.CreateDataSource('test.shp')
lyr=ds.CreateLayer('test',None,ogr.wkbUnknown)
flddef=ogr.FieldDefn('Name',ogr.OFTString)
flddef.SetWidth(32)
lyr.CreateField(flddef)

feat=ogr.Feature(lyr.GetLayerDefn())
feat.SetField('Name','george')
line=ogr.Geometry(ogr.wkbLineString)
line.AddPoint(1116651.439379124, 637392.6969887456)
line.AddPoint(1188804.0108498496, 652655.7409537067)
line.AddPoint(1226730.3625203592, 634155.0816022386)
line.AddPoint(1281307.30760719, 636467.6640211721)
feat.SetGeometry(line)
lyr.CreateFeature(feat)

feat.Destroy()
ds.Destroy()

however, when attempting to use the sqlite driver, the database and layer is created with no errors. QGIS can connect to the database and load the layer - though nothing is displayed.

drv=ogr.GetDriverByName('SQLite')
if os.path.exists('test.sqlite'):
    os.remove('test.sqlite')
ds=drv.CreateDataSource('test.sqlite',options=['SPATIALITE=yes'])
lyr=ds.CreateLayer('test',None,options=['FORMAT=SPATIALITE'])
flddef=ogr.FieldDefn('Name',ogr.OFTString)
flddef.SetWidth(32)
lyr.CreateField(flddef)

feat=ogr.Feature(lyr.GetLayerDefn())
feat.SetField('Name','george')
line=ogr.Geometry(ogr.wkbLineString)
line.AddPoint(1116651.439379124, 637392.6969887456)
line.AddPoint(1188804.0108498496, 652655.7409537067)
line.AddPoint(1226730.3625203592, 634155.0816022386)
line.AddPoint(1281307.30760719, 636467.6640211721)
feat.SetGeometry(line)
lyr.CreateFeature(feat)

feat.Destroy()
ds.Destroy()

on win7, python 2.7.5 (python (x,y) distribution) BTW - creating a point feature rather than a line works for both cases. also tried removing the spatialite option from the datasource and layer creation - no change. am i missing some obvious step?

0

apparently, this was a geom_type issue, which should be obvious i suppose.

in earlier tests, i would try lyr=ds.CreateLayer('test',geom_type=ogr.wkbLineString,options=['FORMAT=SPATIALITE'])

which would fail with a geometry constraint error.

finally determined that a different geometry type was needed: lyr=ds.CreateLayer('test',geom_type=ogr.wkbLineString25D,options=['FORMAT=SPATIALITE'])

now works great. the line displays fine in QGIS and ArcGIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.