[Qgis-developer] iteration over a layer's features in python plugins

Nathan Woodrow madmanwoo at gmail.com
Mon Jun 17 14:47:17 PDT 2013


You don't need to use a while loop.  I'm really not sure people keep using
that pattern when you don't need to and makes you code more complicated.
list and iterator have the same next() method so they act the same
https://github.com/qgis/Quantum-GIS/blob/master/python/core/qgsfeatureiterator.sip#L12

You can just do:

for f in layer.getFeatures():

So now if you want to just get the selected or all you can do:

if getselection:
   features = layer.selectedFeatures()
else:
  features = layer.getFeatures()

So I hate/gald to say that your FeatureIterator is not neeeded because it
is built into QgsFeatureIterator.

Regards,
Nathan


On Mon, Jun 17, 2013 at 11:47 PM, Ricardo Filipe Soares Garcia da <
ricardo.garcia.silva at gmail.com> wrote:

> Hi list
>
> When building plugins that operate on vector layers, it is a common
> pattern to include a checkbox in the plugin's GUI where the user can choose
> to perform the plugin analysis on the whole layer or just using the
> currently selected features (see for example most fTools dialogs).
>
> However, I think the QGIS API does not make the implementation of this
> pattern as easy as it could be. The QgsVectorLayer.getFeatures() method
> returns an iterator but the QgsVectorLayer.selectedFeatures() returns a
> list. It would be nice if they both returned an iterator.
>
> When working with all the features in a layer, I  use:
>
> feat = QgsFeature()
> feat_iterator = v_layer.getFeatures()
> while feat_iterator.nextFeature(feat):
>     # do stuff with the feature
>
> When working with only the currently selected features the code is like:
>
> selected_feats = v_layer.selectedFeatures()
> for feat in selected_feats:
>     # do stuff with the feature
>
> Since QgsVectorLayer.getFeatures can take a QgsFeatureRequest as argument,
> I think a nice solution could be to add one more filter to
> QgsFeatureRequest to allow iterating over a list of feature ids. It
> currently already allows to pass in a single feature id, it would just be a
> matter of expanding that.
>
> In the meantime I am using a custom iterator class[1] in Python that wraps
> this different behaviour and lets me use the same logic for processing the
> whole layer or just the selected features. I think it would be cleaner if
> this could be done on the API side.
>
> Maybe I am asking for too much too late because of the feature freeze for
> 2.0?
>
>
> [1] - (see the FeatureIterator class)
> https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py
>
>
> --
> ___________________________ ___ __
> Ricardo Garcia Silva
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130618/6503feb1/attachment-0001.html>


More information about the Qgis-developer mailing list