I have folder with geotiffs
folder=D:/mytiffs
the format in like this
distr1_2022-09-13_B02.tif
distr1_2022-09-13_B03.tif
distr1_2022-09-13_B04.tif
distr1_2022-09-13_B05.tif
distr1_2022-09-13_B06.tif
distr1_2022-09-13_B07.tif
distr1_2022-09-13_B08.tif
distr1_2022-09-13_B11.tif
distr1_2022-09-13_B12.tif
2022-09-13 the day the photo was taken, and B2-B12 are channels sentinel -2.
The first
I need to read all the geotifs that are in the folder at the same time, so as not to read them individually, since there are hundreds of them.
The second
Next, for each of these geotiffs, I need to get the latitude and longitude for each pixel, each of the channels, because each geotiff is its own channel.
desired output (it often happens that on the same date the photo was taken several times)
do=structure(list(lon = c(74.307111, 74.307111, 74.307111, 74.307111,
74.307111, 74.307111, 74.307111, 74.307111), lat = c(19.134687,
19.134687, 19.134687, 19.134687, 19.134687, 19.134687, 19.134687,
19.134687), acqdate = c("04.05.2022", "04.05.2022", "04.05.2022",
"04.05.2022", "09.05.2022", "09.05.2022", "09.05.2022", "09.05.2022"
), b02 = c(840L, 840L, 840L, 840L, 1018L, 1018L, 1018L, 1018L
), b03 = c(1246L, 1246L, 1246L, 1246L, 1363L, 1363L, 1363L, 1363L
), b04 = c(1781L, 1781L, 1781L, 1781L, 1904L, 1904L, 1904L, 1904L
), b05 = c(2020L, 2020L, 2020L, 2020L, 2142L, 2142L, 2142L, 2142L
), b06 = c(2383L, 2383L, 2383L, 2383L, 2369L, 2369L, 2369L, 2369L
), b07 = c(2603L, 2603L, 2603L, 2603L, 2535L, 2535L, 2535L, 2535L
), b08 = c(2440L, 2440L, 2440L, 2440L, 2479L, 2479L, 2479L, 2479L
), b8a = c(2654L, 2654L, 2654L, 2654L, 2661L, 2661L, 2661L, 2661L
), b11 = c(2838L, 2838L, 2838L, 2838L, 2735L, 2735L, 2735L, 2735L
), b12 = c(2146L, 2146L, 2146L, 2146L, 2194L, 2194L, 2194L, 2194L
)), class = "data.frame", row.names = c(NA, -8L))
i check this answer How to get values for a pixel from a geoTIFF in R? But either I'm doing something wrong, or this is a little not my question. And also it is not clear how to load all geotifs at once. Please help, any help is important to me.
Something like this would do that
library(terra)
f <- c("distr1_2022-09-13_B02.tif", "distr1_2022-09-13_B03.tif", "distr1_2022-09-13_B04.tif")
tm <- substr(f[1], 8, 17)
utm <- unique(tm)
out <- vector("list", length(utm))
for (u in utm) {
r <- rast(f[u==tm])
d <- as.data.frame(r, xy=TRUE)
d$time <- as.Date(u)
}
x <- do.call(rbind, out)
Or
f <- c("distr1_2022-09-13_B02.tif", "distr1_2022-09-13_B03.tif", "distr1_2022-09-13_B04.tif")
r <- rast(f)
names(r) <- substr(f, 8, 21)
d <- as.data.frame(r, xy=TRUE)
And then reshape d
as needed.