简体   繁体   English

R Leaflet:检测哪个多边形是 map 边界中心

[英]R 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.我想自动检测哪个多边形位于 map 的中心。 And it should update dynamically when the user is moving through the map.当用户通过 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.我想我可以用 shinyjs 或 javascript 模拟input$map_shape_click并获得 input$map_shape_click$id,但在我使用 go 之前,我想确保没有其他方法。

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)
  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)

So I found a way to do it with the function sf::st_intersects所以我找到了一种使用 function sf::st_intersects的方法

  observeEvent(input$map_bounds,{
        rv$center <- data.frame(x = mean(c(input$map_bounds$north, input$map_bounds$south)),
                            y = mean(c(input$map_bounds$east, input$map_bounds$west)))


    res <- sf::st_as_sf(rv$center, coords=c("y","x"), crs=st_crs(cities$geometry))
    
    intersection <- as.integer(st_intersects(res, cities$geometry))
    print(if_else(is.na(intersection), '', cities$NAME_2[intersection]))
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM