appelmar / gdalcubes

Creating and analyzing Earth observation data cubes in R
https://gdalcubes.github.io
Other
120 stars 28 forks source link

unexpected errors in stac_image_collection #71

Closed cboettig closed 1 year ago

cboettig commented 1 year ago

Thanks for this amazing package, the ability to create a virtual cube directly from a STAC search seems very powerful. Unfortunately I'm having a bit of trouble with the following example, but cannot spot where stac_image_collection() is getting snagged up.

library(rstac)
library(gdalcubes)
box <- c(xmin=-122.51006, ymin=37.70801, xmax=-122.36268, ymax=37.80668) 

matches <-
  stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
  stac_search(collections = "modis-15A2H-061",
              datetime = "2022-11-01/2022-11-08",
              bbox = c(box),
              limit = 10) |>
  get_request() |>
  items_fetch() |>
  items_sign(sign_fn = sign_planetary_computer())
#matches |> assets_select("Lai_500m") |> gdalcubes::stac_image_collection()
cube <- gdalcubes::stac_image_collection(matches$features, asset_names = "Lai_500m")
appelmar commented 1 year ago

Thanks, it seems that instead of datetime, the returned STAC items contain start_endtime and end_datetime properties, while stac_image_collection() expects the former. The following workaround works for me:

features_new = lapply(matches$features, function(x) {
  x$properties$datetime = x$properties$start_datetime
  return(x)
})

cube <- gdalcubes::stac_image_collection(features_new, asset_names = "Lai_500m")
cube

Not sure if this is compliant with the STAC specification. To me it seems that datetime is required (see https://github.com/radiantearth/stac-spec/blob/fd8fb4e3cf0b6c4a0fe74822accbc1725a92f92a/item-spec/json-schema/item.json#L136). Can you help, @m-mohr?

m-mohr commented 1 year ago

datetime is allowed to be null if start_datetime and end_datetime are given. If a single datetime is required, people sometimes compute a center datetime or just use the start_datetime. It depends on the use-case which procedure is ideal.

https://github.com/radiantearth/stac-spec/blob/master/item-spec/item-spec.md#datetime

appelmar commented 1 year ago

Great, thanks for clarifying! I'll adapt stac_image_collection() accordingly. However, adding full support for images with datetime intervals when creating data cubes (other than just using the start / center datetime) might need some additional work.

cboettig commented 1 year ago

Thanks both! @appelmar I think it would be fine to fill with the center datetime or start_datetime in most cases. Maybe an optional argument to toggle this, something like duration = c("start", "center", NA) perhaps?

appelmar commented 1 year ago

Thanks for the suggestion, this is now implemented with default being "center".

cboettig commented 1 year ago

awesome! Works great, thanks.