4

Let's say I have the scenario where

  • Polygons A and B intersect polygon c
  • There are also polygons X,Y and Z which do not intersect c.
  • Polygon C has an attribute value 'Priority' which I need the value propagated onto intersecting polygons (that is, onto Poly A and B).

All the data is in a single layer in a geopackage table.

I have no drama's selecting polygons A and B, I'm looking at trying to update them using the Field Calculator and an expression. But I'm having an absolute brain fade as to how to refer to self.

That is - how do I find intersecting polygons on the layer that I'm already using and get their attribute values?

My logic is to write a function and apply it to a selection set (which I would select outside of the function). So far I have the following:

def get_value_func():
# Firstly, get the set of polys I want to grab values for. Lets just use the 'c' poly for now
geometry_col = collect($geometry, , filter:="layer_name" = 'c')

#then lets test if the current geometry intersects anything within the geometry collection
if(intersect($geometry, geometry_col), update_val = <THIS IS THE BIT IM NOT SURE ABOUT>))

return update_val

Its possible I'm overcomplicating things and am happy to hear other ideas about how to achieve this. Maybe collection isn't the right data structure to be using.

0
4

You can use the aggregate() function for this task. see the following situation - hope that describes what your are trying to do:

enter image description here

with the following expression you will aggregate values from the field myfield into a new field intersects for the polygons which intersects other polygons in the same layer:

aggregate(layer:='mylayer',
          aggregate:='concatenate',
          expression:="myfield",
          filter:=intersects($geometry, geometry(@parent))
                  and "myfield" <> attribute(@parent, 'myfield'),
          concatenator:=',')

in the filter you will need 2 conditions to avoid the self intersection of each polygon within the result.

3
  • @parent looks like the missing piece to my puzzle..... im so used to object orientated languages and using '_self'. ....ill give this a try, thanks for that. – nr_aus May 14 at 7:17
  • 1
    @parent works only in context of the attribute() function an is the calling/parent feature... – eurojam May 14 at 7:26
  • ok cheers for that extra bit of info. – nr_aus May 17 at 2:11
5

If you use QGIS 3.16 or later, you can use overlay_intersects() together with @layer variable that refers to the current layer. Use this expression in the Field Calculator:

attribute(
    get_feature_by_id( 
        @layer, 
        array_first (
            overlay_intersects( 
                @layer, 
                $id 
            )
        )
    ), 
    'Priority'
)

enter image description here

1
  • Thanks for that, looks good. – nr_aus May 17 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.