Browse Source

Returns all results in a perimter of 1.5 the best result

Olivier Marty 8 years ago
parent
commit
75372c9d91
1 changed files with 7 additions and 2 deletions
  1. 7 2
      geocoding.py

+ 7 - 2
geocoding.py

@@ -25,10 +25,15 @@ def dist(posa, posb):
         return vincenty(posa, posb).km
 
 def k_neighbors(positions, fro, n):
-  """returns a list of (dist, pos, id) of the n nearest points from fro
+  """returns a list of (dist, pos, id) of the nearest points from fro
+  returns n points (if possible) and all points closer than 1.5 times the distance
+  with the closest point.
   positions is a dictionary id -> (lat, long)"""
+  if not positions:
+    return []
   distances = []
   for (id, pos) in positions.items():
     (dmin, pmin) = min(map(lambda p : (dist(fro, p), p), pos))
     distances.append((dmin, pmin, id))
-  return sorted(distances)[:n]
+  dmin = min([d for (d, __, __) in distances])
+  return list(set(sorted(distances)[:n]) | set(filter(lambda i: i[0] < 1.5*dmin, distances)))