|
| 1 | +#' @title Collections functions |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' These functions provide support to work with |
| 5 | +#' `doc_collections`objects. |
| 6 | +#' |
| 7 | +#' \itemize{ |
| 8 | +#' \item `collections_length()`: `r lifecycle::badge('experimental')` |
| 9 | +#' shows how many items there are in the `doc_items` object. |
| 10 | +#' |
| 11 | +#' \item `collections_matched()`: `r lifecycle::badge('experimental')` |
| 12 | +#' shows how many items matched the search criteria. |
| 13 | +#' |
| 14 | +#' \item `collections_fetch()`: `r lifecycle::badge('experimental')` |
| 15 | +#' request all STAC Items through pagination. |
| 16 | +#' |
| 17 | +#' \item `collections_next()`: `r lifecycle::badge('experimental')` |
| 18 | +#' fetches a new page from STAC service. |
| 19 | +#' |
| 20 | +#' } |
| 21 | +#' |
| 22 | +#' @param collections a `doc_collections` object. |
| 23 | +#' |
| 24 | +#' @param matched_field a `character` vector with the path |
| 25 | +#' where is the number of collections returned. |
| 26 | +#' |
| 27 | +#' @param progress a `logical` indicating if a progress bar must be |
| 28 | +#' shown or not. Defaults to `TRUE`. |
| 29 | +#' |
| 30 | +#' @param ... additional arguments. See details. |
| 31 | +#' |
| 32 | +#' @details |
| 33 | +#' Ellipsis argument (`...`) appears in different items functions and |
| 34 | +#' has distinct purposes: |
| 35 | +#' |
| 36 | +#' \itemize{ |
| 37 | +#' \item `collections_fetch()` and `collections_next()`: ellipsis is used to |
| 38 | +#' pass additional `httr` options to [GET][httr::GET] method, such as |
| 39 | +#' [add_headers][httr::add_headers] or [set_cookies][httr::set_cookies]. |
| 40 | +#' |
| 41 | +#' } |
| 42 | +#' |
| 43 | +#' @return |
| 44 | +#' |
| 45 | +#' \itemize{ |
| 46 | +#' \item `collections_length()`: an `integer` value. |
| 47 | +#' |
| 48 | +#' \item `collections_matched()`: returns an `integer` value if the STAC web |
| 49 | +#' server does support this extension. Otherwise returns `NULL`. |
| 50 | +#' |
| 51 | +#' \item `collections_fetch()`: a `doc_items` with all matched items. |
| 52 | +#' |
| 53 | +#' \item `collections_next()`: fetches a new page from STAC service. |
| 54 | +#' |
| 55 | +#' } |
| 56 | +#' |
| 57 | +#' @examples |
| 58 | +#' \dontrun{ |
| 59 | +#' # doc_items object |
| 60 | +#' stac("https://cmr.earthdata.nasa.gov/stac/LPCLOUD") |> |
| 61 | +#' collections() |> |
| 62 | +#' get_request() |> |
| 63 | +#' collections_fetch() |
| 64 | +#' } |
| 65 | +#' |
| 66 | +#' @name collections_functions |
| 67 | +NULL |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +#' @rdname collections_functions |
| 72 | +#' |
| 73 | +#' @export |
| 74 | +collections_next <- function(collections, ...) { |
| 75 | + check_collection(collections) |
| 76 | + # get url of the next page |
| 77 | + rel <- NULL |
| 78 | + next_link <- links(collections, rel == "next") |
| 79 | + if (length(next_link) == 0) |
| 80 | + .error("Cannot get next link URL.", class = "next_error") |
| 81 | + next_link <- next_link[[1]] |
| 82 | + res <- make_get_request( |
| 83 | + url = next_link$href, |
| 84 | + headers = next_link$headers, |
| 85 | + ..., |
| 86 | + error_msg = "Error while requesting next page" |
| 87 | + ) |
| 88 | + content <- content_response_json(res) |
| 89 | + # return items |
| 90 | + doc_collections(content) |
| 91 | +} |
| 92 | + |
| 93 | +#' @rdname collections_functions |
| 94 | +#' |
| 95 | +#' @export |
| 96 | +collections_matched <- function(collections, matched_field) { |
| 97 | + check_collections(collections) |
| 98 | + matched <- NULL |
| 99 | + if (is.character(matched_field) && matched_field %in% names(collections)) |
| 100 | + matched <- as.numeric(collections[[matched_field]]) |
| 101 | + matched |
| 102 | +} |
| 103 | + |
| 104 | +#' @rdname collections_functions |
| 105 | +#' |
| 106 | +#' @export |
| 107 | +collections_length <- function(collections) { |
| 108 | + check_collections(collections) |
| 109 | + return(length(collections$collections)) |
| 110 | +} |
| 111 | + |
| 112 | +#' @rdname collections_functions |
| 113 | +#' |
| 114 | +#' @export |
| 115 | +collections_fetch <- function(collections, ..., |
| 116 | + progress = TRUE, |
| 117 | + matched_field = NULL) { |
| 118 | + check_collections(collections) |
| 119 | + matched <- collections_matched(collections, matched_field) |
| 120 | + # verify if progress bar can be shown |
| 121 | + progress <- progress & |
| 122 | + (!is.null(matched) && (collections_fetch(collections) < matched)) |
| 123 | + if (progress) { |
| 124 | + pb <- utils::txtProgressBar( |
| 125 | + min = collections_length(collections), |
| 126 | + max = matched, |
| 127 | + style = 3 |
| 128 | + ) |
| 129 | + # close progress bar when exit |
| 130 | + on.exit({ |
| 131 | + if (progress) { |
| 132 | + utils::setTxtProgressBar(pb, matched) |
| 133 | + close(pb) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | + # Initialize the items |
| 138 | + next_collections <- collections |
| 139 | + while (TRUE) { |
| 140 | + # check if features is complete |
| 141 | + if (!is.null(matched) && (collections_length(collections) == matched)) |
| 142 | + break |
| 143 | + # protect against infinite loop |
| 144 | + if (!is.null(matched) && (collections_length(collections) > matched)) |
| 145 | + .error(paste( |
| 146 | + "Length of returned collections (%s) is different", |
| 147 | + "from matched collections (%s)."), |
| 148 | + collections_length(collections), matched) |
| 149 | + next_collections <- tryCatch({ |
| 150 | + collections_next(next_collections, ...) |
| 151 | + }, next_error = function(e) NULL) |
| 152 | + if (is.null(next_collections)) |
| 153 | + break |
| 154 | + collections$collections <- c(collections$collections, |
| 155 | + next_collections$collections) |
| 156 | + # update progress bar |
| 157 | + if (progress) |
| 158 | + utils::setTxtProgressBar(pb, length(next_collections)) |
| 159 | + } |
| 160 | + collections |
| 161 | +} |
0 commit comments