Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JSONDict usage #1189

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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