Feature request #20847

Add options to draw label on the beginning or the end of a line

Added by Regis Haubourg about 6 years ago. Updated almost 6 years ago.

Status:Open
Priority:Normal
Assignee:-
Category:Labelling
Pull Request or Patch supplied:No Resolution:
Easy fix?:No Copied to github as #:28666

Description

I encountered this need for the first time today.
It matches this question. https://gis.stackexchange.com/questions/207736/how-to-label-the-start-end-of-the-line-in-qgis
It show a list of options that would make a lot of sense for us in QGIS.

(note: looking for supporters to do that)

arcgis_labelin_line_options.jpg (120 KB) Regis Haubourg, 2018-12-19 02:46 PM

lines.png (12.9 KB) Nyall Dawson, 2018-12-21 12:48 AM

example.jpg (56.4 KB) Alexey T, 2019-03-06 01:53 PM

History

#1 Updated by Nyall Dawson about 6 years ago

Hey Regis,

I've actually investigated this in the past, and it's not a huge change required.

The bulk of the changes come down to 3 places:

https://github.com/qgis/QGIS/blob/master/src/core/pal/feature.cpp#L746 (straight line labels, when being labeled on "straight" segments)
https://github.com/qgis/QGIS/blob/master/src/core/pal/feature.cpp#L901 (straight line labels, when being labeled over "curved" parts of a line (this is used when the above case fails))
and
https://github.com/qgis/QGIS/blob/master/src/core/pal/feature.cpp#L1240 (curved line labels)

These 3 places calculate a cost for the label candidate based on its distance from the center of the line.

What's needed is to add similar logic for other placements.

What I would suggest is that we add an "lineAnchorPointFraction" setting Pal::Layer. This would default to 0.5 - indicating that labels should be around the midpoint of the line. But it could then be set to any fraction - e.g. 0 to anchor them to the start of the line, 1 for the end, 0.25 for 1/4 of the way from the start, etc. The 3 cost calculation places in feature.cpp would then use this fraction instead of the hardcoded mid point of the line when calculating the costs.

That would allow the labeling engine to respect a "% along line" type setting (which could be exposed to users as mid/start/end/custom %, along with data-defined per-feature custom %).

Ideally we'd also want to allow a custom placement from start/end of line which is set in absolute units, e.g. mm/pixels/map units. This would add a bit to the complexity, but it comes down to adding a second lineAnchorPointOffset setting to Pal::Layer and also handling this in the 3 places in feature.cpp.

That was my earlier findings -- you can easily test these by altering the 3 places in feature.cpp and changing the hardcoded midpoint to something like 0, to force all labels to gravitate to the start of lines. See attached patch/screenshot.

https://github.com/nyalldawson/QGIS/commit/59d80fbc83ea3781f999e2bc0afde713e6862000

#2 Updated by Regis Haubourg almost 6 years ago

Thanks Nyall! I didn't see your answer before today. I'm adding Hugo in the loop, he has been involved in labeling related QEPs.

#3 Updated by Hugo Mercier almost 6 years ago

Thank you Nyall for your analysis !

#4 Updated by Alexey T almost 6 years ago

Please add center alignment and justify alignment as in https://issues.qgis.org/issues/21499

Also available in: Atom PDF