Skip to content

Query (list_layers.R) #6

@rellimylime

Description

@rellimylime

list_layers.R is the query function. It turns STAC item metadata into a clean table that users can filter before retrieval.

list_layers() loads all STAC items, extracts the key fields (id, domain, layer_type, data_type, is_hosted, href), applies optional filters for each of these categories, and returns a data frame.

It also includes load_stac_items() as a helper to read item JSON from extdata and fail early if the catalog path is missing or empty.

  • list_layers()

    fedex/R/list_layers.R

    Lines 1 to 87 in f191448

    #' List available WRI layers
    #'
    #' Query the STAC catalog to find available layers. Filter by domain,
    #' layer type, data type, or hosting status.
    #'
    #' @param domain Character. Filter by WRI domain ("air_quality", "communities",
    #' "infrastructure", "livelihoods", "sense_of_place", "species", "habitats",
    #' "water", or "unknown" for overall WRI score). Default NULL (all domains).
    #' @param layer_type Character. Filter by layer type ("resistance", "resilience",
    #' "recovery", "status", "domain_score"). Default NULL (all types).
    #' @param data_type Character. Filter by data type ("indicator", "aggregate",
    #' "final_score"). Default NULL (all types).
    #' @param hosted_only Logical. If TRUE, return only layers hosted on KNB.
    #' Default FALSE.
    #'
    #' @returns A data.frame with columns: id, domain, layer_type, data_type, is_hosted, href
    #' @export
    #'
    #' @examples
    #' # See all available layers
    #' list_layers()
    #'
    #' # Find all air quality layers
    #' list_layers(domain = "air_quality")
    #'
    #' # Find all resistance indicators
    #' list_layers(layer_type = "resistance", data_type = "indicator")
    #'
    #' # Find what's currently hosted on KNB
    #' list_layers(hosted_only = TRUE)
    #'
    #' # Filter by multiple criteria
    #' list_layers(domain = "communities", layer_type = "resistance")
    list_layers <- function(domain = NULL,
    layer_type = NULL,
    data_type = NULL,
    hosted_only = FALSE) {
    # Load all STAC items
    items <- load_stac_items()
    # Extract metadata into a data.frame
    layer_info <- lapply(items, function(item) {
    props <- item$properties
    href <- item$assets$data$href
    # Check if hosted by examining the href
    # If it starts with http:// or https://, it's hosted
    is_hosted <- grepl("^https?://", href)
    data.frame(
    id = item$id,
    domain = if (is.null(props$wri_domain)) NA_character_ else props$wri_domain,
    layer_type = if (is.null(props$wri_layer_type)) NA_character_ else props$wri_layer_type,
    data_type = if (is.null(props$data_type)) NA_character_ else props$data_type,
    is_hosted = is_hosted,
    href = href,
    stringsAsFactors = FALSE
    )
    })
    # Combine into single data.frame
    df <- dplyr::bind_rows(layer_info)
    # Apply filters
    if (!is.null(domain)) {
    df <- df[df$domain == domain & !is.na(df$domain), ]
    }
    if (!is.null(layer_type)) {
    df <- df[df$layer_type == layer_type & !is.na(df$layer_type), ]
    }
    if (!is.null(data_type)) {
    df <- df[df$data_type == data_type & !is.na(df$data_type), ]
    }
    if (hosted_only) {
    df <- df[df$is_hosted, ]
    }
    # Reset row names
    rownames(df) <- NULL
    # Return sorted by id
    df[order(df$id), ]
    }
  • load_stac_items()

    fedex/R/list_layers.R

    Lines 90 to 114 in f191448

    # Helper function to load all STAC items
    load_stac_items <- function() {
    # Define stac directory
    stac_dir <- system.file("extdata/stac/collections/wri_ignitR/items",
    package = "fedex")
    # Check that it exists
    if (!dir.exists(stac_dir)) {
    stop("STAC catalog not found. Package may not be installed correctly.")
    }
    # Extract list of stac item files
    item_files <- list.files(stac_dir, pattern = "\\.json$", full.names = TRUE)
    # Check that there are items in the catalog
    if (length(item_files) == 0) {
    stop("No items in STAC catalog.")
    }
    # Read all items
    items <- lapply(item_files, jsonlite::read_json, simplifyVector = TRUE)
    items
    }

@FlukeAndFeather

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions