huizezhang-sherry / cubble

A tidy structure for spatio-temporal vector data
https://huizezhang-sherry.github.io/cubble/
Other
55 stars 9 forks source link

Should `add_geometry_column()` add a subclass `sf` to a `cubble_df` object? #16

Closed henningte closed 1 year ago

henningte commented 1 year ago

Hi @huizezhang-sherry and @edzer,

currently, add_geometry_column() does not add the sf subclass to the cubble_df object. Is this desired behavior?

It may cause somewhat unintuitive things like:

library(cubble)
library(sf)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(tibble)
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.2.2
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# create cubble_df from an sf object and a tibble
stations_sf <- 
  cubble::climate_subset |> 
  dplyr::select(-ts) |>
  sf::st_as_sf(coords = c("long", "lat"), crs = 4283, remove = FALSE)

ts <- 
  cubble::climate_aus |>
  cubble::face_temporal() |>
  tibble::as_tibble() |>
  dplyr::filter(id %in% stations_sf$id)

weather1 <- 
  cubble::as_cubble(
    list(spatial = stations_sf, temporal = ts),
    key = id, index = date, coords = c(long, lat)
  )

class(weather1)
#> [1] "cubble_df"  "sf"         "rowwise_df" "tbl_df"     "tbl"       
#> [6] "data.frame"

# create a cubble object without sf and use `add_geometry_column()`
weather2 <- 
  cubble::climate_subset |>
  cubble::add_geometry_column()
#> CRS missing: using OGC:CRS84 (WGS84) as default

class(weather2)
#> [1] "cubble_df"  "rowwise_df" "tbl_df"     "tbl"        "data.frame"

Created on 2023-05-03 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.0 (2022-04-22 ucrt) #> os Windows 10 x64 (build 22621) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate German_Germany.utf8 #> ctype German_Germany.utf8 #> tz Europe/Berlin #> date 2023-05-03 #> pandoc 2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> anytime 0.3.9 2020-08-27 [1] CRAN (R 4.2.3) #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.1) #> class 7.3-20 2022-01-16 [2] CRAN (R 4.2.0) #> classInt 0.4-3 2020-04-07 [1] CRAN (R 4.2.0) #> cli 3.4.1 2022-09-23 [1] CRAN (R 4.2.1) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.2.0) #> cubble * 0.2.1 2023-05-03 [1] Github (huizezhang-sherry/cubble@d6610f4) #> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> dplyr * 1.0.10 2022-09-01 [1] CRAN (R 4.2.2) #> e1071 1.7-9 2021-09-16 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> generics 0.1.2 2022-01-31 [1] CRAN (R 4.2.0) #> geosphere 1.5-18 2022-11-15 [1] CRAN (R 4.2.3) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0) #> KernSmooth 2.23-20 2021-05-03 [2] CRAN (R 4.2.0) #> knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0) #> lattice 0.20-45 2021-09-22 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.2) #> lubridate 1.8.0 2021-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> ncdf4 1.21 2023-01-07 [1] CRAN (R 4.2.2) #> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> proxy 0.4-26 2021-06-07 [1] CRAN (R 4.2.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.2.0) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.2.0) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.2.0) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.2.0) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.1) #> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> sf * 1.0-10 2022-12-07 [1] Github (r-spatial/sf@9534e4a) #> sp 1.5-0 2022-06-12 [1] Github (edzer/sp@cfe9cdb) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0) #> styler 1.7.0 2022-03-13 [1] CRAN (R 4.2.0) #> tibble * 3.1.7 2022-05-03 [1] CRAN (R 4.2.0) #> tidyr 1.2.0 2022-02-01 [1] CRAN (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.2) #> tsibble 1.1.3 2022-10-09 [1] CRAN (R 4.2.3) #> units 0.8-1 2022-12-10 [1] CRAN (R 4.2.3) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.5.1 2022-11-16 [1] CRAN (R 4.2.2) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.30 2022-03-02 [1] CRAN (R 4.2.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0) #> #> [1] C:/Users/henni/AppData/Local/R/win-library/4.2 #> [2] C:/Program Files/R/R-4.2.0/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
edzer commented 1 year ago

Is this desired behavior?

No, this is what I meant in my comment here: https://github.com/huizezhang-sherry/cubble/pull/15

huizezhang-sherry commented 1 year ago

I have a first version that corrects the class/ attributes that gives

[09:52:19]> a <- climate_aus %>% add_geometry_column()
CRS missing: using OGC:CRS84 (WGS84) as default
[09:52:21]> a
# cubble:   id [639]: nested form [sf]
# bbox:     [113.5319, -43.6578, 153.6358, -10.0511]
# temporal: date [date], prcp [dbl], tmax [dbl], tmin [dbl]
   id            lat  long  elev name             wmo_id ts                  geometry
   <chr>       <dbl> <dbl> <dbl> <chr>             <dbl> <list>           <POINT [°]>
 1 ASN00001006 -15.5  128.   3.8 wyndham aero      95214 <tibble>   (128.1503 -15.51)
 2 ASN00001007 -13.8  126.   6   troughton island  94102 <tibble> (126.1485 -13.7542)
 3 ASN00001018 -16.4  126. 546   mount elizabeth   94211 <tibble> (126.1025 -16.4181)
 4 ASN00001019 -14.3  127.  23   kalumburu         94100 <tibble> (126.6453 -14.2964)
 5 ASN00001020 -14.1  126.  51   truscott          95101 <tibble>   (126.3867 -14.09)
 6 ASN00001025 -15.4  126. 385   doongan           94215 <tibble> (126.3114 -15.3797)
 7 ASN00002012 -18.2  128. 422   halls creek air…  94212 <tibble> (127.6636 -18.2292)
 8 ASN00002032 -17.0  128. 203   warmun            94213 <tibble> (128.2175 -17.0156)
 9 ASN00002056 -15.8  129.  44   kununurra aero    94216 <tibble>   (128.71 -15.7814)
10 ASN00002064 -16.6  128. 164   argyle aerodrome  94217 <tibble> (128.4517 -16.6381)
# … with 629 more rows
# ℹ Use `print(n = ...)` to see more rows
[09:52:22]> class(a)
[1] "cubble_df"  "sf"         "rowwise_df" "tbl_df"     "tbl"        "data.frame"

I can check it in GitHub if you need it now.

henningte commented 1 year ago

I want to use it for the conversion to sftime, but there's no need to hurry.

huizezhang-sherry commented 1 year ago

It has already been on GitHub

henningte commented 1 year ago

Thanks, works for me now!