Python 如何找到一个点在其特定距离内的所有相邻点?

Python 如何找到一个点在其特定距离内的所有相邻点?,python,pandas,nearest-neighbor,Python,Pandas,Nearest Neighbor,我有两个数据帧df1和df2, 我想找出df1中的所有相邻点,它是df2中点的相邻点(需要以迭代方式找出df2中的每个点),在特定的径向距离内 我怎么能这么做 在给定的图中:黑点在df1中,红点在df2中,我想找到每个红点的相邻点。 伪代码(非特定语言且类型非常松散): 这是我脑子里想的,而且打字很快,所以简化和实现取决于你。这很可能不是实现你想要的东西的最快或最有效的方法。我相信它可以大大简化,尤其是在Python中。您可能知道,API中充斥着简化代码中数学的方法。 请欢迎来到StackOve

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

我怎么能这么做

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

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

这是我脑子里想的,而且打字很快,所以简化和实现取决于你。这很可能不是实现你想要的东西的最快或最有效的方法。我相信它可以大大简化,尤其是在Python中。您可能知道,API中充斥着简化代码中数学的方法。

请欢迎来到StackOverflow,而且。。。在这里申请。StackOverflow是针对特定编程问题的知识库,而不是设计、编码、研究或教程资源。发布你的代码和你遇到的问题。


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*/
}
#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)])