Skip to content

Commit

Permalink
Merge branch 'master' of github.com:traitecoevo/saltbush
Browse files Browse the repository at this point in the history
  • Loading branch information
wcornwell committed Nov 20, 2024
2 parents 88adbb1 + 8487ce0 commit 2233081
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 160 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(calculate_chv)
export(calculate_chv_nopca)
export(calculate_cv)
export(calculate_field_diversity)
Expand Down
15 changes: 10 additions & 5 deletions R/calculate_field_diversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ calculate_field_diversity <- function(survey_data){

# loop thru each unique site
for (survey in ausplot_surveys) {
# Filter data for the current site
# filter data for the current site
site_survey_data <- survey_data |>
dplyr::filter(site_unique == survey)

Expand All @@ -48,19 +48,24 @@ calculate_field_diversity <- function(survey_data){
inv_simpson <- vegan::diversity(community_matrix[, -1], index = 'invsimpson')

subplot_diversity <- subplot_diversity |>
dplyr::mutate(shannon_diversity = shannon_diversity,
dplyr::mutate(site_location_name = substr(survey, 1, 10),
shannon_diversity = shannon_diversity,
simpson_diversity = simpson_diversity,
pielou_evenness = shannon_diversity / log(species_richness),
exp_shannon = exp(shannon_diversity),
inv_simpson = inv_simpson,
survey = survey)
inv_simpson = inv_simpson)

# order columns
subplot_diversity <- subplot_diversity |>
dplyr::select(site_location_name, everything())


# store result for current site
all_site_results[[survey]] <- subplot_diversity
}

# combine into one df
field_diversity <- dplyr::bind_rows(all_site_results, .id = "site")
field_diversity <- dplyr::bind_rows(all_site_results, .id = "site_unique")

return(list(
field_diversity = field_diversity,
Expand Down
21 changes: 4 additions & 17 deletions R/calculate_spectral_metrics.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' @title Calculate Spectral Metrics
#' @description Calculates CV, SV, and CHV from a pixel values dataframe with columns for each wavelength, `site_name`, and `aoi_id`.
#' This help file applies to the functions `calculate_cv`, `calculate_sv`, `calculate_chv`, `calculate_chv_nopca`, and `calculate_spectral_metrics`.
#' This help file applies to the functions `calculate_cv`, `calculate_sv`, `calculate_chv_nopca`, and `calculate_spectral_metrics`.
#' @param pixel_values_df A data frame containing pixel values, typically obtained from the `extract_pixel_values` function.
#' @param wavelengths A list of wavelengths that correspond to column names in `pixel_values_df`.
#' @param rarefaction Logical; if TRUE, applies a rarefaction step that increases processing time.
#' @param min_points Integer; minimum number of pixels per `aoi` to standardize uneven pixel numbers across sites (used if `rarefaction = TRUE`).
#' @param n Integer; number of subset permutations if `rarefaction = TRUE`.
#' @return A dataframe containing spectral metrics for each `aoi` within each site/raster.
#' @aliases calculate_cv calculate_sv calculate_chv calculate_chv_nopca calculate_spectral_metrics
#' @aliases calculate_cv calculate_sv calculate_chv_nopca calculate_spectral_metrics
#' @export
#' @import data.table
#' @examples
Expand Down Expand Up @@ -99,20 +99,7 @@ calculate_sv <- function(pixel_values_df, wavelengths) {
}


# chv function
#' @import data.table
#' @export
calculate_chv <- function(df, dim) {
CHV_df <- df |>
select(1:dim)

# convert to matrix
CHV_matrix <- as.matrix(CHV_df)

# calculate chv
CHV <- geometry::convhulln(CHV_matrix, option = "FA")
return(CHV)
}

# function to calculate chv for each aoi
#' @import data.table
Expand All @@ -126,7 +113,7 @@ calculate_chv_nopca <- function(df,
# convert to data.table for better performance
setDT(df)

results <- data.table::data.table(aoi_id = double(), CHV_nopca = double())
results <- data.table::data.table(aoi_id = double(), CHV = double())

# loop through each aoi_id
for (aoi in unique(df$aoi_id)) {
Expand Down Expand Up @@ -155,7 +142,7 @@ calculate_chv_nopca <- function(df,
}

# store results in data.table
results <- rbind(results, data.table::data.table(aoi_id = aoi, CHV_nopca = mean_chv), fill = TRUE)
results <- rbind(results, data.table::data.table(aoi_id = aoi, CHV = mean_chv), fill = TRUE)
}

return(results)
Expand Down
2 changes: 1 addition & 1 deletion R/create_masked_raster.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @title Create Masked Raster
#' @description Creates a masked raster, non-vegetation pixels using input ndvi & nir threshold values
#' @description Creates a masked raster, removes non-vegetation pixels using input ndvi & nir threshold values
#' @param input can be a directory containing multiple tif/envi files, string of files, or single file.
#' @param output_dir an output directory for the masked raster/s to be saved
#' @param ndvi_threshold NDVI threshold (values beneath this contain non-veg pixels and should be masked)
Expand Down
13 changes: 4 additions & 9 deletions R/extract_pixel_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
#' @description Extracts pixel values from each layer of multiband image and creates pixel value df
#' @param raster_files directory of input raster files
#' @param aoi_files area of interest file - shapefile containing one or more site polygons for each raster
#' @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
#' aoi_files <- list.files('inst/extdata/fishnet',
#' pattern = '_fishnet.shp$', full.names = TRUE)
#' aoi_files <- list.files('inst/extdata/aoi',
#' pattern = 'image_aoi.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'))
#' pixelvalues <- extract_pixel_values(raster_files, aoi_files)
#' @export

extract_pixel_values <- function(raster_files, aoi_files, wavelength_names){
extract_pixel_values <- function(raster_files, aoi_files){

all_pixel_values_list <- list()

Expand All @@ -32,9 +30,6 @@ extract_pixel_values <- function(raster_files, aoi_files, wavelength_names){
# read in raster file
raster_data <- raster::stack(raster_file)

# apply names - should be saved in wavelength order as per sect 1 of this script
names(raster_data) <- wavelength_names

# create empty list
pixel_values_list <- list()

Expand Down
1 change: 0 additions & 1 deletion R/saltbush-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#'
#' * [calculate_cv]
#' * [calculate_sv]
#' * [calculate_chv]
#' * [calculate_chv_nopca]
#' * [calculate_spectral_metrics]
#' * [create_masked_raster]
Expand Down
41 changes: 37 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,47 @@ library(saltbush)
remotes::install_github("traitecoevo/saltbush")
```

## Usage

## Spectral metrics

### Spectral diversity metrics:

+ co-efficient of variance (CV)
+ spectral variance (SV)
+ convex hull volume (CHV)

1. List raster files and area of interest files

```{r}
raster_files <- list.files(system.file("extdata/example", package = "saltbush"),
pattern = '.tif$', full.names = TRUE)
aoi_files <- list.files(system.file("extdata/aoi", package = "saltbush"),
pattern = 'NSABHC0009_aoi.shp$', full.names = TRUE)
```

2. Extract pixel values

```{r}
pixel_values <- extract_pixel_values(raster_files,
aoi_files)
head(pixel_values)
```

3. Calculate spectral metrics
```{r}
metrics <- calculate_spectral_metrics(pixel_values, masked = F, wavelengths = colnames(pixel_values[, 2:6]), rarefaction = F)
head(metrics)
```

## Taxonomic metrics

### Taxonomic diversity metrics:

+ species richness
Expand All @@ -49,17 +84,15 @@ library(saltbush)
+ exponential shannon's index
+ inverse simpson's index

## Usage

1. Download example plot data from AusPlots. The `veg.PI` part extracts the point intercept data from the AusPlots data structure.

```{r}
my.data <- ausplotsR::get_ausplots(my.Plot_IDs=c("SATFLB0004", "QDAMGD0022", "NTASTU0002"), veg.PI=TRUE)$veg.PI
```

2. Calculate diversity from the point intercepts using different diversity metrics
2. Calculate diversity from the point intercepts using different diversity metrics. The output is a list which includes taxonomic metrics, and also community matrices for checks.

```{r}
field_diversity <- calculate_field_diversity(my.data)
field_diversity
field_diversity$field_diversity
```
Loading

0 comments on commit 2233081

Please sign in to comment.