Skip to content

Commit

Permalink
#6 add, UT and document sftime_as_spatvector() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Eva Marques committed Feb 16, 2024
1 parent 3b0ce81 commit be3a597
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export(modis_preprocess_vnp46)
export(modis_warp_stars)
export(modis_worker)
export(read_commands)
export(sftime_as_spatvector)
export(test_download_functions)
import(rvest)
import(sf)
Expand Down
15 changes: 15 additions & 0 deletions R/manipulate_spacetime_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ as_mysftime <- function(x, ...) {
return(output)
}

#' Convert sftime object to SpatVector
#'
#' @param x a sftime
#' @import sftime
#' @author Eva Marques
#' @export
sftime_as_spatvector <- function(x) {
stopifnot("x is not a sftime" = class(x)[1] == "sftime")
timecol <- attributes(x)$time_column
tosf <- x[, !(colnames(x) %in% c(timecol))]
tosf[, timecol] <- as.data.table(x)[, get(timecol)]
return(terra::vect(tosf))
}


#' Convert a stdt to sf/sftime/SpatVector
#' @param stdt A stdt object
#' @param class_to character(1). Should be one of
Expand Down
17 changes: 17 additions & 0 deletions man/sftime_as_spatvector.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-manipulate_spacetime_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,22 @@ test_that("as_mysftime works as expected", {
"x class not accepted")
})

test_that("sftime_as_spatvector as expected", {
# open testing data
stdata <- data.table::fread(paste0(testthat::test_path("..", "testdata/", ""),
"spacetime_table.csv"))
mysftime <- sftime::st_as_sftime(stdata,
coords = c("lon", "lat"),
time_column_name = "time",
crs = 4326)
expect_no_error(sftime_as_spatvector(mysftime))
# with a different time column name:
attributes(mysftime)$time_column <- "date"
mysftime <- dplyr::rename(mysftime, "date" = "time")
expect_no_error(sftime_as_spatvector(mysftime))
# doesn't work with other classes:
expect_error(sftime_as_spatvector(stdata))
})

test_that("dt_to_sf works as expected", {
withr::local_package("terra")
Expand Down

0 comments on commit be3a597

Please sign in to comment.