4

I'm doing some DTM processing, the first couple of stages are a slope calculation and then a Raster calculator process using QGIS's native raster calculator. I want to use the output layer from my slope calculation within the formula. Slope is done as follows, and works fine:

 Slope = processing.run("native:slope", {
 'INPUT':set_NoData_toNull['OUTPUT'], #There is a step before, Nullifying <0 values
 'Z_FACTOR':1,
 'OUTPUT': 'TEMPORARY_OUTPUT' })

The calculator is to then remove any Raster values lower than a 45 degree slope, this formula works when using on a named Raster layer and replacing the @1 with "layername@1", but to save processing power I dont want to save the output of "Slope" every time i run this script, and instead want to use the 'TEMPORARY_OUTPUT' of "Slope", though after much reading I still can't work out how I should Call this in the Raster Calculator 'EXPRESSION' field.

 calculatedOver45deg = processing.run("qgis:rastercalculator", {
 'EXPRESSION':'((\"@1\">45)*\"@1\") / ((\"@1\">45)*1 + (\"@1\"<=45)*0)',
 'LAYERS': Slope['OUTPUT'],
 'CELLSIZE':None,
 'EXTENT':None,
 'CRS':QgsCoordinateReferenceSystem('EPSG:32633'),
 'OUTPUT': 'TEMPORARY_OUTPUT' })

Does anyone know what 'TEMPORARY_OUTPUT' from slope is named and therefore how it can be used in the expression in replacement of @1.

Thanks

asked Mar 21, 2020 at 15:31
0

2 Answers 2

5

Following script (with path to my first layer directly: utah_demUTM2_clip in below image) works for me. I used 'runAndLoadResults' processing method instead 'run' method.

import processing
layer = iface.activeLayer()
parameters = {'INPUT': layer,
 'Z_FACTOR':1,
 'OUTPUT': 'TEMPORARY_OUTPUT' }
processing.runAndLoadResults("native:slope", parameters)
calculatedOver45deg = processing.runAndLoadResults("qgis:rastercalculator", {
 'EXPRESSION':'((\"Slope@1\">45)*\"Slope@1\") / ((\"Slope@1\">45)*1 + (\"Slope@1\"<=45)*0)',
 'LAYERS': Slope['OUTPUT'],
 'CELLSIZE':None,
 'EXTENT':None,
 'CRS':QgsCoordinateReferenceSystem('EPSG:32633'),
 'OUTPUT': 'TEMPORARY_OUTPUT' })

After running it in Python Console of QGIS 3, I got result of following image:

enter image description here

In Map Legend, Output is the resulting layer of operations with "qgis:rastercalculator" Processing method.

answered Mar 22, 2020 at 0:47
3

Your variable OUTPUT['TEMPORARY_OUTPUT'] contains a string that is an internal ID to the created layer.

So if you do

layer=QgsProject.instance().mapLayer(output['TEMPORARY_OUTPUT'])
name=layer.name()

You should have the name of the layer

Although, as xunilk said in his answer, the name of the layer is always 'Output'. The problem is that you may end up grabbing the wrong layer in case you are doing more processing. Having the layer variable, you are able to programatically give your layer a unique and/or descriptive name,

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Mar 22, 2020 at 6:17
1
  • Thanks a lot, both yours an xunilk answers where exactly what i was looking for :) Commented Mar 22, 2020 at 20:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.