TransWikia.com

Group neighbouring features based on sum of attribute

Geographic Information Systems Asked on January 27, 2021

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.

enter image description here

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/

One Answer

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"

enter image description here

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 expressions 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
    )
)

Answered by babel on January 27, 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