Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prodes clouds final corrections #246

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ sidra_download <- function(sidra_code = NULL, year, geo_level = "municipality",
code_micro <- NULL

# Obs: Sometimes there are non-catched municipalieis - user should check on IBGE SIDRA
# f = geo %>% filter(!(code_muni %in% unique(dat_uf$`Município (Código)`)))
# f = geo %>% filter(!(code_muni %in% unique(dat_uf$`MunicC-pio (CC3digo)`)))

# ----------------------------------------------------

## Download from Sidra IBGE

## Include Progress Bar
## Omit Warnings
## We should include support for microregion/mesoregion

# ------------------------------------------------------
----------------

##############################
## Setting Basic Parameters ##
Expand All @@ -28,6 +23,11 @@ sidra_download <- function(sidra_code = NULL, year, geo_level = "municipality",
param$sidra_code <- sidra_code
param$year <- year
param$classific <- classific
## Include Progress Bar
## Omit Warnings
## We should include support for microregion/mesoregion

# --------------------------------------
param$category <- category

if (geo_level == "country") {
Expand Down Expand Up @@ -418,7 +418,33 @@ external_download <- function(dataset = NULL, source = NULL, year = NULL,
"&disableProjSubset=on&addLatLon=true&accept=netcdf"
)
}


if (source == "prodes") {
if (year == 2016){
path = "https://drive.google.com/file/d/1bMDRs5-EYLQu-GRj6NREh9ukk_72HqKz/view?usp=sharing"
}
if (year == 2017){
path = "https://drive.google.com/file/d/1YvV-zSmdxuItNgHexzR5feTVozCZoS2t/view?usp=sharing"
}
if (year == 2018){
path = "https://drive.google.com/file/d/1k0BLOsFPnkW2LQQqO62ppWb6hcSPKsGA/view?usp=sharing"
}
if (year == 2019){
path = "https://drive.google.com/file/d/1OWFShUSk4mcg2G5HH_2gUnfvZgtTyf6Y/view?usp=sharing"
}
if (year == 2020){
path = "https://drive.google.com/file/d/1OggXwTbOjLo6w8sqiPI9PAj8TG4BlwVp/view?usp=sharing"
}
if (year == 2021){
path = "https://drive.google.com/file/d/1GbhWEPxXh9M_MHvYdoet-9CLDeLVFyuU/view?usp=sharing"
}
if (year == 2022){
path = "https://drive.google.com/file/d/1Ga0iVlq-F-kiUPwjQjt6n0h8dlSGyvcS/view?usp=sharing"
}
if (year == 2023){
path = "https://drive.google.com/file/d/1fbyFxLsxGygeDBaa7IKZf59IolqUdlHx/view?usp=sharing"
}
}
#######################
## Initiate Download ##
#######################
Expand All @@ -441,7 +467,11 @@ external_download <- function(dataset = NULL, source = NULL, year = NULL,
file_extension <- ".xlsx"
}
if (source == "prodes") {
file_extension <- ".txt"
if (dataset == "deforestation") {
file_extension <- ".txt"
}else{
file_extension <- ".rds"
}
}
if (source == "terraclimate") {
file_extension <- ".nc"
Expand Down Expand Up @@ -480,7 +510,8 @@ external_download <- function(dataset = NULL, source = NULL, year = NULL,

dir <- tempdir()
temp <- tempfile(fileext = file_extension, tmpdir = dir)



## Picking the way to download the file

download_method <- "standard" # works for most functions
Expand All @@ -502,6 +533,12 @@ external_download <- function(dataset = NULL, source = NULL, year = NULL,
download_method <- "googledrive"
}
}

if (source == "prodes") {
if (dataset == "cloud") {
download_method <- "googledrive"
}
}
if (source %in% c("deter", "terraclimate", "baci", "sigmine", "mapbiomas")) {
download_method <- "curl"
quiet <- FALSE
Expand All @@ -520,6 +557,7 @@ external_download <- function(dataset = NULL, source = NULL, year = NULL,
download_method <- "googledrive"
}
}


## Downloading file by the selected method

Expand Down Expand Up @@ -689,7 +727,8 @@ datasets_link <- function(source = NULL, dataset = NULL, url = FALSE) {
## PRODES

"prodes", "deforestation", NA, "2000-2022", "Municipality", "http://www.dpi.inpe.br/prodesdigital/tabelatxt.php?ano=2022&estado=&ordem=MUNICIPIO&type=tabela&output=txt&",

"prodes", "cloud", NA, NA, "Municipality", "https://drive.google.com/uc?export=download&id=1bMDRs5-EYLQu-GRj6NREh9ukk_72HqKz",

## DETER

"deter", "deter_amz", NA, "2016-2022", "Municipality", "http://terrabrasilis.dpi.inpe.br/file-delivery/download/deter-amz/shape",
Expand Down Expand Up @@ -969,4 +1008,4 @@ datasets_link <- function(source = NULL, dataset = NULL, url = FALSE) {
}

return(link)
}
}
117 changes: 62 additions & 55 deletions R/prodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description Loads data on deforestation in the Legal Amazon region.
#'
#' @param dataset A dataset name ("deforestation").
#' @param dataset A dataset name ("deforestation", "cloud").
#' @inheritParams load_baci
#'
#' @return A \code{tibble} with the selected data.
Expand All @@ -19,108 +19,115 @@
#'
#' @export

load_prodes <- function(dataset, raw_data = FALSE,
load_prodes <- function(dataset, year, raw_data = FALSE,
language = "eng") {
###########################
## Bind Global Variables ##
###########################

year <- municipio <- cod_ibge <- estado <- area_km2 <- increment <- NULL
municipio <- cod_ibge <- estado <- area_km2 <- increment <- NULL
municipality <- municipality_code <- state <- deforestation <- desmatamento2000 <- NULL

#############################
## Define Basic Parameters ##
#############################

param <- list()
param$source <- "prodes"
param$dataset <- dataset
param$raw_data <- raw_data
param$language <- language

param$year <- year

# check if dataset and time_period are supported

check_params(param)

###################
## Download Data ##
###################

## Column Names come with numbers at the side - we need to clean those

dat <- external_download(
dataset = param$dataset,
source = param$source
source = param$source,
year = param$year
)

## Return Raw Data

if (param$raw_data) {
return(dat)
}

######################
## Data Engineering ##
######################

# keep only deforestation-related variables

dat <- dat %>%
janitor::clean_names() %>%
dplyr::select(
municipio, cod_ibge, estado, area_km2, desmatamento2000, dplyr::starts_with("incremento")
)

# change to long format with increment variable

dat <- dat %>%
dplyr::rename("incremento2000" = "desmatamento2000") %>%
tidyr::pivot_longer(
dplyr::starts_with("incremento"),
names_prefix = "incremento",
names_to = "year",
values_to = "increment"
)

# calculating cumulative deforestation

dat <- dat %>%
dplyr::arrange(municipio, year) %>%
dplyr::mutate(
deforestation = cumsum(increment),
.by = municipio
)

dat <- dat %>%
dplyr::mutate(
increment = dplyr::case_when(
year == 2000 ~ NA,
.default = increment

# keep only deforestation-related variables if the dataset is "deforestation"

if (dataset == "deforestation") {
required_columns <- c("municipio", "cod_ibge", "estado", "area_km2", "desmatamento2000")

# Check if the required columns are present
missing_columns <- setdiff(required_columns, colnames(dat))
if (length(missing_columns) > 0) {
stop(paste("The following required columns are missing in the dataset:", paste(missing_columns, collapse = ", ")))
}

dat <- dat %>%
janitor::clean_names() %>%
dplyr::select(
municipio, cod_ibge, estado, area_km2, desmatamento2000, dplyr::starts_with("incremento")
)
)


# change to long format with increment variable
dat <- dat %>%
dplyr::rename("incremento2000" = "desmatamento2000") %>%
tidyr::pivot_longer(
dplyr::starts_with("incremento"),
names_prefix = "incremento",
names_to = "year",
values_to = "increment"
)

# calculating cumulative deforestation
dat <- dat %>%
dplyr::arrange(municipio, year) %>%
dplyr::mutate(
deforestation = cumsum(increment),
.by = municipio
) %>%
dplyr::mutate(
increment = dplyr::case_when(
year == 2000 ~ NA,
.default = increment
)
)
}

################################
## Harmonizing Variable Names ##
################################

if (param$language == "eng") {
dat_mod <- dat %>%
dplyr::rename(
"municipality" = municipio,
"municipality_code" = cod_ibge,
"state" = estado
)
}
if (param$language == "pt") {
} else if (param$language == "pt") {
dat_mod <- dat %>%
dplyr::rename(
"ano" = year,
"cod_municipio" = "cod_ibge",
"cod_municipio" = cod_ibge,
"uf" = estado,
"incremento" = increment,
"desmatamento" = deforestation
)
}

return(dat_mod)
}
Loading