Skip to content

Commit

Permalink
fixing namespace issues that i created
Browse files Browse the repository at this point in the history
  • Loading branch information
wcornwell committed Nov 15, 2024
1 parent 584d93b commit f17a044
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Imports:
pROC,
raster,
data.table,
stringr,
sf,
geometry,
ausplotsR,
terra
Expand Down
14 changes: 7 additions & 7 deletions R/extract_pixel_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion R/find_optimum_thresholds.R
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion man/find_optimum_thresholds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-test_spectral.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit f17a044

Please sign in to comment.