From 584d93be951365fb26ffee38ef4730fdf4197b7c Mon Sep 17 00:00:00 2001 From: Will Cornwell Date: Fri, 15 Nov 2024 16:32:12 +1100 Subject: [PATCH] more organizing of examples --- R/calculate_spectral_metrics.R | 25 +++++++++++++------------ R/create_masked_raster.R | 9 +++++++++ R/extract_pixel_values.R | 18 ++++++------------ R/find_optimum_thresholds.R | 8 ++++++-- man/calculate_cv.Rd | 20 ++++++++++---------- man/create_masked_raster.Rd | 10 ++++++++++ man/extract_pixel_values.Rd | 18 ++++++------------ man/find_optimum_thresholds.Rd | 6 +++++- 8 files changed, 65 insertions(+), 49 deletions(-) diff --git a/R/calculate_spectral_metrics.R b/R/calculate_spectral_metrics.R index 419117a..a4f033f 100644 --- a/R/calculate_spectral_metrics.R +++ b/R/calculate_spectral_metrics.R @@ -15,16 +15,17 @@ #' @examples #' set.seed(123) #' df <- data.frame( -#' site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000), -#' aoi_id = 1, -#' blue = runif(20000, min = 0, max = 1), -#' green = runif(20000, min = 0, max = 1), -#' red = runif(20000, min = 0, max = 1), -#' red_edge = runif(20000, min = 0, max = 1), -#' nir = runif(20000, min = 0, max = 1)) -#' pixelvalues <- calculate_cv(df, -#' wavelengths = c('blue','green','red','red_edge','nir'), -#' rarefaction = TRUE, min_points = 5000, n = 999) +#' site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000), +#' aoi_id = 1, +#' blue = runif(20000, min = 0, max = 1), +#' green = runif(20000, min = 0, max = 1), +#' red = runif(20000, min = 0, max = 1), +#' red_edge = runif(20000, min = 0, max = 1), +#' nir = runif(20000, min = 0, max = 1)) +#' spectral_metrics <- calculate_cv(df, +#' wavelengths = c('blue','green','red','red_edge','nir'), +#' rarefaction = TRUE, min_points = 5000, n = 999) + calculate_cv <- function(pixel_values_df, wavelengths, rarefaction = FALSE, @@ -72,7 +73,7 @@ calculate_cv <- function(pixel_values_df, -# sv function\ +# sv function #' @import data.table #' @export calculate_sv <- function(pixel_values_df, wavelengths) { @@ -178,7 +179,7 @@ calculate_spectral_metrics <- function(pixel_values_df, # calculate metrics, pass rarefaction where needed cv <- calculate_cv(site_pixel_values, wavelengths = wavelengths, rarefaction = rarefaction, n = n, min_points = min_points) sv <- calculate_sv(site_pixel_values, wavelengths = wavelengths) - chv <- calculate_chv_nopca(site_pixel_values, wavelengths, rarefaction = rarefaction, min_points = min_points) + chv <- calculate_chv_nopca(site_pixel_values, wavelengths, rarefaction = rarefaction, n = n, min_points = min_points) results[[site]] <- list(CV = cv, SV = sv, CHV = chv) } diff --git a/R/create_masked_raster.R b/R/create_masked_raster.R index ba1b8a0..03da7eb 100644 --- a/R/create_masked_raster.R +++ b/R/create_masked_raster.R @@ -8,6 +8,15 @@ #' @param nir_threshold_df optional - a two columned df with 'site' col (site values must match first string of input file name) and threshold values for each file #' @param red_band_index layer number for red band #' @param nir_band_index layer number for nir band +#' @examples +#' input_folder <- 'inst/extdata/create_multiband_image' +#' output_folder <- tempdir() +#' # create_masked_raster(input_folder, +#' # output_dir = output_folder, +#' # ndvi_threshold = 0.02, +#' # nir_threshold = 0.04, +#' # red_band_index = 3, +#' # nir_band_index = 5) #' @return A masked raster image, saved in the output directory #' @export diff --git a/R/extract_pixel_values.R b/R/extract_pixel_values.R index e7921a6..ffa447f 100644 --- a/R/extract_pixel_values.R +++ b/R/extract_pixel_values.R @@ -5,18 +5,12 @@ #' @param wavelength_names wavelength names for each band, must match order of stacked layers #' @return a df with pixel values for each of the image layers #' @examples -#' set.seed(123) -#' df <- data.frame( -#' site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000), -#' aoi_id = 1, -#' blue = runif(20000, min = 0, max = 1), -#' green = runif(20000, min = 0, max = 1), -#' red = runif(20000, min = 0, max = 1), -#' red_edge = runif(20000, min = 0, max = 1), -#' nir = runif(20000, min = 0, max = 1)) -#' pixelvalues <- calculate_cv(df, -#' wavelengths = c('blue','green','red','red_edge','nir'), -#' rarefaction = TRUE, min_points = 5000, n = 999) +#' 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')) #' @export extract_pixel_values <- function(raster_files, aoi_files, wavelength_names){ diff --git a/R/find_optimum_thresholds.R b/R/find_optimum_thresholds.R index 82a207b..8ece462 100644 --- a/R/find_optimum_thresholds.R +++ b/R/find_optimum_thresholds.R @@ -6,9 +6,13 @@ #' @param class_col name of column where object class are stored #' @param class_value value/s of class that should be masked - e.g. for ndvi, 'non-veg' or c('bare_ground','rocks'), for nir, 'shadow' #' @return a df with optimum thresholds for each site -#' @export #' @examples -#' rnorm(500) +#' 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') +#' @export # add a for loop so nir and ndvi ground truth values can be given in the same df instead of seperately :) diff --git a/man/calculate_cv.Rd b/man/calculate_cv.Rd index 2dfc56f..862ce10 100644 --- a/man/calculate_cv.Rd +++ b/man/calculate_cv.Rd @@ -37,14 +37,14 @@ This help file applies to the functions \code{calculate_cv}, \code{calculate_sv} \examples{ set.seed(123) df <- data.frame( -site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000), -aoi_id = 1, -blue = runif(20000, min = 0, max = 1), -green = runif(20000, min = 0, max = 1), -red = runif(20000, min = 0, max = 1), -red_edge = runif(20000, min = 0, max = 1), -nir = runif(20000, min = 0, max = 1)) -pixelvalues <- calculate_cv(df, -wavelengths = c('blue','green','red','red_edge','nir'), -rarefaction = TRUE, min_points = 5000, n = 999) + site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000), + aoi_id = 1, + blue = runif(20000, min = 0, max = 1), + green = runif(20000, min = 0, max = 1), + red = runif(20000, min = 0, max = 1), + red_edge = runif(20000, min = 0, max = 1), + nir = runif(20000, min = 0, max = 1)) +spectral_metrics <- calculate_cv(df, + wavelengths = c('blue','green','red','red_edge','nir'), + rarefaction = TRUE, min_points = 5000, n = 999) } diff --git a/man/create_masked_raster.Rd b/man/create_masked_raster.Rd index 07f880d..a0d42e8 100644 --- a/man/create_masked_raster.Rd +++ b/man/create_masked_raster.Rd @@ -38,3 +38,13 @@ A masked raster image, saved in the output directory \description{ Creates a masked raster, non-vegetation pixels using input ndvi & nir threshold values } +\examples{ +input_folder <- 'inst/extdata/create_multiband_image' +output_folder <- tempdir() +# create_masked_raster(input_folder, +# output_dir = output_folder, +# ndvi_threshold = 0.02, +# nir_threshold = 0.04, +# red_band_index = 3, +# nir_band_index = 5) +} diff --git a/man/extract_pixel_values.Rd b/man/extract_pixel_values.Rd index 17d32cb..3902227 100644 --- a/man/extract_pixel_values.Rd +++ b/man/extract_pixel_values.Rd @@ -20,16 +20,10 @@ a df with pixel values for each of the image layers Extracts pixel values from each layer of multiband image and creates pixel value df } \examples{ -set.seed(123) -df <- data.frame( -site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000), -aoi_id = 1, -blue = runif(20000, min = 0, max = 1), -green = runif(20000, min = 0, max = 1), -red = runif(20000, min = 0, max = 1), -red_edge = runif(20000, min = 0, max = 1), -nir = runif(20000, min = 0, max = 1)) -pixelvalues <- calculate_cv(df, -wavelengths = c('blue','green','red','red_edge','nir'), -rarefaction = TRUE, min_points = 5000, n = 999) +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')) } diff --git a/man/find_optimum_thresholds.Rd b/man/find_optimum_thresholds.Rd index 2ff1289..3caf03e 100644 --- a/man/find_optimum_thresholds.Rd +++ b/man/find_optimum_thresholds.Rd @@ -30,5 +30,9 @@ a df with optimum thresholds for each site Takes input ground truth data to find optimum thresholds for image masking. } \examples{ -rnorm(500) +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') }