# NOT RUN {
if (interactive()) {
library(leaflet)
library(leafem)
library(stars)
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x1 = read_stars(tif)
x1 = x1[, , , 3] # band 3
tmpfl = tempfile(fileext = ".tif")
write_stars(st_warp(x1, crs = 4326), tmpfl)
myCustomJSFunc = htmlwidgets::JS(
"
pixelValuesToColorFn = (raster, colorOptions) => {
const cols = colorOptions.palette;
var scale = chroma.scale(cols);
if (colorOptions.breaks !== null) {
scale = scale.classes(colorOptions.breaks);
}
var pixelFunc = values => {
let clr = scale.domain([raster.mins, raster.maxs]);
if (isNaN(values)) return colorOptions.naColor;
return clr(values).hex();
};
return pixelFunc;
};
"
)
leaflet() %>%
addTiles() %>%
addGeotiff(
file = tmpfl
, opacity = 0.9
, colorOptions = colorOptions(
palette = grey.colors
, na.color = "transparent"
)
, pixelValuesToColorFn = myCustomJSFunc
)
}
# }
Run the code above in your browser using DataLab