2

I want to label the sums of surfaces ("Groesse") grouped by common attribute values ("Block") in QGIS 3.16. I need one label for each group. The following pictures help to describe the expected result.

Here is an example of my attribute table:

enter image description here

Here is the result I want to get. 5 sums grouped by the "Block" column are represented below: 3 orange (D1, D2 and D3), 1 grey (S1) and 1 green (G1)

enter image description here

  • I thought to create a virtual layer with a function that sum the surfaces ("Groesse") per attribute value("Block"). Despite my attempts, I do not know how to write it correctly in SQL (sum("Groesse","Block") in Python).

  • I also tried to display labels through a Rule-based expression in using the aggregate function, without success.

Taras
  • 32,823
  • 4
  • 66
  • 137
Xavier Michael
  • 392
  • 2
  • 11

3 Answers3

2

Let's assume there is a polygon layer 'LayerB' with it's corresponding attribute table, see image below.

input

First of all duplicate your layer via RMC > Duplicate...

Then proceed with RMC > Layer Properties > Symbology > Single Symbol > Geometry Generator.

In the field for the Geometry Generator paste the following expression

buffer(collect($geometry, group_by:="group"), 0)

geom_generator

Then in Label with field apply the following expression

sum("Value", group_by:="group")

And then for Placement (via Coordinate Edit) use

X | x(centroid(buffer(collect($geometry, group_by:="group"), 0)))
Y | y(centroid(buffer(collect($geometry, group_by:="group"), 0)))

labelling_rules

And get the output

result

Taras
  • 32,823
  • 4
  • 66
  • 137
1

In QGIS I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume there is a polygon layer 'LayerB' with it's corresponding attribute table, see image below.

input

With the following query, it is possible to create one label for each group.

SELECT st_union(geometry) AS geom,
       SUM("Value") AS sum_v,
       COUNT(*) AS count_f
FROM "LayerB"
GROUP BY "group"

The output Virtual Layer will look like

result

where

  • "sum_v" shows the summed values for grouped features
  • "count_f" represents how many features were grouped

And afterwards apply for a Virtual Layer a common labeling.

Taras
  • 32,823
  • 4
  • 66
  • 137
  • This is also very interesting that way, thank you. However, the categorized representation of the polygons following the "count_f" is not right. For an unknown reason, surfaces counted in the "count_if" fields sum too many polygons at once (for instance D1 + D2, D4 + D7). Therefore, too few groups are created. A workound is to also symbolize polygons per category following the "sum_v" values. – Xavier Michael Nov 30 '20 at 13:47
  • Strange that this happens, maybe simply skip the "count_f"-field. BTW, is the geometry congruent? – Taras Nov 30 '20 at 14:01
  • Geometry is not congruent in this case. But now I find out where lies the problem. The SQL query is right, the categorized representation of the "count_f" field may render it wrongly in some cases: the COUNT(*) AS count_f line counts how many values are in the "Block" field. Yet, when there is same counted values (for example, 2 times "7" in "count_f" field), then the categorized symbology consider them identical, and represent them with the same color, although the st_union(geometry) worked well. Therefore a categorized representation with the "sum_v" is right. – Xavier Michael Nov 30 '20 at 15:45
0

Let's assume there is a polygon layer 'LayerB' with it's corresponding attribute table, see image below.

input

Step 1. "Dissolve" the original layer via the grouping field, in my case it is "group"

step1

Step 2. For the dissolved layer use the following expression for labeling

aggregate(
      layer:='LayerB',
      aggregate:='sum',
      expression:="Value",
      filter:=intersects(centroid($geometry), geometry(@parent))
    )

step2

Taras
  • 32,823
  • 4
  • 66
  • 137