jiayuasu commented on code in PR #1221: URL: https://github.com/apache/sedona/pull/1221#discussion_r1473614325
########## 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 Review Comment: You should fetch the `NoDataValue` from each band. There are operators functions in RasterUtils can already do it. The `NoDataValue` of the input is to be used when storing in the raster band. ########## 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); Review Comment: Please don't use magic numbers here. Can you use null? ########## 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: If the band value equals to the no data value of a band, then you should set it as `noDataValue` from the input. If the input `noDataValue` is not given, then set it to maxLim. ########## 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; Review Comment: Please check `null` instead of a magic number. -- 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: issues-unsubscr...@sedona.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org