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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Richard Duivenvoorde
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
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: ProcessingAlgorithm: get filename of the INPUT param

info-8
Hi,

l.source() perhaps?

Cheers,
Benoit


On 2019-06-19 14:20, Richard Duivenvoorde wrote:

> 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
> _______________________________________________
> QGIS-Developer mailing list
> [hidden email]
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: ProcessingAlgorithm: get filename of the INPUT param

Richard Duivenvoorde
Nope,

SOURCE = <qgis._core.QgsProcessingFeatureSource object at 0x7fff684dcee8>

which (I think) does not bring you any further:

https://qgis.org/api/classQgsProcessingFeatureSource.html

unless you 'materialize' it to a layer:

https://qgis.org/api/classQgsFeatureSource.html

which is what I actually do below

Regards,

Richard



On 19/06/2019 15.26, info wrote:

> Hi,
>
> l.source() perhaps?
>
> Cheers,
> Benoit
>
>
> On 2019-06-19 14:20, Richard Duivenvoorde wrote:
>> 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
>> _______________________________________________
>> QGIS-Developer mailing list
>> [hidden email]
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: ProcessingAlgorithm: get filename of the INPUT param

Nyall Dawson
In reply to this post by Richard Duivenvoorde
On Wed, 19 Jun 2019 at 22:20, Richard Duivenvoorde <[hidden email]> wrote:
>
> 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).

Short answer -- it's not possible, and if you DO find a workaround, DON'T do it.

By design feature sources aren't layers, and the features coming from
a feature source may not even be associated with any layer at all. If
you need to get the path to a input file, you should be using a vector
layer parameter instead (but be very careful of thread safety issues,
and consider returning the FlagNoThreading flag for your algorithm to
ensure it's not crashy)

Nyall
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer