2

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.

2 Answers 2

5

Are they multiple layers of a single (3D) raster?

brick(filez)

Are they disparate windows from a single layer parent (2D) raster?

Reduce(merge, lapply(filez, raster))

Are they disparate 2D rasters with no common grid? (sounds like it, given "Granule")

Reduce(mosaic, lapply(filez, raster))

I suggest testing each of those options on small subsets of filez. Treat all R work like that, get the set of all filenames, test a single thing, like myrasters <- lapply(filez, raster), explore a few with a plot plot(myrasters[[1]]) plot(myrasters[[10]]) then try the next thing like merge (on a small example, two of those rasters).

My use of Reduce adds another level of abstraction here, but consider that

merge(myrasters[[1]], myrasters[[10]])

allows us to merge just two rasters at a time, Reduce avoids us needing to call merge on every pair in sequence and accumulate as we go, so it's pretty abstract but super powerful.

Don't use assign, it just creates lots of named things lying around you have to futz with.

2
  • is there a stars way to merge "disparate windows from a single layer parent (2D) raster"?
    – kennyB
    Commented Feb 13, 2019 at 3:55
  • 1
    I don't know - I haven't explored stars for this, or anything with these cell-abstractions that raster has yet, I don't think c.stars or merge.stars does this
    – mdsumner
    Commented Feb 14, 2019 at 1:14
4

I think your problem is because your list contains only the names of the rasters and not the rasters themselves. First get the rasters and then you can apply do.call:

namez1.get <- lapply(namez1, get)
m <- do.call(merge, namez1.get)

But, the best way would be to directly create this list of rasters in your loop:

r.list <- list()
for(i in 1:length(filez)){  
   r.list[[i]] <- raster(paste(pathz, "/Granules/", filez[i], sep = ""))  
} 
m <- do.call(merge, r.list)
1
  • You're welcome. You can thank by upvoting and validating the answer. Thanks. Commented Oct 5, 2017 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.