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)?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 13, 2019 at 2:18

2 Answers 2

8

I was able to get the path using the following:

self.parameterDefinition('INPUT').valueAsPythonString(parameters['INPUT'], context)
answered Feb 15, 2019 at 0:29
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')
)
Pierrick Rambaud
3,4151 gold badge17 silver badges62 bronze badges
answered Apr 16, 2021 at 2:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.