<div dir="ltr">Hi list<div><br></div><div>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).</div>

<div><br></div><div>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. </div>

<div><br></div><div>When working with all the features in a layer, I  use:</div><div><br></div><div style>feat = QgsFeature()</div><div>feat_iterator = v_layer.getFeatures()</div><div style>while feat_iterator.nextFeature(feat):</div>

<div style>    # do stuff with the feature</div><div><br></div><div style>When working with only the currently selected features the code is like:</div><div style><br></div><div style>selected_feats = v_layer.selectedFeatures()</div>

<div style>for feat in selected_feats:</div><div style>    # do stuff with the feature</div><div><div><br></div><div style>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.</div>

<div style><br></div><div style>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.</div>

<div style><br></div><div style>Maybe I am asking for too much too late because of the feature freeze for 2.0?</div><div style><br></div><div style><br></div><div style>[1] - (see the FeatureIterator class) <a href="https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py">https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py</a></div>

<div style><br></div><div style><br></div>-- <br>___________________________ ___ __<br>Ricardo Garcia Silva
</div></div>