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
I was able to get the path using the following:
self.parameterDefinition('INPUT').valueAsPythonString(parameters['INPUT'], context)
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')
)