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

[SEDONA-479] Update RS_Normalize #1232

Merged
merged 1 commit into from
Feb 4, 2024
Merged
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
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 @@ -2468,7 +2468,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.
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by this?

Copy link
Contributor Author

@prantogg prantogg Feb 2, 2024

Choose a reason for hiding this comment

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

Arrays with all identical values are set to 0 after normalizing


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

Expand Down
Loading