r-spatial / leafem

leaflet extensions for mapview
https://r-spatial.github.io/leafem/
Other
107 stars 28 forks source link

addCOG #65

Open frzambra opened 1 year ago

frzambra commented 1 year ago

I'm testing addCOG with some data I have in an AWS S3 bucket. I tried to follow to the examples on other issues but cannot get a colored map, all I can get is a gray scale.

url <- 'https://chelsa-chile.s3.amazonaws.com/monthly/pet/CHELSA_pet_v2.1_19790201.tif'

min_scale = 0; max_scale = 1
js_scale = paste0("function (values) {
                    var scale = chroma.scale(['white', '#22c7e8']).domain([", min_scale, ",", max_scale, "]);
                    var val = values[0];
                    if (val === 0) return;
                    if (val < 0) return;
                    return scale(val).hex();
                    }")

leaflet(options = leafletOptions(attributionControl = FALSE)) %>% 
  setView(lng =-70.09635, lat =  -33.01703, zoom = 4) %>% 
  addProviderTiles("Esri.WorldImagery", group = "esri") %>%
  addMapPane("cog", zIndex = 500) %>%
  leafem:::addCOG(
    url = url
    , group = "PET"
    , opacity = 0.7
    , options = list(pane = "cog")
    # , resolution = 96
    , autozoom = FALSE
    , colorOptions = colorOptions(
      palette = terrain.colors(256)
    )
    , pixelValuesToColorFn = JS(js_scale)
  ) %>%
  addMouseCoordinates() %>%
  addLayersControl(
    baseGroups = c("esri")
    , overlayGroups =  c("PET")
  )

Also, I developed a shiny app to test addCOG and addGeoRaster which is in https://frzambra.shinyapps.io/visraster_app/

Any thoughts?

trafficonese commented 3 months ago

maybe try to change the min/max_scale values and the js_scale function. The data has a pretty wide range (from -2000000000 to 150000).

I get a colored image with:

min_scale = 0; max_scale = 100000
js_scale = paste0("function (values) {
                    var scale = chroma.scale(['white', 'red', 'yellow', '#22c7e8']).domain([", min_scale, ",", max_scale, "]);
                    var val = values[0];
                    if (val === 0) return;
                    if (val < 0) return;
                    return scale(val).hex();
                    }")