12

I have performed a selection by expression (Attribute table) on a layer that contains thousands of features. I would like to know the number of features in this selection.

4
  • 3
    Does it not tell you at the top of the Attribute Table?
    – Joseph
    Aug 9, 2016 at 13:35
  • 2
    Yes it does. Wierd place to put it.... Please insert an answer. Aug 9, 2016 at 13:46
  • @LuísdeSousa where would you expect it? Aug 9, 2016 at 15:47
  • 3
    If the only place you could select features was the attribute table, then it would make sense for # of selected features to only display there. However, it is possible to select features by expression (or manually) directly from map display, and so it should probably also be displayed either in the sidebar next to the layers or in the status bar below the map.
    – J. Taylor
    Mar 6, 2018 at 0:37

4 Answers 4

16

Look at the header of the attribute table, it tells you the total number of objects in tables, as well as the filtered and selected ones.

4

There are several approaches that I may suggest:


Approach 1:

Press Shift + F6 to open the attribute table with selected features only. There will be two values that represent the number of selected features: 'Filtered' and 'Selected'.

You can also see the 'Selected' when simply opening the attribute table with F6. As was answered by @Pierre Vivet.


Approach 2:

In the left bottom corner, there will be a description of selected features.

option2


Approach 3:

Similar to @Nenad Stojanovic's answer, however, it is based just on one method called selectedFeatureCount():

from qgis.utils import iface

layer = iface.activeLayer()

print("Selected features count: ", layer.selectedFeatureCount())

Approach 4:

Via opening the Field Calculator and using either num_selected() or num_selected(@layer) expression in the Expression dialogue. It is based on the num_selected() function.

Returns the number of selected features on a given layer. By default works on the layer on which the expression is evaluated.

Also can be achieved with this expression count(1, group_by:=is_selected()), based on the is_selected() function.

Returns True if a feature is selected. Can be used with zero, one or two arguments, see below for details.


Approach 5:

Also by means of the "Show statistical summary". Symbolized as a Sigma symbol(Sigma), placed in the main QGIS working window.

Show_statistical_summary

In the appearing window, you just need to choose the layer in which you are interested choose a random field and tick the 'Selected feature only'. The total number of selected features will be represented in the row called 'Count'.


References:

3

This works fine for me:

from qgis.core import *
import qgis.utils

layer = iface.activeLayer()        
features = layer.selectedFeatures() 
print ("Total count", layer.featureCount())
print ("Selected features count:", len(features))
1

You can also use QGIS expressions to count the number of selected features in the current layer (e.g. if you use this value for further calculation in the field calculator):

array_length (array_agg ($id, filter:=is_selected(@feature)))

If you want to count the features in another layer, use this expression:

array_length(
    aggregate( 
        'layer_1',  -- change layer name
        'array_agg',
        $id,
        filter:=is_selected('layer_1',@feature) -- change layer name
    )
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.