Skip to content

Commit

Permalink
saving prgress
Browse files Browse the repository at this point in the history
  • Loading branch information
wbagge committed Dec 28, 2023
1 parent 7d733c3 commit 4e13d5d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 9 deletions.
81 changes: 81 additions & 0 deletions R/get_slope_aspect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#' .. content for \description{} (no empty lines) ..
#'
#' .. content for \details{} ..
#'
#' @title
#' @param
#' @return
#' @author Whitney Bagge
#' @export
#'
#'
#' https://www.fao.org/soils-portal/data-hub/soil-maps-and-databases/harmonized-world-soil-database-v12/en/
library(archive)
get_slope_aspect<- function(slope_aspect_directory_dataset, slope_aspect_directory_raw, continent_raster_template) {

slope_aspect <- c("aspect_zero", "aspect_fortyfive", "aspect_onethirtyfive", "aspect_twotwentyfive", "aspect_undef",
"slope_zero", "slope_pointfive", "slope_two", "slope_five", "slope_ten", "slope_fifteen",
"slope_thirty", "slope_fortyfive")

for(asp in slope_aspect) {

url_out<- switch(asp, "aspect_zero" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloAspectClN_30as.rar",
"aspect_fortyfive" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloAspectClE_30as.rar",
"aspect_onethirtyfive" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloAspectClS_30as.rar",
"aspect_twotwentyfive" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloAspectClW_30as.rar",
"aspect_undef" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloAspectClU_30as.rar",
"slope_zero" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl1_30as.rar",
"slope_pointfive" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl2_30as.rar",
"slope_two" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl3_30as.rar",
"slope_five" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl4_30as.rar",
"slope_ten" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl5_30as.rar",
"slope_fifteen" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl6_30as.rar",
"slope_thirty" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl7_30as.rar",
"slope_fortyfive" = "https://www.fao.org/fileadmin/user_upload/soils/HWSD%20Viewer/GloSlopesCl8_30as.rar")

filename <- paste("data/slope_aspect/", asp, sep="", ".rar")

#download.file(url=url_out, destfile = filename)

rar_name <- file.path(dirname(filename), system2("unrar", c("lb", filename), stdout = TRUE))
system2("unrar", c("e", "-o+", filename, dirname(filename), ">/dev/null"))

GloAspectClN_30as <- rast("GloAspectClN_30as.asc")
GloAspectClE_30as <- rast("GloAspectClE_30as.asc")
GloAspectClS_30as <- rast("GloAspectClS_30as.asc")
GloAspectClW_30as <- rast("GloAspectClW_30as.asc")
GloAspectClU_30as <- rast("GloAspectClU_30as.asc")

raster_stack_aspect <- c(GloAspectClN_30as, GloAspectClE_30as, GloAspectClS_30as, GloAspectClW_30as, GloAspectClU_30as)

raster_max_aspect <- max(raster_stack_aspect, na.rm = TRUE)

transformed_raster <- transform_raster(raw_raster = rast(raster_max_aspect),
template = rast(continent_raster_template))



#if_else((str_detect(names(transformed_raster), "Aspect", negate=FALSE))==TRUE, c(transformed_raster), 0)
#if_else statement for aspect and one for slope

# Convert to dataframe
#dat_out<- as.data.frame(transformed_raster, xy = TRUE) |>
# as_tibble()

#dat_full <- full_join(dat_out, dat_out, by=c("x", "y"))


}

# Save as parquet
write_parquet(dat_full, "data/slope_aspect_dataset/slope_dataset", compression = "gzip", compression_level = 5)



return(slope_aspect_directory_dataset)

#go with highest percentage of pixels to make categorical variable for aspect 5 categories
#for slope - take the median 3-arc second slope; midpoint of the slope class


}
8 changes: 4 additions & 4 deletions R/preprocess_glw_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
library(raster)
library(terra)
preprocess_glw_data<- function(glw_directory_raw, glw_downloaded, continent_raster_template) {
preprocess_glw_data<- function(glw_directory_dataset, glw_directory_raw, glw_downloaded, continent_raster_template) {

transformed_raster_cat <- transform_raster(raw_raster = rast(paste0(glw_downloaded, "/url_cattle.tif")),
template = rast(continent_raster_template))
Expand All @@ -28,9 +28,9 @@ preprocess_glw_data<- function(glw_directory_raw, glw_downloaded, continent_rast
as_tibble()

# Save as parquet
write_parquet(dat_out_cat, "data/glw/glw_cattle", compression = "gzip", compression_level = 5)
write_parquet(dat_out_sh, "data/glw/glw_sheep", compression = "gzip", compression_level = 5)
write_parquet(dat_out_go, "data/glw/glw_goats", compression = "gzip", compression_level = 5)
write_parquet(dat_out_cat, "data/glw_dataset/glw_cattle", compression = "gzip", compression_level = 5)
write_parquet(dat_out_sh, "data/glw_dataset/glw_sheep", compression = "gzip", compression_level = 5)
write_parquet(dat_out_go, "data/glw_dataset/glw_goats", compression = "gzip", compression_level = 5)


return(glw_directory_raw)
Expand Down
18 changes: 13 additions & 5 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ static_targets <- tar_plan(
tar_target(soil_downloaded, soil_download(soil_directory_raw),
format = "file",
repository = "local"),
tar_target(soil_directory_dataset,
create_data_directory(directory_path = "data/soil_dataset")),
tar_target(soil_preprocessed,
preprocess_soil(soil_downloaded, continent_raster_template)),
preprocess_soil(soil_directory_dataset, soil_directory_raw, continent_raster_template, soil_downloaded)),

# SLOPE and ASPECT -------------------------------------------------
tar_target(slope_aspect_directory_raw,
create_data_directory(directory_path = "data/slope_aspect")),
tar_target(slope_aspect_downloaded, get_slope_aspect(slope_aspect_directory_raw, continent_polygon),
tar_target(slope_aspect_directory_dataset,
create_data_directory(directory_path = "data/slope_aspect_dataset")),
tar_target(slope_aspect_downloaded, get_slope_aspect(slope_aspect_directory_dataset, slope_aspect_directory_raw, continent_raster_template),
format = "file",
repository = "local"),

Expand All @@ -64,18 +68,22 @@ static_targets <- tar_plan(
tar_target(glw_downloaded, get_glw_data(glw_directory_raw),
format = "file",
repository = "local"),
tar_target(glw_directory_dataset,
create_data_directory(directory_path = "data/glw_dataset")),
tar_target(glw_preprocessed,
preprocess_glw_data(glw_directory_raw, glw_downloaded, continent_raster_template)),
preprocess_glw_data(glw_directory_dataset, glw_directory_raw, glw_downloaded, continent_raster_template)),


# ELEVATION -----------------------------------------------------------
tar_target(elevation_directory_raw,
create_data_directory(directory_path = "data/elevation")),
tar_target(elevation_downloaded, get_elevation(elevation_directory_raw),
tar_target(elevation_downloaded, get_elevation(elevation_directory_raw, overwrite = FALSE),
format = "file",
repository = "local"),
tar_target(elevation_directory_dataset,
create_data_directory(directory_path = "data/elevation_dataset")),
tar_target(elevation_preprocessed,
process_elevation(elevation_directory_raw, elevation_downloaded, continent_raster_template)),
process_elevation(elevation_directory_dataset, elevation_downloaded, elevation_directory_raw, continent_raster_template)),


)
Expand Down

0 comments on commit 4e13d5d

Please sign in to comment.