Skip to content

Commit

Permalink
Copy tilesource instances to add styles
Browse files Browse the repository at this point in the history
Instead of reopening a tilesource when adding a style, copy the unstyled
class and add the style information.
  • Loading branch information
manthey committed Jul 25, 2022
1 parent 6638ddb commit 589b66e
Show file tree
Hide file tree
Showing 4 changed files with 34 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 @@ -127,7 +127,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 @@ -137,6 +136,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 @@ -177,7 +177,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

0 comments on commit 589b66e

Please sign in to comment.