我有一个简单的程序,可以处理Geopackage图层中的点。第一次尝试时,我将文件访问封装到一个函数中:
from osgeo import ogr
pointsFile = "points.gpkg"
def getPoints():
driver = ogr.GetDriverByName("GPKG")
dataSource = driver.Open(pointsFile, 0)
layer = dataSource.GetLayer(0)
print("Returning layer")
return layer
def main():
layer = getPoints()
print("Number of points to process: ", layer.GetFeatureCount())
if __name__ == '__main__': main()
返回图层对象时会失败,并出现分段错误:
$ python3 testReturn.py
Returning layer
Segmentation fault (core dumped)
但是,在main
内部具有文件访问权限:
from osgeo import ogr
pointsFile = "points.gpkg"
def main():
driver = ogr.GetDriverByName("GPKG")
dataSource = driver.Open(pointsFile, 0)
layer = dataSource.GetLayer(0)
print("Number of points to process: ", layer.GetFeatureCount())
if __name__ == '__main__': main()
程序按预期运行:
$ python3 testDirect.py
Number of points to process: 21872
什么可能导致此问题?
答案 0 :(得分:1)
使用GDB
测试了代码,调用时出现分段错误:
layer.GetFeatureCount()
一些额外的调试信息:
启动程序:/ usr / bin / python3 testReturn.py
[已启用使用libthread_db进行线程调试]
使用主机libthread_db库“ /lib/x86_64-linux-gnu/libthread_db.so.1”。
返回层 程序收到信号SIGSEGV,分段故障。 来自/usr/local/lib/libgdal.so.20的OGR_L_GetFeatureCount()中的0x00007ffff5c42298 (gdb)