繁体   English   中英

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

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

我试图遍历层并在每一层上运行处理算法但输入错误:

代码

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)

错误

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.

你如何正确解析从 map 到算法的输入层?

您不需要使用“值”方法。

正确的方法是layers = QgsProject.instance().mapLayers()

完整代码是:

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