2

How can I display labels for line features in a QGIS line layer, where the label should appear outside a polygon if the line feature is inside the polygon, and otherwise appear as it is? I have activated the "name" field for labeling the line features.https://i.stack.imgur.com/I3C3p.png

I tried the below expression but it is not working.

difference (
    $geometry,
    buffer (
        aggregate( 
            'Rohrlage', 
            'collect', 
            buffer ($geometry,15)
        ),
        0
))

enter image description here

Matt
  • 16,843
  • 3
  • 21
  • 52
  • Have you tried this answer: https://gis.stackexchange.com/a/338330/63384 or https://gis.stackexchange.com/a/338304/63384? – ahmadhanb Mar 01 '24 at 12:33

1 Answers1

5

Using this expression, if the line is fully within a polygon geometry, the @within variable will return the the containing polygon and use its boundary as the placement. Otherwise, the original line geometry is used.

Change 'poly' for your polygon layer name.

with_variable('within', overlay_within('poly', @geometry)[0],
    case 
        when @within is not NULL
        then @within
        else @geometry
    end
)

enter image description here

Matt
  • 16,843
  • 3
  • 21
  • 52