I have a road layer and "NAME" field. I activated labels, but I don't want to show the labels outside the raster in map canvas (not layout). The raster area is my study area (you can see a part of it below). QGIS becomes slow in rendering labels when all labels are displayed. I reviewed the label settings. I couldn't find a setting related to this.

How can I limit the area to be labeled to the raster extent? Clipping the road layer by the raster extent is not an option here.

enter image description here

  • 2
    try the following expression for your value(s): if(intersects($geometry, layer_property('raster_layer_name_or_id', 'extent')), "labelling_field", "")
    – Taras
    Nov 30, 2023 at 11:16
  • @Taras That rounds out the options i'd say ^^. Yours is slightly slower though for me compared to Matt's and my solution, not totally sure why though. 140ms vs 120ms so not a big difference either but consistent.
    – Kalak
    Nov 30, 2023 at 11:40
  • 1
    You both were too fast with setting up test examples ^_^ and I had my coffee though :) That is why it is only a comment
    – Taras
    Nov 30, 2023 at 12:53

2 Answers 2


You can use Rule-based labeling with this expression as the rule filter

    layer_property('raster', 'extent')

enter image description here


You can set the data defined "Show label" in the "Rendering" category with the expression intersects(layer_property('smaller_layer', 'extent'), $geometry) to do this.

enter image description here Without spatial filtering: enter image description here

With spatial filtering: enter image description here

Update: I did check with the "Map canvas refresh" debugging turned on to check performance and I do get better performance with the spatial filter (@Matt's does too, exactly as fast actually) than without any spatial filter.

For my expample:

  • without spatial filter: Canvas refresh: 289 ms
  • with spatial filter: Canvas refresh: 119 ms
  • Thank you @Louis. Matt's solution renders slightly faster. Nov 30, 2023 at 11:46
  • @KadirŞahbaz I meant as fast depending on where the expression is, not the actual expression (it wasn't very clear in the anwser) since both are different and could give different results so different rendering time, especially for roads where the selection will be different between intersect and within/contains
    – Kalak
    Nov 30, 2023 at 11:52

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.