1

I am building a custom processing script using the QgsProcessingAlgorithm class.

In the early part of my algorithm, I am processing an input feature using the native 'centroids' algorithm. I want to access this output and continue with my algorithm, however the 'OUTPUT' key from the processing.run call only returns a string.

I initialize the input feature as:

   def initAlgorithm(self, config=None):
        """Defines input and output."""

        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.REFERENCE,
                "Choose input grid",
                [QgsProcessing.TypeVectorAnyGeometry]
                )
            )

and inside the processAlgorithm() method I use:

        # get centroids
        parameters_centroids = {
            "INPUT": parameters[self.REFERENCE],
            "ALL_PARTS": False,
            "OUTPUT": "memory":
        }
        centroids = processing.run("native:centroids", 
            parameters_centroids, is_child_algorithm=True,
            context=context, feedback=feedback)["OUTPUT"]
        if feedback.isCanceled():
            return {}
        
        feedback.pushInfo(f"{type(centroids)}")

The actual process runs without errors and gives feedback 'Results: {'OUTPUT': 'output_22428702_fd31_459d_9454_fce67f3e9f14'}' (or some other memory pointer).

The feedback.pushInfo() call gives <class 'str'> and prevents me from further processing the output from the centroids algorithm. I would expect the type to be a QgsVectorLayer.

Is this because of saving the output to 'memory:'?

This is an intermediate step in the algorithm I would happily avoid saving the centroids output. I believed loading layers only had to be done when processing from the console.

3
  • The string is likely a reference to the layer's in-memory ID. Try centroids = QgsVectorLayer(centroids, 'centroids') and continuing with your processing as intended.
    – Matt
    Mar 16 at 15:53
  • I tried this but it returns a QgsVectorLayer with a 'featureCount()' of -2 ? Also, I tried my first attempt while looking at for example this tutorial script where they get the layer directly from the 'OUTPUT' key.
    – anksvans
    Mar 16 at 16:52
  • An update: If I return the intermediate layer as an OUTPUT, I can see that the process has been running as expected and the output is indeed a point vector layer. However I still want to access it as a vector layer within my script (access methods like 'featureCount()'). Matt's comment leaves me with a vector layer that is not behaving as the output from the process.
    – anksvans
    Mar 17 at 8:28

1 Answer 1

1

I found the solution looking at a related question.

The solution lies in retrieving the output layer into the processing script's context using the 'QgsProcessingContext' and its 'takeResultLayer' which can take a context and a memory id (string) as arguments. For me, it was used as follows


# get centroids
parameters_centroids = {
    "INPUT": parameters["REFERENCE"],
    "ALL_PARTS": False,
    "OUTPUT": "memory:"
}
centroids = processing.run("native:centroids", 
    parameters_centroids, is_child_algorithm=True,
    context=context, feedback=feedback)["OUTPUT"]
    if feedback.isCanceled():
        return {} 
centroids_layer = QgsProcessingContext.takeResultLayer(context, centroids) # type QgsVectorLayer

# centroids_layer can now be processed further.. like:
number_of_features = centroids_layer.featureCount()

note that context is an attribute of the processAlgorithm class using the current QGIS framework for processing scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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