# Group neighbouring features based on sum of attribute

Each polygon (building) contains "x" number of flats (eg `"flat_num"=10` in one polygon/building).

I would like to group multiple polygons if:

• the grouped polygons are neighbors

AND

• sum of `"flat_num"= 25` in the group of polygons.

If the conditions are filled, then the polygons of the same group should share the same "group_id". If the conditions are not filled, then the "group_id" is NULL. I tried to use the "Aggregate" function, with the expression: `*sum("flat_num"=25")*` but QGIS groups ALL the polygons together. I think I need to find the correct expression. For the neighboring polygons condition, I use: https://spatialthoughts.com/2019/05/23/neighbor-polygons-aggregate-qgis/

• The task is pretty clear, however, some additional details/information will be nice to have. 1) The alphabetic order of features does not matter, is not it? 2) What to do if there are three neighbours the sum of which is 26 (9+9+8)? So, it is clear that 8+9 (one of two) can be grouped, but which one of 9s to choose? Should some additional factor be considered? 3) If there are 5 groups where each flat has a value of 5 and one more flat with a value of 1, so all 5s will be grouped into one category, and 1 will be left beyond, is it acceptable? or there is a different logic behind? Dec 31, 2020 at 18:53
• 4) Does the length of a border in common within neighbours play a role? Dec 31, 2020 at 18:53
• Can you show your efforts using the expression mentioned in a link? Did you encounter some error or what? Dec 31, 2020 at 18:54
• 1) The alphabetic order of features should not have an influence. 2) After thinking about it, I thought that accepting deviations of +2 or -2 is acceptable. So the result can be either 23,24,25,26 or 27 flats. If two buildings have the same conditions to be selected, it can be randomly selected. 3) It is acceptable to have one left out. 4) no it does not play a role Jan 4, 2021 at 13:15

Adapting the solution linked by you to your case, you could use the following expression to create a new field that calculates for each polygon the sum of flats of all neighbouring polygons. So in your case above, you will get a sum of 25 for flat C:

``````aggregate(
@layer,
aggregate:='sum',
expression:="flat_num",
filter:=touches(\$geometry, geometry(@parent))
) + "flat_num"
`````` You did not provide enough information to see if this solution is sufficients. It works only with two constraints:

1. There is at least one "central" polygon that borders on all others (it does not work if you would have a sum of 25 for let's say A+B+C+E+F because F only borders on A, but A does not border on B).

2. The sum of all neighbouring polygons is 25. If however A+B+C = 25, than you will not find this solution as E also borders on C and thus the sum will be higher.

But at least it is a heuristic starting point: as in your case C, it will give you results in some cases. In others, it will give you hints where to have a closer look.

You probably anyhow need to do this in several steps, iterating over the neighbours and counting different sums to see in which cases your conditions are fulfilled. The information you provided by now is not detailed enough to guarantee an unambiguos solution, so there is no way to give a definitive answer. What if A+B+C+E = 23 flats and D, F and G all have 2 flats each? So there would be 3 valid solutions: the four flats suming up to 23 plus one of D, F or G. Which one to select?

I just can advice on some further steps you could try:

If using QGIS 3.16, you can use the new `overlay_touches` function to create a field "neighbours" that lists all the neighbours of each polygon: this can be used to calculate the sum of all possible combinations:

``````array_to_string (
array_filter(
overlay_touches(
@layer,
"name"
),
length ( @element ) >0
)
)
``````