identicalCRS(x, y) is not TRUE error in R for individual tree detection 使用 chm
identicalCRS(x, y) is not TRUE error in R for individual tree detection using chm
我有一个尺寸为 250*250 m^2 区域(森林区域)的 LiDAR 点云数据。我需要使用该数据分离出单独的树。
我使用 LASTools 创建了树冠高度模型 (CHM),并将该 CHM 用于树木描绘。我附上 chm file(此光栅将提供高度信息)
我尝试使用 rLiDAR
R 中可用的包。
我是这样编码的
library(rLiDAR)
schm <- CHMsmoothing(chm, "mean", 5)
# Setting the fws:
fws <- 5 # dimention 5x5
# Setting the specified height above ground for detectionbreak
minht <- 8.0
# Getting the individual tree detection list
treeList <- FindTreesCHM(schm, fws, minht)
但是报错
Error: identicalCRS(x, y) is not TRUE
我该如何克服这个问题?
在函数 FindTreesCHM
的第 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
我有一个尺寸为 250*250 m^2 区域(森林区域)的 LiDAR 点云数据。我需要使用该数据分离出单独的树。
我使用 LASTools 创建了树冠高度模型 (CHM),并将该 CHM 用于树木描绘。我附上 chm file(此光栅将提供高度信息)
我尝试使用 rLiDAR
R 中可用的包。
我是这样编码的
library(rLiDAR)
schm <- CHMsmoothing(chm, "mean", 5)
# Setting the fws:
fws <- 5 # dimention 5x5
# Setting the specified height above ground for detectionbreak
minht <- 8.0
# Getting the individual tree detection list
treeList <- FindTreesCHM(schm, fws, minht)
但是报错
Error: identicalCRS(x, y) is not TRUE
我该如何克服这个问题?
在函数 FindTreesCHM
的第 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