4

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.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Feb 6, 2018 at 2:27
0

1 Answer 1

5

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)

Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Feb 11, 2018 at 1:07
0

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.