leafem icon indicating copy to clipboard operation
leafem copied to clipboard

addFgb color depend on a variable?

Open trafficonese opened this issue 1 year ago • 4 comments

I am wondering if this is possible (using color = "MaxWind" or a palette)? I guess not, since color is not used in scaleOptions.

library(leaflet)
library(leafem)
library(sf)

lines <- st_as_sf(leaflet::atlStorms2005)

fgb_file <- tempfile(fileext = ".fgb")
st_write(lines, fgb_file)

leaflet() %>% 
  addTiles() %>% 
  leafem::addFgb(fgb_file, color = "MaxWind", opacity=1, weight = 1) %>% 
  leafem::addMouseCoordinates() %>%
  setView(-50, 45, 3)

I would like to use something similar to leafgl:

library(leafgl)
leaflet() %>% 
  addTiles() %>% leafem::addMouseCoordinates() %>% 
  addGlPolylines(data = lines, color = "MaxWind", opacity=1, weight = 1) 

trafficonese avatar Jul 29 '24 12:07 trafficonese

It works, when adding the color as a column name and passing color = NULL. This was hard to to find, maybe we can add some documentation to the function.

library(leaflet)
library(leafem)
library(sf)

lines <- st_as_sf(leaflet::atlStorms2005)

lines$color <- colorNumeric("viridis", lines$MaxWind)(lines$MaxWind)

fgb_file <- tempfile(fileext = ".fgb")
st_write(lines, fgb_file)

leaflet() %>% 
  addTiles() %>% 
  leafem::addFgb(fgb_file, color = NULL, opacity=1, weight = 1) %>% 
  leafem::addMouseCoordinates() %>%
  setView(-50, 45, 3)

trafficonese avatar Jul 29 '24 14:07 trafficonese

Yeah, given that we load the file directly in JavaScript, we need to have the coloring be part of the data... Unless we come up with a solution in JavaScript, which we can using chroma.js

tim-salabim avatar Jul 29 '24 14:07 tim-salabim

Thanks for your help! Yes we could do that, but I am not too familiar with chroma.js and for now I am fine with the current setup, but I thought we can document it better, as it is maybe not so intuitive to assign it as column with the correct name.

trafficonese avatar Jul 30 '24 08:07 trafficonese

Maybe, as a compromise, we can point out to assign a variable of any name and then let the user supply that name as the color/fillColor variable?

tim-salabim avatar Jul 30 '24 09:07 tim-salabim