Search code examples
pythonnumpypython-itertools

Python - Library function that given a X,Y pair of point find Xn, Yn which is the closest pair to that pair


Disclaimer note: I'm looking for a library, or pre-existing function that accomplishes this. Similar questions ask about the fundamental algorithm where I am looking for a quick implementation. So I apoligize if this appears to be a duplicate question as I'm just looking for a black boxed answer

Given a pair of geo coordinate points:

[34.232,-119.123]

And an array of other points:

[ [36.232,-117.123], [35.232,-119.123], [33.232,-112.123] ]

I'm looking for a function out there that would return a pair from the list above that is closest to the original coordinate

Edited from simple integers to float values


Solution

  • Per comment:

    from scipy.spatial.distance import cdist
    import numpy as np
    
    def closest(point, ref):
        dist = cdist(ref, [point])
        return ref[np.argmin(dist)]
    
    point = [1,2]
    ref = [ [3,1], [4,1], [2,5] ]
    
    closest(point,ref)
    # out [3,1]