Skip to content

Commit

Permalink
Move off call_match() to play nicer with mapply()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemahoney218 committed Jan 2, 2024
1 parent 25f2910 commit 7faa3dc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Description: Tools for downloading spatial data from 'spatiotemporal' asset
catalogs ('STAC') and computing standard spectral indices from raster data.
License: Apache License (>= 2)
Depends:
R (>= 4.0)
R (>= 4.1)
Imports:
future.apply,
glue,
Expand Down
51 changes: 46 additions & 5 deletions R/get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#' [landsat_band_mapping].
#' @param sign_function A function that takes the output from `query_function`
#' and signs the item URLs, if necessary.
#' @param ... Passed to `item_filter_functiion`.
#' @param ... Passed to `item_filter_function`.
#' @param rescale_bands Logical of length 1: If the STAC collection implements
#' the `raster` STAC extension, and that extension includes `scale` and `offset`
#' values, should this function attempt to automatically rescale the downloaded
Expand Down Expand Up @@ -203,6 +203,19 @@ get_stac_data <- function(aoi,
)
}

if (...length() && any(...names() == "")) {
# mostly because of how we put dots in our argument list in sub-functions;
# if any of `...` are not named, then they're getting written one after
# another to "" in the list, which means some will be missing
#
# so, it's an issue with the other get_*_data functions, but we're enforcing
# it everywhere for purposes of consistency
rlang::abort(
"All arguments to `...` must be named.",
class = "rsi_unnamed_dots"
)
}

if (sf::st_is_longlat(aoi) && !(is.null(pixel_x_size) || is.null(pixel_y_size)) && all(c(pixel_x_size, pixel_y_size) %in% c(10, 30))) {
rlang::warn(c(
"The default pixel size arguments are intended for use with projected AOIs, but `aoi` appears to be in geographic coordinates.",
Expand Down Expand Up @@ -602,7 +615,14 @@ get_sentinel1_imagery <- function(aoi,
GDAL_HTTP_MERGE_CONSECUTIVE_RANGES = "YES",
GDAL_NUM_THREADS = "ALL_CPUS"
)) {
args <- as.list(rlang::call_match(defaults = TRUE))[-1]
args <- mget(names(formals()))
args$`...` <- NULL
if (...length()) {
dot_names <- ...names()
for (i in seq_len(...length())) {
args[[dot_names[[i]]]] <- ...elt(i)
}
}
do.call(get_stac_data, args)
}

Expand Down Expand Up @@ -645,7 +665,14 @@ get_sentinel2_imagery <- function(aoi,
GDAL_HTTP_MERGE_CONSECUTIVE_RANGES = "YES",
GDAL_NUM_THREADS = "ALL_CPUS"
)) {
args <- as.list(rlang::call_match(defaults = TRUE))[-1]
args <- mget(names(formals()))
args$`...` <- NULL
if (...length()) {
dot_names <- ...names()
for (i in seq_len(...length())) {
args[[dot_names[[i]]]] <- ...elt(i)
}
}
do.call(get_stac_data, args)
}

Expand Down Expand Up @@ -689,7 +716,14 @@ get_landsat_imagery <- function(aoi,
GDAL_HTTP_MERGE_CONSECUTIVE_RANGES = "YES",
GDAL_NUM_THREADS = "ALL_CPUS"
)) {
args <- as.list(rlang::call_match(defaults = TRUE))[-1]
args <- mget(names(formals()))
args$`...` <- NULL
if (...length()) {
dot_names <- ...names()
for (i in seq_len(...length())) {
args[[dot_names[[i]]]] <- ...elt(i)
}
}
do.call(get_stac_data, args)
}

Expand Down Expand Up @@ -732,7 +766,14 @@ get_dem <- function(aoi,
GDAL_HTTP_MERGE_CONSECUTIVE_RANGES = "YES",
GDAL_NUM_THREADS = "ALL_CPUS"
)) {
args <- as.list(rlang::call_match(defaults = TRUE))[-1]
args <- mget(names(formals()))
args$`...` <- NULL
if (...length()) {
dot_names <- ...names()
for (i in seq_len(...length())) {
args[[dot_names[[i]]]] <- ...elt(i)
}
}
do.call(get_stac_data, args)
}

Expand Down
2 changes: 1 addition & 1 deletion man/get_stac_data.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/test-get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ test_that("simple merge method works", {
})

test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `mask_function`", {
skip_on_cran()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000)
Expand All @@ -201,3 +202,20 @@ test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `m
)
)
})

test_that("get_*_data works with mapply() (#17)", {
skip_on_cran()
san_antonio = sf::st_point(c(-98.491142, 29.424349))
san_antonio = sf::st_sfc(san_antonio, crs = "EPSG:4326")
san_antonio = sf::st_buffer(st_transform(san_antonio, "EPSG:3081"), 1000)

expect_no_error(
mapply(
get_landsat_imagery,
start_date = c("2023-09-01", "2023-10-01"),
end_date = c("2023-09-30", "2023-10-31"),
output_filename = replicate(2, tempfile(fileext = ".tif")),
MoreArgs = c(aoi = list(san_antonio))
)
)
})

0 comments on commit 7faa3dc

Please sign in to comment.