3

I have a QGIS line vector layer that contains two attributes: "attr1", "attr2".

I want to display it as shown below. The same line geometry two times with an offset in both directions, but the catch is that the coloring is based on the two attributes. Right now I did it by copying the layer and using a "categorized" symbology with the same color ramp, but once for the first, and once for the other attribute. Also, I set an opposite offset in both cases.

Is there a way to do this using only one layer and without a large amount of manual work? I could do it with rule-based symbology, but then I would have to set the colors manually. I also tried to merge the .qml style file of the two layer, but it didn't work out.

enter image description here

1 Answer 1

7

You could use data defined override on the colour of your lines, depending on the attributes.

Using ramp_color() you can use any number between 0 and 1 in order to get a colour within the mentioned ramp. Either you use one of the available ramps, or you create your own ramp using create_ramp().

Since your values exceed 1 by a margin, you need to normalize them first, using "attr1"/max("attr1"). max() returns the maximum numeric value within a column. If both attributes have a similar range of values, you could normalize them using the maximum of both attributes: "attr1"/max(max("attr1"),max("attr2"))

So for your first attribute the whole expression could be

ramp_color('Spectral',"attr1"/max(max("attr1"),max("attr2")))

Then insert your second attribute-name into the expression for the other line symbol and you should be left with something like the following:

enter image description here

Downside of this solution: You can't display the ramp reliable in the legend item in the layout manager.

1
  • Awesome, thank you! I really need to discover this data defined override part. Looks like it has almost everything.
    – 588chm
    Commented Feb 20 at 19:28

Your Answer

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

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