Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

casting SpatRaster -> stars -> SpatRaster with time #717

Closed
btupper opened this issue Oct 9, 2024 · 1 comment
Closed

casting SpatRaster -> stars -> SpatRaster with time #717

btupper opened this issue Oct 9, 2024 · 1 comment

Comments

@btupper
Copy link
Contributor

btupper commented Oct 9, 2024

I think this may be related to this issue, but it might just be operator error. I'm helping to make the tidysdm R package work with both terra and stars but I'm hitting a snag.

I'm looking to make a round trip excursion starting with a single attribute SpatRaster with time bands. The conversion to stars works as expected, but getting back to the original SpatRaster leaves me scratching my head. Is there a trick to converting a single attribute multi-layer stars object to the same as a SpatRaster?

The example below is an adaption of the one used in the earlier issue.

library(terra)
#> terra 1.7.78
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
s <- rast(system.file("ex/logo.tif", package="terra"))   
time(s) <- as.Date("2001-05-04") + 0:2
ss <- c(s, s)
time(ss) <- as.Date("2001-05-04") + 0:5
(x <- c(ss[[1:3]], ss[[1:2]]))
#> class       : SpatRaster 
#> dimensions  : 77, 101, 5  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#> coord. ref. : Cartesian (Meter) 
#> sources     : logo.tif  (3 layers) 
#>               logo.tif  (2 layers) 
#> names       : red, green, blue, red, green 
#> min values  :   0,     0,    0,   0,     0 
#> max values  : 255,   255,  255, 255,   255 
#> time (days) : 2001-05-04 to 2001-05-06
(s = st_as_stars(x))
#> stars object with 3 dimensions and 1 attribute
#> attribute(s):
#>                           Min. 1st Qu. Median     Mean 3rd Qu. Max.
#> red.green.blue.red.green     0     136    203 185.6155     254  255
#> dimension(s):
#>      from  to offset delta            refsys point                    values
#> x       1 101      0     1 Cartesian (Meter) FALSE                      NULL
#> y       1  77     77    -1 Cartesian (Meter) FALSE                      NULL
#> time    1   5     NA    NA              Date    NA 2001-05-04,...,2001-05-05
#>      x/y
#> x    [x]
#> y    [y]
#> time
as(s, "SpatRaster")
#> class       : SpatRaster 
#> dimensions  : 77, 101, 5  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#> coord. ref. : Cartesian (Meter) 
#> source(s)   : memory
#> names       : time2001-05-04, time2001-05-05, time2001-05-06, time2001-05-04, time2001-05-05 
#> min values  :              0,              0,              0,              0,              0 
#> max values  :            255,            255,            255,            255,            255
sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-apple-darwin20
#> Running under: macOS Sonoma 14.6.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] stars_0.6-6  sf_1.0-17    abind_1.4-8  terra_1.7-78
#> 
#> loaded via a namespace (and not attached):
#>  [1] cli_3.6.3          knitr_1.48         rlang_1.1.4        xfun_0.48         
#>  [5] DBI_1.2.3          KernSmooth_2.23-24 glue_1.8.0         htmltools_0.5.8.1 
#>  [9] e1071_1.7-16       rmarkdown_2.28     grid_4.4.1         evaluate_1.0.0    
#> [13] classInt_0.4-10    fastmap_1.2.0      yaml_2.3.10        lifecycle_1.0.4   
#> [17] compiler_4.4.1     codetools_0.2-20   fs_1.6.4           Rcpp_1.0.13       
#> [21] rstudioapi_0.16.0  digest_0.6.37      class_7.3-22       reprex_2.1.1      
#> [25] parallel_4.4.1     magrittr_2.0.3     tools_4.4.1        withr_3.0.1       
#> [29] proxy_0.4-27       units_0.8-5

Created on 2024-10-09 with reprex v2.1.1

Cheers,
Ben

@edzer edzer closed this as completed in a588913 Oct 9, 2024
@btupper
Copy link
Contributor Author

btupper commented Oct 9, 2024

Wow! The ink was still wet! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant