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.

3
  • What software do you use?
    – Zoltan
    Commented Nov 26, 2020 at 11:53
  • Qgis 3.16. You're right I just changed the title of my question. Thx Commented Nov 26, 2020 at 13:02
  • You can use DB Manage to create virtual layer using SQL.
    – Zoltan
    Commented Nov 26, 2020 at 14:06

3 Answers 3

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

3
  • Even if I preferred the accepted solution, this one worked too. Commented Nov 30, 2020 at 15:47
  • You dont really need to duplicate the layer (but it could be easier to manage your project with two separate layer)
    – J.R
    Commented Jul 6, 2021 at 8:26
  • You are correct @J.R !
    – Taras
    Commented Jul 6, 2021 at 8:27
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.

3
  • 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. Commented Nov 30, 2020 at 13:47
  • Strange that this happens, maybe simply skip the "count_f"-field. BTW, is the geometry congruent?
    – Taras
    Commented Nov 30, 2020 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. Commented Nov 30, 2020 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

2
  • This solution is working as long as you do not change the size of the original polygons, which won't be my case unfortunately. Commented Nov 30, 2020 at 13:50
  • okay I did not know that =) This is more a one-time solution rather than continuous.
    – Taras
    Commented Nov 30, 2020 at 13:56

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.