R Shiny Leaflet显示重叠标记/多边形的数量?

huangapple go评论87阅读模式
英文:

R Shiny Leaflet Show number of overlapping markers / polygons?

问题

以下是您要翻译的内容:

  1. 我正在处理一大批位置数据,发现很多位置共享相同的经度和纬度数值。有没有办法通过弹出窗口或其他小部件来显示重叠的标记/多边形数量?
  2. 我无法从我的数据集中删除共享经度和纬度数值的站点。
  3. #############################################
  4. # 必需的库和输入文件
  5. library(shiny)
  6. library(shinydashboard)
  7. library(leaflet)
  8. ## 数据
  9. Point_ID = c("A1", "B1", "C1")
  10. Latitude = c(38.00, 38.00, 38.00)
  11. Longitude = c(-107.00, -107.00, -107.00)
  12. Map_DF <- data.frame(Point_ID, Latitude, Longitude)
  13. choiseList <- c("A1", "B1", "C1")
  14. #############################################
  15. # 用户界面
  16. ui <- dashboardPage(
  17. dashboardHeader(),
  18. dashboardSidebar(checkboxGroupInput(inputId = "IDPointInput", label = "选择点ID", choices = choiseList, selected = choiseList)),
  19. dashboardBody(fluidRow(leafletOutput(outputId = 'mapA')))
  20. )
  21. #############################################
  22. # 服务器
  23. server <- function(input, output, session) {
  24. ## 过滤器
  25. filter_df <- reactive({
  26. Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
  27. })
  28. ## 创建基础地图
  29. output$mapA <- renderLeaflet({
  30. leaflet() %>%
  31. addProviderTiles(
  32. providers$Esri.DeLorme,
  33. options = providerTileOptions(
  34. updateWhenZooming = FALSE,
  35. updateWhenIdle = TRUE)
  36. ) %>%
  37. setView(lng = -107.50, lat = 39.00, zoom = 7)
  38. })
  39. ## 使用过滤选择更新地图
  40. observe({
  41. leafletProxy("mapA", session) %>%
  42. clearMarkers() %>%
  43. addCircleMarkers(
  44. data = filter_df(),
  45. radius = 10,
  46. color = "red",
  47. lat = ~Latitude,
  48. lng = ~Longitude,
  49. popupOptions(autoPan = FALSE),
  50. popup = ~paste("PointID: ", filter_df()$Point_ID))
  51. # 显示重叠站点的数量
  52. })
  53. }
  54. ############################################
  55. shinyApp(ui = ui, server = server)
英文:

I'm working with a large set of location data, and it turns out a lot of my locations share longitude and latitude values. Is there a way to show the number of markers / polygons that overlap, either through a popup or some other widget?

I can not remove sites that share longitude and latitude values from my dataset.

  1. #############################################
  2. # Needed Libraries &amp; Input Files
  3. library(shiny)
  4. library(shinydashboard)
  5. library(leaflet)
  6. ## The Data
  7. Point_ID = c(&quot;A1&quot;, &quot;B1&quot;, &quot;C1&quot;)
  8. Latitude = c(38.00, 38.00, 38.00)
  9. Longitude = c(-107.00, -107.00, -107.00)
  10. Map_DF &lt;- data.frame(Point_ID, Latitude, Longitude)
  11. choiseList &lt;- c(&quot;A1&quot;, &quot;B1&quot;, &quot;C1&quot;)
  12. #############################################
  13. # UI
  14. ui &lt;- dashboardPage(
  15. dashboardHeader(),
  16. dashboardSidebar(checkboxGroupInput(inputId = &quot;IDPointInput&quot;, label = &quot;Select Point ID&quot;, choices = choiseList, selected = choiseList)),
  17. dashboardBody(fluidRow(leafletOutput(outputId = &#39;mapA&#39;)))
  18. )
  19. #############################################
  20. # SERVER
  21. server &lt;- function(input, output, session) {
  22. ## The Filter
  23. filter_df &lt;- reactive({
  24. Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
  25. })
  26. ## Base Map Creation
  27. output$mapA &lt;- renderLeaflet({
  28. leaflet() %&gt;%
  29. addProviderTiles(
  30. providers$Esri.DeLorme,
  31. options = providerTileOptions(
  32. updateWhenZooming = FALSE,
  33. updateWhenIdle = TRUE)
  34. ) %&gt;%
  35. setView(lng = -107.50, lat = 39.00, zoom = 7)
  36. })
  37. ## Update Map with Filter Selection
  38. observe({
  39. leafletProxy(&quot;mapA&quot;, session) %&gt;%
  40. clearMarkers() %&gt;%
  41. addCircleMarkers(
  42. data = filter_df(),
  43. radius = 10,
  44. color = &quot;red&quot;,
  45. lat = ~Latitude,
  46. lng = ~Longitude,
  47. popupOptions(autoPan = FALSE),
  48. popup = ~paste(&quot;PointID: &quot;, filter_df()$Point_ID))
  49. # Show number of sites that overlap oneanother
  50. })
  51. }
  52. ############################################
  53. shinyApp(ui = ui, server = server)

答案1

得分: 2

addAwesomeMarkers在您这里工作吗?

英文:

Does addAwesomeMarkers work for you?

  1. library(shiny)
  2. library(shinydashboard)
  3. library(leaflet)
  4. ## The Data
  5. Point_ID = c(&quot;A1&quot;, &quot;B1&quot;, &quot;C1&quot;)
  6. Latitude = c(38.00, 38.00, 38.00)
  7. Longitude = c(-107.00, -107.00, -107.00)
  8. Map_DF &lt;- data.frame(Point_ID, Latitude, Longitude)
  9. choiseList &lt;- c(&quot;A1&quot;, &quot;B1&quot;, &quot;C1&quot;)
  10. #############################################
  11. # UI
  12. ui &lt;- dashboardPage(
  13. dashboardHeader(),
  14. dashboardSidebar(checkboxGroupInput(inputId = &quot;IDPointInput&quot;, label = &quot;Select Point ID&quot;, choices = choiseList, selected = choiseList)),
  15. dashboardBody(fluidRow(leafletOutput(outputId = &#39;mapA&#39;)))
  16. )
  17. #############################################
  18. # SERVER
  19. server &lt;- function(input, output, session) {
  20. ## The Filter
  21. filter_df &lt;- reactive({
  22. Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
  23. })
  24. ## Base Map Creation
  25. output$mapA &lt;- renderLeaflet({
  26. leaflet(filter_df()) %&gt;%
  27. addProviderTiles(
  28. providers$Esri.DeLorme,
  29. options = providerTileOptions(
  30. updateWhenZooming = FALSE,
  31. updateWhenIdle = TRUE)
  32. ) %&gt;%
  33. setView(lng = -107.50, lat = 39.00, zoom = 7) %&gt;%
  34. # use addAwesomeMarkers #
  35. addAwesomeMarkers(
  36. lat = ~Latitude,
  37. lng = ~Longitude,
  38. icon=~makeAwesomeIcon(
  39. icon = &#39;ios-close&#39;,
  40. iconColor = &#39;black&#39;,
  41. library = &#39;ion&#39;,
  42. markerColor = &#39;orange&#39;),
  43. label=~as.character(Point_ID),
  44. popup = ~paste(&quot;PointID: &quot;, Point_ID),
  45. clusterOptions = markerClusterOptions()
  46. )
  47. })
  48. }
  49. ############################################
  50. shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年2月7日 02:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定