defining custom function for crown area sum in ForestTools R package?

94 Views Asked by At

I'm using ForestTools package to segment trees from the chm raster, and calculating crown area. The package works quite fine, but I'm unable to calculate the sum of all crown areas within a specific grid of 30x30m. Is there a way to define the custom sum function, so that I can get sum of crown areas in every cell of the 30x30m. The code I'm using is below, but sum calculations yield wrong values.

if (!require(ForestTools) ){ install.packages("ForestTools", dependencies=TRUE);library(ForestTools)}
lin <- function(x){x * 0.3}
ttops <- vwf(CHM = schm, winFun = lin, minHeight = 1.5) # vwf function is variable window function based on developed by Popescu and Wynne (2004)
crowns <- mcws(treetops = ttops, CHM = schm, minHeight = 1, verbose = FALSE)
# Plot crowns
plot(crowns, col = sample(rainbow(50), length(unique(crowns[])), replace = TRUE), legend = FALSE, xlab = "", ylab = "", xaxt='n', yaxt = 'n')

# Create polygon crown map
crownsPoly <- mcws(treetops = ttops, CHM = schm, format = "polygons", minHeight = 1.5, verbose = FALSE)
# Compute average crown diameter
crownsPoly[["crownDiameter"]] <- sqrt(crownsPoly[["crownArea"]]/ pi) * 2
# Mean crown diameter
mean(crownsPoly$crownDiameter)
writeOGR(obj=crownsPoly,dsn=getwd(),layer="crowns_final", driver="ESRI Shapefile")

# Summed crown areas and quantile per grid
crowns_summed<-shapefile("crowns_final")
gridpolygon<-shapefile("X")
quant98 <- function(x, ...) quantile(x, c(.98), na.rm = TRUE)
custFuns <- list(quant98, sum)
names(custFuns) <- c("98thQuantile", "Sum")
# Generate statistics for crown areas and tree heights
Grid_summary<-sum(crowns_summed, areas=gridpolygon, variables = "area",statFuns=custFuns)
0

There are 0 best solutions below