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

st_crop not updating dimensions? #722

Closed dominicroye closed 3 weeks ago

dominicroye commented 3 weeks ago

When I crop a star object, it seems to crop it, but the dimension metavariable "from" is not expected 1. Then, when I do a raster calculation with another stars object with the cropped dimensions, it tells me that it doesn't match even if they have the same extent. I have also seen that x is set to one in some cases, but y is not.

library(stars)
library(elevatr)
library(giscoR)

bel <- gisco_get_countries(country = "Belgium")

rbel <- get_elev_raster(bel, z = 6)
rbel <- st_as_stars(rbel)

bb <- st_bbox(c(xmin = 4, ymin = 50.4, xmax = 5, ymax = 51), crs = 4326)

> st_crop(rbel, bb)
stars object with 2 dimensions and 1 attribute
attribute(s):
                  Min. 1st Qu. Median     Mean 3rd Qu. Max.
file42b040be6724     2      50     89 91.91582     136  276
dimension(s):
  from  to offset    delta refsys point x/y
x  389 486      0  0.01031 WGS 84  TRUE [x]
y  144 203  52.48 -0.01031 WGS 84  TRUE [y]
edzer commented 3 weeks ago

That is because offset is kept constant; you can reset this by

> st_crop(rbel, bb) |> st_normalize()
stars object with 2 dimensions and 1 attribute
attribute(s):
                   Min. 1st Qu. Median     Mean 3rd Qu. Max.
file7625f5520afef     2      50     89 91.91582     136  276
dimension(s):
  from to offset    delta refsys point x/y
x    1 98  3.999  0.01031 WGS 84  TRUE [x]
y    1 60  51.01 -0.01031 WGS 84  TRUE [y]

which modifies offset while setting from back to 1.

dominicroye commented 3 weeks ago

Ok, now I understand the idea. For me, it is not so clear in the documentation.