Skip to content

Commit

Permalink
fix #20 - texture_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Tompalski committed Mar 15, 2024
1 parent 33e8b23 commit 3104c2c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(.metrics_rumple)
export(.metrics_set1)
export(.metrics_set2)
export(.metrics_set3)
export(.metrics_texture)
export(.metrics_voxels)
export(metrics_HOME)
export(metrics_Lmoments)
Expand All @@ -34,5 +35,6 @@ export(metrics_rumple)
export(metrics_set1)
export(metrics_set2)
export(metrics_set3)
export(metrics_texture)
export(metrics_voxels)
importFrom(magrittr,"%>%")
14 changes: 8 additions & 6 deletions R/metrics_texture.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' Canopy texture metrics
#'
#' Generates a suite of GLCM (Grey-Level Co-Occurence Matrix) metrics of a canopy height model (CHM). CHM is calculated on the fly
#' to allow easy integration with e.g. \code{lidR::grid_metrics} function.
#' to allow easy integration with e.g. \code{lidR::pixel_metrics} function.
#'
#' @inheritParams metrics_rumple
#' @param chm_algorithm Function used to generate the CHM. By default \code{lidR::p2r(na.fill = lidR::knnidw())} is used.
#' @param ... additional parameters passed to \code{ForestTools::glcm_img()}.
#' @param ... additional parameters passed to \code{ForestTools::glcm()}.
#' @return A list. GLCM metrics.
#'
#' @details Function first uses the \code{lidR::grid_canopy()} algorithm to create a CHM.
#' \code{ForestTools::glcm_img()} function is then used to calculate GLCM statistics (see package manual for details).
#' @details Function first uses the \code{lidR::rasterize_canopy()} algorithm to create a CHM.
#' \code{ForestTools::glcm()} function is then used to calculate GLCM statistics (see package manual for details).
#' This implementation of GLCM does not allow for missing values - after CHM is created, any missing values are converted to 0.
#'
#'
Expand All @@ -22,6 +22,7 @@
#' m1 <- cloud_metrics(las, ~metrics_texture(x = X, y = Y, z = Z, pixel_size = 1))
#'
#' m2 <- pixel_metrics(las, ~metrics_texture(x = X, y = Y, z = Z, pixel_size = 1), res = 20)
#' @export


metrics_texture <- function(x, y, z, pixel_size, zmin=NA, chm_algorithm = NULL, ...) {
Expand Down Expand Up @@ -53,15 +54,15 @@ metrics_texture <- function(x, y, z, pixel_size, zmin=NA, chm_algorithm = NULL,
D <- lidR::LAS(D, header = rlas::header_create(D), check=F)

#create chm
chm <- lidR::grid_canopy(D, res = pixel_size, algorithm = chm_algorithm)
chm <- lidR::rasterize_canopy(D, res = pixel_size, algorithm = chm_algorithm)

# glcm_img does not accept NA values.
# NAs are replaced with 0
chm[is.na(chm)] <- 0

#calculating texture
try({
tex <- ForestTools::glcm_img(chm, ...)
tex <- ForestTools::glcm(chm, ...)
}, silent = T)

}
Expand All @@ -70,6 +71,7 @@ metrics_texture <- function(x, y, z, pixel_size, zmin=NA, chm_algorithm = NULL,
}

#' @rdname metrics_texture
#' @export
.metrics_texture = ~metrics_texture(X,Y,Z,1)


8 changes: 4 additions & 4 deletions man/metrics_texture.Rd

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

0 comments on commit 3104c2c

Please sign in to comment.