6

In QGIS 3.22 I am trying to visually represent a difference between polygons after its change. In the end I want to know where the polygon was enlarged (#72ff85) and where it was degraded (#ff7272).

To download data for testing purposes: https://we.tl/t-fqlOCAjLx5 (valid until 16.06.2022)

Let's assume there is a polygon layer called 'master', see the image below.

master

Later on the 'master' layer was saved as 'branch' and manually edited, see the image below.

branch

Hereupon I want to visualize the difference between these two polygon layers. For that purpose I duplicated the 'branch' layer and applied the following graphical settings:

settings1

sym_difference(
  $geometry,
  geometry(get_feature('master','id',"id"))
  )

settings2

if(
    intersects(
        $geometry,
        geometry(get_feature('master','id',"id"))
        ),
    '#ff7272',
    '#72ff85'
    )

However, I ended up with nothing meaningful. Seems like the output of the Geometry Generator is a MultiPolygon for which the expression does not work.

result_false

How can I achieve my visual aim just with Symbology from the Layer Properties menu?

I could get the desired result after using several geoalgorithms one after another, namely the "Symmetrical difference" and then the "Multipart to singleparts". And also applying some settings for the colour:

settings3

result_true

So, the reddish colour means that the polygon was degraded, and greenish colour means that it was enlarged.

2
  • 1
    Different colours within the same polygon? Nice idea.
    – Erik
    Commented Jun 9, 2022 at 7:48
  • Yes, I want to make these styles as much as possible dynamic. So as soon as a person applies a change in the 'branch' layer, a new styles set is already there.
    – Taras
    Commented Jun 9, 2022 at 7:50

1 Answer 1

6

As you have Multipart geometries, you must be able to address each part individually. This can be done using the variable @geometry_part_num (current geometry part number for feature being rendered), combined with geometry_n(). See below for more detailed explanation.

Then use a within() function and make sure that the parts of your layer 'branch copy' are really completely within the corresponding feature of the layer 'master' by applying a small, negative buffer().

Use this expression to override color:

if(
    within(
        buffer(
            geometry_n($geometry, @geometry_part_num),
            -0.1
            ),
        geometry(
            get_feature('master','id',"id")
            )
        ),
    '#ff7272',
    '#72ff85'
    )

settings_true

and get the output:

result

How the above expression works

When you create symmetrical difference with Geometry Generator on your layer 'branch copy', you get Multipart geometries. Using @geometry_part_num, each part gets an individual number. Using geometry_n(), you can create each part as a separate geometry.

Each of these separate geometries is then checked individually if it is within a feature of the layer 'master'.

1
  • This solution should also work with MultiPolygons
    – Taras
    Commented Jun 9, 2022 at 14:00

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.