Skip to content

Commit 864f091

Browse files
authored
Merge pull request #181 from rolfsimoes/dev
Dev version 1.0.1
2 parents 97f2c41 + 96b5b60 commit 864f091

40 files changed

Lines changed: 745 additions & 386 deletions

DESCRIPTION

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: rstac
22
Title: Client Library for SpatioTemporal Asset Catalog
3-
Version: 1.0.0
3+
Version: 1.0.1
44
Authors@R:
55
c(person("Rolf", "Simoes",
66
email = "rolfsimoes@gmail.com",
@@ -22,7 +22,7 @@ License: MIT + file LICENSE
2222
URL: https://brazil-data-cube.github.io/rstac/
2323
BugReports: https://github.com/brazil-data-cube/rstac/issues
2424
Encoding: UTF-8
25-
RoxygenNote: 7.3.1
25+
RoxygenNote: 7.3.3
2626
Depends:
2727
R (>= 3.5)
2828
Imports:
@@ -38,7 +38,14 @@ Imports:
3838
Suggests:
3939
lifecycle,
4040
testthat,
41-
knitr
41+
knitr,
42+
tmap,
43+
leaflet,
44+
stars,
45+
slider,
46+
ggplot2,
47+
purrr,
48+
dplyr
4249
Collate:
4350
'cql2-expr-funs.R'
4451
'cql2-types.R'
@@ -54,6 +61,7 @@ Collate:
5461
'assets-funs.R'
5562
'check-utils.R'
5663
'conformance-query.R'
64+
'collections-funs.R'
5765
'collections-query.R'
5866
'deprec-funs.R'
5967
'doc-funs.R'

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ export(assets_rename)
174174
export(assets_select)
175175
export(assets_url)
176176
export(collections)
177+
export(collections_fetch)
178+
export(collections_length)
179+
export(collections_matched)
180+
export(collections_next)
177181
export(conformance)
178182
export(cql2_bbox_as_geojson)
179183
export(cql2_date)

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# rstac (development version)
2+
3+
# rstac 1.0.1 (Released 2024-07-18)
4+
5+
* Add support to `jpg` in `preview_plot()` function (#161)
6+
* Fix variable in filter expressions (non-standard evaluation) (#160)
7+
* Fix `limit` parameter as integer type in `stac_search()` and `items()`
8+
* Improve `items_reap()` documentation (#152)
9+
110
# rstac 1.0.0 (Released 2024-02-14)
211

312
* Add support to static catalogs;

R/assets-funs.R

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
#' @param progress a `logical` indicating if a progress bar must be
4444
#' shown or not. Defaults to `TRUE`.
4545
#'
46+
#' @param use_gdal a `logical` indicating if the file should be downloaded
47+
#' by GDAL instead httr package.
48+
#'
4649
#' @param download_fn a `function` to handle download of assets for
4750
#' each item to be downloaded. Using this function, you can change the
4851
#' hrefs for each asset, as well as the way download is done.
@@ -88,7 +91,10 @@
8891
#' Multiple expressions are combine with `AND` operator. Expressions can
8992
#' use `asset` helper functions (i.e. `asset_key()`, `asset_eo_bands()`,
9093
#' and `asset_raster_bands()`). Multiple expressions are combined with
91-
#' `AND` operator.
94+
#' `AND` operator. `assets_select()` uses non-standard evaluation to evaluate
95+
#' its expressions. That means users must escape any variable or call to
96+
#' be able to use them in the expressions. The escape is done by using
97+
#' `double-curly-braces`, i.e., `{{variable}}`.
9298
#'
9399
#' **WARNING:** Errors in the evaluation of expressions are
94100
#' considered as `FALSE`.
@@ -117,8 +123,8 @@
117123
#' @examples
118124
#' \dontrun{
119125
#' # assets_download function
120-
#' stac("https://brazildatacube.dpi.inpe.br/stac/") %>%
121-
#' stac_search(collections = "CB4-16D-2",
126+
#' stac("https://data.inpe.br/bdc/stac/v1/") %>%
127+
#' stac_search(collections = "CBERS4-WFI-16D-2",
122128
#' datetime = "2019-06-01/2019-08-01") %>%
123129
#' stac_search() %>%
124130
#' get_request() %>%
@@ -127,22 +133,26 @@
127133
#'
128134
#' \dontrun{
129135
#' # assets_url function
130-
#' stac_item <- stac("https://brazildatacube.dpi.inpe.br/stac/") %>%
131-
#' stac_search(collections = "CB4-16D-2", limit = 100,
132-
#' datetime = "2017-08-01/2018-03-01",
133-
#' bbox = c(-48.206,-14.195,-45.067,-12.272)) %>%
134-
#' get_request() %>% items_fetch(progress = FALSE)
136+
#' stac_item <- stac("https://data.inpe.br/bdc/stac/v1/") %>%
137+
#' stac_search(
138+
#' collections = "CBERS4-WFI-16D-2",
139+
#' limit = 100,
140+
#' datetime = "2017-08-01/2018-03-01",
141+
#' bbox = c(-48.206,-14.195,-45.067,-12.272)) %>%
142+
#' get_request() %>% items_fetch(progress = FALSE)
135143
#'
136144
#' stac_item %>% assets_url()
137145
#' }
138146
#'
139147
#' \dontrun{
140148
#' # assets_select function
141-
#' stac_item <- stac("https://brazildatacube.dpi.inpe.br/stac/") %>%
142-
#' stac_search(collections = "CB4-16D-2", limit = 100,
143-
#' datetime = "2017-08-01/2018-03-01",
144-
#' bbox = c(-48.206,-14.195,-45.067,-12.272)) %>%
145-
#' get_request() %>% items_fetch(progress = FALSE)
149+
#' stac_item <- stac("https://data.inpe.br/bdc/stac/v1/") %>%
150+
#' stac_search(
151+
#' collections = "CBERS4-WFI-16D-2",
152+
#' limit = 100,
153+
#' datetime = "2017-08-01/2018-03-01",
154+
#' bbox = c(-48.206,-14.195,-45.067,-12.272)) %>%
155+
#' get_request() %>% items_fetch(progress = FALSE)
146156
#'
147157
#' stac_item %>% assets_select(asset_names = "NDVI")
148158
#' }
@@ -182,6 +192,7 @@ assets_download <- function(items,
182192
asset_names = NULL,
183193
output_dir = getwd(),
184194
overwrite = FALSE, ...,
195+
use_gdal = FALSE,
185196
download_fn = NULL) {
186197
# check output dir
187198
if (!dir.exists(output_dir))
@@ -197,6 +208,7 @@ assets_download.doc_item <- function(items,
197208
asset_names = NULL,
198209
output_dir = getwd(),
199210
overwrite = FALSE, ...,
211+
use_gdal = FALSE,
200212
create_json = FALSE,
201213
download_fn = NULL) {
202214
if (!is.null(asset_names)) {
@@ -209,7 +221,7 @@ assets_download.doc_item <- function(items,
209221
}
210222
items$assets <- lapply(
211223
items$assets, asset_download, output_dir = output_dir,
212-
overwrite = overwrite, ..., download_fn = download_fn
224+
overwrite = overwrite, use_gdal = use_gdal, download_fn = download_fn, ...
213225
)
214226
if (create_json) {
215227
file <- "item.json"
@@ -228,6 +240,7 @@ assets_download.doc_items <- function(items,
228240
asset_names = NULL,
229241
output_dir = getwd(),
230242
overwrite = FALSE, ...,
243+
use_gdal = FALSE,
231244
download_fn = NULL,
232245
create_json = TRUE,
233246
items_max = Inf,
@@ -249,7 +262,7 @@ assets_download.doc_items <- function(items,
249262
items$features[[i]] <- assets_download(
250263
items = items$features[[i]], asset_names = asset_names,
251264
output_dir = output_dir, overwrite = overwrite,
252-
create_json = FALSE, download_fn = download_fn, ...
265+
use_gdal = use_gdal, create_json = FALSE, download_fn = download_fn, ...
253266
)
254267
}
255268
if (create_json)
@@ -336,10 +349,7 @@ assets_select <- function(items, ..., asset_names = NULL, select_fn = NULL) {
336349
assets_select.doc_item <- function(items, ...,
337350
asset_names = NULL,
338351
select_fn = NULL) {
339-
exprs <- unquote(
340-
expr = as.list(substitute(list(...), env = environment())[-1]),
341-
env = parent.frame()
342-
)
352+
exprs <- as.list(substitute(list(...), env = environment()))[-1]
343353
init_length <- length(items$assets)
344354
if (!is.null(asset_names)) {
345355
asset_names <- intersect(names(items$assets), asset_names)
@@ -348,9 +358,10 @@ assets_select.doc_item <- function(items, ...,
348358
if (length(exprs) > 0) {
349359
if (!is.null(names(exprs)))
350360
.error("Select expressions cannot be named.")
351-
for (i in seq_along(exprs)) {
361+
for (expr in exprs) {
362+
expr <- unquote(expr = expr, env = parent.frame())
352363
sel <- map_lgl(names(items$assets), function(key) {
353-
select_eval(key = key, asset = items$assets[[key]], expr = exprs[[i]])
364+
select_eval(key = key, asset = items$assets[[key]], expr = expr)
354365
})
355366
items$assets <- items$assets[sel]
356367
}

R/assets-utils.R

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,36 @@ select_exec <- function(key, asset, select_fn) {
5656
asset_download <- function(asset,
5757
output_dir,
5858
overwrite, ...,
59+
use_gdal = FALSE,
5960
download_fn = NULL) {
6061
if (!is.null(download_fn))
6162
return(download_fn(asset))
6263
# create a full path name
63-
path <- url_get_path(asset$href)
64-
out_file <- path_normalize(output_dir, path)
65-
dir_create(out_file)
66-
make_get_request(
67-
url = asset$href,
68-
httr::write_disk(path = out_file, overwrite = overwrite),
69-
...,
70-
error_msg = "Error while downloading"
71-
)
72-
asset$href <- path
64+
out_file <- path_normalize(output_dir, url_get_path(asset$href))
65+
out_dir <- dirname(out_file)
66+
if (!dir.exists(out_dir))
67+
dir.create(out_dir, recursive = TRUE)
68+
stopifnot(dir.exists(out_dir))
69+
if (use_gdal) {
70+
if (file.exists(out_file) && !overwrite)
71+
.error("File already exists. Use `overwrite=TRUE`.")
72+
if (file.exists(out_file))
73+
unlink(out_file)
74+
sf::gdal_utils(
75+
util = "translate",
76+
source = gdalvsi_append(asset$href),
77+
destination = out_file, ...
78+
)
79+
if (!file.exists(out_file)) {
80+
.error("Download failed. File: '%s'.", asset$href)
81+
}
82+
} else {
83+
make_get_request(
84+
url = asset$href,
85+
httr::write_disk(path = out_file, overwrite = overwrite),
86+
error_msg = "Error while downloading", ...
87+
)
88+
}
89+
asset$href <- out_file
7390
asset
7491
}

R/check-utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ check_collection <- function(collection) {
7474
collection
7575
}
7676

77+
check_collections <- function(collections) {
78+
if (!is.list(collections) || is.null(names(collections)))
79+
.error("Invalid doc_collections object.")
80+
if (!"links" %in% names(collections))
81+
.error("Invalid doc_collections object. Expecting `links` key.")
82+
collections
83+
}
84+
7785
check_character <- function(x, msg, ...) {
7886
if (!is.character(x))
7987
.error(msg, ...)

0 commit comments

Comments
 (0)