TransWikia.com

Calculating percentage of number of features in layer in QGIS

Geographic Information Systems Asked on June 16, 2021

I’m using QGIS to get some "stats" on a current layer e.g. to determine the number of features of a certain class. That I do by using the Aggregate-Algorithm on the class-attribute with count on "ID". I get a table that shows the number of features per class.

But how could I get another attribute giving me the percentage of the total number of features?

While this is easily done by hand (the number of total features in the layer can be easily determined, then it’s just count / total_number), I don’t know how to access the total number of features of the layer via an expression.

2 Answers

The solution is to use the function aggregate().

I did it in a two-step process: Tool Aggregate with count as aggregation, and subsequently tool Field Calculator with the following expression (where count is the attribute respectively column created with the Aggregate tool):

"count" / aggregate(layer:=@layer, aggregate:='sum', expression:="count")

Aggregate settings

Field Calculator settings

Answered by Honeybear on June 16, 2021

You can use a one-step expression using count() function:

count("ID","CLASS") / count("ID") * 100

This will return the percentage of the current class of the total feature count.

Where "ID" is a unique field and "CLASS" an identifier for your class, so not unique.


If you need an overview-table, you can run Statistics by categories from processing toolbox and then use "count" / sum("count") * 100 on the result table to calculate the percentage of each category.

Answered by MrXsquared on June 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP