Created
July 30, 2023 08:49
-
-
Save jrosell/8810f763212adcc530e90ac982dce310 to your computer and use it in GitHub Desktop.
Combine multilayered_raster vtr using terra package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# combine_multylayered_raster_vtr.R: Combine multilayered_raster vtr using terra package | |
library(terra) | |
test <- TRUE | |
test_create_vrt_from_multilayers <- function() { | |
raster_objects <- create_multiplayered_rasters() | |
print(class(raster_objects[[1]]) == "SpatRaster") | |
print(class(raster_objects[[2]]) == "SpatRaster") | |
out <- create_vrt_from_multilayers(raster_objects) | |
print(out) | |
invisible(NULL) | |
} | |
create_multiplayered_rasters <- function() { | |
A <- rast( nrows=10, ncols=10, xmin=0, xmax=10 ) | |
B <- rast( nrows=10, ncols=10, xmin=0, xmax=10 ) | |
C <- rast( nrows=10, ncols=10, xmin=0, xmax=10 ) | |
values(A) <- 1:100 | |
values(B) <- 0:99 | |
values(C) <- rep(0,100) | |
raster1 <- rast(list(A,B,C)) | |
D <- rast( nrows=10, ncols=10, xmin=0, xmax=10 ) | |
E <- rast( nrows=10, ncols=10, xmin=0, xmax=10 ) | |
values(D) <- 101:200 | |
values(E) <- 100:199 | |
raster2 <- rast(list(D,E)) | |
return(list(raster1, raster2)) | |
} | |
create_vrt_from_multilayers <- function( | |
raster_objects, | |
output_directory = "rasters", | |
output_vrt_file = "rasters/stacked_rasters.vrt") { | |
layer_files <- save_raster_layers_files(raster_objects, output_directory) | |
layer_list <- lapply(layer_files, rast) | |
raster_stack <- rast(layer_list) | |
raster_file <- save_raster_file(raster_stack) | |
raster_vrt <- vrt(raster_file, filename = output_vrt_file, overwrite = TRUE) | |
str(raster_vrt) | |
return(raster_vrt) | |
} | |
save_raster_layers_files <- function(multilayered_rasters, output_directory) { | |
if (!all(sapply(multilayered_rasters, inherits, "SpatRaster"))) { | |
stop("Input must be a list of multilayered SpatRaster objects.") | |
} | |
if (!dir.exists(output_directory)) { | |
dir.create(output_directory) | |
} | |
all_raster_files <- list() | |
for (raster_index in 1:length(multilayered_rasters)) { | |
raster_obj <- multilayered_rasters[[raster_index]] | |
for (i in 1:nlyr(raster_obj)) { | |
file_name <- paste0("raster",raster_index, "_layer", i) | |
output_file <- file.path(output_directory, paste0(file_name,".tif")) | |
writeRaster(raster_obj[[i]], filename = output_file, overwrite = TRUE) | |
all_raster_files <- c(all_raster_files, output_file) | |
} | |
} | |
return(all_raster_files) | |
} | |
save_raster_file <- function(raster_obj, output_directory = "rasters", file_name = "raster.tif") { | |
if (!dir.exists(output_directory)) { | |
dir.create(output_directory) | |
} | |
output_file <- file.path(output_directory, file_name) | |
writeRaster(raster_obj, filename = output_file, overwrite = TRUE) | |
return(output_file) | |
} | |
if (test && interactive()){ | |
test_create_vrt_from_multilayers() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results here: https://gist.github.com/jrosell/c07d8c2d979f1d17a435efb75788012a