MyNixOS website logo
Description

Client Library for SpatioTemporal Asset Catalog.

Provides functions to access, search and download spacetime earth observation data via SpatioTemporal Asset Catalog (STAC). This package supports the version 1.0.0 (and older) of the STAC specification (<https://github.com/radiantearth/stac-spec>). For further details see Simoes et al. (2021) <doi:10.1109/IGARSS47720.2021.9553518>.

rstac

R Client Library for SpatioTemporal Asset Catalog (rstac)

SoftwareLicense

R-CMD-check Buildstatus codecov Software LifeCycle CRANstatus STACAPI Join us atDiscord

STAC is a specification of files and web services used to describe geospatial information assets. The specification can be consulted in https://stacspec.org/.

R client library for STAC (rstac) was designed to fully support STAC API v1.0.0. It also supports earlier versions (>= v0.8.0).

Installation

# install via CRAN 
install.packages("rstac")

Development version

To install the development version of rstac, run the following commands

remotes::install_github("brazil-data-cube/rstac")

Importing rstac package:

library(rstac)

Usage

rstac implements the following STAC endpoints:

STAC endpointsrstac functionsAPI version
/stac()>= 0.9.0
/stacstac()< 0.9.0
/collectionscollections()>= 0.9.0
/collections/{collectionId}collections(collection_id)>= 0.9.0
/collections/{collectionId}/itemsitems()>= 0.9.0
/collections/{collectionId}/items/{itemId}items(feature_id)>= 0.9.0
/searchstac_search()>= 0.9.0
/stac/searchstac_search()< 0.9.0
/conformanceconformance()>= 0.9.0
/collections/{collectionId}/queryablesqueryables()>= 1.0.0

These functions can be used to retrieve information from a STAC API service. The code below creates a stac object and list the available collections of the STAC API of the Brazil Data Cube project of the Brazilian National Space Research Institute (INPE).

s_obj <- stac("https://brazildatacube.dpi.inpe.br/stac/")

get_request(s_obj)
#> ###Catalog
#> - id: bdc
#> - description: Brazil Data Cube Catalog
#> - field(s): description, id, stac_version, links

The variable s_obj stores information to connect to the Brazil Data Cube STAC web service. The get_request method makes a HTTP GET connection to it and retrieves a STAC Catalog document from the server. Each links entry is an available collection that can be accessed via STAC API.

In the code below, we get some STAC items of CB4-16D-2 collection that intersects the bounding box passed to the bbox parameter. To do this, we call the stac_search function that implements the STAC /search endpoint. The returned document is a STAC Item Collection (a geojson containing a feature collection).


it_obj <- s_obj %>%
  stac_search(collections = "CB4-16D-2",
              bbox = c(-47.02148, -17.35063, -42.53906, -12.98314),
              limit = 100) %>% 
  get_request()

it_obj
#> ###Items
#> - matched feature(s): 1096
#> - features (100 item(s) / 996 not fetched):
#>   - CB4-16D_V2_007004_20240101
#>   - CB4-16D_V2_007005_20240101
#>   - CB4-16D_V2_007006_20240101
#>   - CB4-16D_V2_008004_20240101
#>   - CB4-16D_V2_008006_20240101
#>   - CB4-16D_V2_008005_20240101
#>   - CB4-16D_V2_007004_20231219
#>   - CB4-16D_V2_007006_20231219
#>   - CB4-16D_V2_007005_20231219
#>   - CB4-16D_V2_008004_20231219
#>   - ... with 90 more feature(s).
#> - assets: 
#> BAND13, BAND14, BAND15, BAND16, CLEAROB, CMASK, EVI, NDVI, PROVENANCE, thumbnail, TOTALOB
#> - item's fields: 
#> assets, bbox, collection, geometry, id, links, properties, stac_extensions, stac_version, type

The rstac uses the httr package to manage HTTP requests, allowing the use of tokens from the authorization protocols OAuth 1.0 or 2.0 as well as other configuration options. In the code below, we present an example of how to pass a parameter token on a HTTP request.

it_obj <- s_obj %>%
  stac_search(collections = "CB4-16D-2",
              bbox = c(-47.02148, -17.35063, -42.53906, -12.98314)) %>%
  get_request(add_headers("x-api-key" = "MY-TOKEN"))

In addition to the functions mentioned above, the rstac package provides some extra functions for handling items and to bulk download the assets.

Items functions

rstac provides some functions that facilitates the interaction with STAC data. In the example below, we get how many items matched the search criteria:

# it_obj variable from the last code example
it_obj %>% 
  items_matched()
#> [1] 1096

However, if we count how many items there are in it_obj variable, we get 10, meaning that more items could be fetched from the STAC service:

it_obj %>% 
  items_length()
#> [1] 100
# fetch all items from server 
# (but don't stored them back in it_obj)
it_obj <- it_obj %>% 
  items_fetch(progress = FALSE) 

it_obj %>%
  items_length()
#> [1] 1096

Download assets

All we’ve got in previous example was metadata to STAC Items, including links to geospatial data called assets. To download all assets in a STAC Item Collection we can use assets_download() function, that returns an update STAC Item Collection referring to the downloaded assets. The code below downloads the thumbnail assets (.png files) of 10 items stored in it_obj variable.

download_items <- it_obj %>%
  assets_download(assets_name = "thumbnail", items_max = 10)

CQL2 query filter

rstac also supports advanced query filter using common query language (CQL2). Users can write complex filter expressions using R code in an easy and natural way. For a complete

s_obj <- stac("https://planetarycomputer.microsoft.com/api/stac/v1")

it_obj <- s_obj %>% 
  ext_filter(
    collection == "sentinel-2-l2a" && `s2:vegetation_percentage` >= 50 &&
      `eo:cloud_cover` <= 10 && `s2:mgrs_tile` == "20LKP" && 
      anyinteracts(datetime, interval("2020-06-01", "2020-09-30"))
  ) %>%
  post_request()

Getting help

You can get a full explanation about each STAC (v1.0.0) endpoint at STAC API spec. A detailed documentation with examples on how to use each endpoint and other functions available in the rstac package can be obtained by typing ?rstac in R console.

Citation

To cite rstac in publications use:

R. Simoes, F. C. de Souza, M. Zaglia, G. R. de Queiroz, R. D. C. dos Santos and K. R. Ferreira, “Rstac: An R Package to Access Spatiotemporal Asset Catalog Satellite Imagery,” 2021 IEEE International Geoscience and Remote Sensing Symposium IGARSS, 2021, pp. 7674-7677, doi: 10.1109/IGARSS47720.2021.9553518.

Acknowledgements for financial support

We acknowledge and thank the project funders that provided financial and material support:

  • Amazon Fund, established by the Brazilian government with financial contribution from Norway, through the project contract between the Brazilian Development Bank (BNDES) and the Foundation for Science, Technology and Space Applications (FUNCATE), for the establishment of the Brazil Data Cube, process 17.2.0536.1.

  • Radiant Earth Foundation and STAC Project Steering Committee for the advance of STAC ecosystem programme.

  • OpenGeoHub Foundation and the European Commission (EC) through the project Open-Earth-Monitor Cyberinfrastructure: Environmental information to support EU’s Green Deal (1 Jun. 2022 – 31 May 2026 - 101059548)

How to contribute?

The rstac package was implemented based on an extensible architecture, so feel free to contribute by implementing new STAC API extensions/fragments based on the STAC API specifications.

  1. Make a project fork.
  2. Create a file inside the R/ directory called ext_{extension_name}.R.
  3. In the code, you need to specify a subclass name (e.g.my_subclass) for your extension and use it when calling rstac_query() function. You also need to implement for your subclass the following S3 generic functions: before_request(), after_response(), and parse_params(). With these S3 generics methods you can define how parameters should be submitted to the HTTP request and the types of the returned documents. See the implemented ext_filter API extension as an example.
  4. Make a Pull Request on the most recent development branch.
Metadata

Version

1.0.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows