raster icon indicating copy to clipboard operation
raster copied to clipboard

Error converting subset of SpatRaster to RasterStack if out of memory

Open ailich opened this issue 1 year ago • 0 comments

When converting a subset of a multilayer SpatRaster that is out of memory to a RasterStack an error message appears but returns a RasterStack with the incorrect number of layers.

library(terra)
#> Warning: package 'terra' was built under R version 4.3.3
#> terra 1.7.76
packageVersion("raster")
#> [1] '3.6.27'

r<- rast(volcano)
r<- c(r, r*2,r*3,r*4)         
names(r)<- paste0("lyr.", 1:nlyr(r))

inMemory(r)
#> [1] TRUE TRUE TRUE TRUE
raster::stack(r[[1:2]]) |> raster::nlayers() #If in memory it works
#> [1] 2

writeRaster(r,"test.tif", overwrite=TRUE)

r<- rast("test.tif")
inMemory(r)
#> [1] FALSE
raster::stack(r[[1:2]]) |> raster::nlayers() #If out of memory an error message is shown but, a RasterStack of the wrong size is returned
#> Error in `names<-`(`*tmp*`, value = names(from)) : 
#>   incorrect number of layer names
#> [1] 4

raster::stack(r)[[1:2]] |> raster::nlayers() #However, the whole SpatRaster can be converted and then the RasterStack subset
#> [1] 2

Created on 2024-05-06 with reprex v2.0.2

ailich avatar May 06 '24 17:05 ailich