段错误(段错误)使用Python中OGR CreateField()的时候
题
在Ubuntu运行这个很短的脚本当接收到一个段错误。
from osgeo import ogr, osr
shpfile = 'Census_County_TIGER00_IN.shp'
def cust_field(field):
'''cust_field(shpfile, field) creates a field definition, which, by calling cust_field(), can be used to create a field using the CreateField() function.
cust_field() DOES NOT create a field -- it simply creates a "model" for a field, that can then be called later. It's weird, but that's GDAL/OGR, as far as I can tell.'''
fieldDefn = ogr.FieldDefn(field, ogr.OFTInteger)
fieldDefn.SetWidth(14)
fieldDefn.SetPrecision(6)
return fieldDefn
ds = ogr.Open(shpfile, 1)
lyr = ds.GetLayerByIndex(0)
field = cust_field("Test")
lyr.CreateField(field)
一切操作顺利,直到最后一行,当IPython中,正常壳Python和空闲命令行所有转储分段故障。这是在我结束一个错误或底层的C,我不是正确的解决问题?
解决方案
这是我的结束或一个问题的错误 与底层的C,我不是 寻址正确?
这可能是两者。 GDAL / OGR的绑定的不的倾向偶尔段错误,当对象去的范围之并垃圾回收。虽然这是一个已知的错误,它是不可能很快地固定的任何时间。
有可能到头来你可以找到一个方法来解决这个问题。我不能重现这个段错误与Windows XP的另一个shape文件和GDAL / OGR以下版本:
>>> gdal.VersionInfo('')
'GDAL 1.6.0, released 2008/12/04'
您可以尝试暂时重构cust_field
功能到像这样的脚本主体:
from osgeo import ogr, osr
shpfile = 'Census_County_TIGER00_IN.shp'
ds = ogr.Open(shpfile, 1)
lyr = ds.GetLayerByIndex(0)
fieldDefn = ogr.FieldDefn("Test", ogr.OFTInteger)
fieldDefn.SetWidth(14)
fieldDefn.SetPrecision(6)
lyr.CreateField(fieldDefn)
让我知道如果这能解决你的问题。
不隶属于 StackOverflow