예제 #1
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')
예제 #2
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')
예제 #3
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"
예제 #4
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"
예제 #5
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)