Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEDONA-446] Add floating point datatype support in RS_AsBase64 #1146

Merged
merged 9 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion examples/flink-sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,3 @@
</resources>
</build>
</project>

1 change: 0 additions & 1 deletion flink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,3 @@
</dependency>
</dependencies>
</project>