brazil-data-cube / rstac

R Client Library for SpatioTemporal Asset Catalog
https://brazil-data-cube.github.io/rstac
Other
69 stars 15 forks source link

Error in rstac::stac() Value '' is not a valid property name #156

Open rokoeh opened 3 months ago

rokoeh commented 3 months ago

I was following along the code in https://stacspec.org/en/tutorials/2-using-rstac-and-cql2-to-query-stac-api/ and it seems that this part is not running:


rstac::stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
  rstac::ext_filter(
    collection == "landsat-c2-l2" &&
      t_intersects(datetime, )
  )

Not sure I'm doing something wrong or if it is a bug

Complete code :

require(sf)
require(rstac)

ashe <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))[1, ]

ashe_bbox <- ashe |>
  sf::st_transform(4326) |>
  sf::st_bbox()

stac_query <- rstac::stac(
  "https://planetarycomputer.microsoft.com/api/stac/v1"
) |>
  rstac::stac_search(
    collections = "landsat-c2-l2",
    bbox = ashe_bbox,
    datetime = "2021-01-01/2021-01-31"
  ) |>
  rstac::get_request()

#stac_query

lapply(stac_query$features, \(x) names(x$properties)) |> 
  unlist() |> 
  unique()

lapply(
  stac_query$features, 
  \(x) data.frame(id = x$id, platform = x$properties$platform)
) |> 
  do.call(what = rbind)

ashe_bbox_geojson <- rstac::cql2_bbox_as_geojson(ashe_bbox)
#ashe_bbox_geojson

time_range <- rstac::cql2_interval("2021-01-01", "2021-01-31")
#time_range

rstac::stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
  rstac::ext_filter(
    collection == "landsat-c2-l2"
  )

rstac::stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
  rstac::ext_filter(
    collection == "landsat-c2-l2" &&
      t_intersects(datetime, )
  )

Error: Value '' is not a valid property name
rolfsimoes commented 1 month ago

Dear @rokoeh,

The issue you're encountering stems from missing arguments in the t_intersects() and s_intersects() functions. It appears that the tutorial inadvertently omitted these parameters, likely due to the requirement of double curly braces ({{...}}) to properly reference the variables time_range and ashe.

Here is the corrected example:

library(sf)
library(rstac)

ashe <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))[1, ]

time_range <- rstac::cql2_interval("2021-01-01", "2021-01-31")

rstac::stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
  rstac::ext_filter(
    collection == "landsat-c2-l2" &&
      t_intersects(datetime, {{time_range}}) &&
      s_intersects(geometry, {{ashe}})
  ) |>
  rstac::post_request()

CQL2 utilizes non-evaluated expressions to construct complex queries using R syntax. Without the double curly braces, the names provided will be interpreted as feature properties. For example, using t_intersects(datetime, time_range) would still be syntactically correct, but it will not return any results because time_range will be interpreted as NULL inside the items, as it does not exist as a property. To refer to the variable time_range, you need to escape the expression by enclosing it within double curly braces {{...}}.

Using double curly braces ensures that the actual variables time_range and ashe are correctly passed to the functions t_intersects and s_intersects.

Please let us know if you have any further questions or issues.

Best regards, Rolf