For those closest calculations, as a rule, an effective method is associated with one of such quick searches for the nearest neighbor based on the kd-tree. Using the Cython-powered implementation , we will have one approach, for example:
from scipy.spatial import cKDTree def closest_pts(setA_lat, setA_lng, setB_lat, setB_lng): a_x = setA_lat.values a_y = setA_lng.values b_x = setB_lat.values b_y = setB_lng.values a = np.c_[a_x, a_y] b = np.c_[b_x, b_y] indx = cKDTree(b).query(a,k=1)[1] return pd.Series(b_x[indx]), pd.Series(b_y[indx])
Run Example -
1) Inputs:
In [106]: setA_lat Out[106]: 0 40.748043 1 42.361016 dtype: float64 In [107]: setA_lng Out[107]: 0 -73.992953 1 -71.020005 dtype: float64 In [108]: setB_lat Out[108]: 0 42.460000 1 0.645894 2 0.437587 3 40.460000 4 0.963663 dtype: float64 In [109]: setB_lng Out[109]: 0 -71.000000 1 0.925597 2 0.071036 3 -72.000000 4 0.020218 dtype: float64
2) Outputs:
In [110]: c_x,c_y = closest_pts(setA_lat, setA_lng, setB_lat, setB_lng) In [111]: c_x Out[111]: 0 40.46 1 42.46 dtype: float64 In [112]: c_y Out[112]: 0 -72.0 1 -71.0 dtype: float64
source share