Closed dindiarto closed 1 year ago
devtools
pacakage and its pre-requisites are installed.data
folder as .rda
, e.g. lc_lookup_klhk <- read_csv("data/lc_lookup_klhk.csv") save(lc_lookup_klhk, file= "data/lc_lookup_klhk.rda")
inst/data
devtools::use_r()
on the console, and draft the function
#' Add legend to categorical raster
#'
#' This function adds a legend to a categorical raster file, often containing information about land cover or planning units.
#'
#' @param raster_file A categorical raster file (an object of class `SpatRaster`)
#' @param lookup_table A corresponding lookup table of descriptions for each class category
#' @return A raster file that contains descriptions for each class category
#' @importFrom terra levels
#' @export
#'
#' @examples
#' \dontrun{
#' add_legend_to_categorical_raster(raster_file = kalbar_11, lookup_table = lc_lookup_klhk) %>% plot()
#' }
add_legend_to_categorical_raster <- function(raster_file, lookup_table){
levels(raster_file) <- lookup_table
raster_file
}
test()
library(terra)
kalbar_LC11 <- LUMENSR_example("kalbar_LC11.tif") kalbar_LC11 <- terra::rast(kalbar_LC11)
lc_lookup_test <- lc_lookup_klhk %>% filter(!Value %in% c(2004, 2500, 3000)) %>% add_row(Value=0, PL20 ="No Data")
test_that("add_legend_to_categorical_raster adds attribute values to the raster file", { result_raster <- add_legend_to_categorical_raster(kalbar_LC11, lc_lookup_klhk)
expect_s4_class(kalbar_LC11, "SpatRaster")
expect_s4_class(result_raster, "SpatRaster")
expect_equal(sort(na.omit(unique(terra::values(result_raster)))), sort(lc_lookup_test$Value)) })
9. check the documentation via `document()`
10. do the `check`
11. commit and push to the github repo
![image](https://user-images.githubusercontent.com/14798903/235584484-36ebe130-964a-4f16-992c-d50af1479b90.png)
Description:
The goal of this issue is to create a simple function that assigns a legend to a raster file in R. The key command to achieve this is
levels(raster_file) <- subset_legend
.Tasks:
Acceptance Criteria:
levels(raster_file) <- subset_legend