【问题标题】:How to find all the neighboring points of a point within particular distance from it?如何找到一个点在特定距离内的所有相邻点?
【发布时间】:2020-05-27 06:56:24
【问题描述】:

我有两个数据框 df1 和 df2, 我想找出 df1 中的所有相邻点,它是 df2 中点的邻居(需要以迭代方式找出 df2 中的每个点)在特定径向距离内。

我该怎么做?

在给定的图中:黑点在df1,红点在df2,我想找到每个红点的相邻点。

【问题讨论】:

标签: python pandas nearest-neighbor


【解决方案1】:

在伪代码中(非特定语言且类型非常松散):



function getDistance(point pointA, point pointB){
  diffx = absoluteValue(pointA.x - pointB.x);
  diffy = absoluteValue(pointA.y - pointB.y);
  return squareRoot(diffx^2 + diffy^2)
}

for point1 in df1{

  //each obj stores a point and a corresponding distance
  Object distance{
    point2Identifier;
    distanceFromPoint1;
  }
  ObjectArray distances;  //Array of distance objects 

  for point2 in df2{
    distances.add(getDistance(point1, point2)); 
  }
  distances.getSmallest /*Finds the distance obj  with the smallest distanceFromPoint1 prop and stores it however you see fit*/
}

这不是我的想法,而是快速输入,因此简化和实施取决于您。这很可能不是实现您想要的最快或最有效的方法。我相信它可以大大简化,尤其是在 Python 中。您可能知道,API 中充斥着简化代码中数学运算的方法。

【讨论】:

    【解决方案2】:

    Find all nearest neighbors within a specific distance

    #x,y are the x, y columns in the data frame and the radial distance is 0.1
    import numpy as np
    import scipy.spatial as spatial
    
    
    points=df1[['x','y']]
    points_array= points.rename_axis('ID').values
    
    
    point_tree = spatial.cKDTree(points_array)
    
    
    for item in range(0,len(df2),1):
     print(point_tree.data[point_tree.query_ball_point([df2.x.iloc[item], cells_final.lat.iloc[item]], 0.1)])
    
    

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 2015-12-02
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      相关资源
      最近更新 更多