I'm porting my QGIS processing plugin algorithm from 2x - 3x.
The algorithm generates and outputs one to many rasters and adds them to the QGIS map canvas. The number of output rasters is unknown until the algorithm is executed.
In my QGIS 2x version, I set the output of the algorithm to be a processing.core.outputs.OutputDirectory
, save all the output rasters in that directory, then add them to the map using the QgsMapLayerRegistry.instance().addMapLayer
method. This is a bit of a kludge as it means the outputs can't automatically be used as inputs to other processing algorithms, but the 2x version was just a quick proof of concept demo.
Other than doing something similar to my 2x version (i.e. set a QgsProcessingParameterFolderDestination
/QgsProcessingOutputFolder
and add them to the map via QgsProject.instance().addMapLayer
), is there a recommended approach for QGIS 3x processing algorithm plugins to handle an unknown number of outputs? Perhaps something analogous to an arcpy.Parameter
with direction='output'
and multiValue=True
properties.
I would like to add the output rasters to the map when the alg is run manually from the toolbox (which I can), but this is not the primary aim of this question. It's more about having the multiple output rasters usable as inputs to other algorithms in a workflow.
1 Answer 1
There was a missing API call to allow these outputs to be used within a model - see: https://github.com/qgis/QGIS/pull/6308.
However, if you're just trying to add the layers to the project (and not use them within a bigger model), you can call
context.addLayerToLoadOnCompletion( path_to_raster, QgsProcessingContext.LayerDetails( name = 'name to use for this layer when added to the project', project=context.project() )
for each layer generated within your model. That's the thread-safe way to do this (directly adding to the project is NOT thread safe, and you'll run into issues with this approach)
Explore related questions
See similar questions with these tags.