Skip to content

Commit

Permalink
Merge pull request #57 from umr-amap/subplot_summary
Browse files Browse the repository at this point in the history
Subplot summary
  • Loading branch information
ArthurBailly authored Dec 2, 2024
2 parents c21891d + 18cec95 commit 6172baf
Show file tree
Hide file tree
Showing 25 changed files with 556 additions and 357 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Suggests:
rmarkdown,
prettydoc,
testthat,
vdiffr,
curl,
geodata,
httr2,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export(modelHD)
export(numberCorner)
export(predictHeight)
export(retrieveH)
export(subplot_summary)
export(summaryByPlot)
importFrom(data.table,"%between%")
importFrom(data.table,"%chin%")
Expand Down Expand Up @@ -51,12 +52,14 @@ importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_polygon)
importFrom(ggplot2,geom_segment)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,geom_smooth)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_color_manual)
importFrom(ggplot2,scale_fill_gradientn)
importFrom(ggplot2,scale_shape_manual)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,scale_y_continuous)
Expand Down Expand Up @@ -84,6 +87,7 @@ importFrom(sf,st_sfc)
importFrom(stats,SSmicmen)
importFrom(stats,as.formula)
importFrom(stats,coef)
importFrom(stats,dist)
importFrom(stats,formula)
importFrom(stats,lm)
importFrom(stats,median)
Expand All @@ -102,5 +106,6 @@ importFrom(terra,vect)
importFrom(utils,askYesNo)
importFrom(utils,data)
importFrom(utils,download.file)
importFrom(utils,globalVariables)
importFrom(utils,head)
importFrom(utils,unzip)
20 changes: 20 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
* version 2.2
- Modified bilinear interpolation to generalized bilinear interpolation that can deal with rectangular plot and non (0;0) origin corner plot
- Added check_plot_coord function which will replace correctCoordGPS() :
- calculation of projected coordinates of plot corners depend on trust_GPS_corners argument : if TRUE, a bilinear interpolation is applied (after averaging plot corners and removing outliers) to retrieve tree projected coordinates, if FALSE a procrust analyses is applied as before
- rangeX and rangeY are no longer needed : they are calculated using relative coordinates dimensions of the plot corners
- a ggplot is returned as output instead of a plot of the plot
- calculation of the projected coordinates of the trees is done, using tree_df and tree_coords arguments
- Added divide_plot function which will replace cutPlot()
- No corner numerotation is needed anymore, neither dimX and dimY (the dimensions of the plot)
- plot division is made on relative coordinates and then transformed in projected coordinates (if supplied) with a bilinear interpolation
- trees are attributed to each subplot using tree_df and tree_coords agruments
- Message warning if the grid dimensions don't match the plot dimensions
- Used of the argument grid_tol to control the plot area which is included in the grid
- The grid can be plot-centred using centred_grid argument
- Added subplot_summary which will replace summaryByPlot
- the function can take any tree metric using the value argument
- any valid function can be applied as a summary
- a ggplot is returned as output instead of just plot the plot division
- Added "Spatialized trees and forest stands metrics with BIOMASS" vignette which will replace the "Manage trees and plot coordinates with BIOMASS" vignette

* version 2.1.14
- Fix an issue about the inversion of subplot locations in cutPlot
- Change the automatic corner numbering from counter-clockwise to clockise direction in cutPlot
Expand Down
9 changes: 9 additions & 0 deletions R/BIOMASS-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#' @keywords internal
"_PACKAGE"

# The following block is used by usethis to automatically manage
# roxygen namespace tags. Modify with care!
## usethis namespace: start
## usethis namespace: end

NULL
2 changes: 1 addition & 1 deletion R/bilinear_interpolation.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bilinear_interpolation = function(coord, from_corner_coord, to_corner_coord, ord
}

# Verification of a rectangular plot for from_corner_coord
if(!all(abs(dist(rbind(from_corner_coord[,1:2],centroid))[c(4,7,9,10)] - mean(dist(rbind(from_corner_coord[,1:2],centroid))[c(4,7,9,10)]))<0.1)) {
if(!all(abs(stats::dist(rbind(from_corner_coord[,1:2],centroid))[c(4,7,9,10)] - mean(stats::dist(rbind(from_corner_coord[,1:2],centroid))[c(4,7,9,10)]))<0.1)) {
stop("The plot in the relative coordinate system is not a rectangle (or a square). You may consider using trustGPScorners = F")
}

Expand Down
2 changes: 1 addition & 1 deletion R/checkPlotCoord.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ checkPlotCoord <- function(projCoord = NULL, longlat = NULL, relCoord, trustGPSc

cornerCoord <- data.frame(cbind(cornerProjCoord[,1:2], cornerRelCoord[,1:2]))
colnames(cornerCoord) <- c("x_proj","y_proj","x_rel","y_rel")

if(!is.null(cornerID)) cornerCoord$cornerID <- unique(cornerID)
} # End trustGPScorners = "FALSE"

Expand Down
2 changes: 0 additions & 2 deletions R/check_plot_coord.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
#' draw_plot = TRUE
#' )
#' }
#'
check_plot_coord <- function(proj_coord = NULL, longlat = NULL, rel_coord, trust_GPS_corners, corner_ID=NULL, max_dist = 15, rm_outliers = TRUE, draw_plot = TRUE, tree_df = NULL, tree_coords=NULL) {

# Checking arguments -------------------------------------------------
Expand Down Expand Up @@ -136,7 +135,6 @@ check_plot_coord <- function(proj_coord = NULL, longlat = NULL, rel_coord, trust
stop(paste(length((unique(corner_ID))),"unique corners are detected in corner_ID instead of 4"))
}


# function -------------------------------------------------------------------

# Transform the geographic coordinates into UTM coordinates
Expand Down
2 changes: 1 addition & 1 deletion R/correctTaxo.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if (getRversion() >= "2.15.1") {
#' @author Ariane TANGUY, Arthur PERE, Maxime REJOU-MECHAIN, Guillaume CORNU
#'
#' @examples
#' \donttest{
#' \dontrun{
#' correctTaxo(genus = "Astrocarium", species = "standleanum")
#' correctTaxo(genus = "Astrocarium standleanum")
#' }
Expand Down
28 changes: 16 additions & 12 deletions R/divide_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#' @details
#' If corner coordinates in the projected coordinate system are supplied (proj_coord), projected coordinates of subplot corners are calculated by a bilinear interpolation in relation with relative coordinates of plot corners. Be aware that this bilinear interpolation only works if the plot in the relative coordinates system is rectangular (ie, has 4 right angles).
#'
#' @param rel_coord a data frame containing the relative (local) coordinates of plot corners, with X and Y corresponding to the first and second column respectively
#' @param proj_coord a data frame containing the projected coordinates of plot corners, with X and Y corresponding to the first and second column respectively, and with the same row order than rel_coord
#' @param rel_coord a matrix or data frame containing the relative (local) coordinates of plot corners, with X and Y corresponding to the first and second column respectively
#' @param proj_coord a matrix or data frame containing the projected coordinates of plot corners, with X and Y corresponding to the first and second column respectively, and with the same row order than rel_coord
#' @param grid_size a vector indicating the dimensions of grid cells (resp. X and Y dimensions). If only one value is given, grid cells will be considered as squares.
#' @param tree_df a data frame containing tree relative coordinates and other tree metrics (one row per tree).
#' @param tree_coords a character vector of size 2 containing the column names of the relative coordinates of the trees.
Expand All @@ -29,6 +29,7 @@
#' @export
#' @author Arthur PERE, Arthur BAILLY
#' @importFrom data.table data.table := setcolorder
#' @importFrom stats dist
#' @examples
#'
#' # Rectangular plot and grid cells
Expand All @@ -37,12 +38,13 @@
#'
#' # Squared plot and projected coordinates associated
#' rel_coord <- data.frame(x_rel = c(0, 200, 0, 200), y_rel = c(0, 0, 200, 200))
#' proj_coord <- data.frame(x_proj = c(110, 190, 60, 145), y_proj = c(110, 160, 196, 245))
#' proj_coord <- data.frame(x_proj = c(210, 383, 110, 283), y_proj = c(210, 310, 383, 483))
#' subplots <- divide_plot(rel_coord, proj_coord = proj_coord, grid_size = 100)
#'
#' # Assigning trees to subplots
#' tree_df <- data.frame(x_tree = runif(50,0,200), y_tree = runif(50,0,200))
#' subplots <- divide_plot(rel_coord, proj_coord, 100, tree_df = tree_df, tree_coords = c("x_tree","y_tree"))
#' subplots <- divide_plot(rel_coord, proj_coord, 100,
#' tree_df = tree_df, tree_coords = c("x_tree","y_tree"))
#' subplots$sub_corner_coord
#' subplots$tree_df
#'
Expand All @@ -56,17 +58,19 @@
#' tree_df <- rbind(tree_df, data.frame(x_tree = runif(50,0,200), y_tree = runif(50,0,200)))
#' corner_plot_ID <- rep(c("plot1","plot2"), e=4)
#' tree_plot_ID <- rep(c("plot1","plot2"), e=50)
#' subplots <- divide_plot(rel_coord, proj_coord, 100, tree_df, c("x_tree","y_tree"), corner_plot_ID, tree_plot_ID)
#' subplots <- divide_plot(rel_coord, proj_coord, 100,
#' tree_df, c("x_tree","y_tree"),
#' corner_plot_ID, tree_plot_ID)
#'

divide_plot <- function(rel_coord, proj_coord = NULL, grid_size, tree_df = NULL, tree_coords = NULL, corner_plot_ID = NULL, tree_plot_ID = NULL, grid_tol = 0.1, centred_grid = F) {

# Parameters verification ----------------------------------------------------
# Checking parameters --------------------------------------------------------
if (is.matrix(rel_coord)) {
rel_coord <- data.frame(x_rel=rel_coord[,1],y_rel=rel_coord[,2])
rel_coord <- data.frame(rel_coord)
}
if (!is.null(proj_coord) && !is.data.frame(proj_coord)) {
proj_coord <- data.frame(x_proj=proj_coord[,1], y_proj=proj_coord[,2])
proj_coord <- data.frame(proj_coord)
}
if (!is.null(corner_plot_ID) && nrow(rel_coord) != length(corner_plot_ID)) {
stop("The length of corner_plot_ID and the number of rows of rel_coord are different")
Expand Down Expand Up @@ -106,7 +110,7 @@ divide_plot <- function(rel_coord, proj_coord = NULL, grid_size, tree_df = NULL,
}


# Formatting data -----------------------------------------------------------
# Data processing ------------------------------------------------------------

if(length(grid_size)!=2) grid_size = rep(grid_size,2)

Expand All @@ -117,7 +121,6 @@ divide_plot <- function(rel_coord, proj_coord = NULL, grid_size, tree_df = NULL,
} else{
rel_coord[,corner_plot_ID:=""]
}
setcolorder(rel_coord , "corner_plot_ID" , after = 2) # if rel_coord has more than 2 col

# Sorting rows in a counter-clockwise direction and check for non-rectangular plot
sort_rows <- function(dat) {
Expand Down Expand Up @@ -187,6 +190,7 @@ divide_plot <- function(rel_coord, proj_coord = NULL, grid_size, tree_df = NULL,
sub_corner_coord <- combine_coord[, divide_plot_fct(.SD, grid_size), by = corner_plot_ID, .SDcols = colnames(combine_coord)]

# Assigning trees to subplots -------------------------------------------------------------

if(!is.null(tree_df)) {
tree_df <- as.data.table(tree_df)

Expand All @@ -205,13 +209,13 @@ divide_plot <- function(rel_coord, proj_coord = NULL, grid_size, tree_df = NULL,
if (anyNA(tree_df[, subplot_id])) {
warning("One or more trees could not be assigned to a subplot (not in a subplot area)")
}

if(is.null(tree_plot_ID)) {
tree_df[ , c("subplot_id","plot_id") := list(paste0("subplot",subplot_id),NULL)]
}
}


# returns --------------------------------------------------------------------
# Returns --------------------------------------------------------------------

if(is.null(corner_plot_ID)) {
sub_corner_coord[ , c("subplot_id","corner_plot_ID") := list(paste0("subplot",subplot_id),NULL)]
Expand Down
19 changes: 19 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' @importFrom utils globalVariables


if(getRversion() >= "2.15.1") {
# modelHD function
utils::globalVariables(c("x","y"))

# check_plot_coord function
utils::globalVariables(c("x_proj","y_proj","outlier","Xmean","Ymean","row_number","whatpoint","Xend","Yend"))

# checkPlotCoord function
utils::globalVariables("nRow")

# divide_plot function
utils::globalVariables(c("x_rel","y_rel","subplot_id","plot_id"))

# subplot_summary function
utils::globalVariables(c("subplot_id","plot_id",".data"))
}
Loading

0 comments on commit 6172baf

Please sign in to comment.