This is an automated email from the ASF dual-hosted git repository.
jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git
The following commit(s) were added to refs/heads/master by this push:
new 4b4ab6fb1 [SEDONA-479] Update RS_Normalize (#1232)
4b4ab6fb1 is described below
commit 4b4ab6fb1ac3088084fc15e0fd4307be81538092
Author: Pranav Toggi <[email protected]>
AuthorDate: Sun Feb 4 14:32:44 2024 -0500
[SEDONA-479] Update RS_Normalize (#1232)
---
.../org/apache/sedona/common/raster/MapAlgebra.java | 19 ++++++++++++-------
.../apache/sedona/common/raster/MapAlgebraTest.java | 18 ++++++++++++++----
docs/api/sql/Raster-operators.md | 2 +-
3 files changed, 27 insertions(+), 12 deletions(-)
diff --git
a/common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java
b/common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java
index b3af4a682..bc73c82d1 100644
--- a/common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java
+++ b/common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java
@@ -423,18 +423,23 @@ public class MapAlgebra
}
/**
- * @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;
}
public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom) {
diff --git
a/common/src/test/java/org/apache/sedona/common/raster/MapAlgebraTest.java
b/common/src/test/java/org/apache/sedona/common/raster/MapAlgebraTest.java
index 09db57880..5fc21549e 100644
--- a/common/src/test/java/org/apache/sedona/common/raster/MapAlgebraTest.java
+++ b/common/src/test/java/org/apache/sedona/common/raster/MapAlgebraTest.java
@@ -317,10 +317,20 @@ public class MapAlgebraTest extends RasterTestBase
@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
diff --git a/docs/api/sql/Raster-operators.md b/docs/api/sql/Raster-operators.md
index 62956ba6b..5d3837cb4 100644
--- a/docs/api/sql/Raster-operators.md
+++ b/docs/api/sql/Raster-operators.md
@@ -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.
Format: `RS_Normalize (Band: ARRAY[Double])`