diff --git a/R/get_elevation.R b/R/get_elevation.R index 0a0f458..191a217 100644 --- a/R/get_elevation.R +++ b/R/get_elevation.R @@ -7,11 +7,31 @@ #' @return #' @author Whitney Bagge #' @export -get_glw <- function() { +library(paws) +get_elevation<- function(elevation_directory_raw, overwrite=FALSE) { - url_cattle <- "https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/LHBICE#" - url_cattle_out <- GET(url_cattle) - unzipped_glw_cattle <- unzip(url_cattle_out) - Aw_layer <- if_else(str_detect(names(unzipped_glw_cattle), "Aw")==TRUE, ) + existing_files <- list.files(elevation_directory_raw) + + download_filename <- tools::file_path_sans_ext(existing_files) + + save_filename <- paste0(download_filename, ".tif") + + message(paste0("Downloading ", download_filename)) + + if(save_filename %in% existing_files & !overwrite) { + message("file already exists, skipping download") + return(file.path(elevation_directory_raw, save_filename)) # skip if file exists + } + + s3_svc <- s3(config = list(region = "af-south-1", credentials = list(anonymous = TRUE))) + # Download the file directly to disk in the current working directory + s3_svc$download_file( + Bucket = "deafrica-input-datasets", + Key = "srtm_dem/srtm_africa.tif", + Filename = "data/elevation/srtm_africa.tif" + ) + + + return(elevation_directory_raw) } \ No newline at end of file diff --git a/R/process_elevation.R b/R/process_elevation.R index 6714755..6e9ff30 100644 --- a/R/process_elevation.R +++ b/R/process_elevation.R @@ -7,11 +7,20 @@ #' @return #' @author Whitney Bagge #' @export -process_elevation <- function(elevation_layer_raw, bounding_boxes) { +process_elevation<- function(elevation_directory_raw, elevation_directory_dataset, elevation_downloaded, continent_raster_template) { - extent_object <- extent(bounding_boxes) - elevation_layer_raw_out <- crop(elevation_layer_raw, extent_object) - return(elevation_layer_raw_out) + transformed_raster <- transform_raster(raw_raster = rast("data/elevation/srtm_africa.tif"), + template = rast(continent_raster_template)) + + # Convert to dataframe + dat_out2 <- as.data.frame(transformed_raster, xy = TRUE) |> + as_tibble() + + # Save as parquet + write_parquet(dat_out2, "data/elevation_dataset/elevation_dataset", compression = "gzip", compression_level = 5) + + + return(elevation_directory_dataset) }