jiayuasu commented on code in PR #1022:
URL: https://github.com/apache/sedona/pull/1022#discussion_r1328003096


##########
common/src/main/java/org/apache/sedona/common/raster/PixelFunctionEditors.java:
##########
@@ -64,18 +77,144 @@ public static GridCoverage2D setValues(GridCoverage2D 
raster, int band, int colX
                 iterator++;
             }
         }
-        return RasterUtils.create(wr, raster.getGridGeometry(), 
raster.getSampleDimensions());
+        return RasterUtils.create(rasterCopied, raster.getGridGeometry(), 
raster.getSampleDimensions());
     }
 
+    /**
+     * Returns a raster by replacing the values of pixels in a specified 
rectangular region. Convenience function without keepNoData parameter.
+     * @param raster Raster to be edited
+     * @param band Band of the raster to be edited
+     * @param colX UpperLeftX of the region
+     * @param rowY UpperLeftY of the region
+     * @param width Width of the said region
+     * @param height Height of the said region
+     * @param values Array of values to be inserted into the said region
+     * @return An updated Raster
+     */
     public static GridCoverage2D setValues(GridCoverage2D raster, int band, 
int colX, int rowY, int width, int height, double[] values) {
         return setValues(raster, band, colX, rowY, width, height, values, 
false);
     }
-  
+
+    /**
+     * Returns a raster by replacing the values of pixels in a specified 
geometry region. It converts the Geometry to a raster using RS_AsRaster.
+     * @param raster Raster to be edited
+     * @param band Band of the raster to be edited
+     * @param geom Geometry region to update
+     * @param value Value to updated in the said region
+     * @param keepNoData To keep no data value or not
+     * @return An updated raster
+     * @throws FactoryException
+     * @throws TransformException
+     */
+    public static GridCoverage2D setValues(GridCoverage2D raster, int band, 
Geometry geom, double value, boolean keepNoData) throws FactoryException, 
TransformException {
+        RasterUtils.ensureBand(raster, band);
+
+        String bandDataType = RasterBandAccessors.getBandType(raster, band);
+
+        GridCoverage2D rasterizedGeom;
+        Double noDataValue = null;
+
+        if (keepNoData) {
+            noDataValue = RasterBandAccessors.getBandNoDataValue(raster, band);
+            rasterizedGeom = RasterConstructors.asRaster(geom, raster, 
bandDataType, value, noDataValue);
+        } else {
+            rasterizedGeom = RasterConstructors.asRaster(geom, raster, 
bandDataType, value);
+        }
+
+        Raster rasterizedGeomData = 
RasterUtils.getRaster(rasterizedGeom.getRenderedImage());
+        double colX = RasterAccessors.getUpperLeftX(rasterizedGeom), rowY = 
RasterAccessors.getUpperLeftY(rasterizedGeom);
+        int height = RasterAccessors.getHeight(rasterizedGeom), width = 
RasterAccessors.getWidth(rasterizedGeom);
+
+        WritableRaster rasterCopied = makeCopiedRaster(raster);
+
+        String geometryType = geom.getGeometryType();
+
+        // Taking a shortcut if the provided geometry is a Point or 
MultiPoint, then taking the lat lon
+        if (geometryType.equalsIgnoreCase(Geometry.TYPENAME_POINT) || 
geometryType.equalsIgnoreCase(Geometry.TYPENAME_MULTIPOINT)) {
+            Coordinate[] coordinates = geom.getCoordinates();
+            for (Coordinate pointCoordinate: coordinates) {
+                int[] pointLocation = raster.getGridGeometry().worldToGrid(new 
DirectPosition2D(pointCoordinate.x, pointCoordinate.y)).getCoordinateValues();
+                double[] pixel = rasterCopied.getPixel(pointLocation[0], 
pointLocation[1], (double[]) null);
+                pixel[band - 1] = rasterizedGeomData.getPixel(0, 0, (double[]) 
null)[0];
+                rasterCopied.setPixel(pointLocation[0], pointLocation[1], 
pixel);
+            }
+        }
+        // Converting geometry to raster and then iterating through them
+        else {
+            // Starting pixel location on the raster
+            GridCoordinates2D pixelLocation = 
rasterizedGeom.getGridGeometry().worldToGrid(new DirectPosition2D(colX, rowY));
+            int x = pixelLocation.x, y = pixelLocation.y;
+            // i & j is for main raster
+            // k & l is for rasterized geom
+            for (int j = x, l = 0; j < height + x; j++, l++) {

Review Comment:
   I think this should be width, not height?



##########
common/src/test/java/org/apache/sedona/common/raster/FunctionEditorsTest.java:
##########
@@ -43,6 +50,39 @@ public void testSetValuesWithEmptyRaster() throws 
FactoryException {
         assertArrayEquals(actual, expected, 0.0);
     }
 
+    @Test
+    public void testSetValuesGeomVariant() throws FactoryException, 
ParseException, TransformException {
+        GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 5, 
5, 1, -1, 1, -1, 0, 0, 0);

Review Comment:
   Please test rasters should always have a different width than height.



##########
common/src/test/java/org/apache/sedona/common/raster/FunctionEditorsTest.java:
##########
@@ -43,6 +50,39 @@ public void testSetValuesWithEmptyRaster() throws 
FactoryException {
         assertArrayEquals(actual, expected, 0.0);
     }
 
+    @Test
+    public void testSetValuesGeomVariant() throws FactoryException, 
ParseException, TransformException {
+        GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 5, 
5, 1, -1, 1, -1, 0, 0, 0);
+        double[] values = new double[] 
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+        emptyRaster = MapAlgebra.addBandFromArray(emptyRaster, values, 1);
+        Geometry geom = Constructors.geomFromWKT("LINESTRING(1 -1, 1 -4)", 0);
+        GridCoverage2D raster = PixelFunctionEditors.setValues(emptyRaster, 1, 
geom, 4235, false);
+        double[] actual = MapAlgebra.bandAsArray(raster, 1);
+        double[] expected = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 4235.0, 
0.0, 0.0, 0.0, 0.0, 4235.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.0, 0.0, 0.0, 0.0};
+        assertArrayEquals(expected, actual, 0.1d);
+
+        // Point
+        geom = Constructors.geomFromWKT("POINT(2 -2)", 0);
+        raster = PixelFunctionEditors.setValues(emptyRaster, 1, geom, 35);
+        actual = MapAlgebra.bandAsArray(raster, 1);
+        expected = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 35.0, 0.0, 0.0, 
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+        assertArrayEquals(expected, actual, 0.1d);
+
+        // MultiPoint
+        geom = Constructors.geomFromWKT("MULTIPOINT((2 -2), (2 -1), (3 -3))", 
0);
+        raster = PixelFunctionEditors.setValues(emptyRaster, 1, geom, 400, 
false);
+        actual = MapAlgebra.bandAsArray(raster, 1);
+        expected = new double[] {0.0, 400.0, 0.0, 0.0, 0.0, 0.0, 400.0, 0.0, 
0.0, 0.0, 0.0, 0.0, 400.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.0, 0.0};
+        assertArrayEquals(expected, actual, 0.1d);
+
+        // Polygon
+        geom = Constructors.geomFromWKT("POLYGON((1 -1, 3 -3, 6 -6, 4 -1, 1 
-1))", 0);

Review Comment:
   Please also test a polygon that is larger than the region which the raster 
covers.



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