leafem
leafem copied to clipboard
mouseHandler for addGeotiff (geoblaze.js)
Using geoblaze.js allows to get the raster value at a certain lat/lng.
I modified leaflet's mouseHandler
to allow click
and mousemove
for addGeotiff
.
It could also be included in addCOG
but so far I didn't manage to use that function successfully.
I'm not sure if you want to include the new dependency (~1MB) or if you have something else in mind already. So feel free to close it or request additional changes.
Shiny example!
library(leaflet)
library(leafem)
library(stars)
library(shiny)
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;
};")
ui <- fluidPage(
leafletOutput("map"),
splitLayout(cellWidths = c("50%", "50%"),
div(h4("Click"),verbatimTextOutput("click")),
div(h4("Mouseover"),verbatimTextOutput("mouseover"))
)
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addGeotiff(
file = tmpfl
, group = "geotiffgroup"
, layerId = "somelayerid"
, opacity = 0.9
, colorOptions = colorOptions(
palette = grey.colors
, na.color = "transparent"
)
, pixelValuesToColorFn = myCustomJSFunc
)
})
output$click <- renderPrint({
txt <- req(input$map_georaster_click)
print(txt)
})
output$mouseover <- renderPrint({
txt <- req(input$map_georaster_mousemove)
print(txt)
})
}
shinyApp(ui, server)