QGIS processing AttributeError: module 'qgis.processing' has no attribute 'runalg'

问题:在QGIS窗口下运行内部的Python代码,出现错误

AttributeError: module 'qgis.processing' has no attribute 'runalg'

主要代码:

import os
import processing 
from qgis.core import *
from PyQt5.QtCore import *
from osgeo import osr, gdal,ogr
from gdalconst import *
from qgis import *

raster_path = "G:\\test\\"
vector_path = "E:\\杂项2020\\"

vlayer = iface.addVectorLayer(vector_path + "fishnet.shp", "nc", "ogr")

for file in os.listdir(raster_path):
    #read the raster layer 
    fileInfo = QFileInfo(file)
    baseName = fileInfo.baseName()
    rlayer = QgsRasterLayer(raster_path + file, baseName)
   
    # run algorithm
    processing.runalg('qgis:zonalstatistics', rlayer, 1.0, vlayer, file + '_',False, raster_path+ file + '.shp')
        
QgsMapLayerRegistry.instance().removeMapLayer('nc')

原因:原来的代码是在QGIS2.0版本下编写的,采用processing.runalg('gdalogr:convertformat', ...现在安装的是QGIS3.0版本,在该版本下,需采用processing.run('gdal:convertformat', ...

解决方法:修改为processing.run(),但同时函数的参数输入需要调整,可以通过输入processing.algorithmHelp("qgis:zonalstatistics") 获取需要输入的参数。该版本下的调用方式是

        params = { 
            'INPUT_RASTER' : rlayer,
            'RASTER_BAND' : 1.0, 
            'INPUT_VECTOR' : vlayer,
            'COLUMN_PREFIX': file + '_',
            'STATS': 12,
        }
        feedback = QgsProcessingFeedback()

        res = processing.run('qgis:zonalstatistics', params, feedback=feedback)
        temp_layer = res['INPUT_VECTOR'] # Access your output layer

最后附上在本文的情况下,得到的工具箱调用参数是

processing.algorithmHelp("qgis:zonalstatistics")
Zonal statistics (qgis:zonalstatistics)

This algorithm calculates statistics of a raster layer for each feature of an overlapping polygon vector layer.


----------------
Input parameters
----------------

INPUT_RASTER: Raster layer

    Parameter type: QgsProcessingParameterRasterLayer

    Accepted data types:
        - str: layer ID
        - str: layer name
        - str: layer source
        - QgsProperty
        - QgsRasterLayer

RASTER_BAND: Raster band

    Parameter type: QgsProcessingParameterBand

    Accepted data types:
        - int
        - QgsProperty

INPUT_VECTOR: Vector layer containing zones

    Parameter type: QgsProcessingParameterVectorLayer

    Accepted data types:
        - str: layer ID
        - str: layer name
        - str: layer source
        - QgsProperty
        - QgsVectorLayer

COLUMN_PREFIX: Output column prefix

    Parameter type: QgsProcessingParameterString

    Accepted data types:
        - str
        - QgsProperty

STATS: Statistics to calculate

    Parameter type: QgsProcessingParameterEnum

    Available values:
        - 0: Count
        - 1: Sum
        - 2: Mean
        - 3: Median
        - 4: Std. dev.
        - 5: Min
        - 6: Max
        - 7: Range
        - 8: Minority
        - 9: Majority (mode)
        - 10: Variety
        - 11: Variance
        - 12: All

    Accepted data types:
        - int
        - str: as string representation of int, e.g. '1'
        - QgsProperty

----------------
Outputs
----------------

INPUT_VECTOR:  
    Zonal statistics

参考资料:
https://gis.stackexchange.com/questions/274764/processing-runalg-throws-typeerror-catching-classes-that-do-not-inherit-from
https://gis.stackexchange.com/questions/279874/using-qgis3-processing-algorithms-from-standalone-pyqgis-scripts-outside-of-gui

你可能感兴趣的:(QGIS processing AttributeError: module 'qgis.processing' has no attribute 'runalg')