找到numpy数组中最近的xy点,第二个最接近条件

时间:2016-03-24 13:49:53

标签: python arrays numpy tree

我的问题就像线程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): source: Von 112BKS - Eigenes WerkOriginal graph/Data from [.. ? ..], CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=5949185

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

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

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

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

第三步:orangeline中每个元素的mytree = scipy.spatial.cKDTree(datafield[:, 1:3]) dist1, indexes1 = mytree.query(orangeline) 与两个已建立的值以及与这些元素的距离进行插值。

第一步可以用

解决
{{1}}

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

1 个答案:

答案 0 :(得分:0)

@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]))
相关问题