0

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.

#############################################
# 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)
rwjam
  • 163
  • 10
  • Consider this outline of a solution. Compute a buffer around a given lat/lon with a specified radius. The next step would be to identify all the points that fall inside the buffer. Then you can deal with duplicates as you see fit, Note that package sf has a function st_buffer, and other functions to find points that fall inside a buffer. This site may be helpful https://crd150.github.io/buffers.html If you like the approach and you are still in need of help, I can do more. – YLC Feb 06 '23 at 23:54

1 Answers1

2

Does addAwesomeMarkers work for you?

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)
bdedu
  • 383
  • 1
  • 8