[QGIS-Developer] Programmatically iterate over features with Processing in QGIS 3

Nyall Dawson nyall.dawson at gmail.com
Thu Sep 13 16:59:17 PDT 2018


On Thu, 13 Sep 2018 at 15:40, DelazJ <delazj at gmail.com> wrote:
>
> Hi,
> CCing to qgis-dev as nobody in community seems... inspired by the question and I'd like to close that old PR and rebase other changes.
>
> Thanks for any hint.
>
> Harrissou
>
> Le mar. 11 sept. 2018 à 18:12, DelazJ <delazj at gmail.com> a écrit :
>>
>> Hi all,
>>
>> QGIS documentation states [0] that Processing can be used to iterate over features with the following code:
>>
>> import processing
>> features = processing.features(layer)
>> for feature in features:
>>     # do whatever you need with the feature
>>
>> This code no longer works in QGIS3 and needs an update [1] I fail to find.
>> So anyone to suggest the right replacement, please?

Just iterate directly over the features from the source. The things
which processing.features used to do is taken care of transparently
for you now.

I.e.

    source = self.parameterAsSource(parameters, 'INPUT', context)
    for f in source.getFeatures():
       # do something with f

As much as possible, Processing-specific methods were removed in QGIS
3.0 in favour of the standard PyQGIS calls. This was done to reduce
code duplication, and make the API more consistent (i.e. easier to
move code between Processing algorithms -> standalone scripts/plugins
and vice versa).

Nyall


More information about the QGIS-Developer mailing list