TransWikia.com

Different layer.name() in batchmode in QGIS processing script

Geographic Information Systems Asked on April 22, 2021

Different layer.name() in single layer processing and batchmode in QGIS scripts.

QGIS version 3.10.7.

My example script:
# -*- coding: utf-8 -*-

"""
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink)
from qgis import processing


class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
   
    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        """
        Returns a translatable string with the self.tr() function.
        """
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return ExampleProcessingAlgorithm()

    def name(self):
        return 'myscript'

    def displayName(self):
        return self.tr('My Script')

    def group(self):
        return self.tr('Example scripts')

    def groupId(self):
        return 'examplescripts'

    def shortHelpString(self):
        return self.tr("Example algorithm short description")

    def initAlgorithm(self, config=None):
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )
        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('Output layer')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
       
        feedback.pushInfo(parameters['INPUT'])

        source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )

        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        (sink, dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context,
            source.fields(),
            source.wkbType(),
            source.sourceCrs()
        )

        # Send some information to the user
        feedback.pushInfo('CRS is {}'.format(source.sourceCrs().authid()))
   

        if sink is None:
            raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))

        # Compute the number of steps to display within the progress bar and
        # get features from source
        total = 100.0 / source.featureCount() if source.featureCount() else 0
        features = source.getFeatures()

        for current, feature in enumerate(features):
            # Stop the algorithm if cancel button has been clicked
            if feedback.isCanceled():
                break

            # Add a feature in the sink
            sink.addFeature(feature, QgsFeatureSink.FastInsert)

            # Update the progress bar
            feedback.setProgress(int(current * total))

        if False:
            buffered_layer = processing.run("native:buffer", {
                'INPUT': dest_id,
                'DISTANCE': 1.5,
                'SEGMENTS': 5,
                'END_CAP_STYLE': 0,
                'JOIN_STYLE': 0,
                'MITER_LIMIT': 2,
                'DISSOLVE': False,
                'OUTPUT': 'memory:'
            }, context=context, feedback=feedback)['OUTPUT']

        return {self.OUTPUT: dest_id}

Outputs from single mode:

QGIS verze: 3.10.13-A Coruna
QGIS revize kódu: 311f3bbade
Qt verze: 5.11.2
GDAL verze: 3.1.4
GEOS verze: 3.8.1-CAPI-1.13.3
PROJ verze: Rel. 6.3.2, May 1st, 2020
Zpracovávám algoritmus...
Algoritmus 'My Script' se spouští…
Vstupní parametry:
{ 'INPUT' : 'G:/work/weird_layer_name/wl/layers/example.gpkg|layername=example', 'OUTPUT' : 'TEMPORARY_OUTPUT' }

example_82da6a66_eea8_4ad9_8b94_f7fbd945449f
CRS is EPSG:4326
Spuštění dokončeno za 0.10 sekund
Výsledky:
{'OUTPUT': 'Output_layer_076e3e5f_5400_409b_9e59_9224f5be8e15'}

Načítám výsledné vrstvy
Algoritmus 'My Script' dokončen

Layer.name() = example_82da6a66_eea8_4ad9_8b94_f7fbd945449f

Outputs from batchmode:

Zpracovávám algoritmus 1/2...
Algoritmus My Script se spouští…
Vstupní parametry:
{'INPUT': 'example',
'OUTPUT': <QgsProcessingOutputLayerDefinition {'sink':G:/work/weird_layer_name/wl/layers/result1.gpkg, 'createOptions': {}}>}

example
CRS is EPSG:4326
Algoritmus My Script v poøádku spuštìn...
Execution completed in 2.04 seconds
Výsledky:
{'OUTPUT': 'G:/work/weird_layer_name/wl/layers/result1.gpkg'}

Naèítám výsledné vrstvy

Zpracovávám algoritmus 2/2...
Algoritmus My Script se spouští…
Vstupní parametry:
{'INPUT': 'Output layer',
'OUTPUT': <QgsProcessingOutputLayerDefinition {'sink':G:/work/weird_layer_name/wl/layers/result2.gpkg, 'createOptions': {}}>}

Output layer
CRS is EPSG:4326
Algoritmus My Script v poøádku spuštìn...
Execution completed in 0.28 seconds
Výsledky:
{'OUTPUT': 'G:/work/weird_layer_name/wl/layers/result2.gpkg'}

Naèítám výsledné vrstvy
Batch execution completed in 2.40 seconds

Layer.name() = example

Different between batch and single mode is example_82da6a66_eea8_4ad9_8b94_f7fbd945449f (single) and example (batchmode).

Is this different bug or true result?

In QGIS version 3.16 are layer.name() equal.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP