[Qgis-developer] Qgis Api - select all features of a vector layer

kimaidou kimaidou at gmail.com
Tue Nov 9 14:15:31 EST 2010


Thanks for this answer

2010/11/9 Carson Farmer <carson.farmer at gmail.com>

> ...and with the newer PyQGIS API, we can do things even more Pythonically:
>
>
Is the new api usable from 1.5, or should I wait for the next version ?


> def layerIds(layer):
>     layer.select([]) # we don't actually need the attributes
>    ids = [feat.id() for feat in layer]
>     return ids
>
> Obviously you could skip the step of creating the layerIds function,
> and then you'd reduce the entire thing down to:
>
> layer = self.iface.mapCanvas().currentLayer() # or however else you
> get the layer
> layer.select([]) # we don't actually need the attributes
> layer.setSelectedFeatures([feat.id() for feat in layer]) # select all
> the feature ids
>

Yes , it is more concise and the list comprehension is much more pythonistic
;)
I was on other php projects, and lost my python :)


>
> My guess is that this would be quite a bit faster than boundingbox
> method, especially because select(QgsRectangle, bool) actually
> contains this:
>
> QgsFeature f;
> while ( nextFeature( f ) )
> {
>  select( f.id(), false );
> }
>
>
Ok. Thanks again !

> whereas setSelectedFeatures is more direct.
>
> Carson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20101109/01be38b3/attachment.html


More information about the Qgis-developer mailing list