def test_get_distance(self): meters1 = get_distance(Location(12.3456, -65.4321), Location(12.7373, -65.1212)) meters2 = get_distance_unit(Location(12.3456, -65.4321), Location(12.7373, -65.1212), 3) self.assertEqual(meters1, meters2) self.assertAlmostEqual(meters1, 55098.21, places=2)
def test_jitter(self): lat = 40.777452 lon = -111.887663 loc = Location(lat, lon) loc.jitter(0.03, 0.07) self.assertNotEqual(loc[0], lat) self.assertNotEqual(loc[1], lon) self.assertTrue(lat - 0.03 <= loc[0] <= lat + 0.03) self.assertTrue(lon - 0.07 <= loc[1] <= lon + 0.07)
def test_get_cell_ids(self): cache = CellCache() cells = cache.get_cell_ids(Location(-75.56283, 153.23452)) expected = array('Q', [ 12614130531357949952, 12614130645174583296, 12614130647322066944, 12614130649469550592, 12614130662354452480, 12614130664501936128, 12614130666649419776, 12614130668796903424, 12614130670944387072, 12614130673091870720, 12614130675239354368, 12614130677386838016, 12614130780466053120, 12614130782613536768 ]) self.assertEqual(cells, expected) self.assertEqual(len(cache), 1)
def test_cache(self): cache = CellCache() loc = Location(40.12345, -110.6789) cells = cache.get_cell_ids(loc) expected = array('Q', [ 9749833325740032000, 9749833327887515648, 9749833336477450240, 9749833338624933888, 9749833340772417536, 9749833342919901184, 9749833345067384832, 9749833347214868480, 9749833349362352128, 9749833351509835776, 9749833353657319424, 9749833355804803072, 9749833357952286720, 9749833360099770368, 9749833383722090496, 9749833385869574144, 9749833388017057792, 9749833390164541440, 9749833392312025088, 9749833437409181696, 9749833439556665344 ]) self.assertEqual(cells, expected) self.assertEqual(len(cache), 1) cells = cache.get_cell_ids(loc) cells = cache.get_cell_ids(loc) self.assertEqual(cells, expected) self.assertEqual(len(cache), 1) cells = cache.get_cell_ids(Location(0.56283, -0.23452)) self.assertEqual(len(cache), 2)
def test_get_distance_meters(self): meters = get_distance_unit(Location(88, 188), Location(89, 189), 3) self.assertAlmostEqual(meters, 111228.94, places=2)
def test_get_distance_kilometers(self): kilometers = get_distance_unit(Location(.5, .5), Location(-.5, -.5), 2) self.assertAlmostEqual(kilometers, 157.2526, places=4)
def test_get_distance_miles(self): miles = get_distance_unit(Location(-37.12345, 73.6789), Location(-37.54321, 73.9876), 1) self.assertAlmostEqual(miles, 33.5971, places=4)
def test_contains_location(self): polygon = Polygon((TRAPEZOID, CW_TRIANGLE)) self.assertTrue(Location(40.7704, -111.8910) in polygon) self.assertFalse(Location(40.2497, -111.6492) in polygon)
def test_geocode(self): log = getLogger('geocoder') place = geocode('Salt Lake Temple', log) self.assertAlmostEqual(place.area, 0.002, places=3) self.assertEqual(place.center, Location(40.77046869463424, -111.89191200565921))
def test_diagonal_distance(self): lat, lon = diagonal_distance(Location(40.2497, -111.6492), 500.0) self.assertAlmostEqual(lat, 0.00383, places=5) self.assertAlmostEqual(lon, 0.00309, places=5)
def test_distance_to_latlon(self): lat, lon = distance_to_latlon(Location(40.2497, -111.6492), 70.0) self.assertAlmostEqual(lat, 0.00063, places=5) self.assertAlmostEqual(lon, 0.00082, places=5)
def test_set_random(self): loc = Location(40.1, -110.1) cache = AltitudeCache(13, 'AIzafake', 1373.7, 1559.23) cache.set_random(loc) self.assertTrue(1373.7 <= loc[2] <= 1559.23)
def test_contains_location(self): rectangle = Rectangle((40.2557, -111.6561), (40.2459, -111.643241)) self.assertTrue(Location(40.2497, -111.6492) in rectangle) self.assertFalse(Location(40.7704, -111.8910) in rectangle)
def test_multiple(self): points = (Location(40.2634, -111.6406), Location(40.2484, -111.6513), Location(40.2362, -111.6364)) self.assertEqual(encode_multiple(points), 'g}vtFvxkhTv|AzaAfkAc|A')
def test_single(self): self.assertEqual(encode_single(Location(40.761731, -111.901111)), 'ygxwF|t~iT')
def test_location_to_cellid(self): loc = Location(40.2637, -111.639794) raw = location_to_cellid(loc, 20, False) stripped = location_to_cellid(loc, 20, True) self.assertEqual(raw, 9749608109427392512) self.assertEqual(stripped, 9297950848987)
def test_location_to_token(self): loc = Location(40.2637, -111.639794) token = location_to_token(loc, 20) self.assertEqual(token, '874d90eb7db')
def test_pickle(self): loc = Location(40.768721, -111.901673) pickled = pickle_dumps(loc) unpickled = pickle_loads(pickled) self.assertEqual(loc.coords, unpickled.coords)
def test_get_bearing(self): loc1 = Location(40.239416, -111.643654) loc2 = Location(40.248302, -111.620660) self.assertAlmostEqual(get_bearing(loc1, loc2), 63.14, places=2)
def test_time(self): loc = Location(40.768721, -111.901673) t = time() loc.update_time() self.assertTrue(t - 1 <= loc.time <= t + 1)
def test_contains_location(self): loop = Loop(CCW_TRIANGLE) self.assertTrue(Location(40.7704, -111.8910) in loop) self.assertFalse(Location(40.2497, -111.6492) in loop)