2

I'm new to QGIS and trying to change river style while crossing a lake. Something like this with one line and multipolygon:

line crossing a multipolygon

Currently, I have two styles on one line:

enter image description here

enter image description here

It is one single line with "waterway"='stream' tag and one polygon with "natural"='water' tag. Have data from OSM file.

Is it possible to check if the line crosses a polygon and automatically changes the style?

I want to apply this rule to all lines with "waterway"='stream' tag.

2 Answers 2

2

Based on this awesome answer something like this should work:

  1. Create a Geometry Generator with type LineString where you want to get the difference of your current geometry and the aggregated geometry of your polygon layer (only for elements that intersect with lines - see other answer for a very good explanation). This will give you the lines without the parts where they overlap the water polygons
difference(
    collect($geometry), 
    aggregate(
        'waterway_polygons',
        'collect',
        $geometry,
        intersects(
            $geometry,
            geometry(@parent)
        )
    )
)
  1. Then you create a second Geometry Generator where you do the same but this time we are not calculating the difference but the intersection with the polygons, which will give you all the parts where you want to display only markers. Here you can use a Marker Line for styling.
intersection(
    collect($geometry), 
    aggregate(
        'waterway_polygons',
        'collect',
        $geometry,
        intersects(
            $geometry,
            geometry(@parent)
        )
    )
)

enter image description here

1
  • 1
    Thank you, it works nice. I finally understand the rules and the geometry generator. I forgot to mention that I use QuickOSM to download all data into lines and polygons layers. Wrote this: intersection(aggregate('lines', 'collect', $geometry, "waterway"='stream'), aggregate('polygons', 'collect', $geometry, "natural"='water')) Oct 8, 2022 at 11:24
0

I use QuickOSM plugin to download all data with the Canvas Extent setting:

enter image description here

After it is finished I rename the polygon layer to polygons and the line layer to lines:

enter image description here

Then set lines layer symbology to rule-based and created one rule ("waterway"='stream'):

enter image description here

For the polygons layer I set another rule ("natural"='water'):

enter image description here

Then create two geometry generators under the "waterway"='stream' rule:

enter image description here

For the first geometry generator (blue arrows) I use this code:

intersection(
     aggregate('lines', 'collect',$geometry, "waterway"='stream'),
     aggregate(
         'polygons',
         'collect',
         $geometry,
         "natural"='water'
     )
)

And for the second geometry generator (blue line) I use this:

difference(
     aggregate('lines', 'collect',$geometry, "waterway"='stream'),
     aggregate(
         'polygons',
         'collect',
         $geometry,
         "natural"='water'
     )
)

And it works: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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