在 functionFindTreesCHM
中,在第 17-18 行,我们发现:
XYmax <- SpatialPoints(xyFromCell(setNull, Which(setNull ==
1, cells = TRUE)))
这会创建一个SpatialPoints
. 问题是对象没有投影集:
projection(XYmax)
# [1] NA
然后,第 19 行
htExtract <- over(XYmax, as(chm, "SpatialGridDataFrame"))
抛出错误,因为XYmax
没有投影集,而chm
有:
projection(chm)
# [1] "+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
作为函数over
首先检查对象的投影,我们得到错误:
identicalCRS(XYmax, as(chm, "SpatialGridDataFrame"))
# [1] FALSE
一种解决方法是编写自己的函数,添加一条将 的投影设置XYmax
为 的投影的行chm
。
此外,由于第 21 行,第 22 行引发了错误。
这个功能可以很容易地修复,但我强烈建议联系包的维护者(maintainer("rLiDAR")
)。
这是一种可能的解决方法:
library(rLiDAR)
library(raster)
FindTreesCHM.fix <- function(chm, fws = 5, minht = 1.37)
{
if (class(chm)[1] != "RasterLayer") {
chm <- raster(chm)
}
if (class(fws) != "numeric") {
stop("The fws parameter is invalid. It is not a numeric input")
}
if (class(minht) != "numeric") {
stop("The minht parameter is invalid. It is not a numeric input")
}
w <- matrix(c(rep(1, fws * fws)), nrow = fws, ncol = fws)
chm[chm < minht] <- NA
f <- function(chm) max(chm)
rlocalmax <- focal(chm, fun = f, w = w, pad = TRUE, padValue = NA)
setNull <- chm == rlocalmax
XYmax <- SpatialPoints(xyFromCell(setNull, Which(setNull ==
1, cells = TRUE)))
projection(XYmax) <- projection(chm)
htExtract <- over(XYmax, as(chm, "SpatialGridDataFrame"))
treeList <- cbind(slot(XYmax, "coords"), htExtract)
colnames(treeList) <- c("x", "y", "height")
return(treeList)
}
chm <- raster("dem_test.tif")
schm <- CHMsmoothing(chm, "mean", 5)
fws <- 5
minht <- 8.0
treeList <- FindTreesCHM.fix(schm, fws, minht)
# x y height
# 1 256886.5 4110940 14.1200
# 2 256805.5 4110884 13.8384
# 3 256756.5 4110880 15.2004
# 4 256735.5 4110874 17.6100
# 5 256747.5 4110840 18.2592
# 6 256755.5 4110828 19.9252
# 7 256777.5 4110806 12.7180
# 8 256780.5 4110802 14.6512
# 9 256780.5 4110792 15.8532
# 10 256763.5 4110786 18.7128
# 11 256766.5 4110764 14.4972