Skip to content

Commit

Permalink
Modify view and options to viz geoms
Browse files Browse the repository at this point in the history
Add options to reset view, set view to selected objects.
Changed add and remove features to work with leaflet proxy
Set validate tab so that when clicked, it resets the map and hence gets rid of users polygon created with mapedit
  • Loading branch information
david-beauchesne committed Feb 13, 2022
1 parent 5bcf1ea commit 326dd50
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
9 changes: 6 additions & 3 deletions app/R/selectionMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ selectionMap <- function(geoms = NULL, set_view = TRUE) {
baseGroups = c('OpenStreetMap', 'Ocean Basemap', 'OpenTopoMap'),
position = 'bottomleft')

if (!is.null(geoms)) out <- leafem::addFeatures(out, geoms)

if (!is.null(geoms)) {
out <- leafem::addFeatures(out, geoms)
} else {
out <- leaflet::setView(out, lat = 45.6, lng = -63.6, zoom = 7)
}

if (set_view) {
out %>% leaflet::setView(lat = 45.6, lng = -63.6, zoom = 7)
} else out

}

47 changes: 41 additions & 6 deletions app/server.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server <- function(input, output, session) {

# INITIATE MAP
map <- selectionMap()
map <- selectionMap(set_view = TRUE)
edits <- callModule(editMod, leafmap = map, id = "map")

# SWITCH MAIN TAB FROM MAP TO REPORT WHEN SIDE TAB IS REPORT
Expand Down Expand Up @@ -170,15 +170,50 @@ server <- function(input, output, session) {

}, ignoreNULL = FALSE)

# MAP VIEWS
# Reset map when clicking on validate
observeEvent(input$geometries, {
if(input$geometries == "Validate") {
map <- selectionMap(geoms$select[input$check_input_areas, ], FALSE) # Switch to TRUE to set it to default Halifax view
callModule(editMod, leafmap = map, id = "map")
}
})

# Clear all polygons
observeEvent(input$clear_map, {
map <- selectionMap()
callModule(editMod, leafmap = map, id = "map")
leafletProxy("map-map") %>%
clearShapes()
})

# Add selected polygons
observeEvent(input$add_geoms_to_map, {
map <- selectionMap(geoms$select[input$check_input_areas, ])
callModule(editMod, leafmap = map, id = "map")
}, ignoreNULL = FALSE)
if (!is.null(geoms)) {
leafletProxy("map-map") %>%
clearShapes() %>%
leafem::addFeatures(geoms$select[input$check_input_areas, ])
}
})

# Reset map in explore
observeEvent(input$reset_view, {
leafletProxy("map-map") %>%
leaflet::setView(lat = 45.6, lng = -63.6, zoom = 7)
})

# Reset map in validate
observeEvent(input$reset_view_valid, {
leafletProxy("map-map") %>%
leaflet::setView(lat = 45.6, lng = -63.6, zoom = 7)
})

# Set view on selected polygons
observeEvent(input$select_view, {
# print(st_bbox(geoms$select[input$check_input_areas, ]))
bbv <- st_bbox(geoms$select[input$check_input_areas, ])
leafletProxy("map-map") %>%
leaflet::fitBounds(lng1 = bbv[[1]], lat1 = bbv[[2]], lng2 = bbv[[3]], lat2 = bbv[[4]])
})


observeEvent(input$valid_geoms, {
n <- length(input$check_input_areas)
Expand Down
15 changes: 9 additions & 6 deletions app/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ ui <- fluidPage(
"View map",
icon = icon("map"),
myhelptxt("Search and explore map."),
h5("Set view"),
actionButton("reset_view", "Reset view", icon = icon("rotate")),
actionButton('select_view', "View selected", icon = icon("check")),
br(),br(),
div(style="display: inline-block;",
textInput("location", label = "Explore by location", value = "Halifax")),
actionButton("search_loc", "Search", icon = icon("search")),
actionButton("reset_view", "Reset view", icon = icon("arrow-right")),
br(),

myhelptxt("The following tabs allow you to select and validate the geometries used to generate the report. 1. Select geometries in `Geometries`. 2. Validate geometries in `Validate`."),
tabsetPanel(
id = "geometries",
# LAYER CREATION
tabPanel(
"Geometries",
Expand Down Expand Up @@ -149,11 +147,16 @@ ui <- fluidPage(
myhelptxt("This tab allows you to validate geometries you wish to use to generate a report and. It also allows you to look up a specific location."),
checkboxGroupInput("check_input_areas", choiceNames = "Input placeholder1", choiceValues = 1, c("none")),
actionButton('add_geoms_to_map', 'Add to map', icon = icon("pencil-alt")),
actionButton('valid_geoms', "Validate", icon = icon("check")),
actionButton('select_view', "Set view on selection", icon = icon("check")),
actionButton('clear_map', "Clear map", icon = icon("trash-alt")),
actionButton("reset_view_valid", "Reset view", icon = icon("arrow-right")),
br(),
br(),
uiOutput("nb_geoms_selected"),
br(),
uiOutput("nb_geoms_selected")
br(),
actionButton('valid_geoms', "Validate", icon = icon("check")),

),
),
),
Expand Down

0 comments on commit 326dd50

Please sign in to comment.