icraf-indonesia / LUMENSR

An R package for land use planning and management of multiple environmental services.
https://icraf-indonesia.github.io/LUMENSR/
Other
0 stars 0 forks source link

Develop Function to Assign Legend to Raster File in R #4

Closed dindiarto closed 1 year ago

dindiarto commented 1 year ago

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:

dindiarto commented 1 year ago

Steps in writing a new "LUMENSR" function

  1. Fork this repository (git@github.com:icraf-indonesia/LUMENSR.git) onto your machine and open in an IDE of your preference.
  2. make sure you have devtools pacakage and its pre-requisites are installed.
  3. Define the key features of your function: a. What is the purpose of the function? Assign Legend to Raster File b. What are the inputs? 1. a raster file and 2. a lookup table of the land-cover. c. What is the output? a categorical land use map with its corresponding class
  4. Store tabular data into 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")
  5. Store raster data in inst/data
  6. run 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
    }
  7. Write a test file test()
    
    library(terra)

Load or create example data for testing

Load example data

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 attribute values are added to the raster file

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)

Check if the input is a SpatRaster

expect_s4_class(kalbar_LC11, "SpatRaster")

Check if the result is a SpatRaster

expect_s4_class(result_raster, "SpatRaster")

Check if the attribute table is added

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)