Skip to content

Commit

Permalink
Better check inputs to query function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemahoney218 committed Aug 20, 2024
1 parent 280d513 commit d0acb46
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
27 changes: 24 additions & 3 deletions R/query_and_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#' available for your AOI, or to perform cloud filtering prior to downloading
#' assets.
#'
#' @param bbox An sfc object representing the spatial bounding box of your area
#' of interest. This must be in EPSG:4326 coordinates (and, if this function is
#' called from within `get_stac_data()`, it will be)
#' @param bbox A `bbox` or `sfc` object, from the sf package, representing the
#' spatial bounding box of your area of interest. This must be in EPSG:4326
#' coordinates (and, if this function is called from within `get_stac_data()`,
#' it will be) or else it will be automatically reprojected.
#' @inheritParams get_stac_data
#' @param start_date,end_date Character strings of length 1 representing the
#' boundaries of your temporal range of interest, in RFC-3339 format. Set either
Expand Down Expand Up @@ -52,6 +53,26 @@ rsi_query_api <- function(bbox,
datetime <- NULL
}

if (!(inherits(bbox, "bbox") | inherits(bbox, "sfc"))) {
rlang::abort(
"`bbox` must be either an sfc or a bbox object from the sf package.",
class = "rsi_bbox_wrong_class"
)
}

if (!(sf::st_crs(bbox) == sf::st_crs("EPSG:4326"))) {
rlang::warn(
"Reprojecting `bbox` to EPSG:4326.",
class = "rsi_reprojecting_bbox"
)
if (inherits(bbox, "bbox")) {
bbox <- sf::st_as_sfc(bbox)
}
bbox <- sf::st_transform(bbox, 4326)
}

if (inherits(bbox, "sfc")) bbox <- sf::st_bbox(bbox)

items <- rstac::stac_search(
rstac::stac(stac_source),
collections = collection,
Expand Down
7 changes: 4 additions & 3 deletions man/rsi_query_api.Rd

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

30 changes: 30 additions & 0 deletions tests/testthat/test-query_and_sign.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
test_that("non-4326 CRS warns", {
nc <- sf::read_sf(
system.file("shape/nc.shp", package = "sf")
)
expect_warning(
rsi_query_api(
sf::st_as_sfc(sf::st_bbox(nc)),
stac_source = "https://planetarycomputer.microsoft.com/api/stac/v1/",
collection = "landsat-c2-l2",
start_date = "2023-08-01",
end_date = "2023-09-01",
limit = 10
),
class = "rsi_reprojecting_bbox"
)
})

test_that("unaccepted bbox objects error well", {
expect_error(
rsi_query_api(
"not a bbox",
stac_source = "https://planetarycomputer.microsoft.com/api/stac/v1/",
collection = "landsat-c2-l2",
start_date = "2023-08-01",
end_date = "2023-09-01",
limit = 10
),
class = "rsi_bbox_wrong_class"
)
})

0 comments on commit d0acb46

Please sign in to comment.