comparison globalneighbors/distance.py @ 1:1b94f3bf97e5

* limit distance function * start gridding * improve unicode handling
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 14:02:14 -0700
parents 5dba84370182
children 49aae0c0293b
comparison
equal deleted inserted replaced
0:5dba84370182 1:1b94f3bf97e5
94 new_distance = haversine(*args, r=Rearth) 94 new_distance = haversine(*args, r=Rearth)
95 95
96 # insert in order 96 # insert in order
97 for i in (id1, id2): 97 for i in (id1, id2):
98 distances = neighbors.setdefault(i, []) 98 distances = neighbors.setdefault(i, [])
99 if len(distances) == k and new_distance > distances[-1][-1]: 99 if len(distances) == k and new_distance >= distances[-1][-1]:
100 break 100 continue
101 101
102 # TODO: Binary Search Tree 102 # TODO: Binary Search Tree
103 for _index, (geoid, old_distance) in enumerate(distances): 103 for _index, (geoid, old_distance) in enumerate(distances):
104 if new_distance < old_distance: 104 if new_distance < old_distance:
105 distances.insert(_index, (i, new_distance)) 105 distances.insert(_index, (i, new_distance))
107 distances.pop() 107 distances.pop()
108 break 108 break
109 else: 109 else:
110 distances.append((i, new_distance)) 110 distances.append((i, new_distance))
111 111
112 print ("DONE")
113 sys.stdout.flush()
114 import pdb; pdb.set_trace()
115 return neighbors 112 return neighbors
116 113
117 114
118 def main(args=sys.argv[1:]): 115 def main(args=sys.argv[1:]):
119 """CLI""" 116 """CLI"""
142 neighbors = calculate_neighbors(city_locations, 139 neighbors = calculate_neighbors(city_locations,
143 k=options.k, 140 k=options.k,
144 output=options.output_counter) 141 output=options.output_counter)
145 142
146 # output 143 # output
147 print ("AFTER")
148 sys.stddout.flush()
149 options.output.write(json.dumps(neighbors, indent=2)) 144 options.output.write(json.dumps(neighbors, indent=2))
150 145
151 if __name__ == '__main__': 146 if __name__ == '__main__':
152 main() 147 main()