[GIS] Place label along line near start and end of line in QGIS

expressionlabelingqgisroadrule-based

I have four fields managing the numbering of the start and end of road sections.

I created two label rules, one for the start and one for the end of the road sections.

I set the position of the labels to "parallel / on the line".

I tried to configure the position of the labels according to these expressions
x(end_point ($ geometry)) and y(end_point ($ geometry)) for the end of the road sections, and a similar expression for the start of the road sections.

The problem is that the labels no longer take into account the position "on the line" and certain labels no longer appear due to their overlapping.

This tool exists in arcmap which allows you to manage the labeling from the start or the end of the road section. Is there a similar tool in QGIS?
enter image description here

If not, do you have an idea for a script allowing you to configure these labels?

Best Answer

2020 Oct update - label placement at start/end of lines or with a given offset is now be an inbuilt feature of QGIS 3.16 ! Link shows it in action.


QGIS 3.8 to 3.14

Consider using the geometry generator option under label placement, and generate just a tiny subsection of your linestring at the beginning or end to place the label on.

First go to Labels > Placement > geometry generator and select geometry type as linestring:

enter image description here

In the expression box (red arrow in image above), to place your label at the start of the line, use

line_substring($geometry,0,1)

To place your label at the end of the line, use

reverse(line_substring(reverse($geometry),0,1))

Finally, I strongly recommend setting 'Overrun feature' (above the geometry generator setting) to a few millimetres at least.

Otherwise, play with the expression and change 1 to a larger number (the label may start drifting away from the end point though).

Result:

enter image description here


The above assumes your layer is in a projected coordinate system. If it is using a geographic coordinate system like EPSG:4326 you either need to use much smaller values or transform your geometry in the expression (or save a copy of your data in a projected coordinate system)

Related Question