【问题标题】:Finding nearest xy-point in numpy array and second nearest with condition在 numpy 数组中找到最近的 xy 点,并在条件下找到第二个最近的 xy 点
【发布时间】:2016-03-24 13:49:53
【问题描述】:

我的问题类似于线程Finding index of nearest point in numpy arrays of x and y coordinates中的问题,但它被扩展了:

为了更好的可视化,这里有一张图片 (处理过的图像,原件来自:by 112BKS - Eigenes WerkOriginal graph/Data from [.. ? ..], CC BY-SA 3.0, link to page):

一方面有一个数组datafield。它由一个带有元素 [value x y] 的 numpy 数组组成。那是带有数字的细蓝线(它们是value)。另一方面,数组orangeline 在一个带有元素[x y] 的numpy 数组中。

我要做的是计算orangeline 中任何元素的value
我用绿色圆圈可视化了orangeline 的一个具体元素。我可以用datafield 中的两个元素对它的值进行插值,用三角形可视化。结果,我得到了绿色圆圈 value 在 225 和 230 之间。

第一步:
orangeline 中的每个元素查找datafield 中最接近的元素。
(在示例中为粉红色三角形。)

第二步:
为 'orangeline' 中的每个元素查找 datafield 中最接近的元素,但与第一步中的另一个 value 相比。
(在示例中是棕色三角形。)

第三步:根据这两个建立的值和到这些元素的距离,为orangeline 中的每个元素插入value

第一步可以解决

mytree = scipy.spatial.cKDTree(datafield[:, 1:3])
dist1, indexes1 = mytree.query(orangeline)

但现在我不知道如何过滤第二步的数据字段。有解决办法吗?

【问题讨论】:

  • 您是否考虑过将using griddata 插入datafield 的值到orangeline

标签: python arrays numpy tree


【解决方案1】:

@unutbu 的评论的帮助下,我发现这个解决方案在orangeline 没有通过该字段的情况下也非常有效。

以下是网格的功能:

import matplotlib.mlab as mlab
import numpy as np
import scipy

def define_grid(rawdata):
    xmin, xmax = np.amin(rawdata[:, 1]), np.amax(rawdata[:,1])
    ymin, ymax = np.amin(rawdata[:, 2]), np.amax(rawdata[:,2])

    x, y, z = rawdata[:, 1], rawdata[:, 2], rawdata[:, 0]

    # Size of regular grid
    ny, nx = (ymax - ymin), (xmax - xmin)

    # Generate a regular grid to interpolate the data.
    xi = np.linspace(xmin, xmax, nx)
    yi = np.linspace(ymin, ymax, ny)
    xi, yi = np.meshgrid(xi, yi)

    # Interpolate using delaunay triangularization
    zi = mlab.griddata(x,y,z,xi,yi)
    return xi, yi, zi

def grid_as_array(xi,yi,zi):
    xi_flat, yi_flat, zi_flat = np.ravel(xi), np.ravel(yi), np.ravel(zi)

    # reduce arrays for faster calculation, take only every second element
    xi_red, yi_red, zi_red = xi_flat[1::2], yi_flat[1::2], zi_flat[1::2]

    # stack to array with elements [x y z], but there are z values that are 'nan'
    xyz_with_nan = np.hstack((xi_red[:, np.newaxis], yi_red[:, np.newaxis],
                              zi_red[:, np.newaxis]))

    # sort out those elements with 'nan'
    xyz = xyz_with_nan[~np.isnan(xyz_with_nan).any(axis=1)]
    return xyz

另一个用于从网格中找到最近点的函数:

def closest_node(points, datafield):
    mytree = scipy.spatial.cKDTree(datafield)
    dist, indexes = mytree.query(points)
    return indexes

现在是代码:

# use function to create from the raw data an interpolated datafield
xi, yi, zi = define_grid(datafield)

# rearrange those values to bring them in the form of an array with [x y z]
xyz = grid_as_array(xi, yi, zi)    

# search closest values from grid for the points of the orangeline
# orangeline_xy is the array with elements [x y]
indexes = self.closest_node(orangeline_xy, xyz[:,0:2])

# take z values from the grid which we found before
orangeline_z = xyz[indexes, 2]

# add those z values to the points of the orangeline
orangeline_xyz = np.hstack((orangeline_xy,orangeline_z[:, np.newaxis]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-09-30
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多