Skip to content

Commit

Permalink
incorporate basic memoise functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Mar 23, 2024
1 parent cc75cc2 commit 41ed519
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ URL: https://doi-usgs.github.io/nhdplusTools/ https://github.com/doi-usgs/nhdplu
BugReports: https://github.com/doi-usgs/nhdplusTools/issues/
Depends:
R (>= 4.0)
Imports: hydroloom, dataRetrieval, dplyr, sf, units, magrittr, jsonlite, httr, xml2, R.utils, utils, tidyr, methods, maptiles, mapsf, fst, arrow, tools, zip, pbapply
Imports: hydroloom, dataRetrieval, dplyr, sf, units, magrittr, jsonlite, httr, xml2, R.utils, utils, tidyr, methods, maptiles, mapsf, fst, arrow, tools, zip, pbapply, memoise
Suggests: testthat, knitr, rmarkdown, ggmap, ggplot2, lwgeom, gifski, leaflet, httptest, future, future.apply
License: CC0
Encoding: UTF-8
Expand Down
18 changes: 18 additions & 0 deletions R/A_nhdplusTools.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ nhdplus_path <- function(path = NULL, warn = FALSE) {
}
}

nhdplusTools_memoise_cache <- function() {
memo_cache <- Sys.getenv("NHDPLUSTOOLS_MEMOISE_CACHE")
if(memo_cache == "memory") {
memoise::cache_memory()
} else {
memoise::cache_filesystem(nhdplusTools_data_dir())
}
}

nhdplusTools_memoise_timeout <- function() {
timeout_env <- Sys.getenv("NHDPLUSTOOLS_MEMOISE_TIMEOUT")
if(timeout_env != "") {
as.numeric(timeout_env)
} else {
# default to one day
oneday_seconds <- 60 * 60 * 24
}
}

#' @title Align NHD Dataset Names
#' @description this function takes any NHDPlus dataset and aligns the attribute names with those used in nhdplusTools.
Expand Down
4 changes: 2 additions & 2 deletions R/arcrest_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ get_3dhp_service_info <- memoise::memoise(function() {
#' @importFrom httr RETRY content
#' @importFrom dplyr filter
#' @importFrom methods as
query_usgs_arcrest <- function(AOI = NULL, ids = NULL,
query_usgs_arcrest <- memoise::memoise(function(AOI = NULL, ids = NULL,
type = NULL, where = NULL,
t_srs = NULL,
buffer = 0.5){
Expand Down Expand Up @@ -216,4 +216,4 @@ query_usgs_arcrest <- function(AOI = NULL, ids = NULL,
all_out[[l]] <- out
}
sf::st_sf(data.table::rbindlist(all_out))
}
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())
8 changes: 4 additions & 4 deletions R/geoserver_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' @importFrom methods as
#' @importFrom xml2 read_xml

query_usgs_geoserver <- function(AOI = NULL, ids = NULL,
query_usgs_geoserver <- memoise::memoise(function(AOI = NULL, ids = NULL,
type = NULL, filter = NULL,
t_srs = NULL,
buffer = 0.5) {
Expand Down Expand Up @@ -177,7 +177,7 @@ query_usgs_geoserver <- function(AOI = NULL, ids = NULL,
NULL
}

}
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())

unify_types <- function(out) {
all_class <- bind_rows(lapply(out, function(x) {
Expand Down Expand Up @@ -359,15 +359,15 @@ tc <- function(x) {
#' @importFrom httr RETRY GET
#' @importFrom jsonlite fromJSON

extact_comid_nwis <- function(nwis){
extact_comid_nwis <- memoise::memoise(function(nwis){
# We could export this from dataRetrieval dataRetrieval:::pkg.env$nldi_base
#but currently its not...
baseURL <- paste0(get_nldi_url(), "/linked-data/")
url <- paste0(baseURL, "nwissite/USGS-", nwis)
c <- rawToChar(httr::RETRY("GET", url)$content)
f.comid <- jsonlite::fromJSON(c, simplifyVector = TRUE)
f.comid$features$properties$comid
}
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())

#' @importFrom sf st_make_valid st_as_sfc st_bbox st_buffer st_transform
check_query_params <- function(AOI, ids, type, where, source, t_srs, buffer) {
Expand Down
4 changes: 2 additions & 2 deletions R/get_nldi.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ get_nldi_index <- function(location) {
#' @importFrom httr GET
#' @importFrom jsonlite fromJSON
#' @noRd
query_nldi <- function(query, base_path = "/linked-data", parse_json = TRUE) {
query_nldi <- memoise::memoise(function(query, base_path = "/linked-data", parse_json = TRUE) {
nldi_base_url <- paste0(get_nldi_url(), base_path)

url <- paste(nldi_base_url, query,
Expand Down Expand Up @@ -303,7 +303,7 @@ query_nldi <- function(query, base_path = "/linked-data", parse_json = TRUE) {
warning("Something went wrong accessing the NLDI.\n", e)
NULL
})
}
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())

#' @noRd
check_nldi_feature <- function(nldi_feature, convert = TRUE) {
Expand Down

0 comments on commit 41ed519

Please sign in to comment.