Skip to content

Commit

Permalink
[SEDONA-446] Add floating point datatype support in RS_AsBase64 (apac…
Browse files Browse the repository at this point in the history
…he#1146)

Co-authored-by: John Bampton <jbampton@users.noreply.github.com>
  • Loading branch information
2 people authored and jiayuasu committed Dec 31, 2023
1 parent 89bc95f commit 0604026
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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/";
Expand Down
5 changes: 4 additions & 1 deletion docs/api/sql/Raster-visualizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down

0 comments on commit 0604026

Please sign in to comment.