Commits

Oliver Tonnhofer  committed 27509fc

add directory_layout option for file cache

  • Participants
  • Parent commits dc67f7f

Comments (0)

Files changed (3)

File mapproxy/cache/file.py

     """
     This class is responsible to store and load the actual tile data.
     """
-    def __init__(self, cache_dir, file_ext, lock_dir=None,
+    def __init__(self, cache_dir, file_ext, lock_dir=None, directory_layout='tc',
                  link_single_color_images=False, lock_timeout=60.0):
         """
         :param cache_dir: the path where the tile will be stored
         self.file_ext = file_ext
         self._lock_cache_id = None
         self.link_single_color_images = link_single_color_images
-    
+        
+        if directory_layout == 'tc':
+            self.tile_location = self._tile_location_tc
+        elif directory_layout == 'tms':
+            self.tile_location = self._tile_location_tms
+        else:
+            raise ValueError('unknown directory_layout "%s"' % directory_layout)
+        
     def level_location(self, level):
         """
         Return the path where all tiles for `level` will be stored.
         else:
             return os.path.join(self.cache_dir, "%02d" % level)
     
-    def tile_location(self, tile, create_dir=False):
+    def _tile_location_tc(self, tile, create_dir=False):
         """
         Return the location of the `tile`. Caches the result as ``location``
         property of the `tile`.
             ensure_directory(tile.location)
         return tile.location
     
+    def _tile_location_tms(self, tile, create_dir=False):
+        """
+        Return the location of the `tile`. Caches the result as ``location``
+        property of the `tile`.
+        
+        :param tile: the tile object
+        :param create_dir: if True, create all necessary directories
+        :return: the full filename of the tile
+        
+        >>> from mapproxy.cache.tile import Tile
+        >>> c = FileCache(cache_dir='/tmp/cache/', file_ext='png')
+        >>> c.tile_location(Tile((3, 4, 2))).replace('\\\\', '/')
+        '/tmp/cache/02/000/000/003/000/000/004.png'
+        """
+        if tile.location is None:
+            x, y, z = tile.coord
+            tile.location = os.path.join(
+                self.level_location(str(z)),
+                str(x), str(y) + '.' + self.file_ext
+            )
+        if create_dir:
+            ensure_directory(tile.location)
+        return tile.location
+    
     def _single_color_tile_location(self, color, create_dir=False):
         """
         >>> c = FileCache(cache_dir='/tmp/cache/', file_ext='png')

File mapproxy/config/loader.py

         
         cache_dir = self.cache_dir()
         grid_conf.tile_grid() #create to resolve `base` in grid_conf.conf
+        directory_layout = self.conf.get('cache', {}).get('directory_layout', 'tc')
         suffix = grid_conf.conf['srs'].replace(':', '')
         cache_dir = os.path.join(cache_dir, self.conf['name'] + '_' + suffix)
         link_single_color_images = self.conf.get('link_single_color_images', False)
         
         lock_timeout = self.context.globals.get_value('http.client_timeout', {})
         
-        return FileCache(cache_dir, file_ext=file_ext,
+        return FileCache(cache_dir, file_ext=file_ext, directory_layout=directory_layout,
             lock_timeout=lock_timeout, link_single_color_images=link_single_color_images)
     
     def _mbtiles_cache(self, grid_conf, file_ext):

File mapproxy/config/spec.py

 
 cache_types = {
     'file': {
+        'directory_layout': str()
     },
     'mbtiles': {
         'filename': str()