pyQgis 处理算法

问题描述

import sys
sys.path.append("c:\\osgeo4w64\\app\qgis\\pyto\\plugins")
    
import processing,os,glob
layer1 = QgsvectorLayer(layer1ShpFilePath,"layer1","ogr")
layer2 = QgsvectorLayer(layer2ShpFilePath,"layer2","ogr")
        
params = {
     'INPUT': layer1,'OVERLAY': layer2,'OUTPUT': "TEST.shp"
}
        
intersectLayer = processing.run("saga:intersect",params)

我想用pyQgis找出2个矢量图层之间的相交多边形,但总是遇到错误 AttributeError: 模块“处理”没有属性“运行” 谁能帮帮我吗?我在window机器上用QGis写python,我已经把基本的QGIS路径加到环境变量里了,但是不确定是否需要把SAGA包加到window环境变量中。

解决方法

遇到了同样的问题 - 可能是因为您在 sys.path.append 中的导入路径不正确(我在您的帖子中看到一个错字)。

To check your installation you can type sys.path in qgis console:
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
Security warning: typing commands from an untrusted source can harm your computer
import sys
sys.path
['/usr/share/qgis/python','/home/<username>/.local/share/QGIS/QGIS3/profiles/default/python','/home/<username>/.local/share/QGIS/QGIS3/profiles/default/python/plugins','/usr/share/qgis/python/plugins','/usr/lib64/python39.zip','/usr/lib64/python3.9','/usr/lib64/python3.9/lib-dynload','/home/<username>/.local/lib/python3.9/site-packages','/usr/local/lib/python3.9/site-packages','/usr/lib64/python3.9/site-packages','/usr/lib/python3.9/site-packages','/home/<username>/.local/share/QGIS/QGIS3/profiles/default/python']

一切顺利,