Skip to content

Instantly share code, notes, and snippets.

@OldLipe
Last active May 10, 2024 20:27
Show Gist options
  • Save OldLipe/b2c0a66e1cf3c742892e504b3512313f to your computer and use it in GitHub Desktop.
Save OldLipe/b2c0a66e1cf3c742892e504b3512313f to your computer and use it in GitHub Desktop.
How to transform brick to stack
#
# function definition: the params name are intuitive, so just follow the
# example in line 52
#
explode_brick <- function(brick_file, output_dir) {
# stop if the provided brick doesnt exists
stopifnot(file.exists(brick_file))
# reading brick
brick <- terra::rast(brick_file)
# dir where your images will be saved
output_dir <- paste0(output_dir, "/stack")
if (!dir.exists(output_dir))
dir.create(output_dir, recursive = TRUE)
stopifnot(dir.exists(output_dir))
for (i in seq_len(terra::nlyr(brick))) {
# file name
output_file <- paste0(output_dir, "/", names(brick[[i]]), ".tif")
terra::writeRaster(brick[[i]],
filename = output_file,
overwrite = TRUE,
gdal = c("COMPRESS=LZW", "BIGTIFF=YES"))
}
return(invisible(NULL))
}
#
# an example of how to use this function.
# NOTE: Repeat this function for each brick.
#
explode_brick(brick_file = "/home/rstudio/brick_ndvi/CB4_64_16D_STK_v001_021025_NDVI.tif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment