TransWikia.com

Getting file path from vector layer object in QGIS processing framework

Geographic Information Systems Asked by dericke on April 22, 2021

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

I was able to get the path using the following:

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

Correct answer by dericke on April 22, 2021

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:someFolderanotherFolderSomeData.csv THEN the output will be c:tempsomeData.gpkg

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

Answered by user3226314 on April 22, 2021

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