Working out which points lat/lon coordinates are closest to
<p>I currently have a list of coordinates</p>

<pre><code>[(52.14847612092221, 0.33689512047881015),
 (52.14847612092221, 0.33689512047881015),
 (52.95756796776235, 0.38027099942700493),
 (51.78723479900971, -1.4214854900618064)
 ...]
</code></pre>

<p>I would like to split this list into 3 separate lists/datafames corresponding to which city they are closest to (in this case the coordinates are all in the UK and the 3 cities are Manchester, Cardiff and London) </p>

<p>So at the end result I would like the current single list of coordinates to be split into either separate lists ideally or it could be a dataframe with 3 columns would be fine eg:</p>

<pre><code> leeds                   cardiff                 london
(51.78723479900971,    (51.78723479900971,      (51.78723479900971,
 -1.4214854900618064)    -1.4214854900618064)    -1.4214854900618064) 
</code></pre>

<p><em>(those are obiously not correct coordinates!)</em></p>

<p>-Hope that makes sense. It doesn't have to be overly accurate (don't need to take into consideration the curvature of the earth or anything like that!)</p>

<p>I'm really not sure where to start with this - I'm very new to python and would appreciate any help!
Thanks in advance</p>

<p>This is a pretty brute force approach, and not too adaptable. However, that can be the easiest to understand and might be plenty efficient for the problem at hand. It also uses only pure python, which may help you to understand some of python's conventions.</p>

<pre><code>points = [(52.14847612092221, 0.33689512047881015), (52.14847612092221, 0.33689512047881015), (52.95756796776235, 0.38027099942700493), (51.78723479900971, -1.4214854900618064), ...]

cardiff = (51.4816546, -3.1791933)
leeds = (53.7974185, -1.543794)
london = (51.5073219, -0.1276473)

def distance(pt, city):
    return ((pt[0] - city[0])**2 + (pt[1] - city[1])**2)**0.5

cardiff_pts = []
leeds_pts = []
london_pts = []
undefined_pts = []  # for points equidistant between two/three cities

for pt in points:
    d_cardiff = distance(pt, cardiff)
    d_leeds = distance(pt, leeds)
    d_london = distance(pt, london)
    if (d_cardiff &lt; d_leeds) and (d_cardiff &lt; d_london):
        cardiff_pts.append(pt)
    elif (d_leeds &lt; d_cardiff) and (d_leeds &lt; d_london):
        leeds_pts.append(pt)
    elif (d_london &lt; d_cardiff) and (d_london &lt; d_leeds):
        london_pts.append(pt)
    else:
        undefined_pts.append(pt)
</code></pre>

<p>Note that this solution assumes the values are on a cartesian reference frame, which latitude longitude pairs are not.</p>

python geospatial geopy