def test_distance(self): home = Point(52.015, -0.221) dest = Point(52.6333, -2.5) assert '%i kM' % home.distance(dest) == '169 kM' assert '%i kM' % home.distance(dest, method='sloc') == '169 kM' with raises(ValueError, message="Unknown method type 'Invalid'"): home.distance(dest, method='Invalid')
def find_nearby(lat,long, limit): """ Returns nearest limit incident objects within max_distance in distance order """ results = [] target = Point(lat,long) for (id, p) in incidents: m = target.distance(p) * 1000 #if m <= max_distance: results.append((m,id)) results.sort(cmp=lambda a,b: cmp(a[0],b[0])) return [dict(distance=i[0], **incident_map[i[1]]) for i in results[:limit]]
def test_destination(self): expect(Point(52.015, -0.221).destination(294, 169)) == \ Point(52.611638750214745, -2.509374081952352, 'metric', 'degrees', 0) home = Point(52.015, -0.221, 'imperial') expect(home.destination(294, 169 / utils.STATUTE_MILE)) == \ Point(52.611638750214745, -2.509374081952352, 'metric', 'degrees', 0) home = Point(52.015, -0.221, 'nautical') expect(home.destination(294, 169 / utils.NAUTICAL_MILE)) == \ Point(52.611638750214745, -2.509374081952352, 'metric', 'degrees', 0) expect(Point(36.1200, -86.6700).destination(274, 2885)) == \ Point(33.6872799137609, -118.32721842114393, 'metric', 'degrees', 0)
def getIds(self, redisConn, lat, lon, radius, store = False ): res = None for r in self.resolutions: if r >= radius: res = r break if not res: logging.warn("Radius too big for available resolutions") return [] closest = set() if radius > 0 and radius <= self.RES_128KM: bitres = self.BIT_RESOLUTIONS[res] cell = self.getGeocell(lat, lon, bitres) closest.add(self.getKey(res, cell)) p = Point(lat, lon) with TimeSampler(None, 'collecting cells'): for bearing in (0, 45, 90, 135, 180, 225, 270, 315): dest = p.destination(bearing, math.sqrt(2 * (radius**2)) if bearing % 90 else radius) cell = self.getGeocell(dest.latitude, dest.longitude, self.BIT_RESOLUTIONS[res]) closest.add(self.getKey(res, cell)) tmpKey = 'box:%s:%s,%s' % (self.className, lat,lon) if not store: redisConn.zunionstore(tmpKey, list(closest)) return redisConn.zrevrange(tmpKey, 0, -1, withscores=True) else: return list(closest) return [] if not store else None
def test_to_grid_locator(self): home = Point(52.015, -0.221) expect(home.to_grid_locator('extsquare')) == 'IO92va33' expect(home.to_grid_locator('subsquare')) == 'IO92va' expect(home.to_grid_locator()) == 'IO92'
def test_distance(self): home = Point(52.015, -0.221) dest = Point(52.6333, -2.5) expect('%i kM' % home.distance(dest)) == '169 kM' expect('%i kM' % home.distance(dest, method='sloc')) == '169 kM' with expect.raises(ValueError, "Unknown method type 'Invalid'"): home.distance(dest, method='Invalid') start = Point(36.1200, -86.6700) dest = Point(33.9400, -118.4000) expect('%i kM' % start.distance(dest)) == '2884 kM' start.units = 'imperial' expect('%i mi' % start.distance(dest)) == '1792 mi' start.units = 'nautical' expect('%i nmi' % start.distance(dest)) == '1557 nmi' start.units = 'metric' expect('%i kM' % start.distance(dest, method='sloc')) == '2884 kM'
def test_destination(self, units, multiplier): home = Point(52.015, -0.221, units=units) assert home.destination(294, 169 / multiplier) == \ Point(52.611638750214745, -2.509374081952352)
def test_distance2(self, units, result): start = Point(36.1200, -86.6700, units=units) dest = Point(33.9400, -118.4000) assert int(start.distance(dest)) == result