Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kadyb opened this issue May 30, 2024 · 2 comments
Open

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

kadyb opened this issue May 30, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kadyb
Copy link
Contributor

kadyb commented May 30, 2024

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 added a commit to rolfsimoes/rstac that referenced this issue Jun 10, 2024
@rolfsimoes rolfsimoes added the bug Something isn't working label Jun 10, 2024
@rolfsimoes rolfsimoes self-assigned this Jun 10, 2024
@rolfsimoes
Copy link
Contributor

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/[email protected]")

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

rolfsimoes added a commit that referenced this issue Jun 10, 2024
@kadyb
Copy link
Contributor Author

kadyb commented Jun 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants