Using Laspy to Create LAS Files from Scratch

laslaspylidar

I have found instructions for creating a las file by opening an existing file, modifying it, then re-saving it.

This re-uses the headers from the original file.

But how do I create a new file from scratch without loading another in the first place? I cannot see any documentation on the required format for the file, points, or headers.

I only need the bare minimum: x,y,z position for each point.

Here is how I assumed it might work…

import numpy as np
import laspy
import laspy.header
import laspy.file

points = []

#x,y,z values
points.append([0,0,10])
points.append([1,0,10])
points.append([2,0,11])

header = laspy.header.Header()
outFile = laspy.file.File("./output.las", mode = "w", header = header)
outFile.points = np.array(points)
outFile.close()

This fails with:

-> outFile.points = np.array(points)

/laspy/file.py
-> self._writer.set_points(new_points)

laspy/base.py
-> self.data_provider._pmap[:] = points[:]

ValueError: could not broadcast input array from shape (3,3) into shape (3)

Could anyone kindly point to some sample code or tips to get this working?

Best Answer

Rather than setting the entire points array in one go, try setting each dimension in turn:

import laspy

header = laspy.header.Header()
outfile = laspy.file.File("output.las", mode="w", header=header)
outfile.X = [1, 2, 3]
outfile.Y = [0, 0, 0]
outfile.Z = [10, 10, 11]
outfile.close()