class TestTileManagerLocking(object): def setup(self): self.tile_dir = tempfile.mkdtemp() self.file_cache = MockFileCache(self.tile_dir, 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source = SlowMockSource() self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts) def test_get_single(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(self.source.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) def test_concurrent(self): def do_it(): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) threads = [threading.Thread(target=do_it) for _ in range(3)] [t.start() for t in threads] [t.join() for t in threads] eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(self.file_cache.loaded_tiles, counting_set([(0, 0, 1), (1, 0, 1), (0, 0, 1), (1, 0, 1)])) eq_(self.source.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) assert os.path.exists(self.file_cache.tile_location(Tile((0, 0, 1)))) def teardown(self): shutil.rmtree(self.tile_dir)
class TestTileManagerDifferentSourceGrid(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_grid = TileGrid(SRS(4326), bbox=[0, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.source_grid, self.client) self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts, locker=self.locker, ) def test_create_tiles(self): self.tile_mgr.creator().create_tiles([Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(1, 0, 1)])) eq_(self.client.requested_tiles, [(0, 0, 0)]) @raises(InvalidSourceQuery) def test_create_tiles_out_of_bounds(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 0))])
class TestTileManagerBulkMetaTiles(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90], origin='ul') self.source_base = SolidColorMockSource(color='#ff0000') self.source_base.supports_meta_tiles = False self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker, bulk_meta_tiles=True, ) def test_bulk_get(self): tiles = self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) eq_(len(tiles), 2 * 2) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2)])) for requested in [ self.source_base.requested, self.source_overlay.requested ]: eq_( set(requested), set([ ((-180.0, 0.0, -90.0, 90.0), (256, 256), SRS(4326)), ((-90.0, 0.0, 0.0, 90.0), (256, 256), SRS(4326)), ((-180.0, -90.0, -90.0, 0.0), (256, 256), SRS(4326)), ((-90.0, -90.0, 0.0, 0.0), (256, 256), SRS(4326)), ])) def test_bulk_get_error(self): self.tile_mgr.sources = [self.source_base, ErrorSource()] try: self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) except Exception as ex: eq_(ex.args[0], "source error") def test_bulk_get_multiple_meta_tiles(self): tiles = self.tile_mgr.creator().create_tiles( [Tile((1, 0, 2)), Tile((2, 0, 2))]) eq_(len(tiles), 2 * 2 * 2) eq_( self.file_cache.stored_tiles, set([ (0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2), (2, 0, 2), (3, 0, 2), (2, 1, 2), (3, 1, 2), ]))
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=10, minimize_meta_requests=True)
class TestTileManagerMultipleSources(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = MockSource() self.source_overlay = MockSource() self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', image_opts=self.image_opts, locker=self.locker, ) self.layer = CacheMapLayer(self.tile_mgr) def test_get_single(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1)])) eq_(self.source_base.requested, [((-180.0, -90.0, 0.0, 90.0), (256, 256), SRS(4326))]) eq_(self.source_overlay.requested, [((-180.0, -90.0, 0.0, 90.0), (256, 256), SRS(4326))])
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source = MockSource() self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts)
class TestTileManagerStaleTiles(object): def setup(self): self.cache_dir = tempfile.mkdtemp() self.file_cache = FileCache(cache_dir=self.cache_dir, file_ext='png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', locker=self.locker) def teardown(self): shutil.rmtree(self.cache_dir) def test_is_stale_missing(self): assert not self.tile_mgr.is_stale(Tile((0, 0, 1))) def test_is_stale_not_expired(self): create_cached_tile(Tile((0, 0, 1)), self.file_cache) assert not self.tile_mgr.is_stale(Tile((0, 0, 1))) def test_is_stale_expired(self): create_cached_tile(Tile((0, 0, 1)), self.file_cache, timestamp=time.time() - 3600) self.tile_mgr._expire_timestamp = time.time() assert self.tile_mgr.is_stale(Tile((0, 0, 1)))
def setup(self): self.cache_dir = tempfile.mkdtemp() self.file_cache = FileCache(cache_dir=self.cache_dir, file_ext='png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png')
def test_sources_with_mixed_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker)
class TestTileManagerSource(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source = MockSource() self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts, locker=self.locker, ) def test_create_tile(self): self.tile_mgr.creator().create_tiles( [Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(sorted(self.source.requested), [((-180.0, -90.0, 0.0, 90.0), (256, 256), SRS(4326)), ((0.0, -90.0, 180.0, 90.0), (256, 256), SRS(4326))])
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts)
def setup(self): self.tile_dir = tempfile.mkdtemp() self.file_cache = MockFileCache(self.tile_dir, 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source = SlowMockSource() self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts)
def test_sources_with_no_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.source_overlay.supports_meta_tiles = False self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0) assert self.tile_mgr.meta_grid is None
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = SolidColorMockSource(color='#ff0000') self.source_base.supports_meta_tiles = True self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = True self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0)
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts, locker=self.locker, )
class TestTileManagerWMSSource(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts, locker=self.locker, ) def test_same_lock_for_meta_tile(self): eq_(self.tile_mgr.lock(Tile((0, 0, 1))).lock_file, self.tile_mgr.lock(Tile((1, 0, 1))).lock_file ) def test_locks_for_meta_tiles(self): assert_not_equal(self.tile_mgr.lock(Tile((0, 0, 2))).lock_file, self.tile_mgr.lock(Tile((2, 0, 2))).lock_file ) def test_create_tile_first_level(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(self.client.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) def test_create_tile(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 0.0, 90.0), (512, 512), SRS(4326))]) def test_create_tiles(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 2)), Tile((2, 0, 2))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2), (2, 0, 2), (3, 0, 2), (2, 1, 2), (3, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 0.0, 90.0), (512, 512), SRS(4326)), ((0.0, -90.0, 180.0, 90.0), (512, 512), SRS(4326))]) def test_load_tile_coords(self): tiles = self.tile_mgr.load_tile_coords(((0, 0, 2), (2, 0, 2))) eq_(tiles[0].coord, (0, 0, 2)) assert isinstance(tiles[0].source, ImageSource) eq_(tiles[1].coord, (2, 0, 2)) assert isinstance(tiles[1].source, ImageSource) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2), (2, 0, 2), (3, 0, 2), (2, 1, 2), (3, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 0.0, 90.0), (512, 512), SRS(4326)), ((0.0, -90.0, 180.0, 90.0), (512, 512), SRS(4326))])
class TestTileManagerWMSSource(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts, locker=self.locker, ) def test_same_lock_for_meta_tile(self): eq_(self.tile_mgr.lock(Tile((0, 0, 1))).lock_file, self.tile_mgr.lock(Tile((1, 0, 1))).lock_file ) def test_locks_for_meta_tiles(self): assert_not_equal(self.tile_mgr.lock(Tile((0, 0, 2))).lock_file, self.tile_mgr.lock(Tile((2, 0, 2))).lock_file ) def test_create_tile_first_level(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(self.client.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) def test_create_tile(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 0.0, 90.0), (512, 512), SRS(4326))]) def test_create_tiles(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 2)), Tile((2, 0, 2))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2), (2, 0, 2), (3, 0, 2), (2, 1, 2), (3, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 0.0, 90.0), (512, 512), SRS(4326)), ((0.0, -90.0, 180.0, 90.0), (512, 512), SRS(4326))]) def test_load_tile_coords(self): tiles = self.tile_mgr.load_tile_coords(((0, 0, 2), (2, 0, 2))) eq_(tiles[0].coord, (0, 0, 2)) assert isinstance(tiles[0].source, ImageSource) eq_(tiles[1].coord, (2, 0, 2)) assert isinstance(tiles[1].source, ImageSource) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2), (2, 0, 2), (3, 0, 2), (2, 1, 2), (3, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 0.0, 90.0), (512, 512), SRS(4326)), ((0.0, -90.0, 180.0, 90.0), (512, 512), SRS(4326))])
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = MockSource() self.source_overlay = MockSource() self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', image_opts=self.image_opts, locker=self.locker, ) self.layer = CacheMapLayer(self.tile_mgr)
class TestTileManagerBulkMetaTiles(object): def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90], origin="ul") self.source_base = SolidColorMockSource(color="#ff0000") self.source_base.supports_meta_tiles = False self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source_base, self.source_overlay], "png", meta_size=[2, 2], meta_buffer=0, locker=self.locker, bulk_meta_tiles=True, ) def test_bulk_get(self): tiles = self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) eq_(len(tiles), 2 * 2) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2)])) for requested in [self.source_base.requested, self.source_overlay.requested]: eq_( set(requested), set( [ ((-180.0, 0.0, -90.0, 90.0), (256, 256), SRS(4326)), ((-90.0, 0.0, 0.0, 90.0), (256, 256), SRS(4326)), ((-180.0, -90.0, -90.0, 0.0), (256, 256), SRS(4326)), ((-90.0, -90.0, 0.0, 0.0), (256, 256), SRS(4326)), ] ), ) def test_bulk_get_error(self): self.tile_mgr.sources = [self.source_base, ErrorSource()] try: self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) except Exception as ex: eq_(ex.args[0], "source error") def test_bulk_get_multiple_meta_tiles(self): tiles = self.tile_mgr.creator().create_tiles([Tile((1, 0, 2)), Tile((2, 0, 2))]) eq_(len(tiles), 2 * 2 * 2) eq_( self.file_cache.stored_tiles, set([(0, 0, 2), (1, 0, 2), (0, 1, 2), (1, 1, 2), (2, 0, 2), (3, 0, 2), (2, 1, 2), (3, 1, 2)]), )
class TestTileManagerTiledSource(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts) def test_create_tiles(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(sorted(self.client.requested_tiles), [(0, 0, 1), (1, 0, 1)])
def test_upscale(self, mock_file_cache, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) image_opts = ImageOptions(format='image/png') tm = TileManager( grid, mock_file_cache, [], 'png', locker=tile_locker, image_opts=image_opts, ) assert is_blank(tm.load_tile_coords([(0, 0, 0)])) assert is_blank(tm.load_tile_coords([(3, 2, 5)])) assert mock_file_cache.stored_tiles == set() assert mock_file_cache.loaded_tiles == counting_set([(0, 0, 0), (3, 2, 5)])
def test_seed_with_res_list(self): if not load_wkt: raise SkipTest('no shapely installed') # box from 10 10 to 80 80 with small spike/corner to -10 60 (upper left) geom = load_wkt( "POLYGON((10 10, 10 50, -10 60, 10 80, 80 80, 80 10, 10 10))") self.grid = TileGrid( SRS(4326), bbox=[-180, -90, 180, 90], res=[360 / 256, 360 / 720, 360 / 2000, 360 / 5000, 360 / 8000]) self.tile_mgr = TileManager(self.grid, MockCache(), [self.source], 'png', locker=DummyLocker()) task = self.make_geom_task(geom, SRS(4326), [0, 1, 2, 3, 4]) seeder = TileWalker(task, self.seed_pool, handle_uncached=True) seeder.walk() eq_(len(self.seed_pool.seeded_tiles), 5) eq_(self.seed_pool.seeded_tiles[0], set([(0, 0)])) eq_(self.grid.grid_sizes[1], (3, 2)) eq_(self.seed_pool.seeded_tiles[1], set([(1, 0), (1, 1), (2, 0), (2, 1)])) eq_(self.grid.grid_sizes[2], (8, 4)) eq_(self.seed_pool.seeded_tiles[2], set([(4, 2), (5, 2), (4, 3), (5, 3), (3, 3)])) eq_(self.grid.grid_sizes[3], (20, 10)) eq_(len(self.seed_pool.seeded_tiles[3]), 5 * 5 + 2)
def tile_mgr(self, file_cache, slow_source, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) image_opts = ImageOptions(format='image/png') return TileManager(grid, file_cache, [slow_source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=image_opts, locker=tile_locker, )
def test_seed_with_res_list(self): # box from 10 10 to 80 80 with small spike/corner to -10 60 (upper left) geom = load_wkt( "POLYGON((10 10, 10 50, -10 60, 10 80, 80 80, 80 10, 10 10))") self.grid = TileGrid( SRS(4326), bbox=[-180, -90, 180, 90], res=[360 / 256, 360 / 720, 360 / 2000, 360 / 5000, 360 / 8000], ) self.tile_mgr = TileManager(self.grid, MockCache(), [self.source], "png", locker=DummyLocker()) task = self.make_geom_task(geom, SRS(4326), [0, 1, 2, 3, 4]) seeder = TileWalker(task, self.seed_pool, handle_uncached=True) seeder.walk() assert len(self.seed_pool.seeded_tiles) == 5 assert self.seed_pool.seeded_tiles[0] == set([(0, 0)]) assert self.grid.grid_sizes[1] == (3, 2) assert self.seed_pool.seeded_tiles[1] == set([(1, 0), (1, 1), (2, 0), (2, 1)]) assert self.grid.grid_sizes[2] == (8, 4) assert self.seed_pool.seeded_tiles[2] == set([(4, 2), (5, 2), (4, 3), (5, 3), (3, 3)]) assert self.grid.grid_sizes[3] == (20, 10) assert len(self.seed_pool.seeded_tiles[3]) == 5 * 5 + 2
def tile_mgr(self, mock_file_cache, mock_source, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) image_opts = ImageOptions(format='image/png') return TileManager(grid, mock_file_cache, [mock_source], 'png', image_opts=image_opts, locker=tile_locker, )
def tile_mgr(self, mock_file_cache, mock_wms_client, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) source = WMSSource(mock_wms_client) return TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=10, minimize_meta_requests=True, locker=tile_locker, )
def test_get_map_with_res_range(self, mock_file_cache, mock_wms_client, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) res_range = resolution_range(1000, 10) source = WMSSource(mock_wms_client, res_range=res_range) image_opts = ImageOptions(resampling='nearest') tile_mgr = TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=image_opts, locker=tile_locker) layer = CacheMapLayer(tile_mgr, image_opts=default_image_opts) with pytest.raises(BlankImage): result = layer.get_map( MapQuery( (-20037508.34, -20037508.34, 20037508.34, 20037508.34), (500, 500), SRS(900913), 'png')) assert mock_file_cache.stored_tiles == set() result = layer.get_map( MapQuery((0, 0, 10000, 10000), (50, 50), SRS(900913), 'png')) assert mock_file_cache.stored_tiles == \ set([(512, 257, 10), (513, 256, 10), (512, 256, 10), (513, 257, 10)]) assert result.size == (50, 50)
def test_get_map_with_res_range(self): res_range = resolution_range(1000, 10) self.source = WMSSource(self.client, res_range=res_range) self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts, locker=self.locker) self.layer = CacheMapLayer(self.tile_mgr, image_opts=default_image_opts) try: result = self.layer.get_map( MapQuery( (-20037508.34, -20037508.34, 20037508.34, 20037508.34), (500, 500), SRS(900913), 'png')) except BlankImage: pass else: assert False, 'expected BlankImage exception' eq_(self.file_cache.stored_tiles, set()) result = self.layer.get_map( MapQuery((0, 0, 10000, 10000), (50, 50), SRS(900913), 'png')) eq_( self.file_cache.stored_tiles, set([(512, 257, 10), (513, 256, 10), (512, 256, 10), (513, 257, 10)])) eq_(result.size, (50, 50))
class TestTileManagerMultipleSourcesWithMetaTiles(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = SolidColorMockSource(color='#ff0000') self.source_base.supports_meta_tiles = True self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = True self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker, ) def test_merged_tiles(self): tiles = self.tile_mgr.creator().create_tiles( [Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(self.source_base.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) eq_(self.source_overlay.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) hist = tiles[0].source.as_image().histogram() # lots of red (base), but not everything (overlay) assert 55000 < hist[255] < 60000 # red = 0xff assert 55000 < hist[256] # green = 0x00 assert 55000 < hist[512] # blue = 0x00 @raises(ValueError) def test_sources_with_mixed_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker) def test_sources_with_no_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.source_overlay.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker) assert self.tile_mgr.meta_grid is None
def test_scaled_tiles(self, name, file_cache, tile_locker, rescale_tiles, tiles, store, expected_load, output): res = [ 1.40625, # 0 0.703125, # 1 0.3515625, # 2 0.17578125, # 3 0.087890625, # 4 0.0439453125, # 5 0.02197265625, # 6 0.010986328125, # 7 0.007, # 8 additional resolution to test unregular grids 0.0054931640625, # 9 0.00274658203125, # 10 ] grid = TileGrid(SRS(4326), origin='sw', bbox=[-180, -90, 180, 90], res=res) image_opts = ImageOptions(format='image/png', resampling='nearest') tm = TileManager( grid, file_cache, [], 'png', locker=tile_locker, image_opts=image_opts, rescale_tiles=rescale_tiles, ) if store: colors = set() if output == "partial": colors.add((255, 255, 255)) for i, t in enumerate(store): color = (150+i*35, 5+i*35, 5+i*35) colors.add(color) tile = Tile(t, ImageSource(create_tmp_image_buf((256, 256), color=color))) file_cache.store_tile(tile) loaded_tiles = tm.load_tile_coords(tiles) assert not is_blank(loaded_tiles) assert len(loaded_tiles) == len(tiles) got_colors = set() for t in loaded_tiles: got_colors.update([c for _, c in t.source.as_image().getcolors()]) assert got_colors == colors else: loaded_tiles = tm.load_tile_coords(tiles) assert is_blank(loaded_tiles) == (output == "blank") assert len(loaded_tiles.tiles) == len(tiles) assert file_cache.stored_tiles == set(store) assert file_cache.loaded_tiles == counting_set(expected_load)
def tile_mgr(self, mock_file_cache, source_base, source_overlay, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90], origin='ul') return TileManager(grid, mock_file_cache, [source_base, source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=tile_locker, bulk_meta_tiles=True, )
def setup(self): self.cache_dir = tempfile.mkdtemp() self.file_cache = FileCache(cache_dir=self.cache_dir, file_ext="png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], "png", locker=self.locker)
def layer(self, mock_file_cache, mock_wms_client, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) source = WMSSource(mock_wms_client) image_opts = ImageOptions(resampling='nearest') tile_mgr = TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=image_opts, locker=tile_locker) return CacheMapLayer(tile_mgr, image_opts=default_image_opts)
class TestTileManagerSource(object): def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source = MockSource() self.image_opts = ImageOptions(format="image/png") self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], "png", image_opts=self.image_opts, locker=self.locker ) def test_create_tile(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_( sorted(self.source.requested), [((-180.0, -90.0, 0.0, 90.0), (256, 256), SRS(4326)), ((0.0, -90.0, 180.0, 90.0), (256, 256), SRS(4326))], )
def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source = MockSource() self.image_opts = ImageOptions(format="image/png") self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], "png", image_opts=self.image_opts, locker=self.locker )
class TestTileManagerMultipleSources(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = MockSource() self.source_overlay = MockSource() self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', image_opts=self.image_opts) self.layer = CacheMapLayer(self.tile_mgr) def test_get_single(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1)])) eq_(self.source_base.requested, [((-180.0, -90.0, 0.0, 90.0), (256, 256), SRS(4326))]) eq_(self.source_overlay.requested, [((-180.0, -90.0, 0.0, 90.0), (256, 256), SRS(4326))])
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts, locker=self.locker, )
class TestTileManagerDifferentSourceGrid(object): def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_grid = TileGrid(SRS(4326), bbox=[0, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.source_grid, self.client) self.image_opts = ImageOptions(format="image/png") self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], "png", image_opts=self.image_opts, locker=self.locker ) def test_create_tiles(self): self.tile_mgr.creator().create_tiles([Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(1, 0, 1)])) eq_(self.client.requested_tiles, [(0, 0, 0)]) @raises(InvalidSourceQuery) def test_create_tiles_out_of_bounds(self): self.tile_mgr.creator().create_tiles([Tile((0, 0, 0))])
class TestTileManagerWMSSourceMinimalMetaRequests(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=10, minimize_meta_requests=True) def test_create_tile_single(self): # not enabled for single tile requests self.tile_mgr.creator().create_tiles([Tile((0, 0, 2))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 2), (0, 1, 2), (1, 0, 2), (1, 1, 2)])) eq_(sorted(self.client.requested), [((-180.0, -90.0, 3.515625, 90.0), (522, 512), SRS(4326))]) def test_create_tile_multiple(self): self.tile_mgr.creator().create_tiles([Tile((4, 0, 3)), Tile((4, 1, 3)), Tile((4, 2, 3))]) eq_(self.file_cache.stored_tiles, set([(4, 0, 3), (4, 1, 3), (4, 2, 3)])) eq_(sorted(self.client.requested), [((-1.7578125, -90, 46.7578125, 46.7578125), (276, 778), SRS(4326))]) def test_create_tile_multiple_fragmented(self): self.tile_mgr.creator().create_tiles([Tile((4, 0, 3)), Tile((5, 2, 3))]) eq_(self.file_cache.stored_tiles, set([(4, 0, 3), (4, 1, 3), (4, 2, 3), (5, 0, 3), (5, 1, 3), (5, 2, 3)])) eq_(sorted(self.client.requested), [((-1.7578125, -90, 91.7578125, 46.7578125), (532, 778), SRS(4326))])
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = MockSource() self.source_overlay = MockSource() self.image_opts = ImageOptions(format='image/png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', image_opts=self.image_opts, locker=self.locker, ) self.layer = CacheMapLayer(self.tile_mgr)
class TestTileManagerStaleTiles(object): def setup(self): self.cache_dir = tempfile.mkdtemp() self.file_cache = FileCache(cache_dir=self.cache_dir, file_ext='png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png') def teardown(self): shutil.rmtree(self.cache_dir) def test_is_stale_missing(self): assert not self.tile_mgr.is_stale(Tile((0, 0, 1))) def test_is_stale_not_expired(self): create_cached_tile(Tile((0, 0, 1)), self.file_cache) assert not self.tile_mgr.is_stale(Tile((0, 0, 1))) def test_is_stale_expired(self): create_cached_tile(Tile((0, 0, 1)), self.file_cache, timestamp=time.time()-3600) self.tile_mgr._expire_timestamp = time.time() assert self.tile_mgr.is_stale(Tile((0, 0, 1)))
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90], origin='ul') self.source_base = SolidColorMockSource(color='#ff0000') self.source_base.supports_meta_tiles = False self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker, bulk_meta_tiles=True, )
class TestTileManagerMultipleSourcesWithMetaTiles(object): def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = SolidColorMockSource(color='#ff0000') self.source_base.supports_meta_tiles = True self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = True self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker, ) def test_merged_tiles(self): tiles = self.tile_mgr.creator().create_tiles([Tile((0, 0, 1)), Tile((1, 0, 1))]) eq_(self.file_cache.stored_tiles, set([(0, 0, 1), (1, 0, 1)])) eq_(self.source_base.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) eq_(self.source_overlay.requested, [((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326))]) hist = tiles[0].source.as_image().histogram() # lots of red (base), but not everything (overlay) assert 55000 < hist[255] < 60000 # red = 0xff assert 55000 < hist[256] # green = 0x00 assert 55000 < hist[512] # blue = 0x00 @raises(ValueError) def test_sources_with_mixed_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker) def test_sources_with_no_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.source_overlay.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source_base, self.source_overlay], 'png', meta_size=[2, 2], meta_buffer=0, locker=self.locker) assert self.tile_mgr.meta_grid is None
def test_sources_with_no_support_for_meta_tiles(self): self.source_base.supports_meta_tiles = False self.source_overlay.supports_meta_tiles = False self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source_base, self.source_overlay], "png", meta_size=[2, 2], meta_buffer=0, locker=self.locker, ) assert self.tile_mgr.meta_grid is None
def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], "png", meta_size=[2, 2], meta_buffer=10, minimize_meta_requests=True, locker=self.locker, )
def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(format="image/png") self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source], "png", meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts, locker=self.locker, )
def setup(self): self.file_cache = MockFileCache("/dev/null", "png") self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.source_base = SolidColorMockSource(color="#ff0000") self.source_base.supports_meta_tiles = True self.source_overlay = MockSource() self.source_overlay.supports_meta_tiles = True self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager( self.grid, self.file_cache, [self.source_base, self.source_overlay], "png", meta_size=[2, 2], meta_buffer=0, locker=self.locker, )
class TestTileManagerRemoveTiles(object): def setup(self): self.cache_dir = tempfile.mkdtemp() self.file_cache = FileCache(cache_dir=self.cache_dir, file_ext='png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', image_opts=self.image_opts) def teardown(self): shutil.rmtree(self.cache_dir) def test_remove_missing(self): self.tile_mgr.remove_tile_coords([(0, 0, 0), (0, 0, 1)]) def test_remove_existing(self): create_cached_tile(Tile((0, 0, 1)), self.file_cache) assert self.tile_mgr.is_cached(Tile((0, 0, 1))) self.tile_mgr.remove_tile_coords([(0, 0, 0), (0, 0, 1)]) assert not self.tile_mgr.is_cached(Tile((0, 0, 1)))