Skip to content

Commit

Permalink
Improve JSONDict usage (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan authored Jun 1, 2023
1 parent b6c934c commit f610d57
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2771,7 +2771,7 @@ def getPixel(self, includeTileRecord=False, **kwargs):
pixel.update(dict(zip(img.mode.lower(), img.load()[0, 0])))
else:
pixel.update(dict(zip([img.mode.lower()], [img.load()[0, 0]])))
return pixel
return JSONDict(pixel)

@property
def frames(self):
Expand Down
4 changes: 2 additions & 2 deletions sources/gdal/large_image_source_gdal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def getBandInformation(self, statistics=True, dataset=None, **kwargs):
cache = not dataset
if not dataset:
dataset = self.dataset
infoSet = {}
infoSet = JSONDict({})
for i in range(dataset.RasterCount):
band = dataset.GetRasterBand(i + 1)
info = {}
Expand Down Expand Up @@ -609,7 +609,7 @@ def getInternalMetadata(self, **kwargs):
:returns: a dictionary of data or None.
"""
result = {}
result = JSONDict({})
with self._getDatasetLock:
result['driverShortName'] = self.dataset.GetDriver().ShortName
result['driverLongName'] = self.dataset.GetDriver().LongName
Expand Down
9 changes: 5 additions & 4 deletions sources/rasterio/large_image_source_rasterio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from large_image.tilesource.geo import (GDALBaseFileTileSource,
ProjUnitsAcrossLevel0,
ProjUnitsAcrossLevel0_MaxSize)
from large_image.tilesource.utilities import JSONDict

try:
from importlib.metadata import PackageNotFoundError
Expand Down Expand Up @@ -448,7 +449,7 @@ def getBandInformation(self, statistics=True, dataset=None, **kwargs):
dataset = dataset or self.dataset

# loop in the bands to get the indicidative stats (bands are 1 indexed)
infoSet = {}
infoSet = JSONDict({})
for i in dataset.indexes: # 1 indexed

# get the stats
Expand Down Expand Up @@ -490,7 +491,7 @@ def getMetadata(self):
has_gcps = len(self.dataset.gcps[0]) != 0 and self.dataset.gcps[1]
has_affine = self.dataset.transform

metadata = {
metadata = JSONDict({
'geospatial': bool(has_projection or has_gcps or has_affine),
'levels': self.levels,
'sizeX': self.sizeX,
Expand All @@ -503,7 +504,7 @@ def getMetadata(self):
'bounds': self.getBounds(self.projection),
'sourceBounds': self.getBounds(),
'bands': self.getBandInformation(),
}
})

# magnification is computed elswhere
metadata.update(self.getNativeMagnification())
Expand All @@ -518,7 +519,7 @@ def getInternalMetadata(self, **kwargs):
:returns: a dictionary of data or None.
"""
result = {}
result = JSONDict({})
with self._getDatasetLock:
result['driverShortName'] = self.dataset.driver
result['driverLongName'] = self.dataset.driver
Expand Down
3 changes: 3 additions & 0 deletions test/source_geo_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ def testPixel(self):
assert pixel == {
'r': 94, 'g': 98, 'b': 99, 'a': 255, 'bands': {1: 77.0, 2: 82.0, 3: 84.0}}

# Make sure pixel return value is JSON serializable
assert json.dumps(pixel)

def testSourceErrors(self):
testDir = os.path.dirname(os.path.realpath(__file__))
imagePath = os.path.join(testDir, 'test_files', 'rgb_geotiff.tiff')
Expand Down

0 comments on commit f610d57

Please sign in to comment.