header_tag.html

Skip to contents

Coordinate the request and retrieval of STAC calls

Usage

open_stac(con, features)

Arguments

con

Connection

features

return from search_stac

Examples


# \donttest{

monitoring_location_id <- c("USGS-01435000", "USGS-05406360")

features <- search_stac(
    monitoring_location_id = monitoring_location_id,
    collections = c("continuous"))
#> Requesting:
#> https://api.waterdata.usgs.gov/stac/v0/search?collections=continuous&filter=monitoring_location_id%20IN%20%28%27USGS-01435000%27%2C%20%27USGS-05406360%27%29%20OR%20&filter-lang=cql2-text&limit=50000&f=json&lang=en-US
#> Remaining requests this hour:587 

###################################################
# No download, no load: Returns lazy table
con_1 <- initial_connection()
data_tbl <- open_stac(features = features,
                      con = con_1)
head(data_tbl)
#> # A query:  ?? x 11
#> # Database: DuckDB 2.0.0 [unknown@Linux 7.0.0-1017-azure:R 4.6.2/:memory:]
#>   time_series_id              monitoring_location_id parameter_code statistic_id
#>   <chr>                       <chr>                  <chr>          <chr>       
#> 1 696c70dd239f4e5ab750c95e54… USGS-05406360          00060          00011       
#> 2 696c70dd239f4e5ab750c95e54… USGS-05406360          00060          00011       
#> 3 696c70dd239f4e5ab750c95e54… USGS-05406360          00060          00011       
#> 4 696c70dd239f4e5ab750c95e54… USGS-05406360          00060          00011       
#> 5 696c70dd239f4e5ab750c95e54… USGS-05406360          00060          00011       
#> 6 696c70dd239f4e5ab750c95e54… USGS-05406360          00060          00011       
#> # ℹ 7 more variables: time <dttm>, value <chr>, unit_of_measure <chr>,
#> #   approval_status <chr>, qualifier <list>, last_modified <dttm>,
#> #   method_category <chr>

summarize_data <- data_tbl |>
   dplyr::mutate(value = as.numeric(value)) |>
   dplyr::summarize(count = dplyr::n(),
                    start = min(time, na.rm = TRUE),
                    end = max(time, na.rm = TRUE),
                    max_val = max(value, na.rm = TRUE),
                    .by = c(time_series_id, monitoring_location_id, parameter_code)) |>
   dplyr::collect()

summarize_data
#> # A tibble: 61 × 7
#>    time_series_id                   monitoring_location_id parameter_code  count
#>    <chr>                            <chr>                  <chr>           <dbl>
#>  1 0be5a4c566f145998e159920fe6e90f5 USGS-01435000          00010          159825
#>  2 af4996712fbf4d63810d6c133391de71 USGS-01435000          62611          166027
#>  3 f22a2084703c45578e9fb1fcc3880b1f USGS-01435000          00065          497980
#>  4 f9a3961b281441d9bb408f0bca322d56 USGS-01435000          00095          141554
#>  5 fc7dfde1701b41b1b6740b0cda1614ee USGS-01435000          00095           83385
#>  6 e8a4982eb7434d6ea94286bf80d24583 USGS-01435000          00045          151149
#>  7 3a7de90ef9824ec7ab7370b4e0b1340b USGS-01435000          00020          240764
#>  8 6885cbcdc52e4776a7962634cc95f12d USGS-01435000          00095          225810
#>  9 f5079a8831044133a78f2ff27c8a0418 USGS-01435000          00010           11774
#> 10 701417f906ee47399e262abf7522620a USGS-01435000          00010           11774
#> # ℹ 51 more rows
#> # ℹ 3 more variables: start <dttm>, end <dttm>, max_val <dbl>

DBI::dbDisconnect(con_1)

#########################################################
# Download data locally, no load: Returns lazy table
raw_data_store <- file.path(tempdir(), "test_1")
dir.create(raw_data_store, showWarnings = FALSE)

local_path <- download_stac(
  features = features,
  file_directory = raw_data_store)
#> Downloading USGS-05406360: continuous
#> Downloading USGS-01435000: continuous
#> Downloading USGS-01435000: continuous

con_2 <- initial_connection()

data_tbl <- open_stac(features = local_path,
                      con = con_2)
head(data_tbl)
#> # A query:  ?? x 11
#> # Database: DuckDB 2.0.0 [unknown@Linux 7.0.0-1017-azure:R 4.6.2/:memory:]
#>   time_series_id              monitoring_location_id parameter_code statistic_id
#>   <chr>                       <chr>                  <chr>          <chr>       
#> 1 1b2e9bc4974a4d5b8bfa436093… USGS-01435000          00010          00011       
#> 2 0be5a4c566f145998e159920fe… USGS-01435000          00010          00011       
#> 3 a14ffc218c0647fb8554708eaf… USGS-01435000          00010          00011       
#> 4 af4996712fbf4d63810d6c1333… USGS-01435000          62611          00011       
#> 5 1e9b356375e44e5ba4a9fae83b… USGS-01435000          62611          00011       
#> 6 1f10ef6fce32427bb908396225… USGS-01435000          00095          00011       
#> # ℹ 7 more variables: time <dttm>, value <chr>, unit_of_measure <chr>,
#> #   approval_status <chr>, qualifier <list>, last_modified <dttm>,
#> #   method_category <chr>
DBI::dbDisconnect(con_2)


# }