Skip to content

Commit

Permalink
fix: bug in RS_SetValues
Browse files Browse the repository at this point in the history
  • Loading branch information
furqaankhan committed Nov 29, 2023
1 parent 1642af9 commit b27e2d1
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static GridCoverage2D setValues(GridCoverage2D raster, int band, Geometry

Raster rasterizedGeomData = RasterUtils.getRaster(rasterizedGeom.getRenderedImage());
double colX = RasterAccessors.getUpperLeftX(rasterizedGeom), rowY = RasterAccessors.getUpperLeftY(rasterizedGeom);
int height = RasterAccessors.getHeight(rasterizedGeom), width = RasterAccessors.getWidth(rasterizedGeom);
int heightGeometryRaster = RasterAccessors.getHeight(rasterizedGeom), widthGeometryRaster = RasterAccessors.getWidth(rasterizedGeom);
int heightOriginalRaster = RasterAccessors.getHeight(raster), widthOriginalRaster = RasterAccessors.getWidth(raster);
WritableRaster rasterCopied = makeCopiedRaster(raster);

Expand All @@ -144,27 +144,28 @@ public static GridCoverage2D setValues(GridCoverage2D raster, int band, Geometry
int[] pixelLocation = RasterUtils.getGridCoordinatesFromWorld(raster, colX, rowY);
int x = pixelLocation[0], y = pixelLocation[1];

// lower-bound if the rasterized geometry starts at more north or west from the given raster than move rasterized geometry starting pixel accordingly.
if (x < 0) {
x = Math.abs(x);
}
if (y < 0) {
y = Math.abs(y);
}
// i & j is for main raster
// k & l is for rasterized geom
// added an upperbound if the rasterized geometry is bigger than provided raster
for (int j = 0, l = y; j < heightOriginalRaster && l < height; j++, l++) {
for (int i = 0, k = x; i < widthOriginalRaster && k < width; i++, k++) {
double[] pixel = rasterCopied.getPixel(i, j, (double[]) null);
// rasterX & rasterY are the starting pixels for the target raster
int rasterX = Math.max(x, 0);
int rasterY = Math.max(y, 0);
// geometryX & geometryY are the starting pixels for the geometry raster
int geometryX = rasterX - x;
int geometryY = rasterY - y;
// widthRegion & heightRegion are the size of the region to update
int widthRegion = Math.min(widthGeometryRaster - geometryX, widthOriginalRaster - rasterX);
int heightRegion = Math.min(heightGeometryRaster - geometryY, heightOriginalRaster - rasterY);

for (int j = 0; j < heightRegion; j++) {
for (int i = 0; i < widthRegion; i++) {
double[] pixel = rasterCopied.getPixel(rasterX + i, rasterY + j, (double[]) null);
// [0] as only one band in the rasterized Geometry
double pixelNew = rasterizedGeomData.getPixel(k, l, (double[]) null)[0];
if (keepNoData && noDataValue != null && noDataValue == pixel[band - 1]) {
double pixelNew = rasterizedGeomData.getPixel(geometryX + i, geometryY + j, (double[]) null)[0];
// skipping 0 from the rasterized geometry as
if (pixelNew == 0 || keepNoData && noDataValue != null && noDataValue == pixel[band - 1]) {
continue;
} else {
pixel[band - 1] = pixelNew;
}
rasterCopied.setPixel(i, j, pixel);
rasterCopied.setPixel(rasterX + i, rasterY + j, pixel);
}
}
}
Expand Down

0 comments on commit b27e2d1

Please sign in to comment.