'''
fieldszn <- '~/Desktop/hls project/fieldszn'
bands <- list.files(fieldszn, pattern = "*.tiff", recursive = TRUE, full.names = FALSE)
new_names <- c("red", "nir", "fmask")
file.rename(bands, paste0(new_names,"*.tiff"))
red <- list.files(subfolders, pattern = "red.tiff", full.names = TRUE, recursive = TRUE)
nir <- list.files(subfolders, pattern = "nir.tiff", full.names = TRUE, recursive = TRUE)
redstack <- stack(red)
nirstack <- stack(nir)
ndvi <- overlay(redstack, nirstack, fun = function(x,y) {
(y - x)/(y +x)
})
writeRaster(ndvi, paste0(subfolders,'NDVI',i,'.tif'))
,,,
i currently have 9 different sub-folders, with 3 raster bands per sub-folder that present an individual image (all of which are in the format of a asset link that includes granule id, satellite type, etc.). i'm trying to iterate through them to rename to a common name like red, nir, and fmask, so i can then iterate them through to run through a vegetation index calculation. i've been able to rename them but they keep ending up into the working directory rather than their original folder and i haven't figured out how to iterate through them without adding a number to them.
after renaming the files, i'm planning to iterate them through a vegetation index calculation - but i'm not sure how to set it up.
please help! i'm new to R and would appreciate any assistance.
I think the problem is that you don't have the folder path included in the new file names, so file.rename is moving the files to your working directory by default.
You can use file.path()
to combine paths and filenames.
new_names <- file.path(dirname(bands), paste0(c("red", "nir", "fmask"), ".tiff"))
file.rename(bands, new_names)