7

When working with vector layers in QGIS, I can display the feature count of a layer and, if applicable, the feature count per attribute category (e.g. when using categorized or graduated styling) via the context menu of the layer. The counts are added to the layer title that is displayed in the layer widget.

Get feature count in QGIS

How can I set activate this feature via the Python console/running a script? Looking through the API docs, I found a method to get the feature counts and further use them in your script. However, how can I display them as they would show when using the GUI?

It is probably possible somehow by retrieving the feature count as described above and then changing the layer widget symbology, adding the count to the title. However, this is rather cumbersome, so I wonder if there is a direct way to achieve this? If not, how would this best be done manually?

1 Answer 1

9

That is pretty simple by using custom properties by the setCustomProperty() method.

The below example adds a memory layer to the legend and set the "showFeatureCount" property to True (you can run it from pyqgis console):

## create the memory layer and add to the registry
myLayer = QgsVectorLayer("Point", "myLayer", "memory")
QgsMapLayerRegistry.instance().addMapLayer(myLayer, False)

## reference to the layer tree
root = QgsProject.instance().layerTreeRoot()

## adds the memory layer to the layer node at index 0
myLayerNode = QgsLayerTreeLayer(myLayer)
root.insertChildNode(0, myLayerNode)

## set custom property
myLayerNode.setCustomProperty("showFeatureCount", True)

To get a specific property of the current layer (node layer) in layer tree you can use customProperty() or customProperties() to get all the stored properties for that layer:

myLayerNode.customProperty("showFeatureCount")
## the result is True

myLayerNode.customProperties()
## the result is a list [u'showFeatureCount']
4
  • Thank you! Do you also know how to activate the feature count in the legend when using it as part of a print composer composition? Apparently, this is not done by default when adding a legend to a composition, even if the feature count is activated in the layer widget. In the map composer GUI, after adding a legend to the composition, I would then click the "sum" symbol on the Item properties -> Legend items tab. However, I cannot find anything similar in the API docs when scrolling through QgsComposition, QgsComposerLegend and QgsComposerLegendStyle classes...
    – Dirk
    Commented Oct 30, 2014 at 10:23
  • Can't edit my comment... There is setShowFeatureCount method of QgsComposerLayerItem class. Any suggestions how to use it? When I create an instance of the class and call the method with QgsComposerLayerItem().setShowFeatureCount(True), no feature count is shown in the print composer output. Calling updateLegend() on the QgsComposerLegend object also does not change anything.
    – Dirk
    Commented Oct 30, 2014 at 11:03
  • I just tried the line mentioned in my comment above in QGIS dev 2.5 dbed468 and it works like it is supposed to o.O Any clue what could have made the change there?
    – Dirk
    Commented Oct 30, 2014 at 13:05
  • In case one wants to apply it on a layer that is already existing in mapLayerRegistry: Skip the layer creation part and replace root.insertChildNode(0, myLayerNode) with myLayerNode = root.findLayer(myLayer.id()) Commented Apr 9, 2018 at 9:39

Your Answer

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

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