[Qgis-developer] Qgis Api - select all features of a vector layer
Carson Farmer
carson.farmer at gmail.com
Tue Nov 9 12:14:29 EST 2010
...and with the newer PyQGIS API, we can do things even more Pythonically:
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
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 );
}
whereas setSelectedFeatures is more direct.
Carson
On 9 November 2010 16:46, Barry Rowlingson <b.rowlingson at lancaster.ac.uk> wrote:
> On Tue, Nov 9, 2010 at 4:22 PM, kimaidou <kimaidou at gmail.com> wrote:
>> Hi again
>>
>> Still working on my python plugin...
>>
>> I need to simply select all the features of a chosen vector layer. In
>> QgsVectorLayer api, there is no method "selectAllFeatures()"
>>
>> So I use this piece of code to achieve it
>> http://osgeo.pastebin.com/Ma9DxgKR
>
> That's an interesting approach! My selectplus plugin has a 'select
> all' function which does this:
>
> def doit(self):
> layer = self.iface.mapCanvas().currentLayer()
> layer.setSelectedFeatures(layerIds(layer))
> return None
>
> where layerIds is:
>
> def layerIds(layer):
> ids = []
> p = layer.dataProvider()
> allAttrs = p.attributeIndexes()
> p.select(allAttrs)
> f = QgsFeature()
> while p.nextFeature(f):
> ids.append(f.id())
> return ids
>
> Your rectangle overlay should be pretty quick, but maybe my collecting
> all the feature IDs is quicker?
>
> Barry
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
--
Carson J. Q. Farmer
ISSP Doctoral Fellow
National Centre for Geocomputation
National University of Ireland, Maynooth,
http://www.carsonfarmer.com/
More information about the Qgis-developer
mailing list