Skip to content

Commit

Permalink
complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Eva Marques committed Feb 22, 2024
1 parent facad6d commit 4964777
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 36 deletions.
36 changes: 0 additions & 36 deletions R/manipulate_spacetime_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -436,42 +436,6 @@ sftime_as_spatraster <- function(x, varname) {
return(terra::rast(layers))
}

#' Convert sftime object to SpatVector
#'
#' @param x a sftime
#' @import sftime
#' @author Eva Marques
#' @export
sftime_as_spatrds <- function(x) {
stopifnot("x is not a sftime" = class(x)[1] == "sftime")
df <- as.data.frame(x)
col <- colnames(df)
variables <- col[!(col %in% c("lon", "lat", "time"))]
rast_list <- list()
for (var in variables) {
newdf <- stats::reshape(
df[, c("lon", "lat", "time", var)],
idvar = c("lon", "lat"),
timevar = "time",
direction = "wide"
)
colnames(newdf) <- gsub(
paste0(var, "."),
"",
colnames(newdf)
)
var_rast <- terra::rast(newdf,
type = "xyz",
crs = attributes(x$geometry)$crs
)
rast_list[[var]] <- var_rast
}
output <- terra::sds(rast_list)
return(output)
}



#' Convert a stdt to sf/sftime/SpatVector
#' @param stdt A stdt object
#' @param class_to character(1). Should be one of
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-manipulate_spacetime_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ test_that("sf_as_mysftime works as expected", {
b <- mysf |>
dplyr::rename("date" = "time")
expect_no_error(check_mysftime(sf_as_mysftime(b, "date")))
expect_error(sf_as_mysftime(b, "time"),
"time column missing or mispelled")
})

test_that("sftime_as_mysftime works as expected", {
Expand All @@ -606,6 +608,8 @@ test_that("sftime_as_mysftime works as expected", {
attributes(mysft)$time_column <- "date"
mysft <- dplyr::rename(mysft, "date" = "time")
expect_no_error(check_mysftime(sf_as_mysftime(mysft, "date")))
expect_error(sf_as_mysftime(mysft, "time"),
"time column missing or mispelled")
})


Expand Down

0 comments on commit 4964777

Please sign in to comment.