4

在 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、普通 shell Python 和 IDLE 命令行都转储到分段错误时。这是我的错误还是我没有正确解决的底层C问题?

4

1 回答 1

5

这是我的错误还是我没有正确解决的底层C问题?

可能两者兼而有之。当对象超出范围并被垃圾回收时, GDAL/OGR 的绑定偶尔出现段错误。虽然这是一个已知的错误,但不太可能很快得到修复。

您可能会找到解决此问题的方法。我无法在 Windows XP 和以下版本的 GDAL/OGR 上使用另一个 shapefile 重现此段错误:

 >>> 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)

让我知道这是否可以解决您的问题。

于 2010-11-24T01:46:00.223 回答