Skip to content

Commit d7b3c1d

Browse files
authored
[SEDONA-751] RS_Interpolate band index and noDataValue handling (#2958)
1 parent b8eeb95 commit d7b3c1d

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

common/src/main/java/org/apache/sedona/common/raster/RasterEditors.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,9 @@ public static GridCoverage2D interpolate(
840840
maxRadiusOrMinPoints);
841841
interpolatedValue =
842842
(Double.isNaN(interpolatedValue)) ? noDataValue : interpolatedValue;
843-
if (interpolatedValue != noDataValue) {
843+
// Only decrement if interpolation actually succeeded (returned a valid value)
844+
if (!Double.isNaN(interpolatedValue)
845+
&& (Double.isNaN(noDataValue) || interpolatedValue != noDataValue)) {
844846
countNoDataValues--;
845847
}
846848
raster.setSample(x, y, bandIndex, interpolatedValue);
@@ -861,9 +863,9 @@ public static GridCoverage2D interpolate(
861863
0,
862864
raster.getWidth(),
863865
raster.getHeight(),
864-
band,
866+
bandIndex,
865867
rasterData.getSamples(
866-
0, 0, raster.getWidth(), raster.getHeight(), band, (double[]) null));
868+
0, 0, raster.getWidth(), raster.getHeight(), bandIndex, (double[]) null));
867869
}
868870
}
869871

common/src/test/java/org/apache/sedona/common/raster/RasterEditorsTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.sedona.common.raster;
2020

2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
2223
import static org.junit.Assert.assertThrows;
2324

2425
import java.awt.image.DataBuffer;
@@ -3577,6 +3578,39 @@ public void testInterpolate() throws FactoryException {
35773578
assertEquals(0.0, RasterUtils.getNoDataValue(modifiedRaster6.getSampleDimension(1)), 0.01d);
35783579
}
35793580

3581+
/**
3582+
* Test that when interpolating a single band in a multi-band raster, the other bands are
3583+
* preserved unchanged.
3584+
*/
3585+
@Test
3586+
public void testInterpolatePreservesOtherBands() throws FactoryException {
3587+
double[] band1Values =
3588+
new double[] {1, Double.NaN, 3, Double.NaN, 5, Double.NaN, 7, Double.NaN, 9};
3589+
double[] band2Values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
3590+
3591+
GridCoverage2D raster = RasterConstructors.makeEmptyRaster(2, 3, 3, 0, 0, 1);
3592+
// Use -9999 as noDataValue so 0 is preserved as a valid value
3593+
raster = MapAlgebra.addBandFromArray(raster, band1Values, 1, -9999.0);
3594+
raster = MapAlgebra.addBandFromArray(raster, band2Values, 2, -9999.0);
3595+
3596+
// Interpolate only band 1 (specify band=1), leaving band 2 unchanged
3597+
GridCoverage2D result = RasterEditors.interpolate(raster, 2.0, "Fixed", 3.0, 1.0, 1);
3598+
3599+
// Verify band 1 was interpolated (NaN values should be filled)
3600+
double[] resultBand1 = MapAlgebra.bandAsArray(result, 1);
3601+
for (int i = 0; i < resultBand1.length; i++) {
3602+
// Band 1 should not have any NaN values after interpolation
3603+
assertFalse("Band 1 should not have NaN at index " + i, Double.isNaN(resultBand1[i]));
3604+
}
3605+
3606+
// Verify band 2 is EXACTLY the same as original
3607+
double[] resultBand2 = MapAlgebra.bandAsArray(result, 2);
3608+
assertEquals(
3609+
"Band 2 should be preserved unchanged",
3610+
Arrays.toString(band2Values),
3611+
Arrays.toString(resultBand2));
3612+
}
3613+
35803614
@Test
35813615
public void testResample() throws FactoryException, TransformException {
35823616
double[] values = {1, 2, 3, 5, 4, 5, 6, 9, 7, 8, 9, 10};

0 commit comments

Comments
 (0)