James Ball
James Ball

Reputation: 13

Error when using do.call to merge multi-band satellite image raster bricks in RStudio

I am attempting to merge some multi-band satellite images in R (unfortunately I can't share these due to copyright issues). They are of equal size and resolution with a slight overlap between them. When I import each image individually and merge them there is no issue:

library(raster)

raster1 <- brick("path/multi-band-raster1")
raster2 <- brick("path/multi-band-raster2")
raster3 <- brick("path/multi-band-raster3")
raster4 <- brick("path/multi-band-raster4")
raster5 <- brick("path/multi-band-raster5")

merged_scene <- merge(raster1, raster2, raster3, raster4, raster5)

However, I am aiming to make my code as flexible as possible so that I can reapply it to different sets of images. To do this I load the rasters as a list and then use the do.call() function so that the merge function takes all the arguments representing the raster bricks in the list:

folder <- paste0(getwd(),"/Images-folder/")
list.filenames <- list.files(folder, pattern=".tif$", full.names=FALSE)
list.data <- list()

for (i in 1:length(list.filenames)){
  list.data[[i]] <- brick(paste0(folder,list.filenames[i]))
}

names(list.data) <- list.filenames

merged_scene <- do.call(merge, list.data)

However, when I run this I get:

Error in as.data.frame(x) : argument "x" is missing, with no default

Please let me know if you can see where I've gone wrong. Many thanks.

Upvotes: 1

Views: 647

Answers (1)

andrew_reece
andrew_reece

Reputation: 21274

Don't give names to your bricks.

require(raster)

names(rlist) <- c("foo", "bar", "baz")
merged_scene <- do.call(merge, rlist)
# Error in as.data.frame(x) : argument "x" is missing, with no default

rlist <- list(raster1, raster2, raster3) # no names()
merged_scene <- do.call(merge, rlist)

merged_scene
class       : RasterBrick 
dimensions  : 77, 101, 7777, 3  (nrow, ncol, ncell, nlayers)
resolution  : 1, 1  (x, y)
extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=merc +datum=WGS84 
data source : in memory
names       : layer.1, layer.2, layer.3 
min values  :       0,       0,       0 
max values  :     255,     255,     255 

Data:

raster1 <- brick(system.file("external/rlogo.grd", package="raster"))
raster2 <- brick(system.file("external/rlogo.grd", package="raster"))
raster3 <- brick(system.file("external/rlogo.grd", package="raster"))

rlist <- list(raster1, raster2, raster3)

Upvotes: 3

Related Questions