4

Using QGIS 3.22.1. I have straight lines connecting two points. These lines are stored in two separate layers, one layer for each direction.

I am using a graduated size renderer to display these lines. Now they do of course overlap. To prevent that from happening I want to set an offset having the size of the line's width. I know there is a variable to get the current features color (@symbol_color). Is there a similar variable (or some way) to get the current features width? I want to use it in data defined offset:

enter image description here

1
  • 2
    May work with scale_exp() and scale_linear() if one knows the correct settings. E.g. scale_linear("the_val",minimum("the_val"),maximum("the_val"),0.2,3.0) should or could do it, where 0.2 and 3.0 are the Size from and Size to values. If you can verfiy this, it is an acceptable answer ;) Meanwhile I figured out that my actual issue (not the question) is already solved by QGIS: I can just enter a fixed offset half the size of the "fixed" stroke width (even though it is not fixed), and no overlap will happen. Cant really explain in words and logic why this already solves the issue.
    – MrXsquared
    Apr 7, 2022 at 8:40

2 Answers 2

2

When you set the line size with data driven override, you can use the same expression, divided by 2 (offset just half of the line width to keep in touch with the other line) for the offset.

You can use e.g. the assistant to automatically generate stroke width with scale method linear or exponential. When set, you can look up and copy the expression generated based on these settings (see below) under the icon Data defined override > Edit.... Copy the expression and paste it under Offset > Data defined override - don't forget to divide it by 2:

![enter image description here

enter image description here

When using the assistant for data defined override for stroke width with input source the attribute named width with min/max values from 0.1 to 0.7 that should be styled with stroke width from 1 to 10 with exponential scale methdod and exponent 0.57, the expression generated looks like this:

coalesce(scale_exp("width", 0.1, 0.7, 1, 10, 0.57), 0)
1

I don't know of a way to access the "base" symbol size, as you would the "base" symbol color using @symbol_color. You can, however, effectively duplicate it using layer-level variables.

For instance, I have a placemark layer with different symbols (sometimes compound symbols) based on placemark type. In Layer properties / Variables for the layer, I define a variable @symbol_size set to '2.4' (base symbol size in mm).

Then I have a categorized renderer which renders structures as filled grey boxes whose size is a data-driven override, @symbol_size * 0.75. A historical site is an X with size @symbol_size * 0.60. And a marked destination is an O with size @symbol_size (implied * 1.0).

You could do the same in your instance with a graduated renderer accessing the (manually defined) @symbol_size variable in an expression reflecting the scaling attribute desired.

If you save/reuse styles, you should think what will happen (and what should happen) if you or another user load this symbology style but don't define the @symbol_size variable in the layer properties. In this case, QGIS will (depending on expression) most likely evaluate all the data-driven overrides as NULL, and so will not override whatever was entered as the base size for a line or symbol. It would be easy to forget that unlike @symbol_color, your faked @symbol_size is not autopopulated.

To scale all your symbology up or down, you just now have to edit the variable value in layer properties; you should no longer change it using the layer properties size value directly.

Finally, it is worth noting the variable you create is stored and populated as a string, but QGIS seems to be smart enough to convert it silently to a number if used in a simple arithmetic expression. But in a more complex expression you may need to type-convert it.

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.