[Qgis-user] Selection issue for large set of features

Yves Moisan ymoisan at videotron.ca
Wed Dec 9 09:36:07 PST 2015


Hi All,

Sorry for X-posting.  I originally put this message on the qgis-dev list 
but I guess it wasn't the right place.

I'm contributing to a QGIS python plugin.  Dev environment : 2.8.3 
(compiled) Ubuntu 14.04.

I have an issue getting a list of PK ids from selected features on a 
vector layer pointing to a PG view .  The short story is : I would like 
to use selectedFeaturesIds() but I can't rely on ids returned being the 
actual PK.  The longer story now.

I can use QGSFeature objects and get the field I want.  However, all 
variants that give a list of QGSFeature objects that I've tried fail 
given a large enough number of selected features :

# getting all features with attributes and geom; definitely overkill

layer_selected_features = layer.selectedFeatures()  # Then extract the 
attribute I want

# getting all features but only with the (predetermined) table_pk; 
layer_selected_features_ids was obtained by selectedFeaturesIds()

layer.getFeatures(QgsFeatureRequest().setFilterFids(layer_selected_features_ids).setFlags( 
QgsFeatureRequest.NoGeometry ).setSubsetOfAttributes( 
[table_pk],layer.pendingFields() )))  # Then extract the attribute I want

A note on the previous line : I found out order of the filtering 
elements matters.

# Using a feature iterator

layerSelectedFeaturesIterator = layer.selectedFeaturesIterator()
layer_selected_features_ids = list(i[table_pk] for i in 
layerSelectedFeaturesIterator) # I get directly the list of items I want

When the feature selection gets large enough the code fails silently and 
no list is returned.

I was hoping I could use selectedFeaturesIds().  I tried it with my 
largest data sets and it works.  Only problem is : the id list it 
returns may or may not correspond with the actual table PK the view 
points to.  setKeyColumn() on the layer won't make selectedFeaturesIds() 
return a list of ids with the new keyColumn().  I was thinking maybe I 
could pass in the field name for the PK (that I know) to some 
constructor (datasource, layer ?) and then be satisfied the list of ids 
returned by selectedFeaturesIds() would be fine.

Pointers appreciated.

Cheers,

Yves



More information about the Qgis-user mailing list