title: how do I cluster a list of geographic points by distance?
body: <p>I have a list of points P=[p1,...pN] where pi=(latitudeI,longitudeI).</p>
  
  <p>Using Python 3, I would like to find a smallest set of clusters (disjoint subsets of P) such that every member of a cluster is within 20km of every other member in the cluster.</p>
  
  <p>Distance between two points is computed using the <a href="https://geopy.readthedocs.io/en/stable/#module-geopy.distance" rel="noreferrer">Vincenty method</a>.</p>
  
  <p>To make this a little more concrete, suppose I have a set of points such as</p>
  
  <pre><code>from numpy import *
  points = array([[33.    , 41.    ],
         [33.9693, 41.3923],
         [33.6074, 41.277 ],
         [34.4823, 41.919 ],
         [34.3702, 41.1424],
         [34.3931, 41.078 ],
         [34.2377, 41.0576],
         [34.2395, 41.0211],
         [34.4443, 41.3499],
         [34.3812, 40.9793]])
  </code></pre>
  
  <p>Then I am trying to define this function:</p>
  
  <pre><code>from geopy.distance import vincenty
  def clusters(points, distance):
      """Returns smallest list of clusters [C1,C2...Cn] such that
         for x,y in Ci, vincenty(x,y).km &lt;= distance """
      return [points]  # Incorrect but gives the form of the output
  </code></pre>
  
  <p>NOTE: Many questions cluster on geo location <em>and attribute</em>.  My question is for <em>location only</em>.  This is for lat/lon, <em>not</em> Euclidean distance.  There are other questions out there that give sort-of answers but not the answer to this question (many unanswered):</p>
  
  <ul>
  <li><a href="https://datascience.stackexchange.com/questions/761/clustering-geo-location-coordinates-lat-long-pairs">https://datascience.stackexchange.com/questions/761/clustering-geo-location-coordinates-lat-long-pairs</a></li>
  <li><a href="https://gis.stackexchange.com/questions/300171/clustering-geo-points-and-export-borders-in-kml">https://gis.stackexchange.com/questions/300171/clustering-geo-points-and-export-borders-in-kml</a></li>
  <li><a href="https://gis.stackexchange.com/questions/194873/clustering-geographical-data-based-on-point-location-and-associated-point-values">https://gis.stackexchange.com/questions/194873/clustering-geographical-data-based-on-point-location-and-associated-point-values</a></li>
  <li><a href="https://gis.stackexchange.com/questions/256477/clustering-latitude-longitude-data-based-on-distance">https://gis.stackexchange.com/questions/256477/clustering-latitude-longitude-data-based-on-distance</a></li>
  <li>and more, none of which answer this question.</li>
  </ul>