5

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()
  • 1
    Can't confirm as I am not really using 2.99 but try replacing runandload() with runAndLoadResults(). – Joseph Nov 15 '17 at 11:04
  • @Joseph I tried this bu it says TypeError: runAndLoadResults() takes from 2 to 4 positional arguments but 10 were given – Mustafa Uçar Nov 15 '17 at 12:20
  • I also get this error in another script: AttributeError: module 'processing' has no attribute 'RunAndLoadResults' – Mustafa Uçar Nov 15 '17 at 12:46
8

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.

8

I followed @Joseph directions and it helps me to see another visions. I opened processing toolbox in QGIS and complete missing parameters. Here is the last version:

import processing   
rasterLyr = QgsRasterLayer("C:/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,
                'COMBINED': False,
                'ALTITUDE': 45,
                'MULTIDIRECTIONAL': False,
                'OUTPUT': "C:/qgis_data/dem/hillshade.tif"}

processing.runAndLoadResults('gdal:hillshade',parameters)
  • Good catch! I think when I posted, I was using a slightly older version which probably explains why those parameters didn't show up for me. Glad you got it working though :) – Joseph Nov 20 '17 at 10:40
  • 1
    I am happy to hear that :) – Mustafa Uçar Nov 20 '17 at 12:20
  • @MustafaUçar please accept the answer that solved your problem so this thread can be marked as solved – underdark Jan 20 '18 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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