jiayuasu commented on code in PR #1221:
URL: https://github.com/apache/sedona/pull/1221#discussion_r1472494142
##########
common/src/test/java/org/apache/sedona/common/raster/MapAlgebraTest.java:
##########
@@ -321,6 +323,40 @@ public void testNormalize() {
assertArrayEquals(expected, actual, 0.1d);
}
+ @Test
+ public void testNormalizeAll() throws FactoryException {
+ GridCoverage2D raster = RasterConstructors.makeEmptyRaster(3, 4, 4, 0,
0, 1);
+
+ for (int band = 1; band <= 3; band++) {
+ double[] bandValues = new double[4 * 4];
+ for (int i = 0; i < bandValues.length; i++) {
+ bandValues[i] = (i) * band;
+ }
+ raster = MapAlgebra.addBandFromArray(raster, bandValues, band);
+ }
+
+ GridCoverage2D normalizedRaster1 = MapAlgebra.normalizeAll(raster);
+ GridCoverage2D normalizedRaster2 = MapAlgebra.normalizeAll(raster,
256d, 511d);
+
+ for (int band = 1; band <= 3; band++) {
+ double[] normalizedBand1 =
MapAlgebra.bandAsArray(normalizedRaster1, band);
+ double[] normalizedBand2 =
MapAlgebra.bandAsArray(normalizedRaster2, band);
+ double normalizedMin1 =
Arrays.stream(normalizedBand1).min().getAsDouble();
+ double normalizedMax1 =
Arrays.stream(normalizedBand1).max().getAsDouble();
+ double normalizedMin2 =
Arrays.stream(normalizedBand2).min().getAsDouble();
+ double normalizedMax2 =
Arrays.stream(normalizedBand2).max().getAsDouble();
+ double[] expected1 = {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};
Review Comment:
1. Please add tests for bands that have all same values.
2. Please add tests for rasters that have different pixel types.
`NormalizeAll` is not supposed to change the pixel types. Moreover, the data
truncation should happen as expected. If the input raster is integer, the
normalized output raster should still have integer values, which means some
decimal places derived from normalization will get truncated. One example is
from MapAlgebra:
https://sedona.apache.org/1.5.1/api/sql/Raster-map-algebra/#map-algebra
##########
common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java:
##########
@@ -437,6 +437,49 @@ public static double[] normalize(double[] band) {
return result;
}
+ /**
+ *
+ * @param rasterGeom Raster to be normalized
+ * @return a raster with all values in all bands normalized between [0 -
255]
+ */
+ public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom) {
+ return normalizeAll(rasterGeom, 0d, 255d);
+ }
+
+ /**
+ *
+ * @param rasterGeom Raster to be normalized
+ * @param minLim Lower limit of normalization range
+ * @param maxLim Upper limit of normalization range
+ * @return a raster with all values in all bands normalized between minLim
and maxLim
+ */
+ public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom,
double minLim, double maxLim) {
+ if (minLim > maxLim) {
+ throw new IllegalArgumentException("minLim cannot be greater than
maxLim");
+ }
+
+ int numBands = rasterGeom.getNumSampleDimensions();
+
+ for (int bandIndex = 1; bandIndex <= numBands; bandIndex++) {
+ // Get the band values as an array
+ double[] bandValues = bandAsArray(rasterGeom, bandIndex);
+
+ // Find min and max values in the band
+ double minValue =
Arrays.stream(bandValues).min().orElse(Double.NaN);
+ double maxValue =
Arrays.stream(bandValues).max().orElse(Double.NaN);
+
+ // Normalize the band values
+ for (int i = 0; i < bandValues.length; i++) {
+ bandValues[i] = minLim + ((bandValues[i] - minValue) * (maxLim
- minLim)) / (maxValue - minValue);
Review Comment:
What will happen if all band values are the same. Then the denominator
becomes zero and hence will lead to unexpected behavior.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]