r-spatial / stars

Spatiotemporal Arrays, Raster and Vector Data Cubes
https://r-spatial.github.io/stars/
Apache License 2.0
563 stars 94 forks source link

Column names in `sf` object missing band identification after conversion from `stars` object #702

Closed PPaccioretti closed 2 months ago

PPaccioretti commented 2 months ago

When transforming a stars object into sf object, the column names in the resulting sf object, only include the dates for the first band. All the column names have no band identification or names after the first band.

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

system.file("nc/bcsd_obs_1999.nc", package = "stars") |>
  read_stars() -> w
#> pr, tas,

w_sf <- st_as_sf(w)
colnames(w_sf)
#>  [1] "1999-01-31" "1999-02-28" "1999-03-31" "1999-04-30" "1999-05-31"
#>  [6] "1999-06-30" "1999-07-31" "1999-08-31" "1999-09-30" "1999-10-31"
#> [11] "1999-11-30" "1999-12-31" NA           "NA.1"       "NA.2"      
#> [16] "NA.3"       "NA.4"       "NA.5"       "NA.6"       "NA.7"      
#> [21] "NA.8"       "NA.9"       "NA.10"      "NA.11"      "geometry"

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

it seems that the issue arises from this line, where the dates are not being replicated fot the other bands, and the band names are added to the names.

One possible solution might be modifying that line to combine the band names and dates. However, I'm not certain if this approach will work for all cases.

names(df) = paste(rep(names(nc), times = nc), 
                  as.character(e[-ix][[1]]), 
                  sep = ".")