Skip to content

Commit 180b8df

Browse files
author
marus200
committed
first commit
0 parents  commit 180b8df

18 files changed

+1614
-0
lines changed

.Rbuildignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
^wapoR\.Rproj$
2+
^\.Rproj\.user$
3+
^LICENSE\.md$
4+
^data-raw$
5+
^README.rmd$

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.Rproj.user
2+
.README.rmd$

DESCRIPTION

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Package: wapoR
2+
Title: Access to the FAO GISManager API from R
3+
Version: 0.0.0.9000
4+
Authors@R:
5+
person(given = "Darius",
6+
family = "Görgen",
7+
role = c("aut", "cre"),
8+
email = "[email protected]")
9+
Description: The packages queries the FAO GISManager API at <https://io.apps.fao.org/gismgr/api/v1>
10+
for raster datasets and downloads queried products to a local directory. Currently,
11+
the package is in an experimental state and only supports downloading raster files.
12+
Other applications, such as zonal statistics or time-series of points are not supported, yet.
13+
License: GPL-3
14+
Encoding: UTF-8
15+
LazyData: true
16+
Roxygen: list(markdown = TRUE)
17+
RoxygenNote: 7.1.0
18+
Imports:
19+
httr,
20+
jsonlite,
21+
curl,
22+
attempt,
23+
purrr,
24+
sf,
25+
stringr
26+
Suggests:
27+
rnaturalearth

LICENSE.md

+595
Large diffs are not rendered by default.

NAMESPACE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(get_and_parse)
4+
export(wapor_collections)
5+
export(wapor_productMETA)
6+
export(wapor_products)
7+
export(wapor_queryRaster)
8+
export(wapor_signin)
9+
importFrom(httr,GET)
10+
importFrom(httr,POST)
11+
importFrom(httr,add_headers)
12+
importFrom(httr,content)
13+
importFrom(httr,http_error)
14+
importFrom(httr,status_code)
15+
importFrom(httr,write_disk)
16+
importFrom(jsonlite,toJSON)
17+
importFrom(sf,st_bbox)
18+
importFrom(stringr,str_remove_all)
19+
importFrom(stringr,str_replace_all)
20+
importFrom(stringr,str_sub)

R/collections.R

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)