iGN5117 commented on code in PR #1014:
URL: https://github.com/apache/sedona/pull/1014#discussion_r1323173903


##########
common/src/main/java/org/apache/sedona/common/raster/RasterOutputs.java:
##########
@@ -132,4 +132,70 @@ public static String asBase64(GridCoverage2D raster) 
throws IOException {
         ImageIO.write(renderedImage, "png", out);
         return Base64.getEncoder().encodeToString(out.toByteArray());
     }
+
+    public static String asMatrix(GridCoverage2D raster, int band, int 
postDecimalPrecision) {
+        RasterUtils.ensureBand(raster, band);
+        Raster rasterData = RasterUtils.getRaster(raster.getRenderedImage());
+        int dataTypeCode = rasterData.getDataBuffer().getDataType();
+        int width = rasterData.getWidth(), height = rasterData.getHeight();
+        if (RasterUtils.isDataTypeIntegral(dataTypeCode)) {
+            int[] bandValues = rasterData.getSamples(0, 0, width, height, band 
- 1, (int[]) null);
+            return createPaddedMatrixStringFromInt(bandValues, width, height, 
postDecimalPrecision);
+        }else {
+            double[] bandValues = rasterData.getSamples(0, 0, width, height, 
band - 1, (double[]) null);
+            return createPaddedMatrixStringFromDouble(bandValues, width, 
height, postDecimalPrecision);
+        }
+    }
+
+    public static String asMatrix(GridCoverage2D raster, int band) {
+        return asMatrix(raster, band, 6);
+    }
+
+
+    public static String asMatrix(GridCoverage2D raster) {
+        return asMatrix(raster, 1);
+    }
+
+    private static String createPaddedMatrixStringFromDouble(double[] values, 
int width, int height, int decimalPrecision) {
+        StringBuilder res = new StringBuilder();
+        int maxColWidth = 0;
+        int maxDecimalPrecision = 0;
+        for (double value : values) {
+            String[] splitByDecimal = String.valueOf(value).split("\\.");
+            int preDecimal = splitByDecimal[0].length(), postDecimal = 
Math.min(decimalPrecision, splitByDecimal[1].length());
+            maxDecimalPrecision = Math.max(maxDecimalPrecision, postDecimal);
+            int currWidth = preDecimal + postDecimal + 1; //add 1 for space 
occupied for decimal point
+            maxColWidth = Math.max(maxColWidth, currWidth);
+        }
+        for (int i = 0; i < values.length; i++) {
+            int row= i / width, col = i % width;
+            String fmt = String.format("%s%%%d.%df%s",
+                    col == 0 ? "|" : "  ",
+                    maxColWidth,
+                    maxDecimalPrecision,
+                    col < width - 1 ? "" : "|%n");
+            res.append(String.format(fmt, values[i]));
+        }
+
+        return res.toString();
+    }
+
+    private static String createPaddedMatrixStringFromInt(int[] values, int 
width, int height, int decimalPrecision) {

Review Comment:
   Good point, I refactored the code



-- 
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]

Reply via email to