Skip to content

Commit

Permalink
elevation download and processing
Browse files Browse the repository at this point in the history
  • Loading branch information
wbagge committed Nov 3, 2023
1 parent d6410a5 commit fbe73e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
30 changes: 25 additions & 5 deletions R/get_elevation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
17 changes: 13 additions & 4 deletions R/process_elevation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

0 comments on commit fbe73e9

Please sign in to comment.