1

I've written a script which automatically creates a print layout and exports it. Now I'm trying to integrate the code into my plugin's code. The plugin has some existing functionality and I'm adding this on to it. I'm using the existing code from previous steps as a template.

I create a class with an initAlgorithm which looks like this:

class CreatePrintLayoutAndExportMap(QgsProcessingAlgorithm):
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
    INPUT_VECTOR = 'INPUT_VECTOR'
    INPUT_TITLE = 'INPUT_TITLE'
    INPUT_SUBTITLE = 'INPUT_SUBTITLE'
    INPUT_CREDIT_TEXT = 'INPUT_CREDIT_TEXT'
    INPUT_CREDIT_TEXT_DEFAULT = "Default Credit Text"

    def initAlgorithm(self, config):
        """
        Here we define the inputs and output of the algorithm
        """
        #Add Vector Layer as input
        self.addParameter(
            QgsProcessingParameterVectorLayer(
                self.INPUT_VECTOR,
                self.tr('Input original vector layer of study area used in Step 1'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

        #Add String as input
        self.addParameter(
            QgsProcessingParameterString(
                self.INPUT_TITLE,
                self.tr('Input title string (Optional)'),
                " "                                         
            )
        )

Now I define my processAlgorithm, where the code for this part of the plugin will run. The processAlgorithm currently looks like this:

def processAlgorithm(self, parameters, context, feedback):
    """This actually does the processing for creating the print layout and exporting as .pdf"""
    #needs all the arguments (self, parameters, context, feedback)

    log = feedback.setProgressText

    input_vector = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR, context)
    input_title = self.parameterAsString(parameters, self.INPUT_TITLE, context)

    log(f"Input vector name: {input_vector.name()}")            
    log(f"Title: {input_title}")                    

I run the plugin. Right now I am just printing the name of the vector input layer and a string for the input title (which the user writes).

In the results, the plugin prints the results correctly and then returns an error: enter image description here

I don't know why the processAlgorithm is producing an "invalid result". Any suggestions?

3

I think you need to return a dictionary from the processAlgorithm method. https://qgis.org/pyqgis/3.0/core/Processing/QgsProcessingAlgorithm.html#qgis.core.QgsProcessingAlgorithm.processAlgorithm

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.