Colour path segments / polygon vertices
Some layers can accept a different colour per 'segment' (or vertex for polygon). For example we can have a multi-coloured path (eg ).
(and polygon )
The interleaved example is probably the way to go. (worked on here
mapdeck and/or spatialwidget should support colouring coordinates (or the segments between them
## gives the number of segments for each path
sapply( mapdeck::roads$geometry, function(x) ( length(x) / 2 ) - 1 )
So colouring could
- default to
1:n_segmentscolours - user-supplied vector, the same length as n_segments, which will have colours applied (will be a list-column on an
sfobject ZorMcomponent ofsfobject
Use Cases
- speeds (congestion) on roads
- elevation
- per-road attribute (parking, clearway, region, name, district, council... )
Implementation ideas
currently user specifies a column or a hex string. Need to know if the column is a list, and if so, it's the same length as the number of segments/vertices (or should the colours get recycled?)
If it's a list, spatialwidget will need to return a JSON array of hex strings for the colour.
If it's a list of hex strings, we will assume the list will apply to the segments directly
How do we let the user specify to use the sfc column, and and/or the Z/M attribute?
Maybe we could use a add_segmented_path() function, which will only accept list colours?
TODO
- [ ] update this SO answer
FWIW, recent rgl has a meshColor argument, which controls how input colours are recycled - by 'vertices', 'edges', or 'faces'. (face/edge is the same for lines, triangle/edge is distinct). I guess you'd add 'features' - in rgl feature- grouping is effectively stored indirectly as material properties, it's not reliable).
I'm actually writing up more about this now for mesh3d, but thought it might be worth knowing about if you are considering lower level colours.
Forgot to update this issue :s
This is possible by adding a value to each vertex
df_roads <- sfheaders::sf_to_df(mapdeck::roads, fill = TRUE)
## Add a random value for each coordinate
## These will become our colours at each vertex
df_roads$random <- sample(0:100, size = nrow(df_roads), replace = TRUE)
## rebuild the sf and include the `random` column as a `list`, so the value
## persists per vertex
sf_roads <- sfheaders::sf_linestring(
obj = df_roads
, x = "x"
, y = "y"
, linestring_id = "linestring_id"
, list_columns = "random"
, keep = TRUE
)
mapdeck() %>%
add_path(
data = sf_roads
, stroke_colour = "random"
, stroke_width = 10
)