Search code examples
rshinyr-leaflet

Detect on which polygon is the map bounds center


I would like to automatically detect which polygon is at the center of the map. And it should update dynamically when the user is moving through the map.

For the moment I could not find a way to reverse find on which polygon are some coordinates.

I think I could simulate a input$map_shape_click with shinyjs or javascript and so get input$map_shape_click$id, but before I go to this solution, I would like to make sure there is no other way.

Here is a minimal example

library(leaflet)
library(shiny)

# data source :  https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_FRA_2_sp.rds
cities <- readRDS(file = "../gadm36_FRA_2_sf.rds")

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

server <- function(input, output, session) {
  rv <- reactiveValues()
  
  output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(provider = providers$CartoDB.Positron) %>%
      setView(lng = 1, lat = 45, zoom = 8) %>%
      addPolygons(data = cities,layerId = ~NAME_2,label = ~NAME_2)
  })
  
  observeEvent(input$map_bounds,{
    rv$center <- c(mean(input$map_bounds$north, input$map_bounds$south), mean(input$map_bounds$east, input$map_bounds$west))
    # how can I detect on which polygon the center is ?
  })
}
shinyApp(ui = ui, server = server)

Solution

  •   library(leaflet)
      library(shiny)
      library(sf)
      cities <- readRDS(file = "gadm36_FRA_2_sp.rds") %>%
        st_as_sf()
      ui <- fluidPage(leafletOutput("map"))
      server <- function(input, output, session) {
        rv <- reactiveValues()
        output$map <- renderLeaflet({
          leaflet() %>%
            addProviderTiles(provider = providers$CartoDB.Positron) %>%
            setView(lng = 1, lat = 45, zoom = 8) %>%
            addPolygons(data = cities, layerId = ~NAME_2, label = ~NAME_2)
        })
        observeEvent(input$map_bounds, {
          rv$center <- c(mean(input$map_bounds$north, input$map_bounds$south), mean(input$map_bounds$east,
                                                                                    input$map_bounds$west))
          pnt       <- st_point(c(rv$center[2], rv$center[1]))
      
          rslt <- cities[which(st_intersects(pnt, cities, sparse = FALSE)),]$NAME_1
          print(rslt)
        })
      }
      shinyApp(ui = ui, server = server)