From 06040262f4ba1ab15101637f6f0a613c597b4d1b Mon Sep 17 00:00:00 2001 From: Furqaan Khan <46216254+furqaankhan@users.noreply.github.com> Date: Fri, 15 Dec 2023 22:51:42 -0500 Subject: [PATCH] [SEDONA-446] Add floating point datatype support in RS_AsBase64 (#1146) Co-authored-by: John Bampton --- .../apache/sedona/common/raster/RasterOutputs.java | 6 +++++- .../apache/sedona/common/raster/RasterOutputTest.java | 11 +++++++++++ docs/api/sql/Raster-visualizer.md | 5 ++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/apache/sedona/common/raster/RasterOutputs.java b/common/src/main/java/org/apache/sedona/common/raster/RasterOutputs.java index e381e0fdfb..698e7f92e1 100644 --- a/common/src/main/java/org/apache/sedona/common/raster/RasterOutputs.java +++ b/common/src/main/java/org/apache/sedona/common/raster/RasterOutputs.java @@ -135,7 +135,11 @@ public static byte[] asArcGrid(GridCoverage2D raster) { public static String asBase64(GridCoverage2D raster) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); RenderedImage renderedImage = raster.getRenderedImage(); - ImageIO.write(renderedImage, "png", out); + if (RasterUtils.isDataTypeIntegral(RasterUtils.getDataTypeCode(RasterBandAccessors.getBandType(raster)))) { + ImageIO.write(renderedImage, "png", out); + } else { + ImageIO.write(renderedImage, "tiff", out); + } return Base64.getEncoder().encodeToString(out.toByteArray()); } diff --git a/common/src/test/java/org/apache/sedona/common/raster/RasterOutputTest.java b/common/src/test/java/org/apache/sedona/common/raster/RasterOutputTest.java index 55161299f6..a6611de6af 100644 --- a/common/src/test/java/org/apache/sedona/common/raster/RasterOutputTest.java +++ b/common/src/test/java/org/apache/sedona/common/raster/RasterOutputTest.java @@ -23,9 +23,11 @@ import org.junit.Test; import org.opengis.referencing.FactoryException; +import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; import java.net.URLConnection; +import java.util.Arrays; import static org.junit.Assert.*; @@ -42,6 +44,15 @@ public void testAsBase64() throws IOException { assertTrue(resultRaw.startsWith("iVBORw0KGgoAAAANSUhEUgAABaAAAALQCAMAAABR+ye1AAADAFBMVEXE9/W48vOq7PGa5u6L3")); } + @Test + public void testAsBase64Float() throws IOException { + double[] bandData = {202.125, 101.221, 7.468, 27.575, 18.463, 106.103, 80.995, 213.73, 249.73, 147.455, 202.669, 223.379, 6.898, 64.108, 81.585, 51.162, 198.681, 147.957, 14.233, 14.146, 209.691, 121.825, 197.658, 235.804, 129.798}; + GridCoverage2D raster = RasterConstructors.makeNonEmptyRaster(1, "d", 5, 5, 1, 1, 1, 1, 0, 0, 4326, new double[][] {bandData}); + + String resultRaw = RasterOutputs.asBase64(raster); + assertTrue(resultRaw.startsWith("TU0AKgAAAAgADQEAAAMAAAABAAUAAAEBAAMAAAABAAUAAAECA")); + } + @Test public void testAsPNG() throws IOException, FactoryException { String dirPath = System.getProperty("user.dir") + "/target/testAsPNGFunction/"; diff --git a/docs/api/sql/Raster-visualizer.md b/docs/api/sql/Raster-visualizer.md index 3db837ffc4..0650b60c12 100644 --- a/docs/api/sql/Raster-visualizer.md +++ b/docs/api/sql/Raster-visualizer.md @@ -4,7 +4,10 @@ Sedona offers some APIs to aid in easy visualization of a raster object. Sedona offers APIs to visualize a raster in an image form. This API only works for rasters with byte data, and bands <= 4 (Grayscale - RGBA). You can check the data type of an existing raster by using [RS_BandPixelType](../Raster-operators/#rs_bandpixeltype) or create your own raster by passing 'B' while using [RS_MakeEmptyRaster](../Raster-loader/#rs_makeemptyraster). ### RS_AsBase64 -Introduction: Returns a base64 encoded string of the given raster. This function internally takes the first 4 bands as RGBA, and converts them to the PNG format, finally produces a base64 string. To visualize other bands, please use it together with `RS_Band`. You can take the resulting base64 string in [an online viewer](https://base64-viewer.onrender.com/) to check how the image looks like. +Introduction: Returns a base64 encoded string of the given raster. If the datatype is integral then this function internally takes the first 4 bands as RGBA, and converts them to the PNG format, finally produces a base64 string. When the datatype is not integral, the function converts the raster to TIFF format, and then generates a base64 string. To visualize other bands, please use it together with `RS_Band`. You can take the resulting base64 string in [an online viewer](https://base64-viewer.onrender.com/) to check how the image looks like. + +!!!Warning + This is not recommended for large files. Format: `RS_AsBase64(raster: Raster)`