Closed ailich closed 3 years ago
It works with R 4.0.3, stars 0.4-4, and raster 3.4-5. So it's difficult for me to trace back.
Does plot(stars_obj)
work?
@mtennekes I can reproduce this error (see also my system info attached). I plan to change versions of some packages and will let you know if it makes things work.
library(raster)
#> Loading required package: sp
library(tmap)
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.2
r<- raster(volcano)
r_stack<- stack(r, focal(r, w=matrix(data = 1, nrow=3, ncol=3), fun=mean, na.rm=TRUE), focal(r, w=matrix(data = 1, nrow=3, ncol=3), fun=sd, na.rm=TRUE))
names(r_stack)
#> [1] "layer.1" "layer.2" "layer.3"
nlayers(r_stack)
#> [1] 3
tm_shape(r_stack)+
tm_raster()+
tm_facets()
#> Warning: Currect projection of shape r_stack unknown. Long lat (epsg 4326)
#> coordinates assumed.
#> Error in dim.stars(x): length(d) == length(dim(x[[1]])) is not TRUE
stars_obj<- st_as_stars(r_stack)
tm_shape(stars_obj)+
tm_raster()+
tm_facets()
#> Warning: Currect projection of shape stars_obj unknown. Long lat (epsg 4326)
#> coordinates assumed.
#> Error in dim.stars(x): length(d) == length(dim(x[[1]])) is not TRUE
Created on 2021-01-05 by the reprex package (v0.3.0)
Downgrading raster to the CRAN version did not help. Then, I updated all of my packages, and tried to plot a new stars object - it works:
library(raster)
#> Loading required package: sp
library(tmap)
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.2
r<- raster(volcano)
r_stack<- stack(r, focal(r, w=matrix(data = 1, nrow=3, ncol=3), fun=mean, na.rm=TRUE), focal(r, w=matrix(data = 1, nrow=3, ncol=3), fun=sd, na.rm=TRUE))
names(r_stack)
#> [1] "layer.1" "layer.2" "layer.3"
nlayers(r_stack)
#> [1] 3
r_stack_stars = st_as_stars(r_stack)
plot(r_stack_stars)
tm_shape(r_stack)+
tm_raster()+
tm_facets()
#> Warning: Currect projection of shape r_stack unknown. Long lat (epsg 4326)
#> coordinates assumed.
#> Error in dim.stars(x): length(d) == length(dim(x[[1]])) is not TRUE
stars_obj<- st_as_stars(r_stack)
tm_shape(stars_obj)+
tm_raster()+
tm_facets()
#> Warning: Currect projection of shape stars_obj unknown. Long lat (epsg 4326)
#> coordinates assumed.
#> Error in dim.stars(x): length(d) == length(dim(x[[1]])) is not TRUE
Created on 2021-01-05 by the reprex package (v0.3.0)
@Nowosad can you give me a trackback?
Traceback was very long, so I aggregated the original raster before plotting (r_stack2 = aggregate(r_stack, 8)
). It is still long though:
Going down to the CRAN release of stars (‘0.4.3’) fixes it for me
Ok, I found out that it is caused by a conflict the the github version of stars
(at least newer than September 2020).
It may not be a bug in stars
, but a different (improved) behavior.
In these code chunk (https://github.com/mtennekes/tmap/blob/master/R/pre_process_shapes.R#L212-L216) a (possibly multi-attribute) stars objects with possibly more dimensions than the raster ones (normally x
and y
) is converted to a stars object with 1 attribute, just the 2 x/y dimensions, and filled with integer values. At least, that is what I tried to achieve. But this doesn't work anymore, since I cannot overwrite a 3-dimensional array with a 2 dimensional matrix. I guess some checks have been added to prevent this.
@Nowosad do you know how to do this?
No, I do not. However, I would guess it is a result of a fairly recent change in stars (in the last few days).
@edzer A question about stars
that should be fairly trivial: how do I slice a stars
object such that I only keep the raster dimensions (normally x
and y
)? After that I want to replace the values by an integer sequence.
My old approach was something like this, which worked in earlier versions:
library(stars)
library(raster)
r = raster(volcano)
r_stack<- stack(r, focal(r, w=matrix(data = 1, nrow=3, ncol=3), fun=mean, na.rm=TRUE), focal(r, w=matrix(data = 1, nrow=3, ncol=3), fun=sd, na.rm=TRUE))
stars_obj<- st_as_stars(r_stack)
stars_obj[[1]] <- matrix(1L:(nrow(stars_obj)*ncol(stars_obj)), ncol = ncol(stars_obj), nrow = nrow(stars_obj))
attr(stars_obj, "dimensions") <- attr(stars_obj, "dimensions")[c("x", "y")]
I can use the tidyverse slice
method, but I do not want to import dplyr
in tmap
, and I do not know how to filter multiple dimensions programmatically.
See r-spatial/stars#365 ?
It might be related to https://github.com/r-spatial/stars/issues/365, but it is certainly not related to https://github.com/r-spatial/stars/issues/364, since r_stack
does not contain factor levels and it is not saved on disk.
For instance by
> stars_obj<- st_as_stars(r_stack)
> s = adrop(stars_obj[,,,1])
> s[[1]] = seq_len(prod(dim(s)))
> s
stars object with 2 dimensions and 1 attribute
attribute(s):
layer.1
Min. : 1
1st Qu.:1328
Median :2654
Mean :2654
3rd Qu.:3980
Max. :5307
dimension(s):
from to offset delta refsys point values x/y
x 1 61 0 0.0163934 NA NA NULL [x]
y 1 87 1 -0.0114943 NA NA NULL [y]
The [[<-.stars
method is indeed new (and now consistent with $<-.stars
), and checks whether the array dimensions correspond to the dimensions meta-data attribute. So you can't simply change one of them (leaving an inconsistency).
I closed https://github.com/r-spatial/stars/issues/365 btw, with what I consider the solution. Solving it for on-disk grids needs help from someone with deep understanding of raster internals.
Thx! It should be fixed now.
I'm running into issues when trying to plot raster stacks. I believe this is how I've done it in the past. It also may be a stars package issue as I believe tmap converts raster to stars objects before plotting. Currently running the latest development versions of raster, stars, and tmap (raster 3.4-8, stars 0.4-4, tmap 3.2) with R 3.6.3.