有效地计算邻居之间的距离

时间:2018-06-13 20:13:28

标签: python algorithm numpy scipy spatial

我的数据在地理上分散,没有任何模式,我需要创建一个图像,其中每个像素的值是该像素的邻居的平均值,小于X米。

为此,我使用库scipy.spatial生成带有数据(cKDTree)的KDTree。生成数据结构后,我会根据地理位置找到像素,并找到最接近的地理点。

# 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

提前致谢!

1 个答案:

答案 0 :(得分:1)

这可能是KDTrees不是一个好解决方案的案例之一。这是因为您要映射到网格,这是一个非常简单的结构,这意味着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()

enter image description here