class testLocationsKDTree(unittest.TestCase): def setUp(self): self.test = LocationsKDTree() # The 15 points manually selected from the San Francisco Map self.test.mercatorPointsArray = [(552084.9682165304, 4182494.1939107366), (551922.8858612546, 4182574.0555520994), (551687.9949461814, 4182217.968801949), (552775.8082342255, 4183336.7417580322), (553222.9632670761, 4183422.393102693), (552198.7222675259, 4184064.8048690264), (549362.9961397024, 4181902.574203599), (548023.1142878283, 4180961.314933798), (551183.5342344284, 4184494.9211385823), (548547.6683833781, 4179180.1576283425), (549558.9617763544, 4184499.9538624967), (553803.7182814779, 4180131.3817638056), (552354.47799122, 4184532.357535243), (550631.7088304324, 4182068.2753886213), (550951.7531905753, 4181648.8476454713)] self.test.build_kd_tree() def test_lookup_location(self): """ Search for the 5 points closest to the chosen point on the San Francisco Map Chosen point = (552048.7215749588, 4182698.120068086) The known/expected 5 closest points: (551922.8858612546, 4182574.0555520994) (552084.9682165304, 4182494.1939107366) (551687.9949461814, 4182217.968801949) (552775.8082342255, 4183336.7417580322) (552198.7222675259, 4184064.8048690264) """ nearest_nn_points = self.test.find_nn((552048.7215749588, 4182698.120068086),5) self.assertEqual(nearest_nn_points[0],(551922.8858612546, 4182574.0555520994)) self.assertEqual(nearest_nn_points[1],(552084.9682165304, 4182494.1939107366)) self.assertEqual(nearest_nn_points[2],(551687.9949461814, 4182217.968801949)) self.assertEqual(nearest_nn_points[3],(552775.8082342255, 4183336.7417580322)) self.assertEqual(nearest_nn_points[4],(552198.7222675259, 4184064.8048690264))
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client() # An inverted index, mapping UTM (Universal Transverse Mercator) lat/lng # values geolocating each film, to a LIST of films that were filmed # at that location. # # IMPORTANT!: This is a shared data-structure, built only at startup, # that is READ-ONLY by all, and so can be safely shared. invertedPointLocationIndex = InvertedPointLocationIndex() invertedPointLocationIndex.build_inverted_location_index() # A KD tree, implemented using the scipy package's kdtree implementation # under the hood, to allow for fast O(ln) queries of 2D point data. The # points that it stores are geocoded locations coded in UTM to allow them # to be treated as 2D points to an approximation. # # IMPORTANT!: This is a shared data-structure, built only at startup, # that is READ-ONLY by all, and so can be safely shared. filmLocationsKDTree = LocationsKDTree() filmLocationsKDTree.load_point_data() filmLocationsKDTree.build_kd_tree() self.app_context.g.filmlocationsKDTree = filmLocationsKDTree self.app_context.g.invertedPointLocationIndex = invertedPointLocationIndex
# # IMPORTANT!: This is a shared data-structure, built only at startup, # that is READ-ONLY by all, and so can be safely shared. invertedPointLocationIndex = InvertedPointLocationIndex() invertedPointLocationIndex.build_inverted_location_index() # A KD tree, implemented using the scipy package's kdtree implementation # under the hood, to allow for fast O(ln) queries of 2D point data. The # points that it stores are geocoded locations coded in UTM to allow them # to be treated as 2D points to an approximation. # # IMPORTANT!: This is a shared data-structure, built only at startup, # that is READ-ONLY by all, and so can be safely shared. filmLocationsKDTree = LocationsKDTree() filmLocationsKDTree.load_point_data() filmLocationsKDTree.build_kd_tree() # If the request is for the endpoint 'films_near_me,' which is seeking # the 7 films closest to a user's location, only then do we bother to # load the request with the globally existing KD tree and inverted # index. @app.before_request def before_request(): print "before request ..." if request.endpoint == 'films_near_me': g.filmlocationsKDTree = filmLocationsKDTree g.invertedPointLocationIndex = invertedPointLocationIndex def make_shell_context(): return dict(app=app)