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 will get you started:</p>

<pre><code>from geopy.geocoders import Nominatim
geolocator = Nominatim()

places = ['london','cardiff','leeds']
coordinates = {}
for i in places: 
    coordinates[i] = ((geolocator.geocode(i).latitude, geolocator.geocode(i).longitude))

&gt;&gt;&gt;print coordinates
{'cardiff': (51.4816546, -3.1791933), 'leeds': (53.7974185, -1.543794), 'london': (51.5073219, -0.1276473)}
</code></pre>

<p>You can now hook up the architecture for putting this in a pandas dataframe, calculating the distance metric between your coordinates and the above.</p>

<p>Ok so now we want to do distances between what is a very small array (the coordinates).</p>

<p>Here's some code:</p>

<pre><code>import numpy as np
single_point = [3, 4] # A coordinate
points = np.arange(20).reshape((10,2)) # Lots of other coordinates

dist = (points - single_point)**2
dist = np.sum(dist, axis=1)
dist = np.sqrt(dist)
</code></pre>

<p>From here there is any number of things you can do. You can sort it using numpy, or you can place it in a pandas dataframe and sort it there (though that's really just a wrapper for the numpy function I believe). Whichever you're more comfortable with.</p>

python geospatial geopy