tiagodc / TreeLS

R functions for processing individual tree TLS point clouds
GNU General Public License v3.0
82 stars 27 forks source link

nnFilter issue and fix #35

Open spokswinski opened 3 years ago

spokswinski commented 3 years ago

I was playing with the nnFilter function and noticed that it throws an error. I then went to read the readme on cran and noticed that it was removed because of checks. One of the issues is that nnFilter failed. I dove into the code and noticed that the Las2xyz fuction was not defined so I used your code to rewrite the function so that it works, including a section of the Las2xyz that was missing, but archived somewhere. Here's my fix: (note that it doesn't check to see if the input is a LAS)

library(nabor)

nnFilter2 = function(las, d = 0.05, n = 2){ rnn = knn(las %>% las2xyz, k = n+1)$nn.dists[,-1] keep = rep(T, nrow(las@data)) for(i in 1:ncol(rnn)){ keep = keep & rnn[,i] < d } las = filter_poi(las, keep) return(las) }

las2xyz = function(las){

if(class(las)[1] != "LAS") stop("las must be a LAS object")

las = las@data[,c('X','Y','Z')] %>% as.matrix return(las) }

tls = nnFilter2(tls)