diff --git a/src/c3nav/mapdata/utils/cache/indexed.py b/src/c3nav/mapdata/utils/cache/indexed.py index 4fc05ff6..d4ca1b85 100644 --- a/src/c3nav/mapdata/utils/cache/indexed.py +++ b/src/c3nav/mapdata/utils/cache/indexed.py @@ -1,8 +1,9 @@ import math +import os import struct +import threading import numpy as np -from django.conf import settings from PIL import Image from shapely import prepared from shapely.geometry import box @@ -22,7 +23,10 @@ class GeometryIndexed: dtype = np.uint16 variant_id = 0 - def __init__(self, resolution=settings.CACHE_RESOLUTION, x=0, y=0, data=None, filename=None): + def __init__(self, resolution=None, x=0, y=0, data=None, filename=None): + if resolution is None: + from django.conf import settings + resolution = settings.CACHE_RESOLUTION self.resolution = resolution self.x = x self.y = y @@ -181,3 +185,38 @@ class GeometryIndexed: image_data[self.y:self.y+height, self.x:self.x+width] = visible_data return Image.fromarray(np.flip(image_data, axis=0), 'L') + + +class LevelGeometryIndexed(GeometryIndexed): + variant_name = None + + @classmethod + def level_filename(cls, level_id, mode): + from django.conf import settings + return os.path.join(settings.CACHE_ROOT, 'level_%d_%s_%s' % (level_id, cls.variant_name, mode)) + + @classmethod + def open_level(cls, level_id, mode, **kwargs): + # noinspection PyArgumentList + return cls.open(cls.level_filename(level_id, mode), **kwargs) + + cached = {} + cache_key = None + cache_lock = threading.Lock() + + @classmethod + def open_level_cached(cls, level_id, mode): + with cls.cache_lock: + from c3nav.mapdata.models import MapUpdate + cache_key = MapUpdate.current_processed_cache_key() + if cls.cache_key != cache_key: + cls.cache_key = cache_key + cls.cached = {} + else: + result = cls.cached.get((level_id, mode), None) + if result is not None: + return result + + result = cls.open_level(level_id, mode) + cls.cached[(level_id, mode)] = result + return result diff --git a/src/c3nav/mapdata/utils/cache/maphistory.py b/src/c3nav/mapdata/utils/cache/maphistory.py index 41335a3b..b003b164 100644 --- a/src/c3nav/mapdata/utils/cache/maphistory.py +++ b/src/c3nav/mapdata/utils/cache/maphistory.py @@ -1,15 +1,12 @@ -import os import struct -import threading from itertools import chain import numpy as np -from django.conf import settings -from c3nav.mapdata.utils.cache import GeometryIndexed +from c3nav.mapdata.utils.cache.indexed import LevelGeometryIndexed -class MapHistory(GeometryIndexed): +class MapHistory(LevelGeometryIndexed): # metadata format: # 2 bytes (uint16): number of updates # n updates times: @@ -18,6 +15,7 @@ class MapHistory(GeometryIndexed): # each uint16 cell contains the index of the newest update dtype = np.uint16 variant_id = 1 + variant_name = 'history' def __init__(self, updates, **kwargs): super().__init__(**kwargs) @@ -46,35 +44,6 @@ class MapHistory(GeometryIndexed): instance.save() return instance - @staticmethod - def level_filename(level_id, mode): - return os.path.join(settings.CACHE_ROOT, 'level_%d_history_%s' % (level_id, mode)) - - @classmethod - def open_level(cls, level_id, mode, default_update=None): - return cls.open(cls.level_filename(level_id, mode), default_update) - - cached = {} - cache_key = None - cache_lock = threading.Lock() - - @classmethod - def open_level_cached(cls, level_id, mode): - with cls.cache_lock: - from c3nav.mapdata.models import MapUpdate - cache_key = MapUpdate.current_processed_cache_key() - if cls.cache_key != cache_key: - cls.cache_key = cache_key - cls.cached = {} - else: - result = cls.cached.get((level_id, mode), None) - if result is not None: - return result - - result = cls.open_level(level_id, mode) - cls.cached[(level_id, mode)] = result - return result - def add_geometry(self, geometry, update): if self.updates[-1] != update: self.updates.append(update)