Skip to content

Instantly share code, notes, and snippets.

@datagistips
Last active February 18, 2021 17:56
Show Gist options
  • Save datagistips/603b86944d793c64f8347f858fbd41ba to your computer and use it in GitHub Desktop.
Save datagistips/603b86944d793c64f8347f858fbd41ba to your computer and use it in GitHub Desktop.
Color gradient lines based on their slopes
-- NOTE :
-- This gist has two separate parts : one for the geometry generator, one for the filling color
-- inputs : lines, slope raster
-- STEP 1/2 - On geometry generator, set this expression to have interpolated points :
collect_geometries(
array_foreach(
generate_series(0, length($geometry), 5), -- we add lines every 5 meters
line_interpolate_point($geometry, @element)
)
)
-- STEP 2/2 - On filling color, set this expression :
with_variable(
'min',
raster_statistic('slope', 1, 'min'), -- global min value of slope raster
with_variable(
'max',
raster_statistic('slope', 1, 'max'), -- global max value of slope raster
with_variable(
'start',
scale_linear(
raster_value(
'slope',
1,
start_point($geometry) -- we get the raster slope value for the starting node
),
@min, @max,
0,1 -- color gradients are between 0 (bottom of the gradient) and 1 (top of the gradient)
),
with_variable(
'end',
scale_linear(
raster_value(
'pente',
1,
end_point($geometry) -- we get the raster slope value for the ending node
),
@min, @max,
0,1
),
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, -- start node gradient slope value (stored in a variable)
@end -- end node gradient slope value (stored in a variable)
)
)
)
)
)
)
@datagistips
Copy link
Author

line-gradient2 jpeg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment