【问题标题】:Save Las points into a las file: argument error and indexerror将 Las 点保存到 las 文件中:参数错误和索引错误
【发布时间】:2015-12-21 08:25:43
【问题描述】:

我有这个用于在 Las 文件中操作激光雷达点的小代码。目标是从文件 1 中获取 1 个点并在文件 2 中查找最近的点。然后使用这些点的坐标创建一个新点,然后保存。到目前为止我遇到了2个问题,解释如下:

import laspy
import laspy.file
import liblas
from liblas import header
h = header.Header()

inFile2 = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\Sync\\pythonlas\\mapperclip\\2clip.las", mode = "r")
inFile3 = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\Sync\\pythonlas\\mapperclip\\3clip.las", mode = "r")
point_records = inFile2.points
point_records = inFile3.points

t=0
for points in inFile3.points:
    z=0
    q=0
    p=0.1
    while z==0:

        xmin=inFile3.x[t]-p
        ymin=inFile3.y[t]-p
        xmax=inFile3.x[t]+p
        ymax=inFile3.y[t]+p
        n=0
        for points in inFile2.points:
            ax=inFile2.x[n]
            ay=inFile2.y[n]
            if ax > xmin and ax < xmax and ay < ymax and ay > ymin:          
                                f = liblas.file.File("C:\\Users\\Geri\\Desktop\\Sync\\Sync\\pythonlas\\mapperclip\\proba.las",mode='w', header= h)
                                newx = inFile3.x[t]-((inFile3.x[t]-inFile2.x[n])/2)
                                newy = inFile3.y[t]-((inFile3.y[t]-inFile2.y[n])/2)
                                newz = inFile3.z[t]-((inFile3.z[t]-inFile2.z[n])/2)
                                pt = liblas.point.Point(newx, newy, newz)

问题出在这一行之后。我想将 newx,y,z 保存为新 las 文件中新点的 X Y 坐标和 z 值。

得到以下错误:

ctypes.Argumenterror: 参数 1: : 错误 输入

                                f.write(pt)
                                f.close()
                                print t
                                print newx
                                print newy
                                print newz
                                print inFile2.y[n]
                                print inFile2.z[n]                                
                                print inFile3.z[t]
                                n+=1
                                q+=1
                                t+=1
            else:
                n+=1
        if q>0:            
            z+=1
        else:
            p+=0.1

当我尝试从代码中写入文件时,我得到以下信息:(脚本成功运行 415 次)

Traceback (most recent call last):
   File "........
       newx = inFile3.x[t]-((inFile3.x[t]-inFile2.x[n])/2)

IndexError:索引 416 超出轴 0 的范围,大小为 416

【问题讨论】:

  • 关于 Python 的有趣之处:if ax &gt; xmin and ax &lt; xmax and ay &lt; ymax and ay &gt; ymin 写成if xmin &lt; ax &lt; xmax and ymin &lt; ay &lt; ymax 可能更清楚。
  • 你能显示完整的回溯,而不仅仅是一条线吗?特别是,哪个函数调用导致了 ArgumentError?因为这样您就可以查找函数期望的参数类型,并改用正确的参数。
  • 当您迭代文件中的点 (for point in inFile3.points) 时,该点在 point 变量中可用。而不是使用索引(n,t),您可以只比较这些point 变量本身的坐标值。这将消除IndexErrors 在该区域的可能性。
  • 您必须查找 1/ newx、newy 和 newz 的类型,以及 2/ liblas.point.Point 将哪些输入类型作为参数。如果你解决了这个问题,你就可以摆脱 ArgumentError。
  • 对于参数,错误原来在 inFile3.points 我只有 415 个点。所以代码中有一个错误,它试图在不应该的情况下再次运行循环

标签: python gis lidar liblas


【解决方案1】:

这就是我目前得到的结果:

对于第一个问题,我只是省略了 liblas 模块并使用 laspy 来保存创建的新文件。

第二个问题我换了

for points in inFile3.points:

与

 while t < 415:

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 2021-05-16
    • 2021-02-10
    • 2019-04-29
    • 2019-04-11
    • 2023-01-30
    • 2019-10-03
    • 2023-01-03
    • 1970-01-01
    相关资源
    最近更新 更多