|
| 1 | +#' List collections at FAO GISManager |
| 2 | +#' |
| 3 | +#' This function lists available data collections from the FAO GISManager API. |
| 4 | +#' Only the code name used for other functions and the caption (e.g.) long name |
| 5 | +#' is returned. This function is not equivalent to \code{wapor_collection()} |
| 6 | +#' which returns the metadata of a *singular* data collection. |
| 7 | +#' |
| 8 | +#' @return A data.frame object with code and captions of the available collections. |
| 9 | +#' @export wapor_collections |
| 10 | +#' |
| 11 | +wapor_collections <- function(){ |
| 12 | + parsed = get_and_parse(dataurl) |
| 13 | + collections = do.call(rbind, lapply(parsed, function(x){ |
| 14 | + as.data.frame(x[c("code", "caption")]) |
| 15 | + })) |
| 16 | + collections |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +#' FAo GIS List available Products in collection |
| 21 | +#' |
| 22 | +#' This function lists all available datasets within a collection. The result |
| 23 | +#' is returned as a list where each element represents a distinct product and is |
| 24 | +#' named accordingly. One level lower, there are two objects one containing general |
| 25 | +#' information on the product and the other one containing additional metadata, |
| 26 | +#' such as spatio-temporal resolution, extent in space and time, coordinate system etc. |
| 27 | +#' |
| 28 | +#' @param collection A character vector of length one specifying the collection |
| 29 | +#' for which to retrieve available products- |
| 30 | +#' |
| 31 | +#' @return A list object with all available products. One product object contains |
| 32 | +#' of the two data.frames \code{product} and \code{meta} where meta can have |
| 33 | +#' variable column number due to differences in the available meta data. |
| 34 | +#' @export wapor_products |
| 35 | +#' |
| 36 | +#' @importFrom stringr str_remove_all |
| 37 | +wapor_products <-function(collection){ |
| 38 | + url = paste(dataurl, collection, "cubes", sep = "/") |
| 39 | + parsed = get_and_parse(url) |
| 40 | + |
| 41 | + products = lapply(parsed, function(x){ |
| 42 | + y = as.data.frame(x) |
| 43 | + product = y[, c("code", "caption", "description")] |
| 44 | + meta = y[grep("additional", names(y))] |
| 45 | + names(meta) = str_remove_all(names(meta), "additionalInfo.") |
| 46 | + list(product = product, meta = meta) |
| 47 | + }) |
| 48 | + |
| 49 | + prod_names = unlist(lapply(products, function(x) x$product$code)) |
| 50 | + names(products) = prod_names |
| 51 | + products |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +#' FAO GIS get product metadata |
| 56 | +#' |
| 57 | +#' This function retrieves metadata of a specific product within a collection. |
| 58 | +#' The returned object is a three-leveled list with the first object containing |
| 59 | +#' some general information on the product. The second object contains information |
| 60 | +#' of the dimensions the data set is associated with wile the third element contains |
| 61 | +#' additional metadata e.g. on spatio-temporal resolution or the methodology |
| 62 | +#' how the product was generated. |
| 63 | +#' |
| 64 | +#' @param collection A length one character vector specifying the collection. |
| 65 | +#' @param product A length one character vector specifying the product.. |
| 66 | +#' |
| 67 | +#' @return A list object with the obhects \code{info}, \code{dimensions}, and \code{meta}. |
| 68 | +#' @export wapor_productMETA |
| 69 | +#' |
| 70 | +#' @importFrom stringr str_remove_all |
| 71 | +wapor_productMETA <- function(collection, product){ |
| 72 | + |
| 73 | + url = paste(dataurl, collection, "cubes", product, "measures", sep = "/") |
| 74 | + parsed = get_and_parse(url) |
| 75 | + |
| 76 | + info = data.frame(code = parsed[[1]]$code, |
| 77 | + caption = parsed[[1]]$caption, |
| 78 | + unit = parsed[[1]]$unit, |
| 79 | + scale = parsed[[1]]$scale, |
| 80 | + multiplier = parsed[[1]]$multiplier) |
| 81 | + |
| 82 | + |
| 83 | + url = paste(dataurl, collection, "cubes", product, "dimensions", sep = "/") |
| 84 | + parsed = get_and_parse(url) |
| 85 | + |
| 86 | + dimensions = do.call(rbind, lapply(parsed, function(x){ |
| 87 | + data.frame(code = x$code, |
| 88 | + caption = x$caption, |
| 89 | + type = x$type) |
| 90 | + })) |
| 91 | + |
| 92 | + |
| 93 | + url = paste(dataurl, collection, "cubes", product, sep = "/") |
| 94 | + parsed = get_and_parse(url) |
| 95 | + meta = as.data.frame(parsed) |
| 96 | + meta = meta[grep("additional", names(meta))] |
| 97 | + names(meta) = str_remove_all(names(meta), "additionalInfo.") |
| 98 | + |
| 99 | + list(info = info, dimensions = dimensions, meta = meta) |
| 100 | +} |
0 commit comments