I am using the R package lidR to do some analysis on a large forestry point cloud. I am trying to write my own point_metrics() function for identifying outliers. How can I access the X, Y, and Z coordinates of the point that the metric is being run on inside of the function? For example with the following code:

is.outlier <- function(x, y, z) {
point_metrics(las, ~is.outlier(X, Y, Z), k = 5)

returns the x values of the point's k nearest neighbors. I want to find the x value of the point in addition to the k nearest neighbors in order to find the distance away each knn is from the initial point.

1 Answer 1


Actually what you are receiving in your function is the processed point + its k-1 neighbors because the current point is considered as the 1-neighbour with a distance 0 to the reference point. I do agree that this is not explicit in the documentation. So to access the central point you can simply use x[1]. Other points are ordered by distance.

is.outlier <- function(x, y, z) {
 dx = x[1] - x[-1]
 dy = y[1] - y[-1]
 dz = z[1] - z[-1]
 sqdist = dx*dx+dy*dy+dz*dz
 return(mean(sqdist) > 250))

By the way you cannot do:

f <- function(x) { return(x) }
point_metrics(las, ~f(X), k = 5)

because f is expected to return a single or a list of scalar values. Here you are returning a vector. Your example will fail.

  • Thanks for the response, this answered my question!
    – jmay
    Commented Oct 16, 2020 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.