Line meteor gradients with data-defined start and end colors

Doing line gradients in QGIS is an interesting challenge.

Anita Graser gave a hack in a post.

Lutra Consulting had an idea of gradient colored meshes, but they didn’t reach their goal to develop it.

But the question asked by Tammo on Twitter was quite special. Tammo wanted to apply gradients on lines, and data-define the starting and ending color along a certain color ramp.

Here is my result, that I also gave on StackOverflow. It uses a hack based on interpolating points along a line and coloring them based on their position.

How to do this ?

First, set your line to geometry generator to Points

Then, apply this expression to have interpolated points along the line :

collect_geometries(
  array_foreach(
    generate_series(0, length($geometry), 0.000001), -- here, we add a point every 0.000001 unit (I am in WGS84)
    line_interpolate_point($geometry, 
    @element
  )
)
You can not see it, but there are very little points !

Here is a zoomed-in image of the line :

The points are very near from one another. I don’t know if you can see them. That’s why it looks like lines.

Finally, just set the filling color according to the position of the point along the line, and along the subsetted color ramp :

ramp_color( -- this function allows you to select one particular color along a color ramp
  'Viridis', -- I ❤ Viridis
  scale_linear(
    @geometry_part_num, -- @geometry_part_num is the number of the point along the line (its position)
    0, @geometry_part_count, -- @geometry_part_count is the number for points along the line
    "start"/10, "end"/10 -- my values are between 0 and 10, so 10/10 gives 1 (the max of the color ramp)
  )
)
Here it is !

What if you made them meteors ?

Just set the size of each point from a min to a max :

scale_linear(
  @geometry_part_num,
  0, @geometry_part_count,
  0, 10 -- 10 : max size
)

So, what could be the use of such a rendering ?

Imagine you want to visualize the routes that connect the places with the most people, or that will connect places with few people.

Or, for instance, imagine you want to see the “cost” of going from one place to another, depending on relief.

This type of rendering could be useful to see the change of a particular variable between two states (before – after or origin – destination).