1

I have multiple (17) raster files which I want to process. They are from the Sentinel 2 satellite System (ESA) and already transformed in .tif format.

I wanted to build a mosaic image of all these 17 images (Portugal) for each Band I need (Blue, Green, Red, NIR, SWIR). After a short search I found following program: Combine multiple partially overlapping rasters into a single raster in R

When I try:

national_layer <- mosaicList(raster_files )

I get error:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘raster’ for signature ‘"numeric"’

File specifications:

class       : RasterLayer 
dimensions  : 10980, 10980, 120560400  (nrow, ncol, ncell)
resolution  : 10, 10  (x, y)
extent      : 699960, 809760, 4590240, 4700040  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=29 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\###########\Desktop\winter\T29TQG\T29TQG_B04.tif 
names       : T29TQG_B04 
values      : 0, 23739  (min, max)

So if I crop the raster files manually it works, but would be nice if I use a loop.

complete code:

mosaicList <- function(rasList){

  #Internal function to make a list of raster objects from list of files.
  ListRasters <- function(list_names) {
    raster_list <- list() # initialise the list of rasters
    for (i in 1:(length(list_names))){ 
      grd_name <- list_names[i] # list_names contains all the names of the images in .grd format
      raster_file <- raster::raster(grd_name)
      raster_file <- projectRaster(raster_file, snap, method = "ngb")
    }
    raster_list <- append(raster_list, raster_file) # update raster_list at each iteration
  }

  #convert every raster path to a raster object and create list of the results
  raster.list <-sapply(rasList, FUN = ListRasters)

  # edit settings of the raster list for use in do.call and mosaic
  names(raster.list) <- NULL
  #####This function deals with overlapping areas
  raster.list$fun <- sum
  #raster.list$tolerance <- 0.1

  #run do call to implement mosaic over the list of raster objects.
  mos <- do.call(raster::mosaic, raster.list)

  #set crs of output
  crs(mos) <- crs(x = raster(rasList[1]))
  return(mos)
}

setwd("C:/Users/sebastian.r/Desktop/winter")
raster_files <- lapply(list.files(pattern = "02.tif$", recursive = TRUE), raster, values=TRUE, full.names=TRUE,extent.values=TRUE, mean=TRUE)
snap <- raster(resolution = c(10,10), xmn = 399960, xmx = 809760, ymn = 4090200, ymx = 4700040, crs = "+proj=utm +zone=29 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
national_layer <- mosaicList(raster_files )
  • Please supply the entire code before you decided to use the mosaic function from the raster package. There must be an issue with the format of the data in your rasters. – Vijay Ramesh Nov 9 '17 at 18:21
  • Can you add the output of purrr::map(.x = raster_files, .f = function(.x){class(.x)}) as an edit of your question? (you need purrr package installed first) – aldo_tapia Nov 9 '17 at 18:21
  • thanks, well the output of the line: purrr::map(.x = raster_files, .f = function(.x){class(.x)}) is: [[1]] [1] "RasterLayer" attr(,"package") [1] "raster" [[2]] [1] "RasterLayer" attr(,"package") [1] "raster" [[3]] [1] "RasterLayer" attr(,"package") [1] "raster" .... ..... ..... ..... ...... – Sebastian. R Nov 13 '17 at 12:00
1

this is my function, you are having various problems, which are mainly due to the design of the function, which is for single band rasters not stacked imagery.

The functional problem is, i think, the function expects a list of raster file names, what you are imputing is a list of raster objects. Therefore it throws an error. The error specifically means that you cannot make a raster object from a numeric class, it need a file path.

In this case, you have multi-band options, the function wont deal with this, it will only handle single band rasters. So you need to put a list of the individual bands in separately. Also, as you seem to have sub folders try the following:

raster_files <- list.files(path ="folder_with_files",pattern = ".tif$",full.names = TRUE, recursive=TRUE )
#repeat this for each band
raster_files_band <- raster_files[grep("B01",raster_files)]
national_layer <- mosaicList(raster_files_band )

I have to highlight, this is not the most desirable way to mosaic satellite imagery. As no correction will be applied, so if you want to do analysis (land cover mapping etc) be careful and check for obvious errors. You may have more joy using Google Earth Engine, which will be quicker and allows various methods of compositing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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