-1

I have rasters with same basenames in two different folders. I want to perform addition of two rasters with same basenames and save it in an output directory with same basename.

library(raster)
library(rgdal)
library(ggplot2)

dir = "/home//data/AOI/index"
outDir="/home/data/AOI/index/I"
dirs <- list.dirs()
dirs <- dirs[grepl("*ndwi$", dirs)]
files=list.files(dirs)
for (file in files){
  basename(file)==files
  print(file)
}

This is giving me files with same basenames:

[1] "LC08120180107.tif"
[1] "LC08120180107.tif"
[1] "LC08120180328.tif"
[1] "LC08120180328.tif"
[1] "LC08120180904.tif"
[1] "LC08120180904.tif"
[1] "LT05119900315.tif"
[1] "LT05119900315.tif"
[1] "LT05119900518.tif"
[1] "LT05119900518.tif"

Now how to perform addition of rasters which has same basenames?

  • Have you tried? The basenames aren't important, the full file path is what you create a raster from. If you are trying to load from multiple directories then get the full paths from list.files by using the full.names=TRUE parametr and load those into rasters. – Spacedman Jan 9 at 8:36
  • Or use list.files in each directory separately, check you have matching file names, then load one from each directory and add them. By using list.files on two directories you are losing the path each one came from. Do them separately, then check the match (with maybe the match function), load and add. What have you tried? – Spacedman Jan 9 at 8:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.