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.
There are several approaches that I may suggest:
Approach 1: attribute-table
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.
In the left bottom corner, there will be a description of selected features.
Similar to @Nenad Stojanovic's answer, however, it is based just on one method called
from qgis.utils import iface layer = iface.activeLayer() print("Selected features count: ", layer.selectedFeatureCount())
Approach 4: field-calculator
Via opening the Field Calculator and using either
num_selected(@layer) expression in the Expression dialogue. It is based on the
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
Trueif a feature is selected. Can be used with zero, one or two arguments, see below for details.
Approach 5: toolbar
Also by means of the "Show statistical summary". Symbolized as a Sigma symbol(), placed in the main QGIS working window.
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
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 ) )