Kontinuation commented on code in PR #1150:
URL: https://github.com/apache/sedona/pull/1150#discussion_r1433404799
##########
common/src/main/java/org/apache/sedona/common/raster/MapAlgebra.java:
##########
@@ -180,6 +174,86 @@ public static GridCoverage2D mapAlgebra(GridCoverage2D
gridCoverage2D, String pi
}
}
+ public static GridCoverage2D mapAlgebra(GridCoverage2D gridCoverage2D,
String pixelType, String script) {
+ return mapAlgebra(gridCoverage2D, pixelType, script, null);
+ }
+
+ private static ColorModel fetchColorModel(ColorModel originalColorModel,
WritableRaster resultRaster) {
+ if (originalColorModel.isCompatibleRaster(resultRaster)) {
+ return originalColorModel;
+ }else {
+ return PlanarImage.createColorModel(resultRaster.getSampleModel());
+ }
+ }
+
+ public static GridCoverage2D mapAlgebra(GridCoverage2D rast0,
GridCoverage2D rast1, String pixelType, String script, Double noDataValue) {
+ if (rast0 == null || rast1 == null || script == null) {
+ return null;
+ }
+ RasterUtils.isRasterSameShape(rast0, rast1);
+
+ RenderedImage renderedImageRast0 = rast0.getRenderedImage();
+ int rasterDataType = pixelType != null ?
RasterUtils.getDataTypeCode(pixelType) :
renderedImageRast0.getSampleModel().getDataType();
+ int width = renderedImageRast0.getWidth();
+ int height = renderedImageRast0.getHeight();
+ // ImageUtils.createConstantImage is slow, manually constructing a
buffered image proved to be faster.
+ // It also eliminates the data-copying overhead when converting raster
data types after running jiffle script.
+ WritableRaster resultRaster =
RasterFactory.createBandedRaster(DataBuffer.TYPE_DOUBLE, width, height, 1,
null);
+
+ ColorModel cmRast0 =
fetchColorModel(renderedImageRast0.getColorModel(), resultRaster);
+ RenderedImage renderedImageRast1 = rast1.getRenderedImage();
+ ColorModel cmRast1 =
fetchColorModel(renderedImageRast1.getColorModel(), resultRaster);
+
+ // todo temp, figure out if needed
+ if (!cmRast0.equals(cmRast1)) {
+ throw new IllegalArgumentException("Color Model did not match.
Provide rasters that has the same properties.");
+ }
+
+ WritableRenderedImage resultImage = new BufferedImage(cmRast0,
resultRaster, false, null);
+ try {
+ String prevScript = previousScript.get();
+ JiffleDirectRuntime prevRuntime = previousRuntime.get();
+ JiffleDirectRuntime runtime;
+ if (prevRuntime != null && script.equals(prevScript)) {
+ // Reuse the runtime to avoid recompiling the script
+ runtime = prevRuntime;
+ runtime.setSourceImage("rast0", renderedImageRast0);
+ runtime.setSourceImage("rast1", renderedImageRast1);
+ runtime.setDestinationImage("out", resultImage);
+ runtime.setDefaultBounds();
+ } else {
+ JiffleBuilder builder = new JiffleBuilder();
+ runtime = builder.script(script)
+ .source("rast0", renderedImageRast0)
+ .source("rast1", renderedImageRast1)
+ .dest("out", resultImage)
+ .getRuntime();
+ previousScript.set(script);
+ previousRuntime.set(runtime);
+ }
+
+ // TODO is it worth to deduplicate code from 236-246 in this
function
+ // TODO and code from 160-171?
+
+ runtime.evaluateAll(null);
+
+ // If pixelType does not match with the data type of the result
image (should be double since Jiffle only supports
+ // double destination image), we need to convert the resultImage
to the specified pixel type.
+ if (rasterDataType != resultImage.getSampleModel().getDataType()) {
Review Comment:
`convertedRaster.setSamples` casts double values to the data type of the
pixel type. It does not round the values.
PostGIS does [something more
complicated](https://github.com/postgis/postgis/blob/3.3.0/raster/rt_core/rt_util.c#L34-L84):
it clamps the value according to the range of data type. It does not round the
values.
--
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]