class ZipTileStore(TileStore): def __init__(self, zipfile, layout=None, **kwargs): TileStore.__init__(self, **kwargs) self.zipfile = zipfile self.layout = layout if self.layout is None: extension_count = defaultdict(int) for name in self.zipfile.namelist(): extension_count[os.path.splitext(name)[1]] += 1 for extension, _ in sorted( extension_count.items(), key=lambda p: tuple(reversed(p)), reverse=True): if re.match(r'\.(jpe?g|png)\Z', extension, re.I): self.layout = WrappedTileLayout(OSMTileLayout(), suffix=extension) break if self.layout is None: self.layout = OSMTileLayout() def __contains__(self, tile): if not tile: return False try: filename = self.layout.filename(tile.tilecoord, tile.metadata) self.zipfile.getinfo(filename) return True except KeyError: return False def get_one(self, tile): if hasattr(tile, 'zipinfo'): tile.data = self.zipfile.read(tile.zipinfo) else: filename = self.layout.filename(tile.tilecoord, tile.metadata) jpgfile = ''.join((filename, '.jpg')) pngfile = ''.join((filename, '.png')) try: tile.data = self.zipfile.read(jpgfile) except KeyError: try: tile.data = self.zipfile.read(pngfile) except KeyError: print('no file :', filename) return tile def list(self): for zipinfo in self.zipfile.infolist(): try: yield Tile(self.layout.tilecoord(zipinfo.filename), zipinfo=zipinfo) except ValueError: pass def put_one(self, tile): filename = self.layout.filename(tile.tilecoord, tile.metadata) zipinfo = zipfile.ZipInfo(filename) zipinfo.compress_type = getattr(self, 'compress_type', zipfile.ZIP_DEFLATED) zipinfo.date_time = datetime.now().timetuple()[:6] zipinfo.external_attr = 0o644 << 16 self.zipfile.writestr(zipinfo, tile.data) return tile
class TestOSMTileLayout(unittest.TestCase): def setUp(self): self.tilelayout = OSMTileLayout() def test_filename(self): assert self.tilelayout.filename(TileCoord(1, 2, 3)) == '1/2/3' def test_tilecoord(self): assert self.tilelayout.tilecoord('1/2/3') == TileCoord(1, 2, 3) self.assertRaises(ValueError, self.tilelayout.tilecoord, '1/2/')
class TestOSMTileLayout(unittest.TestCase): def setUp(self) -> None: self.tilelayout = OSMTileLayout() def test_filename(self) -> None: assert self.tilelayout.filename(TileCoord(1, 2, 3)) == "1/2/3" def test_tilecoord(self) -> None: assert self.tilelayout.tilecoord("1/2/3") == TileCoord(1, 2, 3) self.assertRaises(ValueError, self.tilelayout.tilecoord, "1/2/")
class TestOSMTileLayout(unittest.TestCase): def setUp(self): self.tilelayout = OSMTileLayout() def test_filename(self): self.assertEqual(self.tilelayout.filename(TileCoord(1, 2, 3)), '1/2/3') def test_tilecoord(self): self.assertEqual(self.tilelayout.tilecoord('1/2/3'), TileCoord(1, 2, 3)) self.assertRaises(ValueError, self.tilelayout.tilecoord, '1/2/')
class ZipTileStore(TileStore): def __init__(self, zipfile, layout=None, **kwargs): TileStore.__init__(self, **kwargs) self.zipfile = zipfile self.layout = layout if self.layout is None: extension_count = defaultdict(int) for name in self.zipfile.namelist(): extension_count[os.path.splitext(name)[1]] += 1 for extension, _ in sorted( extension_count.items(), key=lambda p: tuple(reversed(p)), reverse=True): if re.match(r'\.(jpe?g|png)\Z', extension, re.I): self.layout = WrappedTileLayout(OSMTileLayout(), suffix=extension) break if self.layout is None: self.layout = OSMTileLayout() def __contains__(self, tile): if not tile: return False try: filename = self.layout.filename(tile.tilecoord, tile.metadata) self.zipfile.getinfo(filename) return True except KeyError: return False def get_one(self, tile): if hasattr(tile, 'zipinfo'): tile.data = self.zipfile.read(tile.zipinfo) else: filename = self.layout.filename(tile.tilecoord, tile.metadata) tile.data = self.zipfile.read(filename) return tile def list(self): for zipinfo in self.zipfile.infolist(): try: yield Tile(self.layout.tilecoord(zipinfo.filename), zipinfo=zipinfo) except ValueError: pass def put_one(self, tile): filename = self.layout.filename(tile.tilecoord, tile.metadata) zipinfo = zipfile.ZipInfo(filename) zipinfo.compress_type = getattr(self, 'compress_type', zipfile.ZIP_DEFLATED) zipinfo.date_time = datetime.now().timetuple()[:6] zipinfo.external_attr = 0o644 << 16 self.zipfile.writestr(zipinfo, tile.data) return tile
def __init__(self, zipfile, layout=None, **kwargs): TileStore.__init__(self, **kwargs) self.zipfile = zipfile self.layout = layout if self.layout is None: extension_count = defaultdict(int) for name in self.zipfile.namelist(): extension_count[os.path.splitext(name)[1]] += 1 for extension, count in sorted(extension_count.items(), key=lambda p: tuple(reversed(p)), reverse=True): if re.match(r"\.(jpe?g|png)\Z", extension, re.I): self.layout = WrappedTileLayout(OSMTileLayout(), suffix=extension) break if self.layout is None: self.layout = OSMTileLayout()
def __init__(self, zipfile, layout=None, **kwargs): TileStore.__init__(self, **kwargs) self.zipfile = zipfile self.layout = layout if self.layout is None: extension_count = defaultdict(int) for name in self.zipfile.namelist(): extension_count[os.path.splitext(name)[1]] += 1 for extension, count in sorted(extension_count.items(), key=lambda p: tuple(reversed(p)), reverse=True): if re.match(r'\.(jpe?g|png)\Z', extension, re.I): self.layout = WrappedTileLayout(OSMTileLayout(), suffix=extension) break if self.layout is None: self.layout = OSMTileLayout()
def __init__(self, zipfile: zipfile.ZipFile, layout: Optional[TileLayout] = None, **kwargs: Any): # pylint: disable=redefined-outer-name TileStore.__init__(self, **kwargs) self.zipfile = zipfile if layout is None: extension_count: Dict[str, int] = defaultdict(int) for name in self.zipfile.namelist(): extension_count[os.path.splitext(name)[1]] += 1 for extension, _ in sorted(extension_count.items(), key=lambda p: tuple(reversed(p)), reverse=True): if re.match(r"\.(jpe?g|png)\Z", extension, re.I): layout = WrappedTileLayout(OSMTileLayout(), suffix=extension) break if layout is None: layout = OSMTileLayout() self.layout = layout
def setUp(self): self.tilelayout = WrappedTileLayout(OSMTileLayout(), 'prefix/', '.suffix')
def setUp(self): self.tilelayout = OSMTileLayout()
def setUp(self): self.tilelayout = WrappedTileLayout(OSMTileLayout(), "prefix/", ".suffix")
def setUp(self) -> None: self.tilelayout = OSMTileLayout()