generated from ecohealthalliance/container-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
184 additions
and
60 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' Title | ||
#' | ||
#' @param output_dir | ||
#' @param output_filename | ||
#' @param raster_template | ||
#' | ||
#' @return | ||
#' @export | ||
#' | ||
#' @examples | ||
get_bioclim_data <- function(output_dir, | ||
output_filename, | ||
raster_template) { | ||
|
||
template <- terra::unwrap(raster_template) | ||
bioclim_data <- geodata::worldclim_global(var = "bio", res = 2.5, path = output_dir) | ||
|
||
bioclim_data <- transform_raster(bioclim_data, template) | ||
|
||
bioclim_names <- c( | ||
"Annual_Mean_Temperature", "Mean_Diurnal_Range", "Isothermality", | ||
"Temperature_Seasonality", "Max_Temperature_of_Warmest_Month", | ||
"Min_Temperature_of_Coldest_Month", "Temperature_Annual_Range", | ||
"Mean_Temperature_of_Wettest_Quarter", "Mean_Temperature_of_Driest_Quarter", | ||
"Mean_Temperature_of_Warmest_Quarter", "Mean_Temperature_of_Coldest_Quarter", | ||
"Annual_Precipitation", "Precipitation_of_Wettest_Month", | ||
"Precipitation_of_Driest_Month", "Precipitation_Seasonality", | ||
"Precipitation_of_Wettest_Quarter", "Precipitation_of_Driest_Quarter", | ||
"Precipitation_of_Warmest_Quarter", "Precipitation_of_Coldest_Quarter") | ||
|
||
# Assign the new names to the layers | ||
names(bioclim_data) <- bioclim_names | ||
|
||
filename = paste(output_dir, output_filename, sep = "/") | ||
|
||
if(grepl("\\.parquet", filename)) { | ||
# Convert to dataframe | ||
dat <- as.data.frame(bioclim_data, xy = TRUE) |> as_tibble() | ||
|
||
# Save as parquet | ||
arrow::write_parquet(dat, filename, compression = "gzip", compression_level = 5) | ||
|
||
terra::writeRaster(bioclim_data, filename=gsub("parquet", "tif", filename), overwrite=T, gdal=c("COMPRESS=LZW")) | ||
|
||
} else { | ||
terra::writeRaster(bioclim_data, filename=filename, overwrite=T, gdal=c("COMPRESS=LZW")) | ||
} | ||
|
||
unlink(paste(output_dir, "climate", sep = "/"), recursive=TRUE) | ||
|
||
return(filename) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Title | ||
#' | ||
#' @param output_dir | ||
#' @param output_filename | ||
#' @param raster_template | ||
#' | ||
#' @return | ||
#' @export | ||
#' | ||
#' @examples | ||
get_elevation_data <- function(output_dir, | ||
output_filename, | ||
raster_template) { | ||
|
||
# Create directory if it does not yet exist | ||
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE) | ||
|
||
template <- terra::unwrap(raster_template) | ||
|
||
# Create a bounding bounding box template | ||
elevation_data <- geodata::elevation_global(res = 0.5 , | ||
path = output_dir) | ||
|
||
elevation_data <- transform_raster(elevation_data, | ||
template = template) | ||
|
||
filename = paste(output_dir, output_filename, sep = "/") | ||
|
||
if(grepl("\\.parquet", filename)) { | ||
# Convert to dataframe | ||
dat <- as.data.frame(elevation_data, xy = TRUE) |> as_tibble() | ||
|
||
# Save as parquet | ||
arrow::write_parquet(dat, filename, compression = "gzip", compression_level = 5) | ||
|
||
terra::writeRaster(elevation_data, filename=gsub("parquet", "tif", filename), overwrite=T, gdal=c("COMPRESS=LZW")) | ||
|
||
} else { | ||
terra::writeRaster(elevation_data, filename=filename, overwrite=T, gdal=c("COMPRESS=LZW")) | ||
} | ||
|
||
unlink(paste(output_dir, "elevation", sep = "/"), recursive=TRUE) | ||
|
||
# Return path to compressed raster | ||
return(filename) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
get_landcover_data <- function(output_dir, | ||
output_filename, | ||
raster_template) { | ||
|
||
template <- terra::unwrap(raster_template) | ||
|
||
landcover_types <- c("trees", "grassland", "shrubs", "cropland", "built", "bare", "snow", "water", "wetland", "mangroves", "moss") | ||
|
||
# Fetch each layer, process them and stack them into a single SpatRaster | ||
# Cleaning up files as we go to save space. | ||
landcover_data <- map(landcover_types, function(l) { | ||
landcover <- geodata::landcover(var = l, path = output_dir) | ||
file <- sources(landcover) | ||
landcover <- transform_raster(landcover, template) | ||
unlink(file) | ||
landcover | ||
}) | ||
|
||
landcover_data <- do.call(c, landcover_data) | ||
|
||
filename = paste(output_dir, output_filename, sep = "/") | ||
|
||
if(grepl("\\.parquet", filename)) { | ||
# Convert to dataframe | ||
dat <- as.data.frame(landcover_data, xy = TRUE) |> as_tibble() | ||
|
||
# Save as parquet | ||
arrow::write_parquet(dat, filename, compression = "gzip", compression_level = 5) | ||
|
||
terra::writeRaster(landcover_data, filename=gsub("parquet", "tif", filename), overwrite=T, gdal=c("COMPRESS=LZW")) | ||
|
||
} else { | ||
terra::writeRaster(landcover_data, filename=filename, overwrite=T, gdal=c("COMPRESS=LZW")) | ||
} | ||
|
||
unlink(paste(output_dir, "landuse", sep = "/"), recursive=TRUE) | ||
|
||
return(filename) | ||
} |
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
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
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
Empty file.
Binary file not shown.
Binary file not shown.
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