我有一个包含大量 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/