Example #1
0
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
Example #2
0
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/')
Example #3
0
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/")
Example #4
0
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/')
Example #5
0
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
Example #6
0
 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()
Example #7
0
 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()
Example #8
0
 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
Example #9
0
 def setUp(self):
     self.tilelayout = WrappedTileLayout(OSMTileLayout(), 'prefix/',
                                         '.suffix')
Example #10
0
 def setUp(self):
     self.tilelayout = OSMTileLayout()
Example #11
0
 def setUp(self):
     self.tilelayout = OSMTileLayout()
Example #12
0
 def setUp(self):
     self.tilelayout = WrappedTileLayout(OSMTileLayout(), "prefix/",
                                         ".suffix")
Example #13
0
 def setUp(self) -> None:
     self.tilelayout = OSMTileLayout()