Skip to content

Commit

Permalink
relax test of extent to relation="overlaps" rather than "contains"
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Oct 21, 2024
1 parent 4a3985a commit 660dc97
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion R/fetchSOLUS.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ fetchSOLUS <- function(x = NULL,
xe <- terra::ext(terra::project(terra::as.polygons(x, ext = TRUE), r))

# handle requests out-of-bounds
if (!terra::relate(terra::ext(r), xe, relation = "contains")[1]) {
if (!(terra::relate(terra::ext(r), xe, relation = "contains")[1] ||
terra::relate(terra::ext(r), xe, relation = "overlaps")[1])) {
stop("Extent of `x` is outside the boundaries of the source data extent.", call. = FALSE)
}

Expand Down
23 changes: 19 additions & 4 deletions tests/testthat/test-fetchSOLUS.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test_that("fetchSOLUS works", {
geomIntersection = TRUE
)

# SoilProfileCollection output, using linear interpolation for 1cm slices
# raster and SoilProfileCollection output, using linear interpolation for 1cm slices
# site-level variables (e.g. resdept) added to site data.frame of SPC
res <- fetchSOLUS(
ssurgo.geom,
Expand Down Expand Up @@ -61,17 +61,32 @@ test_that("virtual and out-of-bounds requests", {
depth_slices = "0")

expect_true(inherits(tmp, "SpatRaster"))
expect_true(grepl("claytotal_0_cm_p", terra::sources(tmp)))

# virtual raster covers large area
pe <- terra::as.polygons(tmp, ext = TRUE)
expect_true(terra::expanse(pe, unit = "km") > 1e7)

# extract corner point and buffer 500m
te <- terra::buffer(terra::as.points(terra::simplifyGeom(pe))[1], 500)
# extract corner point and buffer 500km
te1 <- terra::buffer(terra::as.points(terra::simplifyGeom(pe))[1], 5e5)

# only bottom-right corner returns data (expected 5000x5000 cells at 100m resolution)
expect_equal(terra::ncell(
fetchSOLUS(
x = te1,
variables = "claytotal",
output_type = "prediction",
depth_slices = "0"
)
), 2.5e7)

# choose an area that does not overlap the source raster at all
te2 <- terra::as.polygons(terra::ext(-1, 1, -1, 1))
terra::crs(te2) <- "OGC:CRS84"

expect_error(
fetchSOLUS(
x = te,
x = te2,
variables = "claytotal",
output_type = "prediction",
depth_slices = "0"
Expand Down

0 comments on commit 660dc97

Please sign in to comment.