# HG changeset patch # User Jeff Hammel # Date 1498415872 25200 # Node ID 638fad06e556567bd545a33df7e0d3b6afbe36f7 # Parent e3d6919130ca9a196e48daf801928a28e30611b5 use bisect function; it has been tested faster diff -r e3d6919130ca -r 638fad06e556 globalneighbors/distance.py --- a/globalneighbors/distance.py Sun Jun 25 11:21:28 2017 -0700 +++ b/globalneighbors/distance.py Sun Jun 25 11:37:52 2017 -0700 @@ -52,7 +52,9 @@ else: distances.append((i, new_distance)) -class KeyWrapper: + +class KeyWrapper(object): + """wrapper for python's `bisect` methods""" def __init__(self, iterable, key): self.it = iterable self.key = key @@ -63,6 +65,7 @@ def __len__(self): return len(self.it) + def insert_distance_bisect(distances, i, new_distance, k): if not distances: @@ -147,7 +150,7 @@ for i in (id1, id2): distances = neighbors.setdefault(i, []) - insert_distance(distances, i, new_distance, k) + insert_distance_bisect(distances, i, new_distance, k) return neighbors @@ -160,6 +163,10 @@ parser = CitiesParser(description=description) parser.add_argument('output', type=argparse.FileType('w'), help="output file to dump JSON to") + parser.add_argument('--latlon', dest='latlon', type=float, + nargs=2, metavar=("LAT", "LON"), + default=(1., 1.), + help="tolerance of latitude and longitude in degrees [DEFAULT: %(default)s]") parser.add_argument('--counter', '--output-counter', dest='output_counter', type=int, default=100, @@ -178,6 +185,8 @@ # calculate neighbors neighbors = calculate_neighbors(city_locations, k=options.k, + lat_tol=options.latlon[0], + lon_tol=options.latlon[-1], output=options.output_counter) # output diff -r e3d6919130ca -r 638fad06e556 globalneighbors/grid.py --- a/globalneighbors/grid.py Sun Jun 25 11:21:28 2017 -0700 +++ b/globalneighbors/grid.py Sun Jun 25 11:37:52 2017 -0700 @@ -56,3 +56,7 @@ return [(_i, _j) for _i in imat for _j in jmat if (_i,_j) != (i,j)] + + def neighbor_points(self, i, j): + """return all points in a lat-lon box""" + raise NotImplementedError('TODO') # -> record TODO items