rlidarlidar-data

Error in unique.default(x) : unique() applies only to vectors in rLiDAR package


I am getting this error while calculating the convex hull using the chullLiDAR2D function in rLiDAR. Interestingly, I did not get this error while computing the 3D convex hull using the chullLiDAR3D on the same LAS file. I have posted the code and XY_ID variable for further reference.

Thank you,

#Computing and plotting the 2D-convex hull of a LAS file
#Importing the LAS file
library(rLiDAR)
LAS_File <- system.file("test.las", package = "rLiDAR")
#Read the LAS file
LAS <- readLAS("D:/Summer_2019/NRS_Project/01_data/01_las_2013/test.las", short = TRUE)
#Subsetting the height of the data
XYZ <- subset(LAS[,1:3],LAS[,3] >= 1.37)
#Get the LiDAR clusters
set.seed(1)
clLAS <- kmeans(XYZ, 32)
#Setting the IDs of the points
ID <- as.factor(clLAS$cluster)
#Setting the XYZID input
XY_ID <- cbind(XYZ[,1:2],ID)
summary(XY_ID)
View(XY_ID)
#Calculating the LiDAR convex hull of the clusters
chull_Trees <- chullLiDAR2D(XY_ID)

XY_ID enter image description here


Solution

  • The ID vector must be named "id"

    This is somewhat documented in the help page of chullLiDAR2D:

    Arguments

    xyid    
    A 3-column matrix with the x, y coordinates and points id of the LiDAR point cloud.
    

    Looking at the function itself, the "id" is hard-coded.

    # chullLiDAR2D #
    function (xyid) 
    {
        spdfList <- plyr::dlply(as.data.frame(xyid), "id", 
            function(input) { ...
    

    So, just change the name from ID to id and all will be well.

    #Setting the IDs of the points
    id <- as.factor(clLAS$cluster)
    
    #Setting the XYZID input
    XY_ID <- cbind(XYZ[,1:2], id)
    
    #Calculating the LiDAR convex hull of the clusters
    chull_Trees <- chullLiDAR2D(XY_ID)
    
    # Plotting the LiDAR convex hull
    library(sp)
    plot(SpatialPoints(xyid[,1:2]),cex=0.5,col=xyid[,3])
    plot(chullTrees$chullPolygon, add=TRUE, border='black')
    

    enter image description here