Python 如何删除pyqgis中除某些特定图层之外的所有图层?

Python 如何删除pyqgis中除某些特定图层之外的所有图层?,python,python-3.x,geometry,layer,pyqgis,Python,Python 3.x,Geometry,Layer,Pyqgis,我需要为我的QGIS项目加载几个向量层,以便在每个向量层中测试脚本的每个功能。然而,最终我只想处理一到两个感兴趣的层,而放弃其他层,所以我想自动地这样做 我用一些层成功地做到了这一点,但有一层给我带来了问题,我还没有弄清楚原因 下面是一些代码: 加载层(几乎可以肯定,这不是问题): 现在,我创建了这个函数来删除所有加载的层,除了我想使用的层。打印出了,这仅仅是因为我试图理解这个问题 def RemoveAllLayersExcept(*layers): layer_ids = []

我需要为我的QGIS项目加载几个向量层,以便在每个向量层中测试脚本的每个功能。然而,最终我只想处理一到两个感兴趣的层,而放弃其他层,所以我想自动地这样做

我用一些层成功地做到了这一点,但有一层给我带来了问题,我还没有弄清楚原因

下面是一些代码:

加载层(几乎可以肯定,这不是问题):

现在,我创建了这个函数来删除所有加载的层,除了我想使用的层。
打印出了
,这仅仅是因为我试图理解这个问题

def RemoveAllLayersExcept(*layers):
    layer_ids = []
    for l in layers:
        layer_ids.append(l.id())
    print('layer_ids', layer_ids)
    for lyr in QgsProject.instance().mapLayers():
        print(lyr)
        if lyr not in layer_ids:
            print('not')
            QgsProject.instance().removeMapLayer(lyr)
        else:
            pass
然后,我创建了一个新的层-一个给我带来问题的层。我需要在以后的迭代过程中编辑此层。我遵循了OpenSourceOptions教程中的一个分步示例,标题为:

然后我移除了我不感兴趣的图层:

RemoveAllLayersExcept(layer, layer_fireball)
就是这样。当我第一次运行程序时,什么都没有发生。以下是我得到的:

layer_ids ['COS2018_ardida2018_3_clip_cbf56f4b_e668_4c2e_9259_7d22d5943097', 'fireball_points_f92b32e0_f8bf_42c1_95e6_b317ddf6ee84']
COS2018_ardida2018_2_clip_45b241c4_fb9b_4654_9916_5ff08514c559
not
COS2018_ardida2018_3_clip_cbf56f4b_e668_4c2e_9259_7d22d5943097
fireball_points_f92b32e0_f8bf_42c1_95e6_b317ddf6ee84
这正是我所期望的。但在第二次和第n次中,我得到:

Layer failed to load!
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 676, in <module>
  File "<string>", line 104, in RemoveAllLayersExcept
AttributeError: 'NoneType' object has no attribute 'id'
层加载失败!
回溯(最近一次呼叫最后一次):
文件“C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py”,第90行,运行代码
exec(代码,self.locals)
文件“”,第1行,在
文件“”,第676行,在
RemoveAllLayerExcept中第104行的文件“”
AttributeError:“非类型”对象没有属性“id”
你能发现问题出在哪里吗?为什么会出现这种错误?为什么它只发生在第二次跑步之后


谢谢

更改
RemoveAllLayersExcept
方法,如下所示:

def RemoveAllLayersExcept(*保留层):
layers=QgsProject.instance().mapLayers().values()
_是否将被_删除=[l表示层中的l,如果l不在keep_层中]
对于中的图层,将删除:
QgsProject.instance().removeMapLayer(层)

我不熟悉PyQGIS。但是,该错误表明由层中l的
生成的
l
值为
None
。这意味着传递给
RemoveAllLayerExcept()
*层
参数之一就是该值。@martineau没错,但我不明白为什么会发生这种情况。甚至不是只有第一次运行才返回错误。问题与调用
RemoveAllLayersExcept()
时传递的参数有关。然而,你问题中的任何代码都没有显示这种情况是如何发生的,因此很难有人能帮助你。
layer_ids ['COS2018_ardida2018_3_clip_cbf56f4b_e668_4c2e_9259_7d22d5943097', 'fireball_points_f92b32e0_f8bf_42c1_95e6_b317ddf6ee84']
COS2018_ardida2018_2_clip_45b241c4_fb9b_4654_9916_5ff08514c559
not
COS2018_ardida2018_3_clip_cbf56f4b_e668_4c2e_9259_7d22d5943097
fireball_points_f92b32e0_f8bf_42c1_95e6_b317ddf6ee84
Layer failed to load!
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 676, in <module>
  File "<string>", line 104, in RemoveAllLayersExcept
AttributeError: 'NoneType' object has no attribute 'id'