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
-
2If 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
3 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.
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))
There are several methods that I may refer you to:
1. Press Shift + F6 to open 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.
2. In the left bottom corner there will be a description of selected features.
3. Similar to @Nenad Stojanovic answer, however, it is based just on one method called selectedFeatureCount()
:
layer = iface.activeLayer()
print("Selected features count: ", layer.selectedFeatureCount())
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.
5. 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 and 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: