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

Update unscaling to use the per-band scale/offset values #707

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 7 additions & 2 deletions rio_tiler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,13 @@ def read(

if unscale:
data = data.astype("float32", casting="unsafe")
numpy.multiply(data, dataset.scales[0], out=data, casting="unsafe")
numpy.add(data, dataset.offsets[0], out=data, casting="unsafe")

# reshaped to match data
scales = numpy.array(dataset.scales).reshape((-1,) + (1,) * (len(data.shape[1:])))
offsets = numpy.array(dataset.offsets).reshape((-1,) + (1,) * (len(data.shape[1:])))
Comment on lines +266 to +267
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scales = numpy.array(dataset.scales).reshape((-1,) + (1,) * (len(data.shape[1:])))
offsets = numpy.array(dataset.offsets).reshape((-1,) + (1,) * (len(data.shape[1:])))
scales = numpy.array(dataset.scales).reshape(-1, 1, 1)
offsets = numpy.array(dataset.offsets).reshape(-1, 1, 1)

should be enough, I don't see rasterio handling non 2D data


numpy.multiply(data, scales, out=data, casting="unsafe")
numpy.add(data, offsets, out=data, casting="unsafe")

if post_process:
data = post_process(data)
Expand Down
Binary file modified tests/fixtures/cog_scale.tif
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/test_io_rasterio.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ def test_Reader_Options():

with Reader(COG_SCALE, options={"unscale": True}) as src:
p = src.point(310000, 4100000, coord_crs=src.dataset.crs)
assert round(float(p.data[0]), 3) == 1000.892
numpy.testing.assert_allclose(p.data, [1000.892, 1001], atol=1e-03)

# passing unscale in method should overwrite the defaults
p = src.point(310000, 4100000, coord_crs=src.dataset.crs, unscale=False)
assert p.data[0] == 8917
numpy.testing.assert_equal(p.data, [8917, 1001])

cutline = "POLYGON ((13 1685, 1010 6, 2650 967, 1630 2655, 13 1685))"
with Reader(COGEO, options={"vrt_options": {"cutline": cutline}}) as src:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ def test_point():
assert pt.band_names == ["b1"]

pt = reader.point(src_dst, [310000, 4100000], coord_crs=src_dst.crs)
assert pt.data == numpy.array([8917])
assert pt.band_names == ["b1"]
numpy.testing.assert_equal(pt.data, [8917, 1001])
assert pt.band_names == ["b1", "b2"]
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

with pytest.raises(PointOutsideBounds):
reader.point(src_dst, [810000, 4100000], coord_crs=src_dst.crs)
Expand Down
Loading