[Qgis-user] Offsetting rotated labels proportionate to variable angles/rotations

Charles Dixon-Paver charles at kartoza.com
Fri Jul 2 08:58:57 PDT 2021


I haven't got the "code" for this per se, but I guess you could use
something like this as a data defined offset:

to_string((100*cos("angle"))*-1)||','||to_string((100*sin("angle")*-1))

I feel the trick for a good result is to use the geometry generator for
label placement. You could probably achieve similar results with expression
defined offsets, but using the geometry generator will allow you to
visualise where your label positions will end up, so tweaking will be
much easier.

Basically this allows you to place labels at the generated geometry, which
you can specify the location of using the trigonometric expression
functions, and refine/ filter your results for specific outcomes.

I've put a screenshot showing the result on imgur [1] so you can have a
look at what I mean. The red points will be the actual geometries and the
smaller blue points are the positions created by the geometry generator.

I will provide the geometry generator expression I used below, but it's not
"production grade" and I threw in everything including the kitchen sink to
make it easy to modify, but I haven't actually worked out the trig you
would need to accomplish what you want.

with_variable('the_angle',
CASE
  WHEN "angle" >= 0 AND "angle" <= 90 THEN (90 - "angle")
  WHEN "angle" > 90 AND "angle" <= 180 THEN (90 - "angle" - 90)
  WHEN "angle" > 180 AND "angle" <= 270 THEN (90 - "angle" - 180)
  WHEN "angle" > 270 AND "angle" <= 360 THEN (90 - "angle" - 270)
  ELSE "angle"
END,
with_variable('my_offset', 200,
  make_point(
    with_variable('x_offset',
      CASE
        WHEN (@my_offset * cos(@the_angle)) >= 0 THEN (@my_offset *
cos(@the_angle))
        ELSE (@my_offset * cos(@the_angle)) * -1
      END,
      $x - @x_offset),
    with_variable('y_offset',
      CASE
        WHEN (@my_offset * sin(@the_angle)) >= 0 THEN (@my_offset *
sin(@the_angle))
        ELSE (@my_offset * sin(@the_angle)) * -1
      END,
      $y - @y_offset)
  )
))

Note that for "offset" labels, I know the rotation property isn't data
defined but if you scroll down a little bit there is another data defined
property for it.

I hope that is helpful, or at least provides a decent starting point for
what you are after. There's always the label toolbar for modifying labels
and callouts individually as well.

[1] https://imgur.com/a/JMR9w1h

On Fri, 2 Jul 2021 at 00:44, Tony Shepherd (FarmMaps NZ) <tony at farmmaps.nz>
wrote:

> Hi All
>
>
>
> Simple question, but I am not sure how best to word it. Since we all love
> maps, I have a diagram below.
>
>    1. Create some points along a polyline. Easy using “Points along
>    Gemoetry”
>    2. Display and label the points and put a rotation on the labels and
>    justify as needed (middle right in this case)
>
>
>
> What I want to do is shift the label out from the point slightly. That’s
> easy if using Offset X and Y, but it does exactly as expected and moves
> them X and Y, NOT accounting for the angle.
>
>
>
> I’m guessing I would need to use Data Defined X and Y and do some
> trigonometry using the angle and distance to calculate the new preferred
> label position.  Does anybody have code to do this?
>
>
>
> A simple workaround I did use was to just pad a few spaces to the end of
> the label (ie “FIELD NAME” || ‘    ‘)
>
>
>
> Cheers
>
> Tony
>
>
>
> *Tony Shepherd*  *|*  GeoSpatial / Mapping Manager
>
> Ph 027 435 6193  *|*  E tony at farmmaps.nz
>
> FarmMaps NZ, 3 Kerwood Place, Gore 9740
>
>
>
> <http://tonyshepherd8.wixsite.com/home>
> <https://www.wunderground.com/personal-weather-station/dashboard?ID=ISTLGORE2>
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210702/6e32d63f/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 32217 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210702/6e32d63f/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 2467 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210702/6e32d63f/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 2510 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210702/6e32d63f/attachment-0005.png>


More information about the Qgis-user mailing list