this is probably a basic question but I still need some help to figure out what I should do. My code is very simple, there are a bunch of calculations to get raster files stored in variables :NDWI,NDWI, VSI, etc. each calculation is based on an satellite image which has a name with the date in it. I will find a way to extract the date and store it in a variable.
In essence, what I want is a part of my code that goes over all the files created to save them as raster in a specific path of my laptop, under this format ( "variable name"_"date".tif. )
What I have for now :
Dossier <- "C:/Users/Perrin/Desktop/INRA/Raster/sentinel/L1C_T31UDR_A019210_20190225T105315/S2A_MSIL1C_20190225T105021_N0207_R051_T31UDR_20190225T125616.SAFE/GRANULE/L1C_T31UDR_A019210_20190225T105315/IMG_DATA"
library(raster)
list.files(Dossier)
Bande1 <- raster(list.files(pattern = "\\B01.jp2$"))
Bande2 <- raster(list.files(pattern = "\\B02.jp2$"))
Bande3 <- raster(list.files(pattern = "\\B03.jp2$"))
Bande4 <- raster(list.files(pattern = "\\B04.jp2$"))
NDVI <- (Bande8-Bande4)/(Bande8+Bande4)
NDWI <- (Bande8A-Bande11)/(Bande8A-Bande11)
NDDI <- (NDVI-NDWII)/(NDVI+NDWII)
writeRaster(NDWI, "C:/Users/Perrin/Desktop/INRA/résultats R/NDWI.tif", overwrite = T)
writeRaster(NDVI, "C:/Users/Perrin/Desktop/INRA/résultats R/NDVI", overwrite = T)
writeRaster(NDDI, "C:/Users/Perrin/Desktop/INRA/résultats R/NDDI.tif", overwrite = T)
Any help will be very much appreciated.
Create a list with your objects and then use Map
to loop over it:
rasterList <- list(NDVI = NDVI, NDWI = NDWI, NDDI = NDDI)
filenames<-sprintf("C:/Users/Perrin/Desktop/INRA/résultats R/%s_%s.tif",
names(rasterList), format(Sys.Date(), "%Y%m%d"))
Map(writeRaster, rasterList, filenames, MoreArgs = list(overwrite = TRUE))