简体   繁体   English

QGIS 3 python 遍历图层并运行算法

[英]QGIS 3 python loop through layers and run algorithm

I'm attempting to loop through layers and run a processing algorithm on each layer but get an error with the input:我试图遍历层并在每一层上运行处理算法但输入错误:

Code代码

feedback = QgsProcessingFeedback()

# Get current layers
layers = QgsProject.instance().mapLayers().values()

# Loop through layer and run algorithm
for layer in layers:
    processing.run("qgis:pointstopath", {'INPUT': layer, 'ORDER_FIELD': 'name', 'GROUP_FIELD': 'name','OUTPUT': layer+'.gpkg'},feedback)

Error错误

Traceback (most recent call last):
File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 2, in <module>
File "C:/OSGEO4~1/apps/qgis-rel- 
dev/./python/plugins\processing\tools\general.py", line 105, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback,
context)
File "C:/OSGEO4~1/apps/qgis-rel- 
dev/./python/plugins\processing\core\Processing.py", line 183, in 
runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: There were errors executing the algorithm.

How do you correctly parse the input layers from the map to the algorithm?你如何正确解析从 map 到算法的输入层?

You don't need to use the 'value' method.您不需要使用“值”方法。

The right way is layers = QgsProject.instance().mapLayers()正确的方法是layers = QgsProject.instance().mapLayers()

The full code is:完整代码是:

feedback = QgsProcessingFeedback()

# Get current layers
layers = QgsProject.instance().mapLayers()

# Loop through layer and run algorithm
for layer in layers:
    processing.run("qgis:pointstopath", {'INPUT': layer, 'ORDER_FIELD': 'name', 'GROUP_FIELD': 'name','OUTPUT': layer+'.gpkg'},feedback)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM