[Qgis-user] How to use QgsProcessingLayerPostProcessorInterface?

Etienne Trimaille etienne.trimaille at gmail.com
Wed Jan 20 16:25:29 PST 2021

Can you try that?

class Renamer(QgsProcessingLayerPostProcessorInterface):    instance =
None    def postProcessLayer(self, layer, context, feedback):
layer.setName('DiffLayer')    @staticmethod    def create() ->
'Renamer':        Renamer.instance = Renamer()        return

In your processAlgorithm method() :

    data[layer].id()                ).setPostProcessor(
    Renamer.create()                )

Le jeu. 21 janv. 2021 à 03:14, <qgis-user at stripfamily.net> a écrit :

> I thought QgsProcessingLayerPostProcessorInterface might be a solution to
> my earlier question about renaming the output layer of Processing script.
> I derive a class and overload postProcessLayer:
> class Renamer (QgsProcessingLayerPostProcessorInterface):
>     def postProcessLayer(self, layer, context, feedback):
>         layer.setName('DiffLayer')
>         print('Renaming layer in post-processor')
> Near the end of my processAlgorithm implementation I add:
> details = context.LayerDetails(dest_id,context.project())
> details.setPostProcessor(Renamer())
> where dest_id is the value returned by parameterAsSink
> The postProcessLayer method does not appear to run (no output is produced
> by the print statement) and the layer is added, but the name is unchanged.
> I'm not sure if I'm setting up the post process wrong, or not fetching the
> right LayerDetails object.
> Any pointer to how this should be used or an example of its use would be
> appreciated.
> Thanks
> David
> PS - I also tried the postProcessAlgorithm of the QgsProcessingAlgorithm
> class, but the layer hasn't been added yet when the method is called. I
> wonder if I'll have the same problem with the layer post processor?
> PPS I tried postProcess, but it doesn't appear to get called as far as I
> can tell. Not sure why.
>     def postProcess(self, context, feedback):
>         print('Number of layers = ' +
> str(len(QgsProject.instance().mapLayers())))
>         print("PostProcess completed")
>         feedback.pushInfo(1)  # This line should generate an error since
> pushInfo expects a string, but no error is thrown
>         return {self.OUTPUT: self.dest_id}
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210121/c66a1e01/attachment-0001.html>

More information about the Qgis-user mailing list