Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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'>   
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '12 at 15:23
    
Yeah. Mistakes happen. Sorry if my answer seemed patronizing. Glad to be of help :) –  R.K. Sep 13 '12 at 15:24
    
No man, no worries. Your help is very much appreciated. –  LarsVegas Sep 13 '12 at 15:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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