[QGIS-Developer] Updating qgis:rastercalculator to work better in models

Rudi von Staden rudivs at gmail.com
Fri Jun 29 13:35:05 PDT 2018


Hi all,

I'm trying to make some updates to qgis:rastercalculator in processing
to make it work better in models. The raster calculator evaluates
expressions based on the source file name. The problem with using this
in the model builder is that the file names are not always known up
front.

The list of layers in the expression widget are of class
QgsProcessingModelChildParameterSource, so I'm trying to figure out
how to set an appropriate source name for different situations.

1. The layer is an input parameter
In this case there's no way to know the source name up front, but it
could be approximated by lyr.parameterName(). This assumes that the
parameterName and the source name of the layer are the same. It's a
bit of a stretch, but I can't think of a better way to do it.

2. The layer is an anonymous layer output by another algorithm
In this case the name of the output file can be known for certain from
lyr.outputName(). This adds another problem though, since most
algorithms have the same outputName ('OUTPUT'). I have worked around
this by adding an incrementing number to the end of the index (i.e.
OUTPUT, OUTPUT1, OUTPUT2, etc.). It's not perfect but at least then
there would be a viable way to build expressions with multiple inputs.

3. The layer is a named output from another algorithm
If the algorithm has a named output, then the source file name would
be a predictable composite of lyr.outputChildId() and the 'Parameter
name'. I expected to get this from lyr.outputName() once set, but it
seems that outputName is not updated (it remains OUTPUT for most
algorithms). Is there some other way to get the 'Parameter name'
string from QgsProcessingModelChildParameterSource (or some other
way)?

Handling these three situations would improve the situation, but it's
still a bit clunky (and complicated for users to understand). Another
option would be to forego the source() names altogether, and use an a,
b, c notation like many of the other raster calculators do. The
challenge then is to have the layer identifiers in the expression
builder match the layer identifiers that are passed to
QgsRasterCalculator. One way to achieve this would be to have the
'Layers' list in the expression builder show only layers that are
selected as 'Reference layers'. That way, only the layers that are
passed to Raster Calculator as parameters could be included in the
expressions, and there can be a consistent referencing pattern whether
you are using it as a standalone algorithm or in a model. I'm just not
sure how to get the list of selected Reference layers from inside
ExpressionWidgetWrapper. If there's interest in reworking it in this
way and somebody can point me in the right direction I'd be happy to
give it a go.

Some background to this here: https://issues.qgis.org/issues/19302

Kind regards,
Rudi


More information about the QGIS-Developer mailing list