changeset 9:638fad06e556

use bisect function; it has been tested faster
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 11:37:52 -0700
parents e3d6919130ca
children 21ed15391e8a
files globalneighbors/distance.py globalneighbors/grid.py
diffstat 2 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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