Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions R/utils-crs.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ validate_crs <- function(
return(list(spatialReference = c()))
}
if (
!((inherits(crs, "character") ||
!(inherits(crs, "character") ||
inherits(crs, "crs") ||
inherits(crs, "numeric")))
rlang::is_integerish(crs))
) {
cli::cli_abort(c(
"Invalid CRS supplied: {.arg {arg}}",
"i" = "must be compatible with {.fn sf::st_crs}",
cli::cli_abort(
c(
"Invalid CRS supplied: {.arg {arg}}",
"i" = "must be compatible with {.fn sf::st_crs}"
),
arg = arg,
call = call
))
)
}

crs <- sf::st_crs(crs)
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/_snaps/utils-crs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# validate_crs() errors on invalid input

Code
validate_crs(4326.5)
Condition
Error:
! Invalid CRS supplied: `4326.5`
i must be compatible with `sf::st_crs()`
Code
validate_crs(TRUE)
Condition
Error:
! Invalid CRS supplied: `TRUE`
i must be compatible with `sf::st_crs()`
Code
validate_crs(list(4326))
Condition
Error:
! Invalid CRS supplied: `list(4326)`
i must be compatible with `sf::st_crs()`

16 changes: 16 additions & 0 deletions tests/testthat/test-utils-crs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that("validate_crs() accepts crs, character, integer, and whole doubles", {
expected <- list(spatialReference = list(wkid = 4326L))

expect_identical(validate_crs(sf::st_crs(4326)), expected)
expect_identical(validate_crs("EPSG:4326"), expected)
expect_identical(validate_crs(4326L), expected)
expect_identical(validate_crs(4326), expected)
})

test_that("validate_crs() errors on invalid input", {
expect_snapshot(error = TRUE, {
validate_crs(4326.5)
validate_crs(TRUE)
validate_crs(list(4326))
})
})
Loading