brazil-data-cube / rstac

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

Should filtering using variable work in `items_filter()`? #160

Open kadyb opened 5 months ago

kadyb commented 5 months ago

I tried filtering the scene ID using the variable in items_filter(), but it didn't work. However, if I pass it as text, it works. Is this expected behavior?

library("rstac")

stac("https://earth-search.aws.element84.com/v1") |>
  stac_search(
    collections = "sentinel-2-c1-l2a",
    bbox = c(22, 51, 23, 52),
    datetime = "2023-01-01T00:00:00Z/2023-01-02T00:00:00Z") |>
  post_request() -> items

id = items$features[[1]]$properties$`s2:tile_id`
id
#> "S2A_OPER_MSI_L2A_TL_2APS_20230101T135652_A039315_T34UEB_N05.09"

# works
items |>
  items_filter(properties$`s2:tile_id` == "S2A_OPER_MSI_L2A_TL_2APS_20230101T135652_A039315_T34UEB_N05.09")
#> - features (1 item(s) / 3 not fetched):
#>   - S2A_T34UEB_20230101T093407_L2A

# doesn't work
items |>
  items_filter(properties$`s2:tile_id` == id)
#> - features (0 item(s) / 4 not fetched):
#> Warning message:
#> Filter criteria did not match any item.
#> Please, see `?items_filter` for more details on how expressions are evaluated by `items_filter()`.
rolfsimoes commented 4 months ago

Thank you for spotting this. The behavior you observed is indeed a bug in the rstac package related to how variables are escaped in the items_filter() function. The same bug is present in the functions assets_select() and links(). This has been fixed in the b-1.0.1 branch.

The fix will be included in the next release on CRAN, which should be available soon. In the meantime, you can install the development version from GitHub to use the fix right away. Here’s how you can do it using the remotes package:

# Install the remotes package if you haven't already
install.packages("remotes")

# Install the development version of rstac from GitHub
remotes::install_github("brazil-data-cube/rstac@b-1.0.1")

Additionally, the correct way to use variables inside expressions in items_filter() is by passing them inside double curly braces, for example {{id}}, to ensure proper escaping. Here is your example updated with the correct syntax:

library("rstac")

stac("https://earth-search.aws.element84.com/v1") |>
  stac_search(
    collections = "sentinel-2-c1-l2a",
    bbox = c(22, 51, 23, 52),
    datetime = "2023-01-01T00:00:00Z/2023-01-02T00:00:00Z") |>
  post_request() -> items

id = items$features[[1]]$properties$`s2:tile_id`
id
#> "S2A_OPER_MSI_L2A_TL_2APS_20230101T135652_A039315_T34UEB_N05.09"

# Now this works with the fix
items |>
  items_filter(properties$`s2:tile_id` == {{id}})
#> - features (1 item(s) / 3 not fetched):
#>   - S2A_T34UEB_20230101T093407_L2A

I appreciate your patience and contributions to rstac package.

Best regards, Rolf

kadyb commented 4 months ago

Thank you!

Additionally, the correct way to use variables inside expressions in items_filter() is by passing them inside double curly braces, for example {{id}}, to ensure proper escaping.

Maybe you can add such an example to the documentation? I wasn't aware it had to be used in this way.