【问题标题】:Iteratively open a .las file, perform some operations, and save a new .las file with the same name迭代打开一个.las文件,执行一些操作,并保存一个新的同名.las文件
【发布时间】:2023-01-30 00:42:38
【问题描述】:

我有一个包含多个 .las 文件的文件夹。

我的目标是创建一个循环,在这个循环中我迭代地打开一个 .las 文件,我执行一些操作(我已经知道它们有效),然后在另一个文件夹中生成一个新的 .las 文件,与原始文件。

到目前为止,这是我的尝试:

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()

不幸的是,没有文件被保存。如果我修改该行:

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

在:

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

它实际上保存了一个新文件,但迭代以一个名为“newfile.las”的文件结束,该文件在整个循环中被迭代覆盖。

我不明白为什么这条线:

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

不起作用。任何人都可以帮助我吗?

【问题讨论】:

  • 像之前一样使用os.path.join(outFolder, file)
  • 您正在使用 os.path.join 作为输入而不是输出文件。我猜你在 path 文件夹中有很多文件,名称以 las_modified 开头。

标签: python lidar las laspy


【解决方案1】:

你应该使用“os”加入“outFolder”和“file”

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

或者你也可以改变

outFolder= r"pathlas_modified" 

到 :

outFolder= r"pathlas_modified"

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 2021-05-16
    • 2016-04-17
    • 2019-04-05
    • 2010-09-14
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多