Esempio n. 1
0
class TestQuadkeyFileTileCache(TileCacheTestBase):
    def setup(self):
        TileCacheTestBase.setup(self)
        self.cache = FileCache(self.cache_dir, 'png', directory_layout='quadkey')

    def test_store_tile(self):
        tile = self.create_tile((3, 4, 2))
        self.cache.store_tile(tile)
        tile_location = os.path.join(self.cache_dir, '11.png' )
        assert os.path.exists(tile_location), tile_location
Esempio n. 2
0
 def load_tile(self, tile, with_metadata=False):
     if tile.source:
         # Do not record tiles with source as "loaded" as FileCache will
         # return tile without checking/loading from filesystem.
         return True
     self.loaded_tiles.add(tile.coord)
     return FileCache.load_tile(self, tile, with_metadata)
Esempio n. 3
0
 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')
Esempio n. 4
0
 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.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)
Esempio n. 5
0
def file_cache(tmpdir):
    return FileCache(cache_dir=tmpdir.join('cache').strpath, file_ext='png')
Esempio n. 6
0
 def store_tile(self, tile):
     assert tile.coord not in self.stored_tiles
     self.stored_tiles.add(tile.coord)
     if self.cache_dir != '/dev/null':
         FileCache.store_tile(self, tile)
Esempio n. 7
0
 def load_tile(self, tile, with_metadata=False):
     self.loaded_tiles.add(tile.coord)
     return FileCache.load_tile(self, tile, with_metadata)
Esempio n. 8
0
 def store_tile(self, tile):
     assert tile.coord not in self.stored_tiles
     self.stored_tiles.add(tile.coord)
     if self.cache_dir != '/dev/null':
         FileCache.store_tile(self, tile)
Esempio n. 9
0
 def check_level_location(self, layout, level, path):
     cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
     eq_(cache.level_location(level), path)
Esempio n. 10
0
 def check_tile_location(self, layout, tile_coord, path):
     cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
     eq_(cache.tile_location(Tile(tile_coord)), path)
Esempio n. 11
0
class TestFileTileCache(TileCacheTestBase):
    def setup(self):
        TileCacheTestBase.setup(self)
        self.cache = FileCache(self.cache_dir, 'png')

    def test_store_tile(self):
        tile = self.create_tile((5, 12, 4))
        self.cache.store_tile(tile)
        tile_location = os.path.join(self.cache_dir, '04', '000', '000', '005',
                                     '000', '000', '012.png')
        assert os.path.exists(tile_location), tile_location

    def test_single_color_tile_store(self):
        img = Image.new('RGB', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4),
                    ImageSource(img,
                                image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105.png')
        assert is_png(open(loc, 'rb'))

        tile2 = Tile((0, 0, 1), ImageSource(img))
        self.cache.store_tile(tile2)
        assert self.cache.is_cached(tile2)
        loc2 = self.cache.tile_location(tile2)
        assert os.path.islink(loc2)
        assert os.path.realpath(loc2).endswith('ff0105.png')
        assert is_png(open(loc2, 'rb'))

        assert loc != loc2
        assert os.path.samefile(loc, loc2)

    def test_single_color_tile_store_w_alpha(self):
        img = Image.new('RGBA', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4),
                    ImageSource(img,
                                image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105ff.png')
        assert is_png(open(loc, 'rb'))

    def test_load_metadata_missing_tile(self):
        tile = Tile((0, 0, 0))
        self.cache.load_tile_metadata(tile)
        assert tile.timestamp == 0
        assert tile.size == 0

    def create_cached_tile(self, tile):
        loc = self.cache.tile_location(tile, create_dir=True)
        with open(loc, 'wb') as f:
            f.write(b'foo')

    def check_tile_location(self, layout, tile_coord, path):
        cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
        eq_(cache.tile_location(Tile(tile_coord)), path)

    def test_tile_locations(self):
        yield self.check_tile_location, 'mp', (
            12345, 67890, 2), '/tmp/foo/02/0001/2345/0006/7890.png'
        yield self.check_tile_location, 'mp', (
            12345, 67890, 12), '/tmp/foo/12/0001/2345/0006/7890.png'

        yield self.check_tile_location, 'tc', (
            12345, 67890, 2), '/tmp/foo/02/000/012/345/000/067/890.png'
        yield self.check_tile_location, 'tc', (
            12345, 67890, 12), '/tmp/foo/12/000/012/345/000/067/890.png'

        yield self.check_tile_location, 'tms', (
            12345, 67890, 2), '/tmp/foo/2/12345/67890.png'
        yield self.check_tile_location, 'tms', (
            12345, 67890, 12), '/tmp/foo/12/12345/67890.png'

        yield self.check_tile_location, 'quadkey', (0, 0, 0), '/tmp/foo/.png'
        yield self.check_tile_location, 'quadkey', (0, 0, 1), '/tmp/foo/0.png'
        yield self.check_tile_location, 'quadkey', (1, 1, 1), '/tmp/foo/3.png'
        yield self.check_tile_location, 'quadkey', (
            12345, 67890, 12), '/tmp/foo/200200331021.png'

        yield self.check_tile_location, 'arcgis', (
            1, 2, 3), '/tmp/foo/L03/R00000002/C00000001.png'
        yield self.check_tile_location, 'arcgis', (
            9, 2, 3), '/tmp/foo/L03/R00000002/C00000009.png'
        yield self.check_tile_location, 'arcgis', (
            10, 2, 3), '/tmp/foo/L03/R00000002/C0000000a.png'
        yield self.check_tile_location, 'arcgis', (
            12345, 67890, 12), '/tmp/foo/L12/R00010932/C00003039.png'

    def check_level_location(self, layout, level, path):
        cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
        eq_(cache.level_location(level), path)

    def test_level_locations(self):
        yield self.check_level_location, 'mp', 2, '/tmp/foo/02'
        yield self.check_level_location, 'mp', 12, '/tmp/foo/12'

        yield self.check_level_location, 'tc', 2, '/tmp/foo/02'
        yield self.check_level_location, 'tc', 12, '/tmp/foo/12'

        yield self.check_level_location, 'tms', '2', '/tmp/foo/2'
        yield self.check_level_location, 'tms', 12, '/tmp/foo/12'

        yield self.check_level_location, 'arcgis', 3, '/tmp/foo/L03'
        yield self.check_level_location, 'arcgis', 3, '/tmp/foo/L03'
        yield self.check_level_location, 'arcgis', 3, '/tmp/foo/L03'
        yield self.check_level_location, 'arcgis', 12, '/tmp/foo/L12'

    def test_level_location_quadkey(self):
        try:
            self.check_level_location('quadkey', 0, None)
        except NotImplementedError:
            pass
        else:
            assert False, "expected NotImplementedError"
Esempio n. 12
0
 def test_level_location_quadkey(self):
     cache = FileCache('/tmp/foo', 'png', directory_layout='quadkey')
     with pytest.raises(NotImplementedError):
         cache.level_location(0)
Esempio n. 13
0
 def test_level_location(self, layout, level, path):
     cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
     assert os.path.abspath(
         cache.level_location(level)) == os.path.abspath(path)
Esempio n. 14
0
 def test_tile_location(self, layout, tile_coord, path):
     cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
     assert os.path.abspath(cache.tile_location(
         Tile(tile_coord))) == os.path.abspath(path)
Esempio n. 15
0
class TestFileTileCache(TileCacheTestBase):
    def setup(self):
        TileCacheTestBase.setup(self)
        self.cache = FileCache(self.cache_dir, 'png')

    def test_store_tile(self):
        tile = self.create_tile((5, 12, 4))
        self.cache.store_tile(tile)
        tile_location = os.path.join(self.cache_dir, '04', '000', '000', '005',
                                     '000', '000', '012.png')
        assert os.path.exists(tile_location), tile_location

    @pytest.mark.skipif(
        sys.platform == 'win32',
        reason='link_single_color_tiles not supported on windows')
    def test_single_color_tile_store(self):
        img = Image.new('RGB', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4),
                    ImageSource(img,
                                image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105.png')
        assert is_png(open(loc, 'rb'))

        tile2 = Tile((0, 0, 1), ImageSource(img))
        self.cache.store_tile(tile2)
        assert self.cache.is_cached(tile2)
        loc2 = self.cache.tile_location(tile2)
        assert os.path.islink(loc2)
        assert os.path.realpath(loc2).endswith('ff0105.png')
        assert is_png(open(loc2, 'rb'))

        assert loc != loc2
        assert os.path.samefile(loc, loc2)

    @pytest.mark.skipif(
        sys.platform == 'win32',
        reason='link_single_color_tiles not supported on windows')
    def test_single_color_tile_store_w_alpha(self):
        img = Image.new('RGBA', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4),
                    ImageSource(img,
                                image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105ff.png')
        assert is_png(open(loc, 'rb'))

    def test_load_metadata_missing_tile(self):
        tile = Tile((0, 0, 0))
        self.cache.load_tile_metadata(tile)
        assert tile.timestamp == 0
        assert tile.size == 0

    def create_cached_tile(self, tile):
        loc = self.cache.tile_location(tile, create_dir=True)
        with open(loc, 'wb') as f:
            f.write(b'foo')

    @pytest.mark.parametrize('layout,tile_coord,path', [
        ['mp', (12345, 67890, 2), '/tmp/foo/02/0001/2345/0006/7890.png'],
        ['mp', (12345, 67890, 12), '/tmp/foo/12/0001/2345/0006/7890.png'],
        ['tc', (12345, 67890, 2), '/tmp/foo/02/000/012/345/000/067/890.png'],
        ['tc', (12345, 67890, 12), '/tmp/foo/12/000/012/345/000/067/890.png'],
        ['tms', (12345, 67890, 2), '/tmp/foo/2/12345/67890.png'],
        ['tms', (12345, 67890, 12), '/tmp/foo/12/12345/67890.png'],
        ['quadkey', (0, 0, 0), '/tmp/foo/.png'],
        ['quadkey', (0, 0, 1), '/tmp/foo/0.png'],
        ['quadkey', (1, 1, 1), '/tmp/foo/3.png'],
        ['quadkey', (12345, 67890, 12), '/tmp/foo/200200331021.png'],
        ['arcgis', (1, 2, 3), '/tmp/foo/L03/R00000002/C00000001.png'],
        ['arcgis', (9, 2, 3), '/tmp/foo/L03/R00000002/C00000009.png'],
        ['arcgis', (10, 2, 3), '/tmp/foo/L03/R00000002/C0000000a.png'],
        ['arcgis', (12345, 67890, 12), '/tmp/foo/L12/R00010932/C00003039.png'],
    ])
    def test_tile_location(self, layout, tile_coord, path):
        cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
        assert os.path.abspath(cache.tile_location(
            Tile(tile_coord))) == os.path.abspath(path)

    @pytest.mark.parametrize('layout,level,path', [
        ['mp', 2, '/tmp/foo/02'],
        ['mp', 12, '/tmp/foo/12'],
        ['tc', 2, '/tmp/foo/02'],
        ['tc', 12, '/tmp/foo/12'],
        ['tms', '2', '/tmp/foo/2'],
        ['tms', 12, '/tmp/foo/12'],
        ['arcgis', 3, '/tmp/foo/L03'],
        ['arcgis', 3, '/tmp/foo/L03'],
        ['arcgis', 3, '/tmp/foo/L03'],
        ['arcgis', 12, '/tmp/foo/L12'],
    ])
    def test_level_location(self, layout, level, path):
        cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
        assert os.path.abspath(
            cache.level_location(level)) == os.path.abspath(path)

    def test_level_location_quadkey(self):
        cache = FileCache('/tmp/foo', 'png', directory_layout='quadkey')
        with pytest.raises(NotImplementedError):
            cache.level_location(0)
Esempio n. 16
0
class TestFileTileCache(TileCacheTestBase):
    def setup(self):
        TileCacheTestBase.setup(self)
        self.cache = FileCache(self.cache_dir, 'png')

    def test_store_tile(self):
        tile = self.create_tile((5, 12, 4))
        self.cache.store_tile(tile)
        tile_location = os.path.join(self.cache_dir,
            '04', '000', '000', '005', '000', '000', '012.png' )
        assert os.path.exists(tile_location), tile_location

    def test_single_color_tile_store(self):
        img = Image.new('RGB', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4), ImageSource(img, image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105.png')
        assert is_png(open(loc, 'rb'))

        tile2 = Tile((0, 0, 1), ImageSource(img))
        self.cache.store_tile(tile2)
        assert self.cache.is_cached(tile2)
        loc2 = self.cache.tile_location(tile2)
        assert os.path.islink(loc2)
        assert os.path.realpath(loc2).endswith('ff0105.png')
        assert is_png(open(loc2, 'rb'))

        assert loc != loc2
        assert os.path.samefile(loc, loc2)

    def test_single_color_tile_store_w_alpha(self):
        img = Image.new('RGBA', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4), ImageSource(img, image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105ff.png')
        assert is_png(open(loc, 'rb'))

    def test_load_metadata_missing_tile(self):
        tile = Tile((0, 0, 0))
        self.cache.load_tile_metadata(tile)
        assert tile.timestamp == 0
        assert tile.size == 0

    def create_cached_tile(self, tile):
        loc = self.cache.tile_location(tile, create_dir=True)
        with open(loc, 'wb') as f:
            f.write(b'foo')
Esempio n. 17
0
 def setup(self):
     TileCacheTestBase.setup(self)
     self.cache = FileCache(self.cache_dir, 'png')
Esempio n. 18
0
class TestFileTileCache(TileCacheTestBase):
    def setup(self):
        TileCacheTestBase.setup(self)
        self.cache = FileCache(self.cache_dir, 'png')

    def test_store_tile(self):
        tile = self.create_tile((5, 12, 4))
        self.cache.store_tile(tile)
        tile_location = os.path.join(self.cache_dir,
            '04', '000', '000', '005', '000', '000', '012.png' )
        assert os.path.exists(tile_location), tile_location

    def test_single_color_tile_store(self):
        img = Image.new('RGB', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4), ImageSource(img, image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105.png')
        assert is_png(open(loc, 'rb'))

        tile2 = Tile((0, 0, 1), ImageSource(img))
        self.cache.store_tile(tile2)
        assert self.cache.is_cached(tile2)
        loc2 = self.cache.tile_location(tile2)
        assert os.path.islink(loc2)
        assert os.path.realpath(loc2).endswith('ff0105.png')
        assert is_png(open(loc2, 'rb'))

        assert loc != loc2
        assert os.path.samefile(loc, loc2)

    def test_single_color_tile_store_w_alpha(self):
        img = Image.new('RGBA', (256, 256), color='#ff0105')
        tile = Tile((0, 0, 4), ImageSource(img, image_opts=ImageOptions(format='image/png')))
        self.cache.link_single_color_images = True
        self.cache.store_tile(tile)
        assert self.cache.is_cached(tile)
        loc = self.cache.tile_location(tile)
        assert os.path.islink(loc)
        assert os.path.realpath(loc).endswith('ff0105ff.png')
        assert is_png(open(loc, 'rb'))

    def test_load_metadata_missing_tile(self):
        tile = Tile((0, 0, 0))
        self.cache.load_tile_metadata(tile)
        assert tile.timestamp == 0
        assert tile.size == 0

    def create_cached_tile(self, tile):
        loc = self.cache.tile_location(tile, create_dir=True)
        with open(loc, 'wb') as f:
            f.write(b'foo')


    def check_tile_location(self, layout, tile_coord, path):
        cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
        eq_(cache.tile_location(Tile(tile_coord)), path)

    def test_tile_locations(self):
        yield self.check_tile_location, 'mp', (12345, 67890,  2), '/tmp/foo/02/0001/2345/0006/7890.png'
        yield self.check_tile_location, 'mp', (12345, 67890, 12), '/tmp/foo/12/0001/2345/0006/7890.png'

        yield self.check_tile_location, 'tc', (12345, 67890,  2), '/tmp/foo/02/000/012/345/000/067/890.png'
        yield self.check_tile_location, 'tc', (12345, 67890, 12), '/tmp/foo/12/000/012/345/000/067/890.png'

        yield self.check_tile_location, 'tms', (12345, 67890,  2), '/tmp/foo/2/12345/67890.png'
        yield self.check_tile_location, 'tms', (12345, 67890, 12), '/tmp/foo/12/12345/67890.png'

        yield self.check_tile_location, 'quadkey', (0, 0, 0), '/tmp/foo/.png'
        yield self.check_tile_location, 'quadkey', (0, 0, 1), '/tmp/foo/0.png'
        yield self.check_tile_location, 'quadkey', (1, 1, 1), '/tmp/foo/3.png'
        yield self.check_tile_location, 'quadkey', (12345, 67890, 12), '/tmp/foo/200200331021.png'

        yield self.check_tile_location, 'arcgis', (1, 2, 3), '/tmp/foo/L03/R00000002/C00000001.png'
        yield self.check_tile_location, 'arcgis', (9, 2, 3), '/tmp/foo/L03/R00000002/C00000009.png'
        yield self.check_tile_location, 'arcgis', (10, 2, 3), '/tmp/foo/L03/R00000002/C0000000a.png'
        yield self.check_tile_location, 'arcgis', (12345, 67890, 12), '/tmp/foo/L12/R00010932/C00003039.png'


    def check_level_location(self, layout, level, path):
        cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
        eq_(cache.level_location(level), path)

    def test_level_locations(self):
        yield self.check_level_location, 'mp', 2, '/tmp/foo/02'
        yield self.check_level_location, 'mp', 12, '/tmp/foo/12'

        yield self.check_level_location, 'tc',  2, '/tmp/foo/02'
        yield self.check_level_location, 'tc', 12, '/tmp/foo/12'

        yield self.check_level_location, 'tms',  '2', '/tmp/foo/2'
        yield self.check_level_location, 'tms', 12, '/tmp/foo/12'

        yield self.check_level_location, 'arcgis', 3, '/tmp/foo/L03'
        yield self.check_level_location, 'arcgis', 3, '/tmp/foo/L03'
        yield self.check_level_location, 'arcgis', 3, '/tmp/foo/L03'
        yield self.check_level_location, 'arcgis', 12, '/tmp/foo/L12'

    def test_level_location_quadkey(self):
        try:
            self.check_level_location('quadkey', 0, None)
        except NotImplementedError:
            pass
        else:
            assert False, "expected NotImplementedError"
Esempio n. 19
0
 def setup(self):
     TileCacheTestBase.setup(self)
     self.cache = FileCache(self.cache_dir, 'png', directory_layout='quadkey')
Esempio n. 20
0
 def check_tile_location(self, layout, tile_coord, path):
     cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
     eq_(cache.tile_location(Tile(tile_coord)), path)
Esempio n. 21
0
 def load_tile(self, tile, with_metadata=False):
     self.loaded_tiles.add(tile.coord)
     return FileCache.load_tile(self, tile, with_metadata)
Esempio n. 22
0
 def check_level_location(self, layout, level, path):
     cache = FileCache('/tmp/foo', 'png', directory_layout=layout)
     eq_(cache.level_location(level), path)