leafgl icon indicating copy to clipboard operation
leafgl copied to clipboard

Not possible to recover layerID as in leaflet

Open ischlo opened this issue 1 year ago • 7 comments

Hello,

I used your package for rendering a relatively big data set of polygons (12k) and it's impressive how faster and more fluid it is, so thanks for that !

I was wondering if the layerId are passed on with the shapes as well. In my shiny app, I use the layerID when a shape is clicked on. While the popups work as expected, I could not get the layerIDs as with the regular leaflet functions.

Thanks in advance, Best,

ischlo avatar Sep 14 '22 21:09 ischlo

Do you have a small reprex?

tim-salabim avatar Sep 15 '22 06:09 tim-salabim

Hi ! thanks for the quick reply, Here is a reprex, if you replace the leafgl functions by their leaflet equivalent, you should see the id and coordinates appear when clicking. it does not happen with leafgl functions... Thanks in advance

library(shiny)
library(leaflet)
library(leafgl)
library(sf)

test_grid <- data.frame(
    id = 1:3
    ,geometry = c("POLYGON ((-0.08946933215777712 51.47411466935998, -0.08969000802663779 51.47598131471286, -0.08781123839374265 51.476842086027, -0.08507730250047442 51.47619388479495, -0.08485681363623958 51.4743272330522, -0.08673553872787136 51.47346650705812, -0.08946933215777712 51.47411466935998))"   
                  ,"POLYGON ((-0.08651493322924536 51.47159981398401, -0.08673553872787136 51.47346650705812, -0.08485681363623958 51.4743272330522, -0.08212314240894439 51.47367901320982, -0.08190272389120813 51.47181231375328, -0.08378140444241272 51.47095163307104, -0.08651493322924536 51.47159981398401))" 
                  ,"POLYGON ((-0.08817291831604734 51.4688722666752, -0.0883936404299329 51.47073900106984, -0.08651493322924536 51.47159981398401, -0.08378140444241272 51.47095163307104, -0.08356086928788481 51.46908489229133, -0.08543953196103615 51.46822412468794, -0.08817291831604734 51.4688722666752))" )) %>% 
    st_as_sf(wkt = 2,crs = 4326)

map <-  leaflet(data = test_grid) %>% 
    addTiles() %>% 
    addGlPolygons(data = test_grid
                ,color = "dimgray"
                ,popup = paste0("clicked shape: ", test_grid$id)
                ,weight = 5
                ,opacity = 1
                ,layerId = ~id
                ,fill = TRUE
                ,fillOpacity = .9
                ,fillColor = "dimgray"
    )

ui <- fluidPage(
    leafglOutput("map")
    ,textOutput("clicked_shape")
)

server <- function(input, output) {
    output$map <- renderLeaflet({
       map
    })
    
    observe({
        map_click <- reactive({input$map_shape_click})
        output$clicked_shape <- renderPrint({
            map_click()
            # input$map_shape_click
            })
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

ischlo avatar Sep 15 '22 12:09 ischlo

@ischlo yeah, its a bit confusing. LeafGl uses map_glify_click instead of map_shape_click.

trafficonese avatar Sep 15 '22 12:09 trafficonese

@trafficonese should we align this with leaflet?

tim-salabim avatar Sep 15 '22 12:09 tim-salabim

Good question.. Leaflet emits events with the following categories: marker, shape, geojson, and topojson. So somehow it makes sense to have the glify layers interact differently too or? That should probably be clearer in the docs.

I'm thinking that it could maybe be useful to have the glify events handled differently, especially if one has a map with multiple layers where only 1 is a LeafGL layer.

trafficonese avatar Sep 15 '22 12:09 trafficonese

I see, it makes sense now! and , yes, it probably makes sense to have it as separate event. I just did not notice it in the docs. the reprex works now. Thanks !

ischlo avatar Sep 15 '22 13:09 ischlo

Agreed. We should make it clearer in the docs. Would you have time for this @trafficonese ?

tim-salabim avatar Sep 15 '22 15:09 tim-salabim