Find all coordinates within a circle in geographic data in python

前端 未结 2 1848
暖寄归人
暖寄归人 2021-02-01 07:18

I\'ve got millions of geographic points. For each one of these, I want to find all \"neighboring points,\" i.e., all other points within some radius, say a few hundred meters.<

2条回答
  •  长发绾君心
    2021-02-01 08:13

    Tipped off by Eamon, I've come up with a simple solution using btrees implemented in SciPy.

    from scipy.spatial import cKDTree
    from scipy import inf
    
    max_distance = 0.0001 # Assuming lats and longs are in decimal degrees, this corresponds to 11.1 meters
    points = [(lat1, long1), (lat2, long2) ... ]
    tree = cKDTree(points)
    
    point_neighbors_list = [] # Put the neighbors of each point here
    
    for point in points:
        distances, indices = tree.query(point, len(points), p=2, distance_upper_bound=max_distance)
        point_neighbors = []
        for index, distance in zip(indices, distances):
            if distance == inf:
                break
            point_neighbors.append(points[index])
        point_neighbors_list.append(point_neighbors)
    

提交回复
热议问题