rbavery commented on code in PR #1221:
URL: https://github.com/apache/sedona/pull/1221#discussion_r1482151468
##########
common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java:
##########
@@ -437,6 +437,90 @@ public static double[] normalize(double[] band) {
return result;
}
+ public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom) {
+ return normalizeAll(rasterGeom, 0d, 255d, -9999, -99999, -99999);
+ }
+
+ public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom,
double minLim, double maxLim) {
+ return normalizeAll(rasterGeom, minLim, maxLim, -9999, -99999, -99999);
+ }
+
+ public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom,
double minLim, double maxLim, double noDataValue) {
+ return normalizeAll(rasterGeom, minLim, maxLim, noDataValue, -99999,
-99999);
+ }
+
+ /**
+ *
+ * @param rasterGeom Raster to be normalized
+ * @param minLim Lower limit of normalization range
+ * @param maxLim Upper limit of normalization range
+ * @param noDataValue NoDataValue used in raster
+ * @param minValue Minimum value in raster
+ * @param maxValue Maximum value in raster
+ * @return a raster with all values in all bands normalized between minLim
and maxLim
+ */
+ public static GridCoverage2D normalizeAll(GridCoverage2D rasterGeom,
double minLim, double maxLim, double noDataValue, double minValue, double
maxValue) {
+ if (minLim > maxLim) {
+ throw new IllegalArgumentException("minLim cannot be greater than
maxLim");
+ }
+
+ // Unset minmaxFlag if minValue and maxValue are not given;
+ boolean minmaxFlag = minValue != -99999;
+
+ int numBands = rasterGeom.getNumSampleDimensions();
+ RenderedImage renderedImage = rasterGeom.getRenderedImage();
+ int rasterDataType = renderedImage.getSampleModel().getDataType();
+
+ 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, excluding NoDataValue
+ if (!minmaxFlag) {
+ minValue = Arrays.stream(bandValues).filter(val -> val !=
noDataValue).min().orElse(Double.NaN);
+ maxValue = Arrays.stream(bandValues).filter(val -> val !=
noDataValue).max().orElse(Double.NaN);
+ }
+
+ if (minValue == maxValue) {
+ // Set default value for constant bands to minLim
+ Arrays.fill(bandValues, minLim);
+ } else {
+ // Normalize the band values, setting NoDataValue to maxLim
+ for (int i = 0; i < bandValues.length; i++) {
+ if (bandValues[i] == noDataValue) {
+ bandValues[i] = maxLim;
Review Comment:
@jiayuasu setting noDataValue to the maxLim by default will corrupt real
data values.
instead I think the default should be to scale real values from 0, 254 and
set the noDataValue to 255 if it is not given.
Or, scale real values between 1-255, set noDataValue to 0 by default. this
makes more sense to me, 0 is used as a noDataValue more often than 255.
--
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]