[QGIS-Developer] ProcessingAlgorithm: get filename of the INPUT param

Richard Duivenvoorde rdmailings at duif.net
Wed Jun 19 05:20:50 PDT 2019


Hi,

got a question from somebody who needed to find the filename of an input
param in a ProcessingAlgorithm
(because he is going to read that file again or something like that).

He adds the INPUT via the initAlgorithm function:

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

And then tried to get the filename in the
processAlgorithm-implementation from the source via
self.parameterAsSource, but then could not find an uri/filename.

Via:
https://gis.stackexchange.com/questions/280667/using-a-selected-layer-with-processing-in-qgis-3
I gave him:
l = self.parameterAsVectorLayer(parameters, 'INPUT', context)
feedback.pushInfo('l = {}'.format(l))
feedback.pushInfo('l.dataProvider() = {}'.format(l.dataProvider()))
feedback.pushInfo('l.dataProvider().uri() =
{}'.format(l.dataProvider().uri()))
feedback.pushInfo('l.dataProvider().uri().uri() =
{}'.format(l.dataProvider().uri().uri()))

which at least gives him the dataprovider uri WITH the filename.

Question:
1) isn't there an easier way, or is this the preferred way?
2) if the filename contains a space, then the uri().uri() only shows the
part AFTER the space (as if it was a relative path, but then the wrong
path...). Using a filename from a dir WITHOUT a space the uri().uri()
shows you the full file path.

TIA & Regards,

Richard Duivenvoorde


More information about the QGIS-Developer mailing list