Spaces:
Paused
Paused
Download R/workflows.R from abhirsc/spatial-toolkit-py: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/abhirsc/spatial-toolkit-py/resolve/main/R/workflows.R
- Command line
-
hf download hf://spaces/abhirsc/spatial-toolkit-py/R/workflows.R
-
curl -L -o workflows.R https://huggingface.co/spaces/abhirsc/spatial-toolkit-py/resolve/main/R/workflows.R
3.24 kB
| #' Return the built-in workflow catalog | |
| #' | |
| #' @return A named list of workflow definitions. | |
| #' @export | |
| get_workflow_catalog <- function() { | |
| list( | |
| raster_qc = list( | |
| name = "Raster quality control", | |
| description = "Inspect CRS, extent, resolution, bands, names, and values.", | |
| steps = c("metadata", "quality_report", "stack_info") | |
| ), | |
| harmonise_raster = list( | |
| name = "Harmonise raster", | |
| description = "Project, crop, mask, and export a raster for ecological analysis.", | |
| steps = c("project", "crop", "mask", "export_tiff") | |
| ) | |
| ) | |
| } | |
| #' Run a named built-in workflow | |
| #' | |
| #' @param workflow_name Name from `get_workflow_catalog()`. | |
| #' @param ... Arguments forwarded to `run_workflow()`. | |
| #' | |
| #' @return A named list. | |
| #' @export | |
| run_named_workflow <- function(workflow_name, ...) { | |
| catalog <- get_workflow_catalog() | |
| workflow <- catalog[[workflow_name]] | |
| if (is.null(workflow)) { | |
| stop("Unknown workflow: ", workflow_name, call. = FALSE) | |
| } | |
| run_workflow(workflow = workflow, ...) | |
| } | |
| #' Run a workflow against a raster | |
| #' | |
| #' @param workflow A workflow definition. | |
| #' @param raster A path or `SpatRaster`. | |
| #' @param vector Optional path or vector layer for crop and mask steps. | |
| #' @param target_crs Optional CRS for projection. | |
| #' @param output_path Optional output path for export steps. | |
| #' | |
| #' @return A named list of results. | |
| #' @export | |
| run_workflow <- function(workflow, | |
| raster, | |
| vector = NULL, | |
| target_crs = NULL, | |
| output_path = NULL) { | |
| raster_data <- normalize_raster(raster) | |
| results <- list() | |
| for (step in workflow$steps) { | |
| if (identical(step, "metadata")) { | |
| results[[step]] <- get_spatial_metadata(raster_data) | |
| } else if (identical(step, "quality_report")) { | |
| results[[step]] <- raster_quality_report(raster_data) | |
| } else if (identical(step, "stack_info")) { | |
| results[[step]] <- raster_stack_info(raster_data) | |
| } else if (identical(step, "project")) { | |
| if (is.null(target_crs)) { | |
| stop("`target_crs` is required for the project step.", call. = FALSE) | |
| } | |
| raster_data <- project_spatial(raster_data, target_crs) | |
| results[[step]] <- get_spatial_metadata(raster_data) | |
| } else if (identical(step, "crop")) { | |
| if (is.null(vector)) { | |
| stop("`vector` is required for the crop step.", call. = FALSE) | |
| } | |
| raster_data <- crop_raster_to_vector(raster_data, vector, mask = FALSE) | |
| results[[step]] <- get_spatial_metadata(raster_data) | |
| } else if (identical(step, "mask")) { | |
| if (is.null(vector)) { | |
| stop("`vector` is required for the mask step.", call. = FALSE) | |
| } | |
| raster_data <- mask_raster_with_vector(raster_data, vector) | |
| results[[step]] <- get_spatial_metadata(raster_data) | |
| } else if (identical(step, "export_tiff")) { | |
| if (is.null(output_path)) { | |
| stop("`output_path` is required for the export step.", call. = FALSE) | |
| } | |
| terra::writeRaster(raster_data, output_path, overwrite = TRUE, filetype = "GTiff") | |
| results[[step]] <- output_path | |
| } else { | |
| stop("Unsupported workflow step: ", step, call. = FALSE) | |
| } | |
| } | |
| results | |
| } | |