|
| 1 | +#' Generate a static STAC catalog from COGs |
| 2 | +#' |
| 3 | +#' Scans a directory for Cloud-Optimized GeoTIFFs and builds a |
| 4 | +#' STAC catalog JSON file. Each COG becomes one item with |
| 5 | +#' `cd:variable` and `cd:period` properties parsed from the filename. |
| 6 | +#' The resulting catalog is compatible with [cd_catalog()]. |
| 7 | +#' |
| 8 | +#' @param cog_dir Character. Directory containing COG files (.tif). |
| 9 | +#' @param output_path Character. Path to write the catalog JSON. |
| 10 | +#' Default `"catalog.json"`. |
| 11 | +#' @param catalog_id Character. STAC catalog ID. |
| 12 | +#' Default `"era5-land"`. |
| 13 | +#' @param title Character. Human-readable catalog title. |
| 14 | +#' @param description Character. Optional catalog description. |
| 15 | +#' @param base_url Character. Base URL where COGs will be served. |
| 16 | +#' Asset hrefs are built as `{base_url}/{filename}`. |
| 17 | +#' |
| 18 | +#' @return The output path (invisibly). |
| 19 | +#' |
| 20 | +#' @examples |
| 21 | +#' \dontrun{ |
| 22 | +#' cd_stac_catalog( |
| 23 | +#' "data/cogs", |
| 24 | +#' output_path = "data/catalog.json", |
| 25 | +#' base_url = "https://stac-era5-land.s3.us-west-2.amazonaws.com" |
| 26 | +#' ) |
| 27 | +#' } |
| 28 | +#' |
| 29 | +#' @export |
| 30 | +cd_stac_catalog <- function(cog_dir, |
| 31 | + output_path = "catalog.json", |
| 32 | + catalog_id = "era5-land", |
| 33 | + title = "ERA5-Land Climate Data", |
| 34 | + description = NULL, |
| 35 | + base_url = "https://stac-era5-land.s3.us-west-2.amazonaws.com") { |
| 36 | + |
| 37 | + tif_files <- list.files(cog_dir, pattern = "\\.tif$", full.names = TRUE) |
| 38 | + if (length(tif_files) == 0) { |
| 39 | + rlang::abort(paste("No .tif files found in", cog_dir)) |
| 40 | + } |
| 41 | + |
| 42 | + items <- lapply(tif_files, function(f) { |
| 43 | + cd_stac_item(f, base_url) |
| 44 | + }) |
| 45 | + |
| 46 | + catalog <- list( |
| 47 | + type = "Catalog", |
| 48 | + id = catalog_id, |
| 49 | + stac_version = "1.0.0", |
| 50 | + description = description %||% paste(title, "- static STAC catalog"), |
| 51 | + links = list( |
| 52 | + list(rel = "root", href = paste0("./", basename(output_path)), |
| 53 | + type = "application/json") |
| 54 | + ), |
| 55 | + items = items |
| 56 | + ) |
| 57 | + |
| 58 | + dir.create(dirname(output_path), recursive = TRUE, showWarnings = FALSE) |
| 59 | + jsonlite::write_json(catalog, output_path, pretty = TRUE, auto_unbox = TRUE) |
| 60 | + message("Wrote STAC catalog: ", output_path, " (", length(items), " items)") |
| 61 | + invisible(output_path) |
| 62 | +} |
| 63 | + |
| 64 | +#' Build a STAC item from a COG file |
| 65 | +#' |
| 66 | +#' Parses variable and period from the filename, extracts spatial |
| 67 | +#' metadata from the raster. |
| 68 | +#' |
| 69 | +#' @param cog_path Path to a COG file. |
| 70 | +#' @param base_url Base URL for asset hrefs. |
| 71 | +#' @return A list representing a STAC Feature item. |
| 72 | +#' @noRd |
| 73 | +cd_stac_item <- function(cog_path, base_url) { |
| 74 | + fname <- basename(cog_path) |
| 75 | + name_parts <- tools::file_path_sans_ext(fname) |
| 76 | + |
| 77 | + # Parse variable and period from filename |
| 78 | + # Expected patterns: "tmean_annual.tif", "vpd_2024.tif", |
| 79 | + # "example_climate.tif", etc. |
| 80 | + known_vars <- cd_variables()$variable |
| 81 | + known_periods <- cd_periods(include_monthly = TRUE) |
| 82 | + |
| 83 | + var_match <- known_vars[vapply(known_vars, function(v) grepl(v, name_parts), logical(1))] |
| 84 | + period_match <- known_periods[vapply(known_periods, function(p) grepl(p, name_parts), logical(1))] |
| 85 | + |
| 86 | + variable <- if (length(var_match) > 0) var_match[1] else name_parts |
| 87 | + period <- if (length(period_match) > 0) period_match[1] else "unknown" |
| 88 | + |
| 89 | + # Extract spatial metadata |
| 90 | + r <- terra::rast(cog_path) |
| 91 | + e <- as.vector(terra::ext(r)) |
| 92 | + n_bands <- terra::nlyr(r) |
| 93 | + band_names <- names(r) |
| 94 | + |
| 95 | + # Parse years from band names if numeric |
| 96 | + years <- suppressWarnings(as.integer(band_names)) |
| 97 | + years <- years[!is.na(years)] |
| 98 | + |
| 99 | + item_id <- paste(variable, period, sep = "-") |
| 100 | + |
| 101 | + list( |
| 102 | + type = "Feature", |
| 103 | + stac_version = "1.0.0", |
| 104 | + id = item_id, |
| 105 | + geometry = NA, |
| 106 | + bbox = e[c(1, 3, 2, 4)], |
| 107 | + properties = list( |
| 108 | + `cd:variable` = variable, |
| 109 | + `cd:period` = period, |
| 110 | + datetime = NA, |
| 111 | + start_datetime = if (length(years) > 0) paste0(min(years), "-01-01T00:00:00Z") else NULL, |
| 112 | + end_datetime = if (length(years) > 0) paste0(max(years), "-12-31T23:59:59Z") else NULL |
| 113 | + ), |
| 114 | + links = list(), |
| 115 | + assets = list( |
| 116 | + data = list( |
| 117 | + href = paste0(base_url, "/", fname), |
| 118 | + type = "image/tiff; application=geotiff; profile=cloud-optimized", |
| 119 | + title = paste(variable, period) |
| 120 | + ) |
| 121 | + ) |
| 122 | + ) |
| 123 | +} |
0 commit comments