Skip to content

Commit 4055dfe

Browse files
patch: apply scale/offset to statistis (#812)
1 parent c9aaaf0 commit 4055dfe

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

rio_tiler/reader.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,19 @@ def read(
297297
numpy.multiply(data, scales, out=data, casting="unsafe")
298298
numpy.add(data, offsets, out=data, casting="unsafe")
299299

300+
# apply scale/offsets to stats
301+
if dataset_statistics:
302+
scales = numpy.array(dataset.scales)[numpy.array(indexes) - 1].reshape(
303+
(-1, 1)
304+
)
305+
offsets = numpy.array(dataset.offsets)[numpy.array(indexes) - 1].reshape(
306+
(-1, 1)
307+
)
308+
stats_array = numpy.array(dataset_statistics)
309+
numpy.multiply(stats_array, scales, out=stats_array, casting="unsafe")
310+
numpy.add(stats_array, offsets, out=stats_array, casting="unsafe")
311+
dataset_statistics = [tuple(s) for s in stats_array.tolist()]
312+
300313
if post_process:
301314
data = post_process(data)
302315

tests/fixtures/cog_scale_stats.tif

272 KB
Binary file not shown.

tests/test_io_rasterio.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
COG_TAGS = os.path.join(PREFIX, "cog_tags.tif")
3939
COG_NODATA = os.path.join(PREFIX, "cog_nodata.tif")
4040
COG_SCALE = os.path.join(PREFIX, "cog_scale.tif")
41+
COG_SCALE_STATS = os.path.join(PREFIX, "cog_scale_stats.tif")
4142
COG_GCPS = os.path.join(PREFIX, "cog_gcps.tif")
4243
COG_DLINE = os.path.join(PREFIX, "cog_dateline.tif")
4344
COG_EARTH = os.path.join(PREFIX, "cog_fullearth.tif")
@@ -1163,3 +1164,21 @@ def test_titiler_issue_1163_warpedVrt():
11631164
(75.0, 9.0, 77.0, 10.0), bounds_crs="epsg:4326", width=500, height=500
11641165
)
11651166
assert img.statistics()["b1"].valid_percent
1167+
1168+
1169+
def test_unscale_stats():
1170+
"""check if scale/offset were applied on stats."""
1171+
with Reader(COG_SCALE_STATS) as src:
1172+
img = src.read(unscale=True)
1173+
stats = img.statistics()
1174+
minb1, maxb1 = stats["b1"].min, stats["b1"].max
1175+
1176+
assert pytest.approx(img.dataset_statistics[0][0]) == pytest.approx(minb1)
1177+
assert pytest.approx(img.dataset_statistics[0][1]) == pytest.approx(maxb1)
1178+
1179+
img = src.read()
1180+
stats = img.statistics()
1181+
minb1, maxb1 = stats["b1"].min, stats["b1"].max
1182+
1183+
assert pytest.approx(img.dataset_statistics[0][0]) == pytest.approx(minb1)
1184+
assert pytest.approx(img.dataset_statistics[0][1]) == pytest.approx(maxb1)

0 commit comments

Comments
 (0)