Split a point cloud (las file) with classification values

266 views Asked by At

I have a classified point cloud (.las file)

I would like to split it in several smaller point clouds, with respect to the classification values.

I can do it in a non-iterative way:

ground=xyz[np.where(xyz[:,6] == 2)]

header = laspy.header.Header()
x=ground[:,0]
y=ground[:,1]
z=ground[:,2]
intensity=ground[:,3]
return_num=ground[:,4].astype(int)
num_returns=ground[:,5].astype(int)
classification=ground[:,6].astype(int)

xmin = np.floor(np.min(x))
ymin = np.floor(np.min(y))
zmin = np.floor(np.min(z))

outfile = laspy.file.File(outFolder+"\classificationNumber2.las", mode="w", header=header)
outfile.header.offset = [xmin,ymin,zmin]
outfile.header.scale = [0.001,0.001,0.001]
outfile.x = x
outfile.y = y
outfile.z = z
outfile.intensity = intensity
outfile.return_num = return_num
outfile.num_returns = num_returns
outfile.classification = classification
outfile.close()

How can I modify the code to write point clouds for different classification values iteratively?

1

There are 1 answers

0
EvanZarrella On

I believe the LAS specification tells you that the class of a point is stored on 5 bits. Meaning you can have at most 32 classes.

You can probably do something like that :

import laspy

las = laspy.read(path)
for i in range(32):
  new_file = laspy.create(point_format=las.header.point_format, 
                          file_version=las.header.version)
  new_file.points = las.points[las.classification == i]
  new_file.write(f'extracted_class_{i}.las')

This code is a slightly modified version of the doc available here laspy doc on filtering

A "point" contains all the data associated to it. You don't need to filter each and every fields in your LAS file.