2

maybe a simple question but I cannot solve it.

In a simple Processing script I have defined a parameter as QgsProcessingParameterFolderDestination where I want to save some files (vector or raster) that will be created by the script.

The problem is that I cannot get the path of the chosen folder.

Here a simplified version of the script that actually does not do anything, just printing in the log tab the chosen path.

I'm defining a QgsProcessingParameterFolderDestination and I'm trying to get the path of the chosen folder (if chosen, else Processing will take care of the temporary path) with self.parameterAsString. The

# -*- coding: utf-8 -*-

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


class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):

    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        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 initAlgorithm(self, config=None):

        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

        self.addParameter(
            QgsProcessingParameterFolderDestination(
                self.OUTPUT,
                self.tr('Output layer')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):

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

        output = self.parameterAsString(
            parameters,
            self.OUTPUT,
            context
        )

        feedback.pushDebugInfo(output)


        return {self.OUTPUT: output}

UPDATE: fixed now: https://github.com/qgis/QGIS/commit/d157b6e66173

  • I don't see the problem here - in your processingAlgorithm method the output object is the path of the chosen folder, which is what you want. This works for me - if I put /foo/bar in the output folder choice box, I see that sent to the output by feedback.pushDebugInfo... – Spacedman May 29 '18 at 7:19
  • If this really isn't working for you, could you give us the precise version number of QGIS? I'm using 3.0.3 – Spacedman May 29 '18 at 7:29
  • that's weird. So for you the script of above prints your chosen path to in the Log tab? In my case, even if I add createOutput=True the output is still /tmp/processing_320e1069a41a40f2924ec2569a6d67f5/efb011cb999d4ede8c3cb4c70955b6b7/OUTPUT – matteo May 29 '18 at 7:57
  • Wait a second.. if I write the path manually it works, if I choose the path with the button I have to type a space or something in the string. It seems that it is not able to recognize the path if taken from somewhere, only if I type something in the box. – matteo May 29 '18 at 8:03
  • Uh sorry yes. I'm on a Linux Debian Sid machine with QGIS master compiled from source. – matteo May 29 '18 at 8:34

Your Answer

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

Browse other questions tagged or ask your own question.