0

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)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
james
  • 15
  • 3
  • Despite attempts at following what might have been a reproducible example but turned out not to be, I get `Error in raster::crs(CHM) : object 'schm' not found` from the line that starts `ttops <- vwf(CHM = schm`. Please read [ask] and [MCVE]. – IRTFM Mar 10 '21 at 17:10
  • With regret I'm voting to close, since it's not clear where the error is coming from. The "calculations yield wrong values" is impossible to debug since there's no data or even a description about what is "wrong". I tried putting in an object from one of the help pages in the ForestTools package, but it started throwing errors about object type. – IRTFM Mar 10 '21 at 23:31

0 Answers0