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

Error in st_as_sf when converting stars object with interval time dimension from cube class #103

Open
PPaccioretti opened this issue Aug 22, 2024 · 0 comments

Comments

@PPaccioretti
Copy link

I encountered an issue when attempting to convert a stars object, which was created from a cube class object, to an sf object. The error occurs during the conversion process and seems to be related to the interval class in the time dimension.

library(gdalcubes)
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
# create image collection from example Landsat data only
if (!file.exists(file.path(tempdir(), "L8.db"))) {
  L8_files <- list.files(system.file("L8NY18", package = "gdalcubes"),
                         ".TIF", recursive = TRUE, full.names = TRUE)
  create_image_collection(L8_files, "L8_L1TP", file.path(tempdir(), "L8.db"), quiet = TRUE)
}

L8.col = image_collection(file.path(tempdir(), "L8.db"))
v = cube_view(extent=list(left=388941.2, right=766552.4,
                          bottom=4345299, top=4744931, t0="2018-04", t1="2018-04"),
              srs="EPSG:32618", nx = 497, ny=526, dt="P1M")

# Transform cube to stars
# Suppress some warnings about not all variables share the same primary dimension
my_stars <- st_as_stars(select_bands(raster_cube(L8.col, v), c("B04", "B05"))) |>
  suppressWarnings()

my_stars
#> stars object with 3 dimensions and 2 attributes
#> attribute(s):
#>    Min. 1st Qu. Median     Mean 3rd Qu.  Max.  NA's
#> 4  6119    6928   8717 11063.82   10620 53002 99070
#> 5  5564    6411  13519 14300.35   17073 57497 99070
#> dimension(s):
#>      from  to  offset  delta                refsys point
#> x       1 497  388941  759.8 WGS 84 / UTM zone 18N    NA
#> y       1 526 4744931 -759.8 WGS 84 / UTM zone 18N    NA
#> time    1   1      NA     NA               POSIXct FALSE
#>                       values x/y
#> x                       NULL [x]
#> y                       NULL [y]
#> time [2018-04-01,2018-05-01)
st_as_sf(my_stars)
#> Error in names(cargs) <- nmc: 'names' attribute [2] must be the same length as the vector [1]

By removing the interval class from the time dimension, the conversion to sf works:

# Remove interval class from time dimension
vals <- attr(my_stars, "dimensions")$time$values
class(vals) <- 'list'
attr(my_stars, "dimensions")$time$values = vals
# Transform to sf
st_as_sf(my_stars)
#> Simple feature collection with 162352 features and 2 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 388941.2 ymin: 4345299 xmax: 766552.4 ymax: 4744931
#> Projected CRS: WGS 84 / UTM zone 18N
#> First 10 features:
#>        4     5                       geometry
#> 1   8225 13965 POLYGON ((475556.2 4740372,...
#> 2   8225 13965 POLYGON ((476316 4740372, 4...
#> 3   8225 13965 POLYGON ((477075.8 4740372,...
#> 4   8225 13965 POLYGON ((477835.6 4740372,...
#> 5  22048 24057 POLYGON ((478595.4 4740372,...
#> 6  22048 24057 POLYGON ((479355.1 4740372,...
#> 7  22048 24057 POLYGON ((480114.9 4740372,...
#> 8  22048 24057 POLYGON ((480874.7 4740372,...
#> 9   8225 13965 POLYGON ((475556.2 4739613,...
#> 10  8225 13965 POLYGON ((476316 4739613, 4...

Created on 2024-08-22 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/Chicago
#>  date     2024-08-22
#>  pandoc   3.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  abind       * 1.4-5   2016-07-21 [1] CRAN (R 4.4.0)
#>  class         7.3-22  2023-05-03 [2] CRAN (R 4.4.1)
#>  classInt      0.4-10  2023-09-05 [1] CRAN (R 4.4.0)
#>  cli           3.6.3   2024-06-21 [1] CRAN (R 4.4.0)
#>  DBI           1.2.3   2024-06-02 [1] CRAN (R 4.4.0)
#>  digest        0.6.35  2024-03-11 [1] CRAN (R 4.4.0)
#>  e1071         1.7-14  2023-12-06 [1] CRAN (R 4.4.0)
#>  evaluate      0.24.0  2024-06-10 [1] CRAN (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
#>  fs            1.6.4   2024-04-25 [1] CRAN (R 4.4.0)
#>  gdalcubes   * 0.7.0   2024-03-07 [1] CRAN (R 4.4.0)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  jsonlite      1.8.8   2023-12-04 [1] CRAN (R 4.4.0)
#>  KernSmooth    2.23-24 2024-05-17 [2] CRAN (R 4.4.1)
#>  knitr         1.47    2024-05-29 [1] CRAN (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
#>  ncdf4         1.22    2023-11-28 [1] CRAN (R 4.4.0)
#>  proxy         0.4-27  2022-06-09 [1] CRAN (R 4.4.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.4.1)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.4.0)
#>  R.oo          1.26.0  2024-01-24 [1] CRAN (R 4.4.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.4.1)
#>  Rcpp          1.0.12  2024-01-09 [1] CRAN (R 4.4.0)
#>  reprex        2.1.0   2024-01-11 [1] CRAN (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.27    2024-05-17 [1] CRAN (R 4.4.0)
#>  rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  sf          * 1.0-16  2024-03-24 [1] CRAN (R 4.4.0)
#>  stars       * 0.6-5   2024-04-04 [1] CRAN (R 4.4.0)
#>  styler        1.10.3  2024-04-07 [1] CRAN (R 4.4.1)
#>  units         0.8-5   2023-11-28 [1] CRAN (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
#>  withr         3.0.1   2024-07-31 [1] CRAN (R 4.4.1)
#>  xfun          0.45    2024-06-16 [1] CRAN (R 4.4.1)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.4.0)
#> 
#>  [1] C:/Users/ppaccioretti2/AppData/Local/R/win-library/4.4
#>  [2] C:/Program Files/R/R-4.4.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
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