I'm trying to create dynamic leader lines by using a PostGIS view in addition to the QGIS „Move Label“ tool.
CREATE VIEW leader_line AS
SELECT
gid,
ST_MakeLine(geom, ST_SetSRID(ST_MakePoint(xcord_label, ycord_label), SRID))::geometry(linestring, SRID) AS geom
FROM point
WHERE xcord_label IS NOT NULL;
This works fine for all labels WHERE ST_X(geom) < xcord_label
but creates wrong looking leader lines for labels WHERE ST_X(geom) > xcord_label
.
Does anybody know how to get properly placed leader lines for labels WHERE ST_X(geom) > xcord_label
? Is there any way to refer to the xmax coordinate of the labels?
Best Answer
You can use QGIS' quadrant placement specifier determined from the line's azimuth to place a better label. The quadrant specifies 8 positions around a point:
Here's an example around Null Island, creating a table and two views.
Then in QGIS, add:
points
–geom
leader_line
–geom
– primary key needs to begid
point_labels
–geom
– primary key needs to begid
Now configure the layer properties for
point_labels
:label
, and change placement to "Offset from point", modifying the "Quadrant" to use the attribute fieldquadrant
Bingo!
Note that a slightly different approach is required for
geography
types, since ST_Azimuth behaves differently.Update: When adding new points to the
points
layer, thegeom
field is updated as usual, but thelabel_geom
is not. To populate a default value oflabel_geom
with new points, a trigger needs to be created. But if a trigger function is used, thequadrant
specifier can be stored in thepoints
table and thepoint_labels
view can be ignored:For example, let's start again with a slightly different example with one table and one view:
The from the first example, re-do the
INSERT INTO points
andCREATE OR REPLACE VIEW leader_line
statements, as these do not require modification. But ignore theleader_line
view.Then in QGIS, add:
points
–geom
points
–label_geom
leader_line
–geom
– primary key needs to begid
Now configure the layer properties for
points
withlabel_geom
as the first example did forpoint_labels
. Thequadrant
specifier will be modified automatically for new and moved points, but you will only notice these changes each time you save your edits.