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
2 Answers 2
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:
In Map Legend, Output is the resulting layer of operations with "qgis:rastercalculator" Processing method.
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,
-
Thanks a lot, both yours an xunilk answers where exactly what i was looking for :)Snowman– Snowman2020年03月22日 20:09:09 +00:00Commented Mar 22, 2020 at 20:09
Explore related questions
See similar questions with these tags.