Skip to content

Commit

Permalink
Merge pull request #14 from huizezhang-sherry/main
Browse files Browse the repository at this point in the history
update on st_as_sftime.cubble_df
  • Loading branch information
henningte authored Jun 28, 2023
2 parents 35ff989 + 8fe3b46 commit 214937e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions R/sftime.R
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ st_as_sftime.sftraj <- function(x, ...) {
#' @inheritParams cubble::add_geometry_column
#' @examples
#' # convert a cubble_df object from package cubble to an sftime object
#' if (requireNamespace("cubble", quietly = TRUE, versionCheck = "0.2.2")) {
#' if (requireNamespace("cubble", quietly = TRUE, versionCheck = "0.3.0")) {
#'
#' # get a cubble_df object
#' data("climate_aus", package = "cubble")
Expand All @@ -772,28 +772,28 @@ st_as_sftime.sftraj <- function(x, ...) {
#' @export
st_as_sftime.cubble_df <- function(x, ..., sfc = NULL, crs, silent = FALSE) {

if (! requireNamespace("cubble", quietly = TRUE, versionCheck = "0.2.1"))
stop("You need the `cubble` package (>= 0.2.1) to use this function. Install that first.")
if (! requireNamespace("cubble", quietly = TRUE, versionCheck = "0.3.0"))
stop("You need the `cubble` package (>= 0.3.0) to use this function. Install that first.")

# make sure the cubble_df object has the right format
if(! cubble::is_nested(x)) {
if(! cubble::is_cubble_spatial(x)) {
x <- cubble::face_spatial(data = x)
}
if(! inherits(x, "sf")) {
x <- cubble::add_geometry_column(x, sfc = sfc, crs = crs, silent = silent)
x <- cubble::make_spatial_sf(x, sfc = sfc, crs = crs, silent = silent)
}

# extract information needed to create the sftime object
time_column_name <- attr(x, which = "index")
id_column_name <- names(attr(x, "groups"))[[1]]
id_column_name <- head(names(attr(x, "key")), -1)
column_names <- c(setdiff(colnames(x), "ts"), colnames(x$ts[[1]]))
x_ts <- as.data.frame(cubble::face_temporal(x, col = "ts"))

# convert to sf (drop all cubble_df attributes)
attr(x, which = "form") <- NULL
attr(x, which = "coords") <- NULL
attr(x, which = "index") <- NULL
class(x) <- setdiff(class(x), "cubble_df")
class(x) <- setdiff(class(x), c("cubble_df", "spatial_cubble_df"))

# merge spatial and temporal faces
x <- merge(x_ts, x[, !colnames(x) == "ts"], by = id_column_name)
Expand Down

0 comments on commit 214937e

Please sign in to comment.