Hi Yves,

why does the featureId not point to the PK? Can you guarantee that the value is unique in the view? Maybe it is not? Another thought: I normally use QgsVectorLayer.setSubsetString() to reduce the amount of features being loaded into QGIS in the first place (and by using the subsetString I let PG deal with it). Thus I can use plain SQL and do not have to fuzz with QgsFeatureRequest.

hope this helps

Bernhard

Am 09.12.2015 um 18:36 schrieb Yves Moisan:
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
_______________________________________________
Qgis-user mailing list
[email protected]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-user



__________ Information from ESET Mail Security, version of virus signature 
database 12699 (20151210) __________

The message was checked by ESET Mail Security.
http://www.eset.com


_______________________________________________
Qgis-user mailing list
[email protected]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to