library(terra)
library(tidyverse)
Since the data can be in another folder at another location. I shall be using the root path as a way to get the path in my case.
rootDir <- "C:/Users/roywa/Data/LST/LC09_L2SP_168061_20230119_20230313_02_T1/"
all_tif_files <- list.files(rootDir, pattern = "\\.TIF$", full.names = TRUE)
lst_files<-list.files(rootDir, pattern="B([1-9])\\.TIF$", full.names = TRUE)
The get_band function is used to get the band given a
valid Landsat formatted image name.
get_band <- function(name) {
band <- str_split_i(name, "", -1)
return(band)
}
Plotting to make sure we have all the images we require to stack. In this case we are expecting 7 images ie 7 bands.
lst_images <- rast(lst_files)
names(lst_images) <- names(lst_images) |>
map(\(x) str_c("BAND ", get_band(x)))
lst_images %>%
terra::plot(main=names(lst_images),col = blues9 )
Plotting an RGB composite with this method requires selecstion of the bands in the order of: - Red (Band 4) - Green (Band 3) _ Blue (Band 2)
lst_rgb <- c(lst_images["BAND 4"],lst_images["BAND 3"],lst_images["BAND 2"])
plotRGB(lst_rgb, main="RGB", stretch="lin")
I have decided to write the image on
writeRaster(lst_images, str_c(rootDir,"LC09_L2SP_STACK_1_7.TIF"), overwrite=TRUE)
|---------|---------|---------|---------|
=========================================