You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've run into an issue where using the win argument when reading in several raster files into a single SpatRaster causes problems later on. See the example below.
library(terra)
#> terra 1.6.47# save out 10 rasters, each to different filesext<- ext(0, 10, 0, 10)
paths<- sapply(1:10, function(i){
path_i<- tempfile(fileext=".tif") # also tried .nc, .grd, and .asc - results were the samemat<-matrix(i, nrow=10, ncol=10)
writeRaster(rast(mat, extent=ext), path_i)
return(path_i)
})
# read them in, making use of the 'win' argument to specify a spatial subsetsub_ext<- ext(2, 6, 2, 6)
rasts<- rast(paths, win=sub_ext)
# check the resultsrasts[[1]] # correct#> class : SpatRaster #> dimensions : 4, 4, 1 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> window : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source : file8ace2e40c17d.tif #> name : lyr.1 #> min value : >1 #> max value : 1<rasts[[2]] # incorrect - has extent 'sub_ext' but dimensions 10 x 10 (and therefore resolution 0.4 x 0.4), which is incorrect#> class : SpatRaster #> dimensions : 10, 10, 1 (nrow, ncol, nlyr)#> resolution : 0.4, 0.4 (x, y)#> window : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source : file8ace1d5f0d44.tif #> name : lyr.1 #> min value : >2 #> max value : 2<# try performing some operations on the rastersrasts+1# works#> class : SpatRaster #> dimensions : 4, 4, 10 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> extent : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source(s) : memory#> names : lyr.1, lyr.1, lyr.1, lyr.1, lyr.1, lyr.1, ... #> min values : 2, 3, 4, 5, 6, 7, ... #> max values : 2, 3, 4, 5, 6, 7, ...rasts[[1:10]] +1# works#> class : SpatRaster #> dimensions : 4, 4, 10 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> extent : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source(s) : memory#> names : lyr.1, lyr.1, lyr.1, lyr.1, lyr.1, lyr.1, ... #> min values : 2, 3, 4, 5, 6, 7, ... #> max values : 2, 3, 4, 5, 6, 7, ...rasts[[1]] +1# works#> class : SpatRaster #> dimensions : 4, 4, 1 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> extent : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source(s) : memory#> name : lyr.1 #> min value : 2 #> max value : 2rasts[[2]] +1# doesn't work#> Warning: /private/var/folders/w5/21t55k2s2l5g7kj4vvvwhshm0000gn/T/Rtmpk9UmqM/file8ace1d5f0d44.tif:#> Access window out of range in RasterIO(). Requested (2,4) of size 10x10 on#> raster of 10x10. (GDAL error 5)#> class : SpatRaster #> dimensions : 10, 10, 1 (nrow, ncol, nlyr)#> resolution : 0.4, 0.4 (x, y)#> extent : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84
(rasts+1)[[2]] # works#> class : SpatRaster #> dimensions : 4, 4, 1 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> extent : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source(s) : memory#> name : lyr.1 #> min value : 3 #> max value : 3#============================================# now try the same thing, but save the rasters as a single, multi-layered .tifrasts2<- rast(lapply(1:10, function(i){
mat<-matrix(i, nrow=10, ncol=10)
return(rast(mat, extent=ext))
}))
path2<- tempfile(fileext=".tif")
writeRaster(rasts2, path2)
rasts2a<- rast(path2, win=sub_ext)
rasts2a[[1]] # correct#> class : SpatRaster #> dimensions : 4, 4, 1 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> window : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source : file8ace26cf9f01.tif #> name : lyr.1 #> min value : >1 #> max value : 1<rasts2a[[2]] # correct#> class : SpatRaster #> dimensions : 4, 4, 1 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> window : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source : file8ace26cf9f01.tif #> name : lyr.1 #> min value : >2 #> max value : 2<rasts2a[[2]] +1# correct#> class : SpatRaster #> dimensions : 4, 4, 1 (nrow, ncol, nlyr)#> resolution : 1, 1 (x, y)#> extent : 2, 6, 2, 6 (xmin, xmax, ymin, ymax)#> coord. ref. : lon/lat WGS 84 #> source(s) : memory#> name : lyr.1 #> min value : 3 #> max value : 3# this time everything works
I've run into an issue where using the
win
argument when reading in several raster files into a singleSpatRaster
causes problems later on. See the example below.Created on 2023-01-04 with reprex v2.0.2
Session info
The text was updated successfully, but these errors were encountered: