load a tile from cache or tile server

def load_tile(self, tile):
        '''load a tile from cache or tile server'''

        # see if its in the tile cache
        key = tile.key()
        if key in self._tile_cache:
            img = self._tile_cache[key]
            if np.array_equal(img, self._unavailable):
                img = self.load_tile_lowres(tile)
                if img is None:
                    img = self._unavailable
            return img


        path = self.tile_to_path(tile)
        ret = cv2.imread(path)
        if ret is not None:
            # if it is an old tile, then try to refresh
            if os.path.getmtime(path) + self.refresh_age < time.time():
                try:
                        self._download_pending[key].refresh_time()
                except Exception:
                        self._download_pending[key] = tile
                self.start_download_thread()
                    
            # add it to the tile cache
            self._tile_cache[key] = ret
            while len(self._tile_cache) > self.cache_size:
                self._tile_cache.popitem(0)
            return ret

        if not self.download:
            img = self.load_tile_lowres(tile)
            if img is None:
                img = self._unavailable
            return img

        try:
            self._download_pending[key].refresh_time()
        except Exception:
            self._download_pending[key] = tile
        self.start_download_thread()

        img = self.load_tile_lowres(tile)
        if img is None:
            img = self._loading
        return img