1

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?

2
  • This is doable but not trivial. First, shapefiles do not support XYZ points. Do you input Z = 0? Does the table of attributes matters? Please clarify your question
    – JRR
    Nov 4, 2021 at 15:38
  • Yes I do have 3D vectors in the shapefile (PolygonZ or LineStringZ), the table of attributes does not matter.
    – juu
    Nov 5, 2021 at 6:05

1 Answer 1

3

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)
2
  • Generic solution starting from a matrix, it does the job, thank you!
    – juu
    Nov 5, 2021 at 6:06
  • After setting the synthetic flag to FALSE, is there another way to discover synthetic points?
    – Ray J
    Apr 12, 2022 at 19:22

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.