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.
-
3Does it not tell you at the top of the Attribute Table?– JosephAug 9, 2016 at 13:35
-
2Yes it does. Wierd place to put it.... Please insert an answer.– Luís de SousaAug 9, 2016 at 13:46
-
@LuísdeSousa where would you expect it?– Matthias KuhnAug 9, 2016 at 15:47
-
3If 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. TaylorMar 6, 2018 at 0:37
4 Answers
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.
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.
Approach 2:
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 selectedFeatureCount()
:
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()
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: 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 'Count'
.
References:
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))
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
)
)