Manually add points to a LAS object with lidR

lidr

Using lidR, given a LAS object, read from a LAS file:

las <- readLAS(point_cloud_file)

and points (extracted from a shapefile):

shp <- st_read(shp_file)
pts <- st_coordinates(shp)

I would like to add (append) these points to the LAS object (assuming both las and shp share the same CRS)

How can I get to that?

Best Answer

This is not as easy as it might seem. From pts which is a matrix you can extract the columns. Here I'm using runif to make a reproducible example

library(lidR)
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile)


X = runif(1000, 684766, 684993)
Y = runif(1000, 5017773, 5018007)
Z = runif(1000, 0, 30)
data = data.frame(X,Y,Z)

Then we can make a LAS object with LAS() and we provide the header of las to create object of the same format

las2 = LAS(data, las@header)

This triggers some warning because we input some inccorectly quantized coordinates. Check the output of las_check()

las_check(las2)

We can fix that

las2 = las_quantize(las2, TRUE)
las2 = las_update(las2)

las_check(las2)

Now if we want to combine the las and las2 they must have the same columns. We need to make them manually. Here if you load only XYZ in las the job is easier.

las2@data$gpstime = 0
las2@data$Intensity = 0L
las2@data$ReturnNumber = 1L
las2@data$NumberOfReturns = 1L
las2@data$ScanDirectionFlag = 0L
las2@data$EdgeOfFlightline = 0L
las2@data$Classification = 0L
las2@data$Synthetic_flag = FALSE
las2@data$Keypoint_flag = FALSE
las2@data$Withheld_flag = FALSE
las2@data$ScanAngleRank = 0L
las2@data$UserData = 0L
las2@data$PointSourceID = 0L

We can bind the two objects

las3 = rbind(las, las2)
Related Question