Landsat QA rasters are stored in 8bit .tiff files. The flags are encoded by the bit position. But I can not extract them using terra.
I am trying to retrieve the bit position with this code:
library(terra)
toBinary <- function(i){
valuesbin = paste0(as.integer(rev(intToBits(i)[1:8])), collapse = "")
valuesbin = unlist(strsplit(valuesbin, split = ""))
as.numeric(valuesbin)
}
qa = rast('./qarast.tiff')
qa = app(qa,toBinary)
but it crash with:
Error: [app] the number of values returned by 'fun' is not appropriate
How can I use this quality flags in R? Is there any specific library less error prone?
Cheers
toBinaryis not vectorizedThis is a vectorized function
And use it: