Skip to content

Commit

Permalink
Merge pull request #894 from girder/copy-styled-tilesource-class
Browse files Browse the repository at this point in the history
Copy tilesource instances to add styles
  • Loading branch information
manthey authored Aug 5, 2022
2 parents 6ced52f + 7c60918 commit 28766fd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
12 changes: 12 additions & 0 deletions large_image/cache_util/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import functools
import threading

Expand Down Expand Up @@ -193,6 +194,17 @@ def __call__(cls, *args, **kwargs): # noqa - N805
return result
except KeyError:
pass
# This conditionally copies a non-styled class and add a style.
if kwargs.get('style') and hasattr(cls, '_setStyle'):
subkwargs = kwargs.copy()
subkwargs.pop('style')
subresult = cls(*args, **subkwargs)
result = copy.copy(subresult)
result._setStyle(kwargs['style'])
result._classkey = key
with cacheLock:
cache[key] = result
return result
try:
instance = super().__call__(*args, **kwargs)
except Exception as exc:
Expand Down
10 changes: 9 additions & 1 deletion large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def __init__(self, encoding='JPEG', jpegQuality=95, jpegSubsampling=0,
self.sizeX = None
self.sizeY = None
self._styleLock = threading.RLock()
self._bandRanges = {}

if encoding not in TileOutputMimeTypes:
raise ValueError('Invalid encoding "%s"' % encoding)
Expand All @@ -139,6 +138,15 @@ def __init__(self, encoding='JPEG', jpegQuality=95, jpegSubsampling=0,
self.jpegSubsampling = int(jpegSubsampling)
self.tiffCompression = tiffCompression
self.edge = edge
self._setStyle(style)

def _setStyle(self, style):
"""
Check and set the specified style from a json string or a dictionary.
:param style: The new style.
"""
self._bandRanges = {}
self._jsonstyle = style
if style:
if isinstance(style, dict):
Expand Down
10 changes: 10 additions & 0 deletions sources/gdal/large_image_source_gdal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ def __init__(self, path, projection=None, unitsPerPixel=None, **kwargs):
self._getTileLock = threading.Lock()
self._setDefaultStyle()

def _setStyle(self, style):
"""
Check and set the specified style from a json string or a dictionary.
:param style: The new style.
"""
super()._setStyle(style)
if hasattr(self, '_getTileLock'):
self._setDefaultStyle()

def _getLargeImagePath(self):
"""Get GDAL-compatible image path.
Expand Down
4 changes: 3 additions & 1 deletion sources/mapnik/large_image_source_mapnik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def _checkNetCDF(self):

def _setDefaultStyle(self):
"""Don't inherit from GDAL tilesource."""
pass
with self._getTileLock:
if hasattr(self, '_mapnikMap'):
del self._mapnikMap

@staticmethod
def interpolateMinMax(start, stop, count):
Expand Down
44 changes: 44 additions & 0 deletions test/test_cache_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

import large_image
from large_image.cache_util import cachesClear

from .datastore import datastore


@pytest.mark.singular
def testCacheSourceStyle():
cachesClear()
imagePath = datastore.fetch('sample_image.ptif')
ts1 = large_image.open(imagePath)
ts2 = large_image.open(imagePath, style={'max': 128})
ts3 = large_image.open(imagePath, style={'max': 160})
tile1 = ts1.getTile(0, 0, 4)
assert ts1.getTile(0, 0, 4) is not None
assert ts2.getTile(0, 0, 4) is not None
assert ts3.getTile(0, 0, 4) is not None
cachesClear()
assert ts1.getTile(0, 0, 4) == tile1
del ts1
assert ts2.getTile(1, 0, 4) is not None
cachesClear()
assert ts2.getTile(2, 0, 4) is not None
ts1 = large_image.open(imagePath)
assert ts1.getTile(0, 0, 4) == tile1


@pytest.mark.singular
def testCacheSourceStyleFirst():
cachesClear()
imagePath = datastore.fetch('sample_image.ptif')
ts2 = large_image.open(imagePath, style={'max': 128})
ts1 = large_image.open(imagePath)
tile1 = ts1.getTile(0, 0, 4)
assert ts1.getTile(0, 0, 4) is not None
assert ts2.getTile(0, 0, 4) is not None
del ts1
assert ts2.getTile(1, 0, 4) is not None
cachesClear()
assert ts2.getTile(2, 0, 4) is not None
ts1 = large_image.open(imagePath)
assert ts1.getTile(0, 0, 4) == tile1

0 comments on commit 28766fd

Please sign in to comment.