I’m trying to load and merge a bunch of .tif spatial files from ASTER. Sorry, I can’t provide a replicable example, but I hope this ilustrates the situation:
library(raster)
pathz <- "C:/USERS/PATH/TO/WORKING/DIR/"
# The subdirectory where my .tif files are
filez <- list.files(path = paste(pathz, "/Granules", sep = ""), pattern = ".tif")
# I get the list of files
namez <- paste("r", c(1:length(filez)), sep = "")
# I create the bunch of spatial objects
for(i in 1:length(filez)){
temp <- raster(paste(pathz, "/Granules/", filez[i], sep = ""))
assign(namez[i], temp)
}
# This way, I've created 200 RasterLayer objects, for example
r49
class : RasterLayer
dimensions : 3601, 3601, 12967201 (nrow, ncol, ncell)
resolution : 0.0002777778, 0.0002777778 (x, y)
extent : -76.00014, -74.99986, 1.999861, 3.000139 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Paolo\Desktop\Aster DEM\Granules\ASTGTM2_N02W076_dem.tif
names : ASTGTM2_N02W076_dem
values : -32768, 32767 (min, max)
class(namez)
[1] "character"
namez1 <- as.list(namez)
class(namez1)
[1] "list"
namez1
[[1]]
[1] "r1"
[[2]]
[1] "r2"
[[3]]
[1] "r3"
# etc
# Obviously it did not work
m <- do.call(merge, namez1)
Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
# I tried...
namez1 <- as.environment(namez1)
Error in list2env(list("r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", :
names(x) must be a character vector of the same length as x
# Also...
list2env(namez1, envir = NULL)
Error in list2env(namez1, envir = NULL) :
names(x) must be a character vector of the same length as x
# Of course...
is.vector(namez1)
[1] TRUE
Specifically, I wish to know how to create a list of spatial objects so I can merge them.