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

問題:在QGIS窗口下運(yùn)行內(nèi)部的Python代碼,出現(xiàn)錯(cuò)誤

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:\\雜項(xiàng)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')

原因:原來(lái)的代碼是在QGIS2.0版本下編寫的,采用processing.runalg('gdalogr:convertformat', ...現(xiàn)在安裝的是QGIS3.0版本,在該版本下,需采用processing.run('gdal:convertformat', ...

解決方法:修改為processing.run(),但同時(shí)函數(shù)的參數(shù)輸入需要調(diào)整,可以通過輸入processing.algorithmHelp("qgis:zonalstatistics") 獲取需要輸入的參數(shù)。該版本下的調(diào)用方式是

        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

最后附上在本文的情況下,得到的工具箱調(diào)用參數(shù)是

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:  <QgsProcessingOutputVectorLayer>
    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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容