r-spatial / sftime

time extension to sf objects
https://r-spatial.github.io/sftime/
Apache License 2.0
49 stars 5 forks source link

retain tibble class #10

Closed edzer closed 2 years ago

edzer commented 2 years ago
library(sftime)
# Loading required package: sf
# Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE
g <- 
  st_sfc(
    st_point(c(1, 2)), 
    st_point(c(1, 3)), 
    st_point(c(2, 3)), 
    st_point(c(2, 1)), 
    st_point(c(3, 1))
  )
library(tibble)
x4_sf <- st_sf(tibble(a = 1:5, g, time = Sys.time() + 1:5))
st_as_sftime(x4_sf)  |> class()
# [1] "sftime"     "sf"         "data.frame"

Ideally, the sftime class attribute should be added to that of the object, so that the tibble propertie(s) are retained.

henningte commented 2 years ago

This should work now:

library(sftime)
#> Loading required package: sf
#> Warning: package 'sf' was built under R version 4.0.5
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
# Loading required package: sf
# Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE
g <- 
  st_sfc(
    st_point(c(1, 2)), 
    st_point(c(1, 3)), 
    st_point(c(2, 3)), 
    st_point(c(2, 1)), 
    st_point(c(3, 1))
  )
library(tibble)
#> Warning: package 'tibble' was built under R version 4.0.5
x4_sf <- st_sf(tibble(a = 1:5, g, time = Sys.time() + 1:5))
st_as_sftime(x4_sf)  %>% class()
#> [1] "sftime"     "sf"         "tbl_df"     "tbl"        "data.frame"

Created on 2022-02-22 by the reprex package (v2.0.1)