2

In my python script based on Qgis 3.0.3-Girona, the processing.run() is completely ignored. There are similar questions that are marked as duplicate - but those do not address or solve the issue that I list here. Here is my script.... *

from qgis.core import (
     QgsApplication, 
     QgsProcessingFeedback, 
     QgsVectorLayer
)
from qgis.core import QgsApplication
# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix 
QgsApplication.setPrefixPath('e:/Qgis3/apps/qgis', True)
qgs = QgsApplication([], False)
qgs.initQgis()
# Append the path where processing plugin can be found
sys.path.append('e:\\QGIS3\\apps\\qgis\\qtplugins')
sys.path.append('e:\\QGIS3\\apps/Qt5\\plugins')
sys.path.append('e:\\QGIS3\\apps\\qgis\\python\\plugins')
sys.path.append("e:\\QGIS3\\apps\\Python36\\Lib\\site-packages")
from processing.core.Processing import Processing
Processing.initialize()
print("Finished importing")
params = { 
    'INPUT_RASTER':'E:\\arp\\LEAP\\s823_actfilt.tif',
    'RASTER_BAND':1,
    'INPUT_VECTOR':'E:\\arp\\LEAP\\test.shp',
    'COLUMN_PREFIX':'actfilt_',
    'STATS':[0,1,2],
}
feedback = QgsProcessingFeedback()
print("Starting zonalstats")
processing.run("qgis:zonalstatistics", params,feedback)
print("Finished zonalstats")

* The output prints "Starting zonalstats" ; "Finished importing" but never prints "Finished zonalstats". I tried several combinations including capturing the output to res variable etc. (as explained in other similar posts) but none of them work - the processing.run() is simply ignored. Not sure if it is a bug or something I'm doing wrong.

1
  • Capitalize the 'p' in Processing.run
    – geodranic
    Jun 7, 2018 at 17:26

1 Answer 1

1

Well, the issue turned out to be a minor one. I had failed to include "import processing". I post below the code that works. Also note that QGIS3 is rather quirky about the paths - the ones recommended in the documentation is not sufficient...hence my code goes overboard in including all - and hence it works. Also make sure you run the file with python3 (the default python is still python 2.7.5 in Qgis3). Hope this helps some who want to run standalone pyQGIS scripts in Qgis 3.

import sys, os
cwd = os.getcwd().replace("\\","/")

InVecFile = cwd+"/"+sys.argv[1] # Input vector/polygon file
InRasFile = cwd+"/"+sys.argv[2] # Input raster file
inx = sys.argv[2].split(".")
InPrefix = inx[0]+"_"

if len(sys.argv) < 2:
   print("USAGE: qgis3_ZonalStats.py <InVecFile> <InRasFile>")
   sys.exit()

from qgis.core import (
     QgsApplication, 
     QgsProcessingFeedback, 
     QgsVectorLayer
)

from qgis.core import QgsApplication

# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix 
QgsApplication.setPrefixPath('e:/Qgis3/apps/qgis', True)
qgs = QgsApplication([], False, None)
qgs.initQgis()

# Append the path where processing plugin can be found
sys.path.append('e:\\QGIS3\\apps\\qgis\\qtplugins')
sys.path.append('e:\\QGIS3\\apps/Qt5\\plugins')
sys.path.append('e:\\QGIS3\\apps\\qgis\\python')
sys.path.append('e:\\QGIS3\\apps\\qgis\\python\\plugins')
sys.path.append('E:\\Qgis3\\apps\\Python36\\DLLs')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib\\site-packages\\win32')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib\\site-packages\\win32\\lib')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib\\site-packages\\Pythonwin')
sys.path.append("e:\\QGIS3\\apps\\Python36\\Lib\\site-packages")
# Append location of DLLs to current system PATH envrionment variable
# os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;"

import processing
from processing.core.Processing import Processing
Processing.initialize()
print("Finished importing")

# You can see what parameters are needed by the algorithm  
# using: processing.algorithmHelp("qgis:union")
params = { 
    'INPUT_RASTER':InRasFile,
    'RASTER_BAND':1,
    'INPUT_VECTOR':InVecFile,
    'COLUMN_PREFIX':InPrefix,
    'STATS':[0,1,2],
}
feedback = QgsProcessingFeedback()
print("Starting zonalstats")
processing.run("qgis:zonalstatistics", params)
QgsApplication.exitQgis()
app.exit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.