1

I've got a Linestrings layer.

Each Linestring has among other fields, these ones:

  id  ENTRY EXIT
    1   S1  E2
    2   S1  E3
    3   S1  E3
    4   S1  E3
    5   S1  E3
    6   S1  E1
    7   S1  E3
    8   S1  E2
    9   S2  E5
    10  S2  E3
    11  S1  E1
    12  S1  E1
    13  S3  E4
    14  S1  E2
    15  S1  E1

I want to show for each Linestring, the ENTRY value as a label at the beginning of the Linestring and the EXIT value at the ending.

I am using the following solution suggested by @Spacedman here:

You can do this with two "Rule-based Labelling" labels, with no filters. This lets you put multiple labels on a feature. The first rule labels the feature with the attribute you want at the start, the second rule with the end attribute. Initially your labels will appear in the centre of the line, so we need to fix that. Do that with "data defined" expressions for the position. Use the drop-down and hit "Edit...". For the label at the start, use x_at(0) and y_at(0) for the position expressions. For the end label, use x_at(-1) and y_at(-1).

The thing is that if I place each label exactly at the beginning and ending points of a Line, as @Spacedman suggets, some ENTRY and EXIT labels get overlapped (and can't be seen) because some Lines share start/end points.

So, the idea then would be to place each ENTRY/EXIT label a little bit displaced to the inside of the Line and not exactly on the end/beggining point?

Result should be something like: enter image description here

Something like postgis' ST_Line_Interpolate_Point function would be great in this case, but in qgis...

2 Answers 2

1

Have you tried to activate option « Show all labels for this layer (including colliding labels) » ?

This option is in the layer properties dialog > tab Labels > Rendering.

enter image description here

1
  • Yes, they are activated for both labels (rules) the thing is that as label is being placed at an exact XY (given by calculation) they are not automatically moved to show both lavels
    – Egidi
    May 25, 2016 at 13:02
0

Perhaps try extracting the nodes and then duplicate the node layer; one for start labels, one for end labels.

For your first (start) layer, label with the start column. Under 'Placement', on the 'Labels' tab, select the "Offset from point" radio button and select a quadrant you want all you start labels to appear in. You can then use "Offset X,Y" to increase the distance away from the point or move it to a desired location. (Remember to remove the style of the point).

Repeat this using the duplicate layer for your end labels. My preference is to use opposite corners.

EDIT: As your data is live you can try using rule-based labelling. This allows you create two separate labels for every point that you can independently manipulate. Create two rules for start and end labels. You could do:

"ENTRY" LIKE 'S%'
"EXIT" LIKE 'E%'

Then just label with the entry/exit columns and style to your hearts content.

A simple method of placement I like is "Around point". You can then set a standard distance. Then if you hover of the "data defined override" next to Quadrant, it will explain which integer you want for where each label will be placed (same as the quadrant selection find under "offset from point").

EDIT: My bad, your working with linestrings and I got all confuddled with points. Not to worry. If you can add a couple of columns to the attribute table, "START_X", "START_Y", "END_X", "END_Y" then use the field calculator with:

 substr( geom_to_wkt(  $geometry ),13,11)

What this is doing is geom_to_wkt( $geometry ) → 'LineString (start_x start_Y, end_X end_y)'

substr() is used to break down the string into just the coords. You'll have to play around with where substr() is cutting the string up in the area but it doesn't long becuase of the preview at the bottom of the field calculator.

You can now us the rule-based labelling as above, but, under placement > Data defined you can insert coordinate X and Y. NOTE: this will place the label at exactly those coordinates so add to the expression +0.005 or whatever to the Y coordinates to raise the label up and away from the line. Do this for X and similar for the end label coordinates and they should be nicely spaced around the start and end of all your strings.

Sorry that took so long

3
  • I would need to do it without creating new layers because my Linestring layer is alive (being digitized) and the labels need to be shown live while digitizing.
    – Egidi
    May 26, 2016 at 8:19
  • I thanks for your reply! An easier way to obtain the coords is just for start_x: "x_at(0)" (0 stands for beggining point). For start_y: "y_at(0)" for end_x: "x_at(-1)" (-1 stands for ending point) and for end_y: "y_at(-1)"
    – Egidi
    May 26, 2016 at 13:57
  • I did not know that, Cheers :) May 26, 2016 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.