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.sstatic.net/I3C3p.png

I tried the below expression but it is not working.

difference (
    buffer (
            buffer ($geometry,15)

enter image description here


1 Answer 1


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],
        when @within is not NULL
        then @within
        else @geometry

enter image description here

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.