3

I'm using QGIS 3.16.15-Hannover (LTR).

By using "Points to path" from the toolbox I created a path/line between several points from a CSV. However, I did not find any way to colour the generated path by attributes. I would like to have a colour gradient from the first to last entry.

To explain with a gradient from white to black: The line from point 1880 to point 1890 should be white, the line from point 1890 to point 1900 should be light gray, ..., the line from point 1930 to point 1940 should be black.

How is this possible?

I assume I need another tool instead of Points-to-path.

My CSV looks like this:

year;x;y
"1880";26.31547;59.71049
"1890";26.21103;59.63769
"1900";26.23298;59.59843
"1910";26.48264;59.62822
"1920";30.01107;60.03541
"1930";30.13071;59.99484
"1940";27.81746;59.24653

Palette:

year;colour
1880;#0037ff
1890;#8f77ff
1900;#cdbaff
1910;#ffffff
1920;#ffbfaa
1930;#ff7b5a
1940;#ff0000
3
  • 2
    Brake the line into linestrings at vertices, join attributes (i.e. a color attribute) by location if needed (be careful with a start/end point), and apply the categorized styles.
    – Taras
    Jan 6, 2022 at 8:58
  • Thanks! I broke the lines with the tool "Explode lines", it worked fine. But how to conserve the data from the column "year" in the process? Each year is a starting point. However, this data alreday gets lost when using the tool "Points to path"...
    – Alrik
    Jan 6, 2022 at 10:21
  • Dear Taras, is "Explode lines" the correc tway to brake the line? Or do you have another idea (preserving "year")?
    – Alrik
    Jan 6, 2022 at 22:28

3 Answers 3

6

You can set the style of the line to Symbol layer type of Interpolated Line and apply a color ramp. Then the line from the start to the end is styled in a smoothly changing color ramp:

enter image description here

4
  • This could be a good alternative! Thanks for the suggestion!
    – Alrik
    Jan 6, 2022 at 12:03
  • I had to update to QGIS 3.22, but then I had the "interpolated line" option. With continuous years the result is very good. However, with gaps in the data linestrings would be better.
    – Alrik
    Jan 6, 2022 at 15:02
  • 1
    Problem solved! I realized I can set fixed start and end values. Thanks.
    – Alrik
    Jan 6, 2022 at 16:09
  • For me this just makes the points black but then there are no lines.
    – Joooeey
    Jul 4, 2023 at 9:42
5

Another option to style each segment in a different style is to set symbol layer type to Geometry generator with this expression: segments_to_lines ($geometry).

Then you can use data driven override for styling (color, stroke width etc.) with the variable @geometry_part_num. See how and where to make the inputs:

enter image description here

enter image description here

3
  • Interesting approach! But how can I assign colours from a CSV file to the line segments? There is no information about the "names" of the line segments stored, or am I wrong here? I pasted a CSV with colour coldes into my original post.
    – Alrik
    Jan 7, 2022 at 14:13
  • I have some CSVs with data gaps. But I would like to assign always the same colour to the same "year segment" of the line.
    – Alrik
    Jan 7, 2022 at 14:15
  • 1
    I added a new answre to address this problem.
    – Babel
    Jan 7, 2022 at 14:22
2

There's another way to do that and you can use the year attribute from the CSV for setting the color.

Create the line by adding an additional symbol layer of type Geometry generator and create the line with this expression:

make_line (
    geometry (get_feature_by_id (@layer, $id)),
    geometry (get_feature_by_id (@layer, $id+1)))

You can then set year as source of the color ramp: the colors will be selected from the color ramp that is set in the assistant below.

If you want to use color codes defined in your data, it depends if you have two different CSV as in your question (one with year;x;y the other with year;colour) or just one CSV (year;x;y;colour). If you have this last case with everything on the same layer, use Data driven override / Edit (not assistant) and define colour (the name of the field containing the colour values).

If you have the colour values on another layer called col, use this expression (again with Data driven override / Edit... ), to retrieve the color value for the respective year:

attribute (get_feature( 'col', 'year', year), 'colour')

Creating the line with Geometry generator: enter image description here

Setting the year as source of the color ramp - color values come from the white-to-black ramp defined in the lower part: enter image description here

To use color values defined in the layer col, use these settings:

enter image description here

This is how the CSV looks like to produce this result: enter image description here

7
  • 2
    is on fire 🤟 nice 🤙
    – Taras
    Jan 7, 2022 at 14:24
  • Working on this... The problem seems to be the data transfer from the CSV to the color ramp. I adjusted the CSV as you suggested above (year/x/y/colour). However, "year" does not return any colour code. In the "Expression Dialog", the expression "attribute('colour')" returns the correct value.
    – Alrik
    Jan 7, 2022 at 16:14
  • When I'm in the assistant's menu "Symbol stroke color", I can select "year" as source. Then, the values are returned correctly in the right part of the windows (1890, 1900, 1910...). Nevertheless the colour ramp does not change according to the color values in the CSV-file.
    – Alrik
    Jan 7, 2022 at 16:45
  • Is there a problem with the data format in the column "colour"? Does #0037ff not work in QGIS?
    – Alrik
    Jan 7, 2022 at 16:47
  • 1
    If you select "year" in the assistant, then of course the saved color values will not show - the colors that will show are those of the color ramp set in the lower part of the assitant. If you want to use the pre-defined color in your CSV, in the data driven override do not select assistant, but Edit... and insert the expression colour (the field where your colour values are saved, in the case they are in the same layer as the lines), or attribute (get_feature( 'col', 'year', year), 'colour') if the field colour is on another layer called col: i.stack.imgur.com/tLRFs.jpg
    – Babel
    Jan 7, 2022 at 23:05

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.