我有地理上分散的数据,没有任何模式,我需要创建一个图像,其中每个像素的值是该像素的邻居小于x米的平均值。
为此,我使用库scipy.spatial生成包含数据的KDTree(cKDTree)一旦生成了数据结构,我就在地理上定位像素,并定位最接近的地理点。

# Generate scattered data points
coord_cart= [
    [
        feat.geometry().GetY(),
        feat.geometry().GetX(),
        feat.GetField(feature),
    ] for feat in layer
]

# Create KDTree structure
tree = cKDTree(coord_cart)

# Get raster image dimensions
pixel_size = 5
source_layer = shapefile.GetLayer()
x_min, x_max, y_min, y_max = source_layer.GetExtent()

x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)

# Create grid
x = np.linspace(x_min, x_max, x_res)
y = np.linspace(y_min, y_max, y_res)

X, Y = np.meshgrid(x, y)
grid = np.array(zip(Y.ravel(), X.ravel()))

# Get points that are less than 10 meters away
inds = tree.query_ball_point(grid, 10)

# inds is an np.array of lists of different length, so I need to convert it into an array of n_points x maximum number of neighbors
ll = np.array([len(l) for l in inds])
maxlen = max(ll)
arr = np.zeros((len(ll), maxlen), int)

# I don't know why but inds is an array of list, so I convert it into an array of array to use grid[inds]
# I THINK THIS IS A LITTLE INEFFICIENT
for i in range(len(inds)):
    inds[i].extend([i] * (maxlen - len(inds[i])))
    arr[i] = np.array(inds[i], dtype=int)

# AND THIS DOESN'T WORK
d = np.linalg.norm(grid - grid[inds])

有更好的办法吗?我试图使用IDW来执行点之间的插值。我发现这个snippet使用一个函数得到n个最近的点,但它对我不起作用,因为我需要,如果半径r中没有点,像素值为0。
d, inds = tree.query(zip(xt, yt, zt), k = 10)
w = 1.0 / d**2
air_idw = np.sum(w * air.flatten()[inds], axis=1) / np.sum(w, axis=1)
air_idw.shape = lon_curv.shape

提前谢谢!

最佳答案

这可能是kdtree不是一个好的解决方案的情况之一。这是因为您正在映射到一个网格,这是一个非常简单的结构,意味着从kdtree的复杂性中没有任何收获。通过简单的算法可以找到最近的网格点和距离。
下面是一个简单的实现示例。我使用的是高斯核,但如果你愿意的话,可以改为IDW。

import numpy as np
from scipy import stats

def rasterize(coords, feature, gu, cutoff, kernel=stats.norm(0, 2.5).pdf):
    # compute overlap (filter size / grid unit)
    ovlp = int(np.ceil(cutoff/gu))

    # compute raster dimensions
    mn, mx = coords.min(axis=0), coords.max(axis=0)
    reso = np.ceil((mx - mn) / gu).astype(int)
    base = (mx + mn - reso * gu) / 2

    # map coordinates to raster, the residual is the distance
    grid_res = coords - base
    grid_coords = np.rint(grid_res / gu).astype(int)
    grid_res -= gu * grid_coords
    # because of overlap we must add neighboring grid points to the nearest
    gcovlp = np.c_[-ovlp:ovlp+1, np.zeros(2*ovlp+1, dtype=int)]
    grid_coords = (gcovlp[:, None, None, :] + gcovlp[None, :, None, ::-1]
                   + grid_coords).reshape(-1, 2)
    # the corresponding residuals have the same offset with opposite sign
    gdovlp = -gu * (gcovlp+1/2)
    grid_res = (gdovlp[:, None, None, :] + gdovlp[None, :, None, ::-1]
                + grid_res).reshape(-1, 2)
    # discard off fov grid points and points outside the cutoff
    valid, = np.where(((grid_coords>=0) & (grid_coords<=reso)).all(axis=1) & (
        np.einsum('ij,ij->i', grid_res, grid_res) <= cutoff*cutoff))
    grid_res = grid_res[valid]
    feature = feature[valid // (2*ovlp+1)**2]
    # flatten grid so we can use bincount
    grid_flat = np.ravel_multi_index(grid_coords[valid].T, reso+1)
    return np.bincount(
        grid_flat,
        feature * kernel(np.sqrt(np.einsum('ij,ij->i', grid_res, grid_res))),
        (reso + 1).prod()).reshape(reso+1)



gu = 5
cutoff = 10
coords = np.random.randn(10_000, 2) * (100, 20)
coords[:, 1] += 80 * np.sin(coords[:, 0] / 40)
feature = np.random.uniform(0, 1000, (10_000,))

from timeit import timeit

print(timeit("rasterize(coords, feature, gu, cutoff)", globals=globals(), number=100)*10, 'ms')

pic = rasterize(coords, feature, gu, cutoff)

import pylab
pylab.pcolor(pic, cmap=pylab.cm.jet)
pylab.colorbar()
pylab.show()

python - 有效计算邻居之间的距离-LMLPHP

09-15 14:59