Skip to content

Commit

Permalink
saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
wbagge committed Aug 18, 2023
1 parent 4e29dcc commit dd7154d
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 1,483 deletions.
35 changes: 29 additions & 6 deletions R/get_elevation.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,32 @@
#' @export
get_glw <- function() {

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, )

}
url_elevation <- ""
url_elevation_out <- GET(url_elevation)
unzipped_elevation <- unzip(url_elevation_out)

}

# Install and load required packages
install.packages("aws.s3")
library(aws.s3)
install.packages("paws")
library(paws)

renv::install("ecohealthalliance/containerTemplateUtils")

library(containerTemplateUtils)

# Set the AWS region
region <- "af-south-1"

# Set the S3 bucket name and prefix
bucket_name <- "deafrica-input-datasets"
prefix <- "" # Empty prefix to fetch all objects, or specify a specific prefix

Sys.setenv("AWS_REGION"="af-south-1")
aws_s3_download(path="data", bucket=bucket_name, key="")




20 changes: 14 additions & 6 deletions R/get_glw.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
#' @author Whitney Bagge
#' @export
get_glw <- function() {

taxa <- c("url_cattle","url_sheep","url_goats")

for(i in 1:length(taxa)) {

url_out <- switch(taxa[i], "url_cattle" = "https://dataverse.harvard.edu/api/access/datafile/6769710",
"url_sheep" = "https://dataverse.harvard.edu/api/access/datafile/6769629",
"url_goats" = "https://dataverse.harvard.edu/api/access/datafile/6769692"
)

url_cattle <- "https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/LHBICE#"
#taxa cattle sheep goat have three urls here switch function (like if_else); have the three targets
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, )
url_taxa_out<-download.file(url_out, destfile = paste("data/",taxa[i],sep="",".tif"))

}


}
}
17 changes: 0 additions & 17 deletions R/get_grazingcap.R

This file was deleted.

10 changes: 7 additions & 3 deletions R/preprocess_glw.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
#' @return
#' @author Whitney Bagge
#' @export
preprocess_glw <- function(glw_layer, bounding_boxes) {
preprocess_glw <- function(bounding_boxes) {

extent_object <- extent(bounding_boxes)
glw_layer_out <- crop(glw_layer, extent_object)
taxa_tif <- c("url_cattle.tif","url_sheep.tif","url_goats.tif")

for(i in 1:length(taxa_tif)) {
extent_object <- raster::extent(bounding_boxes)
glw_layer_out <- raster::crop(taxa_tif, extent_object)
return(glw_layer_out)

}
}
37 changes: 37 additions & 0 deletions R/preprocess_soil.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' .. content for \description{} (no empty lines) ..
#'
#' .. content for \details{} ..
#'
#' @title
#' @param bounding_boxes
#' @return
#' @author Whitney Bagge
#' @export

preprocess_soil <- function(unzipped_soil, bounding_boxes) {

library(terra)
library(sf)
library(RSQLite)

hwsd <- rast("./data/soil/HWSD2.bil")

hwsd_bounded <- terra::crop(hwsd, bounding_boxes)

print(paste("UTM zone:", utm.zone <-
floor(((sf::st_bbox(hwsd_bounded)$xmin +
sf::st_bbox(hwsd_bounded)$xmax)/2 + 180)/6)
+ 1))

(epsg <- 32600 + utm.zone)

hwsd_bounded.utm <- project(hwsd_bounded, paste0("EPSG:", epsg), method = "near")


# m <- dbDriver("SQLite")
# con <- dbConnect(m, dbname="HWSD2.sqlite")
# dbListTables(con)
#
# print(dbGetQuery(con, "select * from HWSD2_SMU_METADATA"), width=100)
#
}
1 change: 0 additions & 1 deletion R/process_elevation.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ process_elevation <- function(elevation_layer_raw, bounding_boxes) {

extent_object <- extent(bounding_boxes)
elevation_layer_raw_out <- crop(elevation_layer_raw, extent_object)

return(elevation_layer_raw_out)

}
16 changes: 0 additions & 16 deletions R/process_grazingcap.R

This file was deleted.

35 changes: 35 additions & 0 deletions R/soil_download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#' .. content for \description{} (no empty lines) ..
#'
#' .. content for \details{} ..
#'
#' @title
#' @param
#' @return
#' @author Whitney Bagge
#' @export
soil_download <- function() {

options(timeout=200)

location <- c("soil_raster", "soil_database")

for(i in 1:length(location)) {

url_out <- switch(location[i], "soil_raster" = "https://s3.eu-west-1.amazonaws.com/data.gaezdev.aws.fao.org/HWSD/HWSD2_RASTER.zip",
"soil_database" = "https://www.isric.org/sites/default/files/HWSD2.sqlite")


file_name <- paste("data/soil/",location[i],sep="",".zip")

download.file(url=url_out, destfile = file_name)

unzipped_soil <- unzip(file_name, exdir = "data/soil/")



return("data/soil/")

}

}

33 changes: 15 additions & 18 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,25 @@ source_targets <- tar_plan(
format = "file"
),

## SOIL


## GLW

tar_target(glw_cattle, get_glw()),
tar_target(glw_sheep, get_glw()),
tar_target(glw_goats, get_glw()),
tar_target(glw_cattle_preprocessed, preprocess_glw(glw_cattle, bounding_boxes)),
#need to cache

#tar_target(glw_cattle, get_glw(url_cattle)),
#tar_target(glw_sheep, get_glw(url_sheep)),
#tar_target(glw_goats, get_glw(url_goats)),
#tar_target(glw_cattle_preprocessed, preprocess_glw(glw_cattle, bounding_boxes, download_directory = "data/glw")),
#tar_target(glw_sheep_preprocessed, preprocess_glw(glw_sheep, bounding_boxes, download_directory = "data/glw")),
#tar_target(glw_goats_preprocessed, preprocess_glw(glw_goats, bounding_boxes, download_directory = "data/glw")),

## ELEVATION

tar_target(elevation_layer_raw, get_elevation()),
tar_target(elevation_layer_processed,
process_elevation(elevation_layer_processed, bounding_boxes)),
#need to cache

## GRAZING CAPACITY

tar_target(grazingcap_raw, get_grazingcap()),
tar_target(grazingcap_processed,
process_grazingcap(grazingcap_layer_processed, bounding_boxes)),
#need to cache

#tar_target(elevation_layer_raw, get_elevation()),
#tar_target(elevation_layer_processed,
# process_elevation(elevation_layer_processed, bounding_boxes)),


)

# Data Processing -----------------------------------------------------------
Expand Down
Loading

0 comments on commit dd7154d

Please sign in to comment.