huizezhang-sherry / cubble

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

conversion to/from stars #5

Closed edzer closed 7 months ago

edzer commented 2 years ago

Hi, nice project! It might be interesting to have converters between cubble and stars objects, see https://github.com/r-spatial/stars as that also can hold vector data cubes.

huizezhang-sherry commented 2 years ago

Hi @edzer,

Thank for being interested in cubble! Now cubble has a as_cubble() method for the stars object:

library(cubble)
library(units)
#> udunits database from /Library/Frameworks/R.framework/Versions/4.1/Resources/library/units/share/udunits/udunits2.xml

# create a stars object
m <- array(1:60, dim = c(x= 5, y = 4, t = 3))
m_dim <- stars::st_dimensions(
  x =  seq(146, 162, 4), 
  y = seq(-44, -41, 1), 
  t = set_units(1:3, "days since 2015-01-01")
  )
st <- stars::st_as_stars(list(m = m, m2 = m), dimensions = m_dim)
st
#> stars object with 3 dimensions and 2 attributes
#> attribute(s):
#>     Min. 1st Qu. Median Mean 3rd Qu. Max.
#> m      1   15.75   30.5 30.5   45.25   60
#> m2     1   15.75   30.5 30.5   45.25   60
#> dimension(s):
#>   from to                      offset                       delta  refsys point
#> x    1  5                         146                           4      NA FALSE
#> y    1  4                         -44                           1      NA FALSE
#> t    1  3 1 [(days since 2015-01-01)] 1 [(days since 2015-01-01)] udunits FALSE
#>   values x/y
#> x   NULL [x]
#> y   NULL [y]
#> t   NULL

# convert the stars object into a cubble
as_cubble(st)
#> # cubble:   id [20]: nested form
#> # bbox:     [146, -44, 162, -41]
#> # temporal: t [dttm], m [int], m2 [int]
#>        x     y    id ts              
#>    <dbl> <dbl> <int> <list>          
#>  1   146   -44     1 <tibble [3 × 3]>
#>  2   146   -43     2 <tibble [3 × 3]>
#>  3   146   -42     3 <tibble [3 × 3]>
#>  4   146   -41     4 <tibble [3 × 3]>
#>  5   150   -44     5 <tibble [3 × 3]>
#>  6   150   -43     6 <tibble [3 × 3]>
#>  7   150   -42     7 <tibble [3 × 3]>
#>  8   150   -41     8 <tibble [3 × 3]>
#>  9   154   -44     9 <tibble [3 × 3]>
#> 10   154   -43    10 <tibble [3 × 3]>
#> 11   154   -42    11 <tibble [3 × 3]>
#> 12   154   -41    12 <tibble [3 × 3]>
#> 13   158   -44    13 <tibble [3 × 3]>
#> 14   158   -43    14 <tibble [3 × 3]>
#> 15   158   -42    15 <tibble [3 × 3]>
#> 16   158   -41    16 <tibble [3 × 3]>
#> 17   162   -44    17 <tibble [3 × 3]>
#> 18   162   -43    18 <tibble [3 × 3]>
#> 19   162   -42    19 <tibble [3 × 3]>
#> 20   162   -41    20 <tibble [3 × 3]>

Created on 2022-07-18 by the reprex package (v2.0.1)

The converter can handle some simple dimension objects and may emit a message when the dimension object is too complex to handle. I would love to know if you think anything else is needed for this converter.

p.s. I'm having a wired GitHub Action error on Windows and macOS related to sf. I'm unsure if that has anything to do with the latest update.

edzer commented 2 years ago

Great, will check it out!

I'm having a wired GitHub Action error on Windows and macOS related to sf. I'm unsure if that has anything to do with the latest update.

most likely because the CRAN version of sf is newer than the windows binary (which lags behind a week usually)