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

Trying to c() two stars objects #703

Closed mhesselbarth closed 2 months ago

mhesselbarth commented 2 months ago

Hello,

I am currently trying to c() two objects (x1 and x2) which have the same dimensions. I would like to combine the attributes and keep the dimensions. However, I am getting an error the stars don't know how to merge them.

However, the same code works for a different, similar objects (x2 and x3)

library(stars)

x1 <- readRDS("x1.rds")
x2 <- readRDS("x2.rds")
x3 <- readRDS("x3.rds")

stars::st_dimensions(x1)
#>      from  to  offset delta                       refsys
#> x       1 484 2636838  8000 ETRS89-extended / LAEA Eu...
#> y       1 490 5305758 -8000 ETRS89-extended / LAEA Eu...
#> date    1  17      NA    NA                         Date
#>                         values x/y
#> x                         NULL [x]
#> y                         NULL [y]
#> date 2020-01-01,...,2100-01-01
stars::st_dimensions(x2)
#>      from  to  offset delta                       refsys
#> x       1 484 2636838  8000 ETRS89-extended / LAEA Eu...
#> y       1 490 5305758 -8000 ETRS89-extended / LAEA Eu...
#> date    1  17      NA    NA                         Date
#>                         values x/y
#> x                         NULL [x]
#> y                         NULL [y]
#> date 2020-01-01,...,2100-01-01
stars::st_dimensions(x3)
#>      from  to  offset delta                       refsys
#> x       1 484 2636838  8000 ETRS89-extended / LAEA Eu...
#> y       1 490 5305758 -8000 ETRS89-extended / LAEA Eu...
#> date    1  17      NA    NA                         Date
#>                         values x/y
#> x                         NULL [x]
#> y                         NULL [y]
#> date 2020-01-01,...,2100-01-01

# this doesnt work
c(x1, x2, along = NA)
#> Error in c.stars(x1, x2, along = NA): don't know how to merge arrays: please specify parameter along

# this works
c(x2, x3, along = NA)
#> stars object with 3 dimensions and 2 attributes
#> attribute(s), summary of first 1e+05 cells:
#>             Min.      1st Qu.       Median         Mean      3rd Qu.
#> pr   1.46013e-05 2.250983e-05 2.412697e-05 2.486675e-05 2.626826e-05
#> age  2.50000e+00 3.984256e+01 5.025965e+01 5.356940e+01 6.258916e+01
#>              Max.  NA's
#> pr   5.430899e-05 80973
#> age  1.525000e+02 91472
#> dimension(s):
#>      from  to  offset delta                       refsys
#> x       1 484 2636838  8000 ETRS89-extended / LAEA Eu...
#> y       1 490 5305758 -8000 ETRS89-extended / LAEA Eu...
#> date    1  17      NA    NA                         Date
#>                         values x/y
#> x                         NULL [x]
#> y                         NULL [y]
#> date 2020-01-01,...,2100-01-01

Created on 2024-08-23 with reprex v2.1.1

Thanks for the help!

mhesselbarth commented 2 months ago

Here are the files

Files.zip

edzer commented 2 months ago
> all.equal(st_dimensions(x1), st_dimensions(x2))
[1] "Component “x”: Component “refsys”: Component “input”: 1 string mismatch"
[2] "Component “y”: Component “refsys”: Component “input”: 1 string mismatch"
> all.equal(st_dimensions(x2), st_dimensions(x3))
[1] TRUE
mhesselbarth commented 2 months ago

Thanks for the quick reply. So there seems to be a small difference of the CRS? Because on the first glance they all seem to be ETRS89.

edzer commented 2 months ago

Yes, and they're semantically equivalent:

> st_crs(x1) == st_crs(x2)
[1] TRUE

so there is room for improving this; for now you could

st_crs(x1) = st_crs(x2)

i.e. copy it over to ensure equality, and then c(x1,x2) but I'd agree that is not elegant. There must be a reason however, that they're different in the first place, you could try to catch that earlier.

mhesselbarth commented 2 months ago

Perfect that helps me a lot. Thank you very much.

I suspect it’s happening because I set the crs for one object using the epsg code and for one using an existing object.