## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
eval_chunks <-
  CopernicusDataspace::dse_has_client_info() &&
  CopernicusDataspace::dse_has_account() &&
  CopernicusDataspace::dse_has_s3_secret()

## ----odata-search, message=FALSE, eval=eval_chunks----------------------------
library(CopernicusDataspace)
library(dplyr) # Tidiverse package for data manipulation
library(sf)    # package for handling simple (spatial) features

## Define a bounding box for our search query:
bbox <-
  st_bbox(
    c(xmin = 5.261, ymin = 52.680, xmax = 5.319, ymax = 52.715),
    crs = 4326) |>
    st_as_sfc()

## Create an API request:
dse_odata_products_request() |>
  ## Filter content on ContentDate/Start`:
  filter(
    `ContentDate/Start` > "2025-01-01") |>
  ## Only data that intersects with our bounding box:
  st_intersects(bbox) |>
  ## Arrange by descending Id:
  arrange(desc(Id)) |>
  ## Only the first 5 hits:
  slice_head(n = 5) |>
  ## Collect the results:
  collect()

## ----odata-bursts, eval=FALSE-------------------------------------------------
# burst_req <-
#   dse_odata_bursts_request(ParentProductId == "879d445c-2c67-5b30-8589-b1f478904269")
# 
# ## Note that these are large files and may take a while to download:
# dse_odata_download(
#   burst_req,
#   tempdir()
# )

## ----odata-nodes, eval=eval_chunks--------------------------------------------
dse_odata_product_nodes("c8ed8edb-9bef-4717-abfd-1400a57171a4",
                        recursive = TRUE)

## ----odata-quicklook, eval=FALSE----------------------------------------------
# dse_odata_quicklook(
#     "91822f33-b15c-5b60-aa39-6d9f6f5c773b",
#     tempfile(fileext = ".jpg"))

## ----odata-download-path, eval=FALSE------------------------------------------
# if (dse_has_account()) {
#   dse_odata_download_path(
#     product     = "2f497806-0101-5eea-83fa-c8f68bc56b0c",
#     node_path   =
#       paste("DEM1_SAR_DTE_90_20101213T034716_20130408T035028_ADS_000000_5033.DEM",
#             "Copernicus_DSM_30_S09_00_E026_00", "DEM",
#             "Copernicus_DSM_30_S09_00_E026_00_DEM.dt1", sep = "/"),
#     destination = tempdir()
#     )
# }

## ----download-odata, eval=FALSE-----------------------------------------------
# if (dse_has_s3_secret()) {
#   dse_odata_download(
#       dse_odata_products(
#         Name ==
#           "S1A_IW_OCN__2SDH_20250707T210608_20250707T210625_059983_07739B_893E.SAFE"),
#       destination = tempdir())
# }

