Skip to content

Commit

Permalink
[SEDONA-479] Update RS_Normalize (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
prantogg authored and jiayuasu committed May 1, 2024
1 parent eb22dc8 commit 3010082
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,23 @@ public static double[] logicalOver(double[] band1, double[] band2) {
}

/**
* @param band band values
* @param bandValues band values
* @return an array with normalized band values to be within [0 - 255] range
*/
public static double[] normalize(double[] band) {
double[] result = new double[band.length];
double normalizer = Arrays.stream(band).max().getAsDouble() / 255d;
public static double[] normalize(double[] bandValues) {
Double minValue = Arrays.stream(bandValues).min().orElse(Double.NaN);
Double maxValue = Arrays.stream(bandValues).max().orElse(Double.NaN);

for (int i = 0; i < band.length; i++) {
result[i] = (int) (band[i] / normalizer);
if (Double.compare(maxValue, minValue) == 0) {
// Set default value for constant bands to 0
Arrays.fill(bandValues, 0);
} else {
for (int i = 0; i < bandValues.length; i++) {
bandValues[i] = ((bandValues[i] - minValue) * 255) / (maxValue - minValue);
}
}

return result;
return bandValues;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,20 @@ public void testLogicalOver() {

@Test
public void testNormalize() {
double[] band = new double[] {800.0, 900.0, 0.0, 255.0};
double[] actual = MapAlgebra.normalize(band);
double[] expected = new double[] {226.0, 255.0, 0.0, 72.0};
assertArrayEquals(expected, actual, 0.1d);
double[] band1 = {800.0, 900.0, 0.0, 255.0};
double[] band2 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
double[] band3 = {16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
double[] band4 = {-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1};
double[] actual1 = MapAlgebra.normalize(band1);
double[] actual2 = MapAlgebra.normalize(band2);
double[] actual3 = MapAlgebra.normalize(band3);
double[] actual4 = MapAlgebra.normalize(band4);
double[] expected1 = {226.66666666666666, 255.0, 0.0, 72.25};
double[] expected2 = {0.0, 17.0, 34.0, 51.0, 68.0, 85.0, 102.0, 119.0, 136.0, 153.0, 170.0, 187.0, 204.0, 221.0, 238.0, 255.0};
assertArrayEquals(expected1, actual1, 0.1d);
assertArrayEquals(expected2, actual2, 0.1d);
assertArrayEquals(expected2, actual3, 0.1d);
assertArrayEquals(expected2, actual4, 0.1d);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion docs/api/sql/Raster-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,7 @@ This function only accepts integer as factor before `v1.5.0`.

### RS_Normalize

Introduction: Normalize the value in the array to [0, 255]
Introduction: Normalize the value in the array to [0, 255]. Uniform arrays are set to 0 after normalization.

Format: `RS_Normalize (Band: ARRAY[Double])`

Expand Down

0 comments on commit 3010082

Please sign in to comment.