Iteratively open a .las file, perform some operations, and save a new .las file with the same name

234 views Asked by At

i have a folder with several .las files.

My goal is to create a loop, in which iteratively i open a .las file, i perform some operations (which i already know they work), and then generate a new .las file, in another folder, with the same name of the original file.

This is my attempt so far:

inputFolder = r"path\las"
outFolder= r"path\las_modified"

for file in os.listdir(inputFolder): #iteratively open each file in the main folder
inputFile = os.path.join(inputFolder, file)

inFile = laspy.file.File(inputFile, mode='r') #open las file

xyz = np.vstack((inFile.x, inFile.y, inFile.z)).transpose() #data extracted on which i perform some operations. in this example, let's say I want to save an identic .las file, so I won't show operations
x=xyz[:,0]
y=xyz[:,1]
z=xyz[:,2]

header = laspy.header.Header()
outfile = laspy.file.File(outFolder + file, mode="w", header=header)
xmin = np.floor(np.min(x))
ymin = np.floor(np.min(y))
zmin = np.floor(np.min(z))
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.close()

Unluckyly, no file are saved. If I modify the line:

outfile = laspy.file.File(outFolder + file, mode="w", header=header)

in:

outfile = laspy.file.File(outFolder + "newfile.las", mode="w", header=header)

it actually saves a new file, but the iteration ends up with a single file named "newfile.las", which is iteratively overwritten during the entire loop.

i don't understand why the line:

outfile = laspy.file.File(outFolder + file, mode="w", header=header)

doesn't work. Anyone can help me?

1

There are 1 answers

0
saif amdouni On

you should use "os" to join the "outFolder" and "file"

outfile = laspy.file.File(os.path.join(outFolder, file), mode="w", header=header)

or you can also change

outFolder= r"path\las_modified" 

to :

outFolder= r"path\las_modified\"