Skip to main content
added 13 characters in body
Source Link
falcs
  • 1.1k
  • 3
  • 11
  • 24

Full credit to @Tom Higginbottom who answered the hard part but a small modification I made to fix the

Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin

Just added a raster_file <- projectRaster(raster_file, snap, method = "ngb") into the loop and created a snap raster, with a large extent that covers the whole set of rasters

snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700")
 

Full 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)
}

raster_files <- list.files(path ="D:/results/gov-4p/stackoverflow",pattern = ".tif$",full.names = TRUE )
snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700") 
national_layer <- mosaicList(raster_files )

Full credit to @Tom Higginbottom who answered the hard part but a small modification I made to fix the

Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin

Just added a raster_file <- projectRaster(raster_file, snap, method = "ngb") into the loop and created a snap raster, with a large extent that covers the whole set of rasters

snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700")
 
    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)
}

raster_files <- list.files(path ="D:/results/gov-4p/stackoverflow",pattern = ".tif$",full.names = TRUE )
snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700") 
national_layer <- mosaicList(raster_files )

Full credit to @Tom Higginbottom who answered the hard part but a small modification I made to fix the

Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin

Just added a raster_file <- projectRaster(raster_file, snap, method = "ngb") into the loop and created a snap raster, with a large extent that covers the whole set of rasters

snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700")

Full 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)
}

raster_files <- list.files(path ="D:/results/gov-4p/stackoverflow",pattern = ".tif$",full.names = TRUE )
snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700") 
national_layer <- mosaicList(raster_files )
Source Link
falcs
  • 1.1k
  • 3
  • 11
  • 24

Full credit to @Tom Higginbottom who answered the hard part but a small modification I made to fix the

Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin

Just added a raster_file <- projectRaster(raster_file, snap, method = "ngb") into the loop and created a snap raster, with a large extent that covers the whole set of rasters

snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700")

    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)
}

raster_files <- list.files(path ="D:/results/gov-4p/stackoverflow",pattern = ".tif$",full.names = TRUE )
snap <- raster(resolution = c(10,10), xmn = 180000, xmx = 300000, ymn = 60000, ymx = 100000, crs = "+init=epsg:27700") 
national_layer <- mosaicList(raster_files )