R木の使用例
featureに入っている各地点の緯度経度に対して,
import rtree
def find_nearest_uparea(feature, uparea, lat_list, lon_list, lat_list_cut, lon_list_cut):
"""Find the nearest uparea value from one lonlat point"""
tree_idx = rtree.index.Index()
lon_list_tmp, lat_list_tmp = [], []
id = 0
for j in range(len(lat_list_cut)):
for i in range(len(lon_list_cut)):
geom = (lon_list_cut[i], lat_list_cut[j])
tree_idx.insert(id, geom)
lon_list_tmp.append(lon_list_cut[i])
lat_list_tmp.append(lat_list_cut[j])
id += 1
uparea_nearest = []
for lonlat in feature:
idx_nearest = list(tree_idx.nearest(lonlat, 1))[0]
row = np.where(lat_list==lat_list_tmp[idx_nearest])[0][0]
col = np.where(lon_list==lon_list_tmp[idx_nearest])[0][0]
uparea_nearest.append(uparea[row, col])
return np.array(uparea_nearest)
参考リンク