2

Why does ogr's driver method CreateLayer return None? I'm using python 2.7 with gdal 1.9.1.

path = r'D:\_tempAuto'
out_shp = '%s\test_OGR.shp' % (path,) 
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.CreateDataSource(out_shp)
layer = ds.CreateLayer('test_OGR', geom_type=ogr.wkbLineString)

>>> ds
<osgeo.ogr.DataSource; proxy of <Swig Object of type 'OGRDataSourceShadow 
*' at 0x00C71638> >

type(layer)
<type 'NoneType'>   

1 Answer 1

4

I think it's because GDAL wasn't able to create a layer from the shape file. The offending line:

out_shp = '%s\test_OGR.shp' % (path,)

You have to escape the slash, like so:

out_shp = '%s\ \test_OGR.shp' % (path,)

Otherwise, Python would will treat the \test part as \t or tab. Your resulting path would then be:

D:_tempAuto est_OGR.shp

hence the empty layer. You could also tell Python to treat it as a raw string by prefixing the string with r or R like you did with your path variable.

3
  • I was a little too quick asking this question. Thanks for answering, anyway. You are absolutely right with your answer. The same affect has putting a r before the string, making it "raw" (like I did in the first line). I oversaw this at the second line (if you just look too long at your code...).
    – LarsVegas
    Sep 13, 2012 at 15:23
  • Yeah. Mistakes happen. Sorry if my answer seemed patronizing. Glad to be of help :)
    – R.K.
    Sep 13, 2012 at 15:24
  • No man, no worries. Your help is very much appreciated.
    – LarsVegas
    Sep 13, 2012 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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