Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postProcessLayer() of QgsProcessingLayerPostProcessorInterface producing inconsistent results #48087

Closed
2 tasks done
ar-siddiqui opened this issue Apr 4, 2022 · 1 comment
Closed
2 tasks done
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Processing Relating to QGIS Processing framework or individual Processing algorithms

Comments

@ar-siddiqui
Copy link

ar-siddiqui commented Apr 4, 2022

What is the bug or the crash?

In a processing script, the recommended practice to rename the layer and apply style is through postProcessLayer() of QgsProcessingLayerPostProcessorInterface.

I want to apply styles to all layers in my processing script and rename them. I am using the above-recommended method to achieve this.

I have written a script that works most of the time but not all the time. I ran the script multiple times to ensure that nothing in the code was changing yet the QGIS applied style sometimes and sometimes not.

Here is my function that applies styles, full code in the attachment below.

def run_alg_styler(display_name, layer_color1, layer_color2):
    """Create a New Post Processor class and returns it"""
    # Just simply creating a new instance of the class was not working
    # for details see https://gis.stackexchange.com/questions/423650/qgsprocessinglayerpostprocessorinterface-only-processing-the-last-layer
    class LayerPostProcessor(QgsProcessingLayerPostProcessorInterface):
        instance = None
        name = display_name
        color1 = layer_color1
        color2 = layer_color2

        def postProcessLayer(self, layer, context, feedback):
            feedback.pushInfo(f'Inside postProcessLayer: {self.name}')
            if layer.isValid():
                feedback.pushInfo(f'Layer valid: {self.name}')
                layer.setName(self.name)

                prov = layer.dataProvider()
                stats = prov.bandStatistics(
                    1, QgsRasterBandStats.All, layer.extent(), 0
                )
                min = stats.minimumValue
                max = stats.maximumValue
                renderer = QgsSingleBandPseudoColorRenderer(
                    layer.dataProvider(), band=1
                )
                color_ramp = QgsGradientColorRamp(
                    QColor(*self.color1), QColor(*self.color2)
                )
                renderer.setClassificationMin(min)
                renderer.setClassificationMax(max)
                renderer.createShader(color_ramp)
                layer.setRenderer(renderer)
            else:
                feedback.pushInfo(f'Layer not valid: {self.name}')

        # Hack to work around sip bug!
        @staticmethod
        def create() -> "LayerPostProcessor":
            LayerPostProcessor.instance = LayerPostProcessor()
            return LayerPostProcessor.instance

    return LayerPostProcessor.create()

Please see this demo:

2022-04-04.18-03-42_Trim.online-video-cutter.com.1.mp4

Steps to reproduce the issue

  1. Open the project in the attached zip
  2. Run the script in the attached zip
  3. Run multiple times again until you see styles are not applied

style_issue.zip

Versions

3.22.4

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

  • I tried with a new QGIS profile

Additional context

https://gis.stackexchange.com/questions/423650/qgsprocessinglayerpostprocessorinterface-only-processing-the-last-layer

@ar-siddiqui ar-siddiqui added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Apr 4, 2022
@Pedro-Murteira Pedro-Murteira added the Processing Relating to QGIS Processing framework or individual Processing algorithms label Apr 5, 2022
@alexbruy
Copy link
Contributor

Duplicates #47533.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Processing Relating to QGIS Processing framework or individual Processing algorithms
Projects
None yet
Development

No branches or pull requests

3 participants