diff --git a/DESCRIPTION b/DESCRIPTION index 75294a5..e798b7a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,6 +14,8 @@ Imports: pROC, raster, data.table, + stringr, + sf, geometry, ausplotsR, terra diff --git a/R/extract_pixel_values.R b/R/extract_pixel_values.R index ffa447f..5deab30 100644 --- a/R/extract_pixel_values.R +++ b/R/extract_pixel_values.R @@ -20,13 +20,13 @@ extract_pixel_values <- function(raster_files, aoi_files, wavelength_names){ for (raster_file in raster_files) { # identify the string that represents the site name - site_name <- str_extract(basename(raster_file), "^[^_]+") + site_name <- stringr::str_extract(basename(raster_file), "^[^_]+") #choose the corresponding subplot file aoi_file <- aoi_files[grep(paste0('^', site_name), basename(aoi_files))] # read in aoi file and select geometries - aois <- read_sf(aoi_file) %>% + aois <- sf::read_sf(aoi_file) %>% dplyr::select('geometry') # read in raster file @@ -50,11 +50,11 @@ extract_pixel_values <- function(raster_files, aoi_files, wavelength_names){ aoi_sp <- as(aoi, "Spatial") # crop and mask raster using current subplot - cropped_raster <- crop(raster_data, aoi_sp) - masked_raster <- mask(cropped_raster, aoi_sp) + cropped_raster <- raster::crop(raster_data, aoi_sp) + masked_raster <- raster::mask(cropped_raster, aoi_sp) # extract pixel values - pixel_values <- as.data.frame(getValues(masked_raster)) + pixel_values <- as.data.frame(raster::getValues(masked_raster)) # add subplot id to pixel values df pixel_values$aoi_id <- aoi_id @@ -64,13 +64,13 @@ extract_pixel_values <- function(raster_files, aoi_files, wavelength_names){ } # combined all pixel values into one df for current raster - all_pixel_values <- bind_rows(pixel_values_list) %>% + all_pixel_values <- dplyr::bind_rows(pixel_values_list) %>% na.omit() # add to overall list with all raster data pixel values all_pixel_values_list[[site_name]] <- all_pixel_values } - combined_values <- bind_rows(all_pixel_values_list, .id = 'site_name') + combined_values <- dplyr::bind_rows(all_pixel_values_list, .id = 'site_name') return(combined_values) } diff --git a/R/find_optimum_thresholds.R b/R/find_optimum_thresholds.R index 8ece462..44e314a 100644 --- a/R/find_optimum_thresholds.R +++ b/R/find_optimum_thresholds.R @@ -11,7 +11,9 @@ #' point = rep(1:100, times = 2), #' ndvi = runif(200, min = -1, max = 1), #' class = sample(rep(c('veg', 'non-veg'), each = 100))) -#' ndvi_thresholds <- find_optimum_thresholds(ndvi_values, class_col = 'class', band_or_index_col = 'ndvi', site_col = 'site', class_value = 'non-veg') +#' ndvi_thresholds <- find_optimum_thresholds(ndvi_values, +#' class_col = 'class', band_or_index_col = 'ndvi', +#' site_col = 'site', class_value = 'non-veg') #' @export diff --git a/man/find_optimum_thresholds.Rd b/man/find_optimum_thresholds.Rd index 3caf03e..c6b8fc3 100644 --- a/man/find_optimum_thresholds.Rd +++ b/man/find_optimum_thresholds.Rd @@ -34,5 +34,7 @@ ndvi_values <- data.frame(site = rep(c("site_one", "site_two"), each = 100), point = rep(1:100, times = 2), ndvi = runif(200, min = -1, max = 1), class = sample(rep(c('veg', 'non-veg'), each = 100))) -ndvi_thresholds <- find_optimum_thresholds(ndvi_values, class_col = 'class', band_or_index_col = 'ndvi', site_col = 'site', class_value = 'non-veg') +ndvi_thresholds <- find_optimum_thresholds(ndvi_values, +class_col = 'class', band_or_index_col = 'ndvi', +site_col = 'site', class_value = 'non-veg') } diff --git a/tests/testthat/test-test_spectral.R b/tests/testthat/test-test_spectral.R index 2782be3..e1ac8fe 100644 --- a/tests/testthat/test-test_spectral.R +++ b/tests/testthat/test-test_spectral.R @@ -14,3 +14,15 @@ test_that("calculate_cv works", { expect_true(pixelvalues$CV<0.6) expect_true(pixelvalues$aoi_id==1) }) + +test_that("extract_pixel_values works", { + aoi_files <- list.files('../../inst/extdata/fishnet', + pattern = '_fishnet.shp$', full.names = TRUE) + raster_files <- list.files('../../inst/extdata/multiband_image', + pattern = '.tif$', full.names = TRUE) + pixelvalues <- extract_pixel_values(raster_files, aoi_files, + c('blue', 'green', 'red', 'red_edge', 'nir')) + expect_true(dim(pixelvalues)[1]>20000) + expect_true(mean(pixelvalues[,3])>0.05 & mean(pixelvalues[,3])<0.06) +}) +