QGIS 3 Using Processing.runandload – Practical Guide

hillshadepyqgisqgis-3qgis-processing

I want to create and elevation hillshade but I found the script for QGIS 2.18. What should I use instead of processing.runandload? Here is my script on QGIS 2.99 python console:

import processing
rasterLyr = QgsRasterLayer("C:/Users/Mustafa Uçar/Desktop/Tutorial/qgis_data/dem/dem.asc", "hillshade")
rasterLyr.isValid()
processing.runandload("gdalogr:hillshade", rasterLyr, 1, False, False,1.0, 1.0, 315.0, 45.0, "C:/Users/Mustafa Uçar/Desktop/Tutorial/qgis_data/dem/hillshade.tif")
#to verify the output image
processing.runandload()

Best Answer

I think with the new QGIS 3.0, it is much easier to incorporate the input parameters of an algorithm into a dictionary which can then be called using:

processing.runAndLoadResults()

which is the equivalent to the QGIS <= 2.18 version:

processing.runandload()

So your code could look like:

import processing
rasterLyr = QgsRasterLayer("C:/Users/Mustafa Uçar/Desktop/Tutorial/qgis_data/dem/dem.asc", "hillshade")
rasterLyr.isValid()

parameters = {'INPUT': rasterLyr, 
                'BAND': 1, 
                'COMPUTE_EDGES': False,
                'ZEVENBERGEN': False,
                'Z_FACTOR': 1.0,
                'SCALE': 1.0,
                'AZIMUTH': 315,
                'ALTITUDE': 45,
                'OUTPUT': "C:/Users/Mustafa Uçar/Desktop/Tutorial/qgis_data/dem/hillshade.tif"}

processing.runAndLoadResults('gdal:hillshade', parameters)

Note that gdalogr:hillshade is now currently gdal:hillshade.

Related Question