/
register.R
299 lines (266 loc) · 9.59 KB
/
register.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
.pkgenv <- new.env(parent = emptyenv())
.pkgenv$resources <- list()
.pkgenv$indicators <- list()
.pkgenv$avail_resources <- list()
.onLoad <- function(libname, pkgname) {
.pkgenv$outdir <- tempfile()
dir.create(.pkgenv$outdir, showWarnings = FALSE)
.pkgenv$verbose <- TRUE
.pkgenv$aria_bin <- NULL
.pkgenv$testing <- FALSE
.pkgenv$chunk_size <- 100000
invisible()
}
#' Portfolio methods for mapme.biodiversity
#'
#' `mapme_options()` sets default options for mapme.biodiversity to control the
#' behavior of downstream functions.
#' Mainly, the output path as well as the temporal directory for intermediate
#' files can be set. Additionally, the verbosity can be set. The testing options
#' should not be set by users, as it controls the behavior of the package during
#' automated test pipelines. Might be extended by other options in the future.
#'
#' @param ... ignored
#' @param outdir A length one character indicating the output path.
#' @param chunk_size A numeric of length one giving the maximum chunk area in ha.
#' Defaults to 100,000 ha.
#' @param aria_bin A character vector to an aria2c executable for parallel
#' downloads.
#' @param verbose A logical, indicating if informative messages should be printed.
#' @param testing A logical. Not to be set by users. Controls the behavior
#' during automated test pipelines.
#' @return `mapme_options()` returns a list of options if no arguments are specified. Otherwise sets
#' matching arguments to new values in the package's internal environment.
#' @name mapme
#' @export
#'
#' @examples
#' library(mapme.biodiversity)
#' mapme_options()
mapme_options <- function(..., outdir, chunk_size, verbose, aria_bin, testing) {
if (!missing(outdir)) {
stopifnot(is.character(outdir) && length(outdir) == 1)
if (!dir.exists(outdir)) {
stop("outdir must point to an existing directory")
}
.pkgenv$outdir <- outdir
}
if (!missing(chunk_size)) {
stopifnot(length(chunk_size) == 1 && is.numeric(chunk_size))
.pkgenv$chunk_size <- chunk_size
}
if (!missing(verbose)) {
stopifnot(is.logical(verbose))
.pkgenv$verbose <- verbose
}
if (!missing(aria_bin)) {
.pkgenv$aria_bin <- .check_aria2(aria_bin)
}
if (!missing(testing)) {
stopifnot(is.logical(testing))
.pkgenv$testing <- testing
}
if (nargs() == 0) {
return(list(
outdir = .pkgenv$outdir,
chunk_size = .pkgenv$chunk_size,
verbose = .pkgenv$verbose,
aria_bin = .pkgenv$aria_bin,
testing = .pkgenv$testing
))
}
}
.check_aria2 <- function(aria_bin) {
aria_output <- try(system2(aria_bin, args = "--version", stdout = TRUE, stderr = FALSE), silent = TRUE)
if (inherits(aria_output, "try-error") | !grepl("aria2 version", aria_output[1])) {
warning(paste(
"Argument 'aria_bin' does not point to a executable aria2 installation.",
"The package will use R internal download utility."
))
aria_bin <- NULL
}
return(aria_bin)
}
.check_char <- function(obj, name) {
if (!inherits(obj, "character") || length(obj) > 1 || nchar(obj) == 0) {
stop(paste0(name, " needs to be a single charachter string"))
}
}
#' Register or list resources in mapme.biodiversity
#'
#' `register_resource()` is used to register a new resource function with base
#' information to the package's internal environment used to inform users about
#' available resources. Note, registering a custom resource will
#' only have effect for the current R session.
#'
#' @param name A character vector indicating the name of the resource.
#' @param description A character vector with a basic description
#' @param licence A character vector indicating the licence of the resource.
#' In case it is a custom licence, put a link to the licence text.
#' @param source Optional, preferably a URL where the data is found.
#' @param type A character vector indicating the type of the resource. Either
#' 'vector' or 'raster'.
#' @param source Optional, preferably a URL where the data is found.
#'
#' @return `register_resource()` is called for the side-effect of registering a resource.
#' @name resources
#' @export
#'
#' @examples
#' \dontrun{
#' register_resource(
#' name = "gfw_treecover",
#' description = "Global Forest Watch - Percentage of canopy closure in 2000",
#' licence = "CC-BY 4.0",
#' source = "https://data.globalforestwatch.org/documents/tree-cover-2000/explore",
#' type = "raster"
#' )
#' }
register_resource <- function(name = NULL,
description = NULL,
licence = NULL,
source = NULL,
type = NULL) {
if (any(is.null(name), is.null(description), is.null(licence), is.null(source), is.null(type))) {
stop("neither name, description, licence, source, nor type can be NULL")
}
.check_char(name, "name")
.check_char(description, "description")
.check_char(licence, "licence")
.check_char(source, "source")
.check_char(type)
if (name %in% names(.pkgenv$resources)) {
warning(paste("resource with name", name, "already registered"))
}
if (!type %in% c("vector", "raster")) {
stop("type needs to be one of 'vector' or 'raster'")
}
resource <- tibble(
name = name, description = description, licence = licence,
source = source, type = type
)
.pkgenv$resources <- rbind(.pkgenv$resources, resource)
}
#' Register or list indicators in mapme.biodiversity
#'
#' `register_indicator()` is used to register a new indicator function with base
#' information to the package's internal environment used to inform users about
#' available indicators. Note, registering a custom indicator will
#' only have effect for the current R session.
#' @param name A character vector indicating the name of the indicator.
#' @param description A character vector with a basic description
#' @param resources A character vector of the required resources
#' that need to be available to calculate the indicator. The names must
#' correspond with already registered resources.
#'
#' @return `register_indicator()` is called for the side-effect of registering
#' an indicator
#' @name indicators
#' @export
#'
#' @examples
#' \dontrun{
#' register_indicator(
#' name = "treecover_area",
#' description = "Area of forest cover by year",
#' resources = c(
#' "gfw_treecover",
#' "gfw_lossyear"
#' )
#' )
#' }
register_indicator <- function(name = NULL, description = NULL, resources = NULL) {
if (any(is.null(name), is.null(description), is.null(resources))) {
stop("neither name, description nor resources can be NULL")
}
.check_char(name, "name")
.check_char(description, "description")
if (name %in% names(.pkgenv$indicators)) {
warning(paste("indicator with name", name, "already registered"))
}
if (!inherits(resources, "character")) {
stop("resources needs to be a charachter vector")
}
indicator <- tibble(name = name, description = description, resources = list(resources))
.pkgenv$indicators <- rbind(.pkgenv$indicators, indicator)
}
#' Register or list resources in mapme.biodiversity
#'
#' `available_resources()` returns a tibble of registered resources with basic
#' information such as the source and the licence.
#'
#' @param resources If \code{NULL} returns a list of all resources (default).
#' Otherwise only the ones specified.
#'
#' @return `available_resources()` returns a tibble listing available resources.
#' @name resources
#' @export
#' @include register.R
#' @examples
#' available_resources()
available_resources <- function(resources = NULL) {
all_resources <- .pkgenv$resources
if (is.null(resources)) {
return(all_resources[order(all_resources[["name"]]), ])
} else {
if (any(!resources %in% all_resources[["name"]])) {
not_avail <- which(!resources %in% names(all_resources))
not_avail <- resources[not_avail]
msg <- sprintf(
"The following resources are not available:\n%s",
paste(not_avail, collpase = " ")
)
stop(msg)
}
all_resources[all_resources[["name"]] %in% resources, ]
}
}
#' Register or list indicators in mapme.biodiversity
#'
#' `available_indicators()` returns a tibble of registered indicators with basic
#' information such as the required resources.
#'
#' @param indicators If \code{NULL} returns a list of all registered indicators
#' (default). Otherwise only the ones specified.
#'
#' @return `available_resources()` returns a tibble listing available indicators.
#' @name indicators
#' @export
#' @include register.R
#' @examples
#' available_indicators()
available_indicators <- function(indicators = NULL) {
all_indicators <- .pkgenv$indicators
resources <- lapply(all_indicators[["resources"]], function(x) available_resources(x))
all_indicators[["resources"]] <- resources
if (is.null(indicators)) {
return(all_indicators[order(all_indicators[["name"]]), ])
} else {
if (any(!indicators %in% all_indicators[["name"]])) {
not_avail <- which(!indicators %in% names(all_indicators))
not_avail <- indicators[not_avail]
msg <- sprintf(
"The following indicators are not available:\n%s",
paste(not_avail, collpase = " ")
)
stop(msg)
}
all_indicators[all_indicators[["name"]] %in% indicators, ]
}
}
.add_resource <- function(resource) {
stopifnot(inherits(resource, "list"))
stopifnot(!is.null(names(resource)))
if (length(.pkgenv$avail_resources) == 0) {
.pkgenv$avail_resources <- resource
} else {
name <- names(resource)
.pkgenv$avail_resources[name] <- resource
}
}
.avail_resources <- function(name) {
return(.pkgenv$avail_resources)
}
.clear_resources <- function() {
.pkgenv$avail_resources <- list()
}