Skip to content

Commit

Permalink
updating stream network module
Browse files Browse the repository at this point in the history
  • Loading branch information
Erickson authored and Erickson committed Sep 8, 2023
1 parent e2b3ab8 commit f05119a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BugReports: https://github.com/joshualerickson/gwavr/issues/
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
Imports:
dplyr,
httr,
Expand Down
58 changes: 39 additions & 19 deletions R/modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -919,11 +919,12 @@ streamnetworkModUI <- function(id, ...){
#' @param output Shiny server function output
#' @param session Shiny server function session
#' @param values A reactive Values list to pass
#' @param dem A raster or terra object dem.
#' @param dem A raster or terra object dem. (optional)
#' @param threshold A threshold for stream initiation. 1000 (default).
#' @return server function for Shiny module
#' @importFrom promises finally "%...>%"
#' @export
streamnetworkMod <- function(input, output, session, values, dem){
streamnetworkMod <- function(input, output, session, values, dem, threshold = 1000){

ns <- session$ns

Expand Down Expand Up @@ -954,7 +955,7 @@ streamnetworkMod <- function(input, output, session, values, dem){
width = '100%')),
className = "fieldset { border: 0;}") %>%
leaflet::addControl(html = tags$div(tags$style(css),shiny::numericInput(
ns('threshold'), 'Cell Threshold',value = 1000,min = 1, max = 15000,
ns('threshold'), 'Cell Threshold',value = threshold,min = 1, max = 15000,
width = '100%')),
className = "fieldset { border: 0;}") %>%
leaflet::addControl(html = tags$div(tags$style(css),shiny::actionButton(
Expand All @@ -974,8 +975,8 @@ streamnetworkMod <- function(input, output, session, values, dem){
overlayGroups = c("Hydrography"))
})

observe({
if(!is.null(dem) & vals$count == 0) {
observe({
req(!is.null(dem) & vals$count == 0)
p <- shiny::Progress$new()
p$set(message = "Uploading your DEM...",
detail = "This may take a little bit...",
Expand All @@ -984,7 +985,7 @@ streamnetworkMod <- function(input, output, session, values, dem){
promises::future_promise({

ws_poly <- get_whitebox_streams(ele = dem,
threshold = 1000,
threshold = threshold,
prj = sf::st_crs(dem))

}) %...>% {
Expand All @@ -1006,16 +1007,23 @@ streamnetworkMod <- function(input, output, session, values, dem){

leaf_prox %>%
leaflet::addPolygons(data = values$output_ws, fillOpacity = 0,
color = 'black', weight = 3, group = paste0('poly', vals$count)) %>%
leaflet::addPolylines(data = values$streams, color = 'blue', group = paste0('raster', vals$count)) %>%
leaflet::fitBounds(bb[['xmin']], bb[['ymin']], bb[['xmax']], bb[['ymax']])
color = 'black', weight = 3, group = paste0('catchment', vals$count)) %>%
leaflet::addPolylines(data = values$streams, color = 'blue', group = paste0('stream', vals$count))%>%
leaflet::addLayersControl(baseGroups = c("Esri.WorldStreetMap","OpenTopoMap","Esri.WorldImagery", "CartoDB.Positron",
"OpenStreetMap", "CartoDB.DarkMatter"),
overlayGroups = c("Hydrography",
paste0('catchment', vals$count),
paste0('stream', vals$count)))
# %>%
# leaflet::fitBounds(bb[['xmin']], bb[['ymin']], bb[['xmax']], bb[['ymax']])



} %>%
finally(~p$close())
}
})


})

# create a counter

Expand All @@ -1034,20 +1042,27 @@ streamnetworkMod <- function(input, output, session, values, dem){
values$data_sf <- sf::st_sf(sf::st_sfc(sf::st_polygon(list(coords))), crs = sf::st_crs(4326)) %>%
sf::st_as_sf()
}
p <- shiny::Progress$new()
p$set(message = "Downloading data...",
detail = "This may take a little bit...",
value = 1/2)

if(!is.null(input$leaf_map_draw_new_feature)){
p$set(message = "Downloading data...",
detail = "This may take a little bit...",
value = 1/2)
} else {
p <- shiny::Progress$new()
p$set(message = "Updating DEM...",
detail = "This may take a little bit...",
value = 1/2)
}

promises::future_promise({

if(!is.null(input$leaf_map_draw_new_feature)){

ws_poly <- get_whitebox_streams(values$data_sf,
input$map_res,
threshold = input$threshold)
} else {


ws_poly <- get_whitebox_streams(ele = dem,
threshold = input$threshold,
prj = sf::st_crs(dem))
Expand All @@ -1065,7 +1080,7 @@ streamnetworkMod <- function(input, output, session, values, dem){

if(vals$count > 0){
leaf_prox <- leaflet::leafletProxy('leaf_map', session) %>%
leaflet::clearGroup(group = c(paste0('raster', vals$count),paste0('poly', vals$count)))
leaflet::clearGroup(group = c(paste0('stream', vals$count),paste0('catchment', vals$count)))
} else {
leaf_prox <- leaflet::leafletProxy('leaf_map', session)
}
Expand All @@ -1074,8 +1089,13 @@ streamnetworkMod <- function(input, output, session, values, dem){

leaf_prox %>%
leaflet::addPolygons(data = values$output_ws, fillOpacity = 0,
color = 'black', weight = 3, group = paste0('poly', vals$count)) %>%
leaflet::addPolylines(data = values$streams, color = 'blue', group = paste0('raster', vals$count))
color = 'black', weight = 3, group = paste0('catchment', vals$count)) %>%
leaflet::addPolylines(data = values$streams, color = 'blue', group = paste0('stream', vals$count))%>%
leaflet::addLayersControl(baseGroups = c("Esri.WorldStreetMap","OpenTopoMap","Esri.WorldImagery", "CartoDB.Positron",
"OpenStreetMap", "CartoDB.DarkMatter"),
overlayGroups = c("Hydrography",
paste0('catchment', vals$count),
paste0('stream', vals$count)))


} %>%
Expand Down
7 changes: 5 additions & 2 deletions R/streamnetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#' @param title \code{string} to customize the title of the UI window. The default
#' is "Delineate Basin".
#' @param ... other arguments to \code{leafletOutput()} in module.
#' @param dem A terra or raster DEM object if you want to add.
#' @param dem A raster or terra object dem. (optional)
#' @param threshold A threshold for stream initiation. 1000 (default).
#' @note If you add your own DEM then you don't need to draw a bounding box.
#' @details This function uses the package \link{elevatr} to download the DEM (unless you provide your own).
#' Once the user has drawn the bounding box or inputed DEM and selected appropriate zoom (resolution) and threshold then
Expand Down Expand Up @@ -48,6 +49,7 @@ get_stream_network_interactively <- function(ns = 'streamnetwork-ui',
viewer = shiny::paneViewer(),
title = 'Streamnetwork',
dem = NULL,
threshold = 1000,
...) {

#spherical geometry switched off
Expand Down Expand Up @@ -101,7 +103,8 @@ $(document).on('shiny:disconnected', function() {
streamnetworkMod,
ns,
values = values,
dem = dem
dem = dem,
threshold = threshold
))

observe({crud_mod()})
Expand Down
5 changes: 4 additions & 1 deletion man/get_stream_network_interactively.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/streamnetworkMod.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f05119a

Please sign in to comment.