Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error converting subset of SpatRaster to RasterStack if out of memory #336

Closed
ailich opened this issue May 6, 2024 · 1 comment
Closed

Comments

@ailich
Copy link

ailich commented May 6, 2024

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

@rhijmans
Copy link
Member

rhijmans commented Jul 8, 2024

Thank you for reporting and sorry for the delay. Fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants