python - 将 XYZ 文件中的不规则 3d 数据插值到规则网格

标签 python numpy grid interpolation

我有一个包含大量 3D 坐标的 xyz 文件,如下所示:

 370373.771    6535261.431      2.908       
 370373.788    6535261.441      2.911       
 370373.787    6535261.442      2.909       
 370373.809    6535261.449      2.908       
 370373.810    6535261.439      2.909       
 370373.743    6535261.466      2.922       
 370373.584    6535261.455      2.915       
 370373.559    6535261.471      2.898       
 370373.544    6535261.559      2.887       
 370373.552    6535261.538      2.866       
 370373.797    6535261.486      2.876       
 370373.795    6535261.557      2.892       
 ..........    ...........      .....

这个文件非常密集且不规则,我想将这些坐标插值到一个规则的网格上,例如每 5 米有一个点。

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

import numpy as np
from scipy.interpolate import griddata

coord_x = []
coord_y = []
coord_z = []
coord_xy = []

xyzfile = open("xyzfile.txt")
for line in xyzfile:
    x,y,z = line.split()
    coord_x.append(float(x))
    coord_y.append(float(y))
    coord_xy.append([float(x),float(y)])
    coord_z.append(float(z))
xyzfile.close()

lon = np.linspace(min(coord_x), max(coord_x), 200)
lat = np.linspace(min(coord_y), max(coord_y), 200)
X, Y = np.meshgrid(lon, lat)

grid = griddata(np.array(coord_xy), coord_z, (X, Y), method='nearest')

不幸的是我得到一个错误:

TypeError: only integer arrays with one element can be converted to an index

我也不确定如何在规则网格中的每个点之间获得 5m 的间距。我该怎么做?

第二个问题,假设我在这个网格上有 6 个已知点,如何为这 6 个点在这个规则的 5m 间距网格上提取 z 值。

谢谢

最佳答案

传入的coord_z参数也必须是一个数组:

grid = griddata(np.array(coord_xy), np.array(coord_z), (X, Y), method='nearest')

关于python - 将 XYZ 文件中的不规则 3d 数据插值到规则网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33919875/

相关文章:

python - 在python中怎么可能是一个圆圈?

python - 杀死执行多个子进程的python脚本

python - 将 "missing array values"写入文件

python - 从一系列图像中找出最大的 x,y 值

python - 从 Python 字符串中去除标点符号

python - 测试请求中的延迟时间

python - 透明色图

javascript - 在 jqgrid 中传递一个对象

python - 分配许多简单网络任务的解决方案?

css - Bootstrap 4 Grid 在 UL 内部不工作