4

I would like to dynamically label a hiking circuit in the center (if the line is acting like a polygon and the label is centered). You can see it in the screenshot below:

enter image description here

I know I could do the following:

  1. Line to Polygon
  2. Label in the center of the polygon

But I need a fast and dynamic solution to save Hardware Resources. It's not for printing a hiking map, is for working in different scales within QGIS. I tried several label settings but found no solution.

1 Answer 1

6

In the "Geometry Generator" of the label placement tab, use the following expression:

centroid(make_polygon($geometry))  -- use point_on_surface or pole_of_inaccessibility in place of centroid to guarantee the label is inside the shape

A polygon is made from the line $geometry and its centroid is used as the label position.

enter image description here

2
  • 1
    Why not simply centroid ($geometry)? Works also for a polyline - even if it is not closed.
    – Babel
    Commented Feb 1, 2022 at 20:18
  • 1
    make_polygon works on an unclosed line. In the case of a horseshoe shape the centroid might not be 'inside' the line. In which case point_on_surface or pole_of_inaccessibility would be preferable.
    – Matt
    Commented Feb 1, 2022 at 20:32

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.