Hi @benkates,
I have a small improvement since last time.
The user, after creating the polygons he needs first, opens a second map from which he can select the polygons he is interested in, but when I try to recall the user selection I have as output
id selected
1 1 TRUE
2 2 FALSE
it might be a good thing to use this output to intersect it with the previous polygons and finally to get the single output.
library(shiny)
library(shinydashboard)
library(sf)
library(sp)
library(leaflet)
library(leaflet.extras)
library(leafem)
library(mapview)
library(mapedit)
# https://www.r-spatial.org/r/2017/06/09/mapedit_0-2-0.html
ui <- dashboardPage(dashboardHeader(title = "Map edit shiny app",
titleWidth = 350),
dashboardSidebar(width = 350,
sidebarMenu(
menuItem("Map", tabName = "map", icon = icon("map")))),
dashboardBody(tabItems(tabItem(tabName = "map",
fluidRow(editModUI(id="map_a"),
actionButton(inputId = "write_polygon",
label = "Save the polygon"),
selectModUI(id="select_polygon"),
uiOutput(outputId = "map_b"))))))
server <- function(input, output, session) {
map <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap,
options = tileOptions(minZoom = 2, maxZoom = 15)) %>%
addProviderTiles(providers$Esri.WorldImagery,
options = tileOptions(minZoom = 15, maxZoom = 20),
group = "Esri.WorldImagery") %>%
addControlGPS(options = gpsOptions(position = "topleft",
activate = TRUE,
autoCenter = TRUE,
maxZoom = 5,
setView = TRUE)) %>%
leaflet.extras::addSearchOSM(options = searchOptions(collapsed = TRUE, autoCollapse = TRUE)) %>%
addDrawToolbar(targetGroup='drawPoly',
polylineOptions = F,
circleOptions = F,
markerOptions = F,
circleMarkerOptions = F,
rectangleOptions = F,
singleFeature = FALSE,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addMeasure(primaryLengthUnit = "meters",
primaryAreaUnit = "hectares",
position = "topleft",
activeColor = "red",
completedColor = "red",
localization = "en")
edits <- callModule(editMod,
leafmap = map,
id = "map_a")
observeEvent(input$write_polygon, {
polygon<-edits()$finished
output$map_b<-renderUI({
mapviewOutput(outputId = "map_b_1")
actionButton(inputId = "save_selected_polygon",
label = "Save the selected polygon")
})
output$map_b_1<-renderMapview({
mapview(polygon)
})
selected_polygons <- callModule(selectMod,
"select_polygon",
leaflet() %>%
addTiles() %>%
addFeatures(st_sf(polygon),
layerId = ~seq_len(length(X_leaflet_id)))
)
observeEvent(input$save_selected_polygon, {
new_polygon <- selected_polygons()
print(new_polygon)
})
})
}
shinyApp(ui=ui, server = server)