Skip to content

Commit

Permalink
Add methods to convert sftrack and sftraj objects to sftime obj…
Browse files Browse the repository at this point in the history
…ects (#9, #1). Update documentation and news.
  • Loading branch information
henningte committed Apr 3, 2022
1 parent f753297 commit be19630
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 103 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Suggests:
rlang,
vctrs,
spatstat.geom,
spatstat.linnet
spatstat.linnet,
sftrack
Authors@R:
c(person(given = "Henning",
family = "Teickner",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ S3method(st_as_sftime,ppplist)
S3method(st_as_sftime,psp)
S3method(st_as_sftime,sf)
S3method(st_as_sftime,sftime)
S3method(st_as_sftime,sftrack)
S3method(st_as_sftime,sftraj)
S3method(st_as_sftime,stars)
S3method(st_cast,sftime)
S3method(st_crop,sftime)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# sftime (development version)

* Add methods to convert `sftime` objects from:
+ Objects from the `spatstat` package classes (`ppp`, `ppplist`, `psp`, `lpp`)
+ `sftrack` and `sftraj` objects from the `sftrack` package.

* Bug fix in `st_time<-.sftime`:
+ Still contained references to the old `tc`class.
+ Did not allow to give the active time column a character vector as value.

# version 0.2-0

* initial CRAN submission
72 changes: 70 additions & 2 deletions R/sftime.R
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ st_as_sftime.ppplist <- function(x, ..., time_column_name) {

#' @name st_as_sftime
#' @examples
#' # convert a psp object to an sftime object (modified from the sf package)
#' # convert a psp object to an sftime object (modified from the spatstat.geom
#' # package)
#' if (require(spatstat.geom)) {
#' # modified from spatstat.geom:
#' x_psp <-
Expand All @@ -656,7 +657,8 @@ st_as_sftime.psp <- function(x, ..., time_column_name) {

#' @name st_as_sftime
#' @examples
#' # convert an lpp object to an sftime object (modified from the sf package)
#' # convert an lpp object to an sftime object (modified from the
#' # spatstat.linnet package)
#' if (require(spatstat.geom) && require(spatstat.linnet)) {
#' # modified from spatstat.linnet:
#'
Expand Down Expand Up @@ -686,6 +688,72 @@ st_as_sftime.lpp <- function(x, ..., time_column_name) {
st_sftime(sf::st_as_sf(x), time_column_name = time_column_name)
}

#' @name st_as_sftime
#' @examples
#' # convert an sftrack object to an sftime object (modified from sftrack)
#' if (require(sftrack)) {
#'
#' # get an sftrack object
#' data("raccoon")
#'
#' raccoon$timestamp <- as.POSIXct(raccoon$timestamp, "EST")
#'
#' burstz <-
#' list(id = raccoon$animal_id, month = as.POSIXlt(raccoon$timestamp)$mon)
#'
#' x_sftrack <-
#' as_sftrack(raccoon,
#' group = burstz, time = "timestamp",
#' error = NA, coords = c("longitude", "latitude")
#' )
#'
#' # convert to sftime
#' st_as_sftime(x_sftrack)
#' }
#'
#' @export
st_as_sftime.sftrack <- function(x, ...) {
time_column_name <- attr(x, which = "time_column")
attr(x, which = "group_col") <- NULL
attr(x, which = "error_col") <- NULL
class(x) <- setdiff(class(x), "sftrack")
st_sftime(x, time_column_name = time_column_name)
}

#' @name st_as_sftime
#' @examples
#' # convert an sftraj object to an sftime object (modified from sftrack)
#' if (require(sftrack)) {
#'
#' # get an sftrack object
#' data("raccoon")
#'
#' raccoon$timestamp <- as.POSIXct(raccoon$timestamp, "EST")
#'
#' burstz <-
#' list(id = raccoon$animal_id, month = as.POSIXlt(raccoon$timestamp)$mon)
#'
#' x_sftraj <-
#' as_sftraj(raccoon,
#' time = "timestamp",
#' error = NA, coords = c("longitude", "latitude"),
#' group = burstz
#' )
#'
#' # convert to sftime
#' st_as_sftime(x_sftraj)
#' }
#'
#' @export
st_as_sftime.sftraj <- function(x, ...) {
time_column_name <- attr(x, which = "time_column")
attr(x, which = "group_col") <- NULL
attr(x, which = "error_col") <- NULL
class(x) <- setdiff(class(x), "sftraj")
st_sftime(x, time_column_name = time_column_name)
}


#### transform attributes ####

#' Transform method for \code{sftime} objects
Expand Down
55 changes: 53 additions & 2 deletions man/st_as_sftime.Rd

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

135 changes: 37 additions & 98 deletions man/tidyverse.Rd

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

0 comments on commit be19630

Please sign in to comment.