【问题标题】:R Shiny Leaflet Show number of overlapping markers / polygons?R Shiny Leaflet 显示重叠标记/多边形的数量?
【发布时间】:2023-02-07 12:09:05
【问题描述】:

我正在处理大量位置数据,结果发现我的很多位置都共享经度和纬度值。有没有办法通过弹出窗口或其他小部件显示重叠的标记/多边形的数量?

我无法从我的数据集中删除共享经度和纬度值的站点。

#############################################
# Needed Libraries & Input Files

library(shiny)
library(shinydashboard)
library(leaflet)

## The Data
Point_ID = c("A1", "B1", "C1")
Latitude = c(38.00, 38.00, 38.00)
Longitude = c(-107.00, -107.00, -107.00)
Map_DF <- data.frame(Point_ID, Latitude, Longitude)

choiseList <- c("A1", "B1", "C1")

#############################################
# UI
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(checkboxGroupInput(inputId = "IDPointInput", label = "Select Point ID", choices = choiseList, selected = choiseList)),
  dashboardBody(fluidRow(leafletOutput(outputId = 'mapA')))
)

#############################################
# SERVER
server <- function(input, output, session) {
  
  ## The Filter
  filter_df <- reactive({
    Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
  })
  
  ## Base Map Creation
  output$mapA <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(
        providers$Esri.DeLorme,
        options = providerTileOptions(
          updateWhenZooming = FALSE,
          updateWhenIdle = TRUE)
      ) %>%
      setView(lng = -107.50, lat = 39.00, zoom = 7)
  })
  
  ## Update Map with Filter Selection
  observe({
    leafletProxy("mapA", session) %>%
      clearMarkers() %>%
      addCircleMarkers(
        data = filter_df(),
        radius = 10,
        color = "red",
        lat = ~Latitude,
        lng = ~Longitude,
        popupOptions(autoPan = FALSE),
        popup = ~paste("PointID: ", filter_df()$Point_ID))
        # Show number of sites that overlap oneanother
  })
}

############################################
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 考虑这个解决方案的概要。计算具有指定半径的给定纬度/经度周围的缓冲区。下一步是识别缓冲区内的所有点。然后你可以按照你认为合适的方式处理重复项,请注意包 sf 有一个函数 st_buffer 和其他函数来查找落在缓冲区内的点。这个网站可能会有帮助crd150.github.io/buffers.html如果你喜欢这个方法并且你仍然需要帮助,我可以做更多。

标签: r shiny leaflet r-leaflet


【解决方案1】:

addAwesomeMarkers 适合你吗?

library(shiny)
library(shinydashboard)
library(leaflet)

## The Data
Point_ID = c("A1", "B1", "C1")
Latitude = c(38.00, 38.00, 38.00)
Longitude = c(-107.00, -107.00, -107.00)
Map_DF <- data.frame(Point_ID, Latitude, Longitude)

choiseList <- c("A1", "B1", "C1")

#############################################
# UI
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(checkboxGroupInput(inputId = "IDPointInput", label = "Select Point ID", choices = choiseList, selected = choiseList)),
  dashboardBody(fluidRow(leafletOutput(outputId = 'mapA')))
)

#############################################
# SERVER
server <- function(input, output, session) {
  
  ## The Filter
  filter_df <- reactive({
    Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
  })
  
  ## Base Map Creation
  output$mapA <- renderLeaflet({
    leaflet(filter_df()) %>% 

      addProviderTiles(
        providers$Esri.DeLorme,
        options = providerTileOptions(
          updateWhenZooming = FALSE,
          updateWhenIdle = TRUE)
      )  %>%
      
      setView(lng = -107.50, lat = 39.00, zoom = 7) %>% 
      
     # use addAwesomeMarkers #
      addAwesomeMarkers(
        lat = ~Latitude,
        lng = ~Longitude,
        icon=~makeAwesomeIcon(
          icon = 'ios-close',
          iconColor = 'black',
          library = 'ion',
          markerColor = 'orange'),
        
        label=~as.character(Point_ID),
        
        popup = ~paste("PointID: ", Point_ID),
        
        clusterOptions = markerClusterOptions()
      )
  })
}

############################################
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2017-07-03
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多