r-spatial/leafem

addImageQuery() not working with leafletProxy() in R shiny app

gopalpenny opened this issue · 2 comments

First of all, thanks so much for providing the leafem package -- it offers wonderful and elegant functionality for leaflet maps.

I'm not sure if this is a bug or a feature request for leafem, but wanted to let you know about an issue I'm having with the addImageQuery function. I'm writing a shiny app that generates a raster based on user input, and I would like to add this raster to a leaflet map and then have the raster values be displayed using addImageQuery.

So far I've only been able to use the addImageQuery function when I initially generate the map (see example in code block 1). However, when I first generate the map and then use leafletProxy to add the raster and image query, the raster is added to the map but the query does not work (see example in code block 2).

Code block 1 (addImageQuery works):

library(leaflet)
library(plainview)
library(leafem)
library(shiny)

ui <- fluidPage(
  fluidRow(
    leafletOutput("map")
  )
)

server <- function(input, output) {
  output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("OpenStreetMap") %>%
      addRasterImage(poppendorf[[1]], project = TRUE, group = "poppendorf",
                     layerId = "poppendorf") %>%
      addImageQuery(poppendorf[[1]], project = TRUE,
                            layerId = "poppendorf",position="bottomleft",prefix = "")
  })
}

shinyApp(ui, server)

Code block 2 (raster added on click, addImageQuery produces a small "bubble" in the bottom left corner but does not display any values):

library(leaflet)
library(plainview)
library(leafem)
library(shiny)

ui <- fluidPage(
  fluidRow(
    leafletOutput("map")
  )
)

server <- function(input, output) {
  output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("OpenStreetMap") %>%
      setView(11.2,49.73,12)
  })
  
  counter <- reactiveValues(
    counter = 0
  )
  
  observeEvent(input$map_click,{
    counter$counter <- counter$counter + 1
    if (counter$counter == 1) {
      leafletProxy("map") %>%
        addRasterImage(poppendorf[[1]], project = TRUE, group = "poppendorf",
                       layerId = "poppendorf") %>%
        addImageQuery(poppendorf[[1]], project = TRUE,
                      layerId = "poppendorf",position="bottomleft",prefix = "")
      
    }
  })
}

shinyApp(ui, server)

It would be great to have addImageQuery work with leafletProxy calls.

I have the same problem.

This is a structural problem of how the javascript query is contructed at the moment. This will need some effort to implement correctly. Not sure when I can get to this right now. If anyone knows someone with js skills, this should not be too hard...