From 818cca83c1613aa817bcc8995151d76e8dc9c5a2 Mon Sep 17 00:00:00 2001 From: Furqaanahmed Khan Date: Mon, 18 Sep 2023 18:16:26 -0400 Subject: [PATCH] feat & fix: add support for polygon's that are bigger than raster --- .../sedona/common/raster/PixelFunctionEditors.java | 12 +++++++++--- .../sedona/common/raster/FunctionEditorsTest.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/org/apache/sedona/common/raster/PixelFunctionEditors.java b/common/src/main/java/org/apache/sedona/common/raster/PixelFunctionEditors.java index ff0b9385bd..61289b1b4b 100644 --- a/common/src/main/java/org/apache/sedona/common/raster/PixelFunctionEditors.java +++ b/common/src/main/java/org/apache/sedona/common/raster/PixelFunctionEditors.java @@ -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]; diff --git a/common/src/test/java/org/apache/sedona/common/raster/FunctionEditorsTest.java b/common/src/test/java/org/apache/sedona/common/raster/FunctionEditorsTest.java index 7eb615168b..d427ce8f26 100644 --- a/common/src/test/java/org/apache/sedona/common/raster/FunctionEditorsTest.java +++ b/common/src/test/java/org/apache/sedona/common/raster/FunctionEditorsTest.java @@ -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); }