3

I am working on a processing script in QGIS 3, which does the following:

  • Take a few inputs
  • Run a native algorithm on these inputs, saving the resulting new layer in memory
  • Set up the symbology for the layer (specifically, setting the renderer via layer.setRenderer(renderer_instance))
  • Return the layer, retaining its symbology, as the primary algorithm output

The key here is that I want to return a layer after setting up its symbology. This is problematic because I'd like to use the common input parameter QgsProcessingParameterFeatureSink, since this lets the user choose how to output the results (memory layer, temporary file, saved in a format of their choice, etc).

However, as far as I can tell, the ...ParameterFeatureSink parameter is primarily used with QgsProcessingAlgorithm's parameterAsFeatureSink method, which returns an interface to a new layer, which does not allow setting up symbology. Also, it appears that this layer is what the algorithm expects to be returned at the end, and that returning another layer (like the one described in the list above) in its place causes an error.

My question is this: how do I utilize the strengths of QgsProcessingParameterFeatureSink, while still being able to return a layer with its symbology set up as described?

The ideal solution here would be to use another of the parameterAs methods to get a place or name for the layer I'll eventually return. I could feed that name to the internal algorithm as its OUTPUT parameter, making it return a layer that my algorithm could subsequently return without generating an error.

1 Answer 1

2

All right, it looks like there's an official example of this now, at https://docs.qgis.org/testing/en/docs/user_manual/processing/scripts.html. It appears that one simply passes an element from the special parameters dictionary to a child algorithm as its OUTPUT (or other) element, via the child's input parameters dictionary. That way, the child algorithm's output will go to wherever the user specifies in the "run" dialog, and the resulting layer can still have its symbology edited afterward.

Yep, it's confusing. Just look at the example code above - that should illustrate it better than I can describe it in words.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.