Skip to content

[GH-3330] Keep the other bands when replacing a band's no-data value - #3331

Open
Eliaaazzz wants to merge 1 commit into
apache:masterfrom
Eliaaazzz:fix/replace-nodata-wipes-bands
Open

[GH-3330] Keep the other bands when replacing a band's no-data value#3331
Eliaaazzz wants to merge 1 commit into
apache:masterfrom
Eliaaazzz:fix/replace-nodata-wipes-bands

Conversation

@Eliaaazzz

Copy link
Copy Markdown
Contributor

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

RasterBandEditors.setBandNoDataValue(raster, bandIndex, noDataValue, replace = true) allocates a fresh WritableRaster with every band and then wrote only bandIndex - 1 into it. The remaining bands came back as the allocation default of 0, while their sample dimensions still described the original data, so the raster looked intact in its metadata and had lost its pixels.

On a three-band raster, setBandNoDataValue(raster, 1, -999.0, true) returned:

  • band 1 as [1, 2, 3, 4, -999, 6, 7, 8, 9], correct
  • band 2 as all zeros, where [11 ... 19] was expected
  • band 3 as all zeros, where [21 ... 29] was expected

Replacing on a band other than the first lost band 1 the same way. The fix copies every band across and applies the replacement only to the targeted one.

This reaches SQL as RS_SetBandNoDataValue(raster, bandIndex, noDataValue, true) on any raster with two or more bands.

How was this patch tested?

New test RasterBandEditorsTest.testSetBandNoDataValueWithReplaceOptionKeepsOtherBands, which replaces on band 1 and then on band 2 of a three-band raster and asserts the full contents of all three bands each time, plus the resulting no-data metadata. It fails on master with band 2 and band 3 read back as zeros.

Both existing tests for the replace option, testSetBandNoDataValueWithReplaceOption and testSetBandNoDataValueWithReplaceOptionRaster, use single-band rasters, which is why this went unnoticed. They still pass unchanged.

mvn -pl common test on Windows with JDK 17: the same 13 tests fail before and after the change (RasterBandEditorsTest.testClip, 5 RasterEditorsTest.testResample*, 7 RasterOutputTest.testAsMatrix*). They fail identically on unmodified master, so they are pre-existing on this platform and unrelated. Everything else passes.

I could not run spark/common locally: it fails to compile on the generated OSM PBF protobuf sources (package proto4 does not exist) before reaching any raster code. Leaving those suites to CI.

Did this PR include necessary documentation updates?

  • No, this PR does not affect any public API so no need to change the documentation. The behaviour now matches what RS_SetBandNoDataValue already documents.

🤖 Generated with Claude Code

https://claude.ai/code/session_01P6hpqAnXfeaWRLkL1VQDMd

The replacement allocates a fresh WritableRaster with every band and then
wrote only the target band into it, so the remaining bands came back as
the allocation default of 0 while their sample dimensions still described
the original data. On a three-band raster,
setBandNoDataValue(raster, 1, -999, true) returned bands 2 and 3 filled
with zeros.

Copy every band across, replacing the old no-data value only in the
targeted one. Both existing tests for the replace option use single-band
rasters, which is why this went unnoticed.
@Eliaaazzz
Eliaaazzz requested a review from jiayuasu as a code owner September 6, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RS_SetBandNoDataValue with replace=true zeroes every band except the target one

1 participant