我有一个随机位置的纬度,经度和高程值的csv文件。我想应用IDW插值来生成常规网格。我使用 scipy.spatial.cKDTree 进行最近邻搜索,并在未知点找到高程值。 当输出网格具有尺寸(z <1000 X1000)时,以下代码可以正常工作。如果尺寸增加,代码运行速度非常慢。请帮我渲染 for循环而不使用cKDTree删除。谢谢。
## Inverse distance weighted function
def idw(p, dist, values):
dist_pow = np.power(dist, 2)
nominator = np.sum(values/dist_pow)
denominator = np.sum(1/dist_pow)
if denominator > 0:
return nominator/denominator
else:
return none
## Reading the lat/lon and elevation values from file
lat = []
lon = []
ele = []
with open('VSKP_ground_dat.csv') as read:
csvreader = csv.DictReader(read)
for row in csvreader:
lat.append(float(row['LAT']))
lon.append(float(row['LON']))
ele.append(float(row['ALT']))
xycoord = np.c_[lon,lat]
ele_arr = np.array(ele)
## ------------- Creating KDTree
point_tree = spatial.cKDTree(xycoord, leafsize=25)
## ------------- Creating empty grid matrix with np.zeros
xmin, xmax, ymin, ymax = 81.903158, 83.352158, 17.25856, 18.40056
## --------- Defining resolution
xres, yres = 0.01, 0.01
x = np.arange(xmin, xmax, xres)
y = np.arange(ymin, ymax, yres)
z = np.zeros((x.shape[0], y.shape[0]), dtype=np.float16)
for i, val1 in enumerate(x):
for j, val2 in enumerate(y):
p = np.array([val1, val2])
# points_idx = point_tree.query_ball_point(p, dist_2)
distances, points_idx = point_tree.query(p, k=6, eps=0)
ele_vals = ele_arr[points_idx]
value = idw(p, distances, ele_vals)
z[i,j] = value
答案 0 :(得分:1)
首先,修复您的jgen.writeObjectFieldStart("objectFieldName");
jgen.writeStringField("someValue", value.getSomeValue());
//...
jgen.writeEndObject();
函数以处理最后一个索引:
idw
然后根据def idw(dist, values, p = 2):
out = np.empty(dist.shape[:-1])
mask = np.isclose(dist, 0).any(-1)
out[mask] = values[np.isclose(dist, 0)] # should be only one per point
dist_pow = np.power(dist[~mask], -p) # division is costly, do it once
nominator = np.sum(values[~mask] * dist_pow, axis = -1) # over mask to prevent divide by zero
denominator = np.sum(dist_pow, index = -1)
out[~mask] = nominator / denominator
return out
输出
np.meshgrid