Skip to content

Commit e65ae9d

Browse files
authored
Merge pull request #3 from mapme-initiative/main
update
2 parents 5748c6d + e2d5ce5 commit e65ae9d

19 files changed

+136
-97
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ vignettes/assets
3434
^cran-comments\.md$
3535
^CRAN-SUBMISSION$
3636
^data-raw$
37+
^extras$

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: mapme.biodiversity
22
Title: Efficient Monitoring of Global Biodiversity Portfolios
3-
Version: 0.9.5
3+
Version: 0.9.5.9000
44
Authors@R: c(person(given = "Darius A.",
55
family = "G\u00F6rgen",
66
role = "aut",

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ importFrom(httr2,req_perform)
103103
importFrom(httr2,req_retry)
104104
importFrom(httr2,request)
105105
importFrom(httr2,resp_body_json)
106-
importFrom(httr2,resp_check_status)
107106
importFrom(httr2,resp_status)
108107
importFrom(magrittr,"%>%")
109108
importFrom(stats,sd)

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# mapme.biodiversity 0.9.5.9000
2+
3+
## General
4+
5+
- modified `get_acled()` to align with the new ACLED API version ([440](https://github.com/mapme-initiative/mapme.biodiversity/issues/440))
6+
- updated tests related to ACLED resource to reflect the changes in the `get_acled()` function
7+
18
# mapme.biodiversity 0.9.5
29

310
## General

R/calc_biodiversity_intactness_index.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ calc_biodiversity_intactness_index <- function() {
5757
mean_bii <- exactextractr::exact_extract(biodiversity_intactness_index, x, fun = "mean")
5858

5959
results <- tibble::tibble(
60-
datetime = as.POSIXct("2005-01-01T00:00:00Z"),
60+
datetime = as.POSIXct("2015-01-01T00:00:00Z"),
6161
variable = "biodiversity_intactness_index",
6262
unit = "unitless",
6363
value = mean_bii

R/calc_exposed_population_acled.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
#' get_worldpop(years = 2000)
146146
#' ) %>%
147147
#' calc_indicators(
148-
#' conflict_exposure_acled(
148+
#' calc_exposed_population_acled(
149149
#' distance = 5000,
150150
#' years = 2000,
151151
#' precision_location = 1,
@@ -207,7 +207,7 @@ calc_exposed_population_acled <- function(
207207
}
208208
}
209209

210-
years <- check_available_years(years, c(1997:2024), "exposed_population_acled")
210+
years <- check_available_years(years, c(1997:as.integer(format(Sys.Date(), "%Y"))), "exposed_population_acled")
211211

212212
function(x,
213213
acled = NULL,

R/calc_fatalities_acled.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ calc_fatalities_acled <- function(
8989
stratum = c("event_type", "sub_event_type", "disorder_type"),
9090
precision_location = 1,
9191
precision_time = 1) {
92-
years <- check_available_years(years, c(1997:2024), "acled")
92+
years <- check_available_years(years, c(1997:as.integer(format(Sys.Date(), "%Y"))), "acled")
9393
stratum <- match.arg(stratum)
9494

9595
if (!precision_location %in% 1:3) {

R/get_acled.R

Lines changed: 81 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
#' test conflict scenarios, and makes both data and analysis open for free use
99
#' by the public.*
1010
#'
11-
#' In order to access data from the ACLED API, you first must register an
12-
#' an account. Note, that the ACLED API used here provides a *living database*
11+
#' In order to access data from the ACLED API, you first must register
12+
#' an account. Make sure you register with your institutional domain
13+
#' (e.g., organization, university, or company) email address rather than
14+
#' your personal email address, in order to be able to use the API,
15+
#' as explained [here](https://acleddata.com/myacled-faqs).
16+
#' Note that the ACLED API provides a *living database*
1317
#' with single events being altered or removed altogether over time.
1418
#'
1519
#' @name acled
1620
#' @param years A numeric vector specifying the years for which to make
17-
#' ACLED data available (between 1997 and today). Defaults to 2000.
18-
#' @param key ACLED API key obtained by registering with ACLED (see Details).
21+
#' ACLED data available (between 1997 and today).
1922
#' @param email Email addressed used to register with ACLED (see Details).
23+
#' @param password Password used to register with ACLED (see Details).
2024
#' @param accept_terms A logical indicating if you agree to abide by ACLED's terms
2125
#' of use. Defaults to FALSE, thus must be manually set to TRUE.
2226
#' @keywords resource
@@ -27,115 +31,129 @@
2731
#' \doi{doi:10.1057/s41599-023-01559-4}
2832
#' @source Armed Conflict Location & Event Data Project (ACLED).
2933
#' @include register.R
30-
#' @importFrom httr2 request req_perform resp_check_status resp_body_json
34+
#' @importFrom httr2 request req_perform resp_body_json
3135
#' @export
3236
get_acled <- function(
33-
years = 2000,
34-
key = Sys.getenv("ACLED_ACCESS_KEY"),
35-
email = Sys.getenv("ACLED_ACCESS_EMAIL"),
37+
years,
38+
email = Sys.getenv("ACLED_EMAIL"),
39+
password = Sys.getenv("ACLED_PASSWORD"),
3640
accept_terms = FALSE) {
41+
# check input arguments
3742
if (!accept_terms) {
38-
msg <- "Please read and agree to ACLED's Terms of Use here:\nhttps://acleddata.com/terms-of-use/"
43+
msg <- "Please read and agree to ACLED's Terms of Use here:\nhttps://acleddata.com/terms-and-conditions"
3944
stop(msg)
4045
} else {
41-
msg <- "You agreed to abide to ACLED's Terms of Use (https://acleddata.com/terms-of-use/)."
42-
message(msg)
46+
if (isTRUE(mapme_options()[["verbose"]])) {
47+
msg <- "You agreed to abide to ACLED's Terms of Use (https://acleddata.com/terms-and-conditions)."
48+
message(msg)
49+
}
4350
}
44-
4551
if (is.null(email) | email == "") {
4652
msg <- "Please specify your email registered with ACLED."
4753
stop(msg)
4854
}
49-
50-
if (is.null(key) | key == "") {
51-
msg <- "Please specify your API key registered with ACLED."
55+
if (is.null(password) | password == "") {
56+
msg <- "Please specify your password registered with ACLED."
5257
stop(msg)
5358
}
54-
55-
years <- check_available_years(years, 1997:2024)
59+
years <- check_available_years(years, 1997:as.integer(format(Sys.Date(), "%Y")))
5660

5761
function(x,
5862
name = "acled",
5963
type = "vector",
6064
outdir = mapme_options()[["outdir"]],
6165
verbose = mapme_options()[["verbose"]]) {
66+
# token authorization url
67+
.token_url <- "https://acleddata.com/oauth/token"
6268
acled_yearly <- purrr::map_chr(years, function(year) {
6369
filename <- sprintf("acled_events_%s.gpkg", year)
64-
70+
# make full path to output file
6571
if (is.null(outdir)) {
6672
filename <- file.path(tempdir(), filename)
6773
} else {
6874
filename <- file.path(outdir, filename)
6975
}
70-
76+
# if already available just return the file name
7177
if (spds_exists(filename)) {
7278
return(filename)
7379
}
74-
75-
base_url <- .prep_acled_url(key, email, year)
80+
# start building the request
81+
base_url <- "https://acleddata.com/api/acled/read"
82+
req <- httr2::request(base_url)
83+
# add year as query parameter
84+
req <- httr2::req_url_query(req, year = year)
85+
# authenticate the request
86+
req <- httr2::req_oauth_password(
87+
req,
88+
client = httr2::oauth_client("acled", .token_url),
89+
username = email,
90+
password = password
91+
)
92+
# prepare for page iteration
7693
next_page <- TRUE
7794
page <- 1
78-
data <- NULL
79-
95+
data_lst <- NULL
96+
running_count <- 0L
97+
# iterate pages
8098
while (next_page) {
81-
page_url <- paste0(base_url, sprintf("&page=%s", page))
82-
83-
req <- request(page_url)
84-
rsp <- req_perform(req)
85-
resp_check_status(rsp)
86-
cnt <- resp_body_json(rsp)
87-
88-
if (cnt$status != 200) {
89-
stop("ACLED API query failed with message:\n", cnt$error$message)
90-
}
91-
92-
if (cnt$count == 0) {
99+
req <- httr2::req_url_query(req, page = page)
100+
resp <- httr2::req_perform(req)
101+
cnt <- httr2::resp_body_json(resp, simplifyVector = TRUE, flatten = TRUE)
102+
# check if we still have some data
103+
count <- cnt$count
104+
if (count == 0L) {
93105
next_page <- FALSE
94106
next
95107
}
96-
97-
events <- purrr::map(cnt$data, function(y) as.data.frame(y))
98-
events <- purrr::list_rbind(events)
99-
events <- st_as_sf(events,
100-
coords = c("longitude", "latitude"),
101-
crs = st_crs("EPSG:4326")
102-
)
103-
108+
# increment running count
109+
running_count <- running_count + count
110+
# get data as data.frame
111+
events <- cnt$data
112+
# store events data.frame in list
104113
if (page == 1) {
105-
data <- list(events)
114+
data_lst <- list(events)
115+
total_count <- cnt$total_count
106116
} else {
107-
data[[page]] <- events
117+
data_lst[[page]] <- events
108118
}
109-
119+
# increment page number
110120
page <- page + 1
111121
}
112-
113-
if (is.null(data)) {
122+
# check everything OK
123+
if (running_count != total_count) {
124+
msg <- sprintf("ACLED API returned only %d events out of %d.", running_count, total_count)
125+
stop(msg)
126+
}
127+
if (is.null(data_lst)) {
114128
stop("ACLED API returned 0 events.")
115129
}
116-
117-
data <- st_sf(tibble::as_tibble(purrr::list_rbind(data)))
118-
write_sf(data, filename)
130+
# rbind list into global data.frame
131+
data <- purrr::list_rbind(data_lst)
132+
# convert to sf object
133+
data <- sf::st_as_sf(data,
134+
coords = c("longitude", "latitude"),
135+
crs = sf::st_crs("EPSG:4326"))
136+
# convert all non-geometry columns to character to be compatible with previous version
137+
# temporarily extract geometry
138+
geom <- sf::st_geometry(data)
139+
# convert non-geometry columns
140+
data_df <- sf::st_drop_geometry(data)
141+
data_df[] <- lapply(data_df, as.character)
142+
# reattach geometry
143+
data <- sf::st_sf(data_df, geom)
144+
# write the file and return file name
145+
sf::write_sf(data, filename)
119146
return(filename)
120147
})
121-
148+
# make footprint
122149
bbox <- c(xmin = -180.0, ymin = -90.0, xmax = 180.0, ymax = 90.0)
123-
fps <- st_as_sfc(st_bbox(bbox, crs = "EPSG:4326"))
124-
fps <- st_as_sf(rep(fps, length(acled_yearly)))
150+
fps <- sf::st_as_sfc(sf::st_bbox(bbox, crs = "EPSG:4326"))
151+
fps <- sf::st_as_sf(rep(fps, length(acled_yearly)))
125152
fps[["source"]] <- acled_yearly
126153
fps <- make_footprints(fps, what = "vector")
127154
}
128155
}
129156

130-
.prep_acled_url <- function(key, email, year) {
131-
url <- "https://api.acleddata.com/acled/read?"
132-
url <- paste0(url, "terms=accept")
133-
url <- paste0(url, "&key=", key)
134-
url <- paste0(url, "&email=", email)
135-
url <- paste0(url, "&year=", year)
136-
url
137-
}
138-
139157
register_resource(
140158
name = "acled",
141159
description = "Armed Conflict Location & Event Data (ACLED)",

R/get_key_biodiversity_areas.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#' Rainforest Trust, Royal Society for the Protection of Birds, Wildlife
2121
#' Conservation Society and World Wildlife Fund. Available at
2222
#' www.keybiodiversityareas.org.
23-
#' @source \url{https://www.keybiodiversityareas.org/kba-data}
23+
#' @source \url{https://www.keybiodiversityareas.org/request-gis-data}
2424
#' @include register.R
2525
#' @export
2626
get_key_biodiversity_areas <- function(path = NULL) {

R/get_mcd64A1.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ get_mcd64a1 <- function(years = 2000:2022) {
7878
register_resource(
7979
name = "mcd64a1",
8080
description = "MODIS Burned Area Monthly Product (Aqua and Terra)",
81-
licence = "https://lpdaac.usgs.gov/data/data-citation-and-policies/",
81+
licence = "https://www.earthdata.nasa.gov/engage/open-data-services-software-policies/data-use-guidance",
8282
source = "https://planetarycomputer.microsoft.com/dataset/modis-64A1-061",
8383
type = "raster"
8484
)

0 commit comments

Comments
 (0)