Skip to content

Commit

Permalink
feat & fix: add support for polygon's that are bigger than raster
Browse files Browse the repository at this point in the history
  • Loading branch information
furqaankhan committed Sep 18, 2023
1 parent 0bee1f4 commit 818cca8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,19 @@ public static GridCoverage2D setValues(GridCoverage2D raster, int band, Geometry
// 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));
GridCoordinates2D pixelLocation = raster.getGridGeometry().worldToGrid(new DirectPosition2D(colX, rowY));
int x = pixelLocation.x, y = pixelLocation.y;
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 = y, l = 0; j < height + y && l < heightOriginalRaster; j++, l++) {
for (int i = x, k = 0; i < width + x && k < widthOriginalRaster; i++, k++) {
for (int j = 0, l = y; j < height + y && l < heightOriginalRaster; j++, l++) {
for (int i = 0, k = x; i < width + x && k < widthOriginalRaster; i++, k++) {
double[] pixel = rasterCopied.getPixel(i, j, (double[]) null);
// [0] as only one band in the rasterized Geometry
double pixelNew = rasterizedGeomData.getPixel(k, l, (double[]) null)[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testSetValuesGeomVariant() throws FactoryException, ParseException,
geom = Constructors.geomFromWKT("POLYGON((-1 1, 3 4, 4 -4, 5 -5, 9 -9, -1 1))", 0);
raster = PixelFunctionEditors.setValues(emptyRaster, 1, geom, 56);
actual = MapAlgebra.bandAsArray(raster, 1);
expected = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 56.0, 0.0, 56.0, 56.0, 56.0, 56.0, 56.0, 56.0, 56.0, 0.0, 56.0, 56.0, 56.0, 0.0, 0.0, 56.0, 56.0};
expected = new double[] {56.0, 56.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, 0.0, 0.0, 0.0, 0.0};
assertArrayEquals(expected, actual, 0.1d);
}

Expand Down

0 comments on commit 818cca8

Please sign in to comment.