2

Using the QGIS 3.x processing framework, given a vector layer as an input parameter:

(from the default script template)

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

How can I get this layer's source file path as a string (assuming that only file-based data sources will be used, not PostGIS tables, etc)?

2 Answers 2

8

I was able to get the path using the following:

self.parameterDefinition('INPUT').valueAsPythonString(parameters['INPUT'], context)
0
1

This answer might be slightly off-topic, but your question keeps coming up whilst searching for the graphical modeller answers.

If you are using the graphical modeller, you can use the parameter function to retrieve a string of any inputs. Unfortunately, you can not use the shorthand @parameter way of accessing data if you want a string.

The below code takes a folder input (outputFolder) and a vector layer input (dataFile) and produces a new string. i.e.:

IF outputFolder == c:\temp AND dataFile == c:\someFolder\anotherFolder\SomeData.csv THEN the output will be c:\temp\someData.gpkg

concat(
    @outputFolder,
    '\\',
    replace(file_name(to_string(parameter('dataFile'))),'.csv','.gpkg')
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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