sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: GridCoverage2D.render(GridExtent) should verify the number of dimension of given extent, for consistency with other GridCoverage.render(GridExtent) implementations.
Date Thu, 04 Jun 2020 13:01:33 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new a9cf30c  GridCoverage2D.render(GridExtent) should verify the number of dimension
of given extent, for consistency with other GridCoverage.render(GridExtent) implementations.
a9cf30c is described below

commit a9cf30cf47cfcaef1dd4741313bc3ccbe1ce3523
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Jun 4 15:00:21 2020 +0200

    GridCoverage2D.render(GridExtent) should verify the number of dimension of given extent,
    for consistency with other GridCoverage.render(GridExtent) implementations.
---
 .../org/apache/sis/coverage/grid/GridCoverage.java |  2 +
 .../apache/sis/coverage/grid/GridCoverage2D.java   | 11 +++++
 .../apache/sis/coverage/grid/ImageRenderer.java    |  4 +-
 .../coverage/j2d/BufferedGridCoverage.java         |  2 +-
 .../sis/coverage/grid/GridCoverage2DTest.java      | 52 ++++++++++++++++------
 .../coverage/grid/ResampledGridCoverageTest.java   | 24 +++++-----
 .../org/apache/sis/internal/netcdf/Raster.java     |  4 +-
 7 files changed, 67 insertions(+), 32 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
index e4830a7..7109f72 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage.java
@@ -24,6 +24,7 @@ import java.util.Optional;
 import java.awt.image.ColorModel;
 import java.awt.image.RenderedImage;
 import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.datum.PixelInCell;
 import org.opengis.referencing.operation.MathTransform;
@@ -420,6 +421,7 @@ public abstract class GridCoverage {
      * @param  sliceExtent  a subspace of this grid coverage extent where all dimensions
except two have a size of 1 cell.
      *         May be {@code null} if this grid coverage has only two dimensions with a size
greater than 1 cell.
      * @return the grid slice as a rendered image. Image location is relative to {@code sliceExtent}.
+     * @throws MismatchedDimensionException if the given extent does not have the same number
of dimensions than this coverage.
      * @throws SubspaceNotSpecifiedException if the given argument is not sufficient for
reducing the grid to a two-dimensional slice.
      * @throws DisjointExtentException if the given extent does not intersect this grid coverage.
      * @throws CannotEvaluateException if this method can not produce the rendered image
for another reason.
diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
index 329ffb7..373eec0 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverage2D.java
@@ -36,6 +36,7 @@ import org.opengis.metadata.spatial.DimensionNameType;
 import org.opengis.util.NameFactory;
 import org.opengis.util.InternationalString;
 import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
 import org.opengis.referencing.datum.PixelInCell;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.operation.NoninvertibleTransformException;
@@ -49,6 +50,7 @@ import org.apache.sis.internal.system.DefaultFactories;
 import org.apache.sis.util.collection.TableColumn;
 import org.apache.sis.util.collection.TreeTable;
 import org.apache.sis.util.resources.Vocabulary;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.Workaround;
@@ -94,6 +96,7 @@ import org.opengis.coverage.PointOutsideCoverageException;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Johann Sorel (Geomatys)
+ * @author  Alexis Manin (Geomatys)
  * @version 1.1
  * @since   1.1
  * @module
@@ -539,6 +542,7 @@ public class GridCoverage2D extends GridCoverage {
      *
      * @param  sliceExtent  area of interest, or {@code null} for the whole image.
      * @return the grid slice as a rendered image. Image location is relative to {@code sliceExtent}.
+     * @throws MismatchedDimensionException if the given extent does not have the same number
of dimensions than this coverage.
      * @throws DisjointExtentException if the given extent does not intersect this grid coverage.
      * @throws CannotEvaluateException if this method can not produce the rendered image
for another reason.
      *
@@ -553,6 +557,13 @@ public class GridCoverage2D extends GridCoverage {
                 return data;
             }
             sliceExtent = extent;
+        } else {
+            final int expected = gridGeometry.getDimension();
+            final int dimension = sliceExtent.getDimension();
+            if (expected != dimension) {
+                throw new MismatchedDimensionException(Errors.format(
+                        Errors.Keys.MismatchedDimension_3, "sliceExtent", expected, dimension));
+            }
         }
         if (extent != null) {
             final int n = min(sliceExtent.getDimension(), extent.getDimension());
diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
index 52de407..7b9f8d3 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
@@ -69,8 +69,8 @@ import static org.apache.sis.image.PlanarImage.GRID_GEOMETRY_KEY;
  *     class MyResource extends GridCoverage {
  *         &#64;Override
  *         public RenderedImage render(GridExtent sliceExtent) {
+ *             ImageRenderer renderer = new ImageRenderer(this, sliceExtent);
  *             try {
- *                 ImageRenderer renderer = new ImageRenderer(this, sliceExtent);
  *                 renderer.setData(data);
  *                 return renderer.image();
  *             } catch (IllegalArgumentException | ArithmeticException | RasterFormatException
e) {
@@ -263,7 +263,7 @@ public class ImageRenderer {
             final int dimension = sliceExtent.getDimension();
             if (source.getDimension() != dimension) {
                 throw new MismatchedDimensionException(Errors.format(
-                        Errors.Keys.MismatchedDimension_3, "target", source.getDimension(),
dimension));
+                        Errors.Keys.MismatchedDimension_3, "sliceExtent", source.getDimension(),
dimension));
             }
         } else {
             sliceExtent = source;
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
index 505b432..7586e44 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
@@ -159,8 +159,8 @@ public class BufferedGridCoverage extends GridCoverage {
      */
     @Override
     public RenderedImage render(final GridExtent sliceExtent) {
+        final ImageRenderer renderer = new ImageRenderer(this, sliceExtent);
         try {
-            final ImageRenderer renderer = new ImageRenderer(this, sliceExtent);
             renderer.setData(data);
             return renderer.image();
         } catch (IllegalArgumentException | ArithmeticException | RasterFormatException e)
{
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
index ce621f9..84ecda3 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverage2DTest.java
@@ -25,6 +25,7 @@ import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
 import java.awt.image.WritableRenderedImage;
 import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
 import org.opengis.coverage.PointOutsideCoverageException;
 import org.opengis.referencing.operation.MathTransform1D;
 import org.opengis.referencing.datum.PixelInCell;
@@ -47,12 +48,13 @@ import static org.apache.sis.test.FeatureAssert.*;
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
+ * @author  Alexis Manin (Geomatys)
  * @version 1.1
  * @since   1.1
  * @module
  */
 public strictfp class GridCoverage2DTest extends TestCase {
-    /*
+    /**
      * Width and height of the grid tested in this class.
      */
     protected static final int GRID_SIZE = 2;
@@ -106,6 +108,22 @@ public strictfp class GridCoverage2DTest extends TestCase {
     }
 
     /**
+     * Asserts that the sample values in the given coverage are equal to the expected values.
+     *
+     * @param  coverage  the coverage containing the sample values to check.
+     * @param  expected  the expected sample values.
+     */
+    private static void assertSamplesEqual(final GridCoverage coverage, final double[][]
expected) {
+        final Raster raster = coverage.render(null).getData();
+        for (int y=0; y<expected.length; y++) {
+            for (int x=0; x<expected[y].length; x++) {
+                double value = raster.getSampleDouble(x, y, 0);
+                assertEquals(expected[y][x], value, STRICT);
+            }
+        }
+    }
+
+    /**
      * Tests reading the values provided by {@link GridCoverage2D#forConvertedValues(boolean)}.
      */
     @Test
@@ -195,7 +213,7 @@ public strictfp class GridCoverage2DTest extends TestCase {
     }
 
     /**
-     * Ensure that calling {@link GridCoverage#render(GridExtent)} with a sub-extent (crop
operation)
+     * Ensures that calling {@link GridCoverage#render(GridExtent)} with a sub-extent (crop
operation)
      * returns precisely the requested area, not a smaller or bigger one.
      */
     @Test
@@ -211,6 +229,9 @@ public strictfp class GridCoverage2DTest extends TestCase {
         final GridExtent singleRow = new GridExtent(2, 1).translate(0, 1);
         assertPixelsEqual(coverage.render(null), new Rectangle(0, 1, 2, 1),     // Expected
values.
                           coverage.render(singleRow), null);                    // Actual
values to test.
+        assertSamplesEqual(coverage, new double[][] {
+            {2, 5}                                      // Redundant with previous assert,
but let be explicit.
+        });
         /*
          * Column extraction:
          *   - Expected size (1,2) is verified by `assertPixelsEqual(…)`.
@@ -221,21 +242,26 @@ public strictfp class GridCoverage2DTest extends TestCase {
         final GridExtent singleCol = new GridExtent(1, 2).translate(1, 0);
         assertPixelsEqual(coverage.render(null), new Rectangle(1, 0, 1, 2),     // Expected
values.
                           coverage.render(singleCol), null);                    // Actual
values to test.
+        assertSamplesEqual(coverage, new double[][] {
+            { 2},                                       // Redundant with previous assert,
but let be explicit.
+            {-5}
+        });
     }
 
     /**
-     * Asserts that the sample values in the given coverage are equal to the expected values.
-     *
-     * @param  coverage  the coverage containing the sample values to check.
-     * @param  expected  the expected sample values.
+     * Verifies that calling {@link GridCoverage#render(GridExtent)} with an extent
+     * having the wrong number of exception causes an exception to be thrown.
      */
-    private static void assertSamplesEqual(final GridCoverage coverage, final double[][]
expected) {
-        final Raster raster = coverage.render(null).getData();
-        for (int y=0; y<expected.length; y++) {
-            for (int x=0; x<expected[y].length; x++) {
-                double value = raster.getSampleDouble(x, y, 0);
-                assertEquals(expected[y][x], value, STRICT);
-            }
+    @Test
+    public void testInvalidDimension() {
+        final GridCoverage coverage = createTestCoverage();
+        final GridExtent sliceExtent = new GridExtent(null, null, new long[] {GRID_SIZE,
GRID_SIZE, 0}, true);
+        try {
+            coverage.render(sliceExtent);
+            fail("Should not have accepted an extent with wrong number of dimensions.");
+        } catch (MismatchedDimensionException e) {
+            // This is the expected exception.
+            assertTrue(e.getMessage().contains("sliceExtent"));
         }
     }
 }
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
index 53a78e2..b184602 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
@@ -76,14 +76,9 @@ public final strictfp class ResampledGridCoverageTest extends TestCase
{
     private Random random;
 
     /**
-     * Arbitrary non-zero pixel coordinates for image origin.
+     * Arbitrary non-zero grid coordinate for the <var>z</var> dimensions.
      */
-    private int minX, minY;
-
-    /**
-     * Arbitrary non-zero grid coordinate for the <var>z</var> and <var>t</var>
dimensions.
-     */
-    private int gridZ, gridT;
+    private int gridZ;
 
     /**
      * Creates a small grid coverage with arbitrary data. The rendered image will
@@ -94,11 +89,10 @@ public final strictfp class ResampledGridCoverageTest extends TestCase
{
         random = TestUtilities.createRandomNumberGenerator();
         final int width  = random.nextInt(8) + 3;
         final int height = random.nextInt(8) + 3;
-        minX = random.nextInt(32) - 10;
-        minY = random.nextInt(32) - 10;
         final TiledImageMock image = new TiledImageMock(
                 DataBuffer.TYPE_USHORT, 2,      // dataType and numBands
-                minX,  minY,
+                random.nextInt(32) - 10,        // minX (no effect on tests)
+                random.nextInt(32) - 10,        // minY (no effect on tests)
                 width, height,                  // Image size
                 width, height,                  // Tile size
                 random.nextInt(32) - 10,        // minTileX
@@ -166,8 +160,8 @@ public final strictfp class ResampledGridCoverageTest extends TestCase
{
          * complete testing, but actually the tests in this class are independent of image
origin.
          * Note that grid extent origin does not need to be the same than image origin.
          */
-        minX = random.nextInt(5) - 2;
-        minY = random.nextInt(5) - 2;
+        final int minX = random.nextInt(5) - 2;
+        final int minY = random.nextInt(5) - 2;
         GridGeometry gg = createGridGeometryND(withTime ? HardCodedCRS.WGS84_4D : HardCodedCRS.WGS84_3D,
0, 1, 2, 3, false);
         final TiledImage shiftedImage = new TiledImage(
                 image.getColorModel(),
@@ -206,7 +200,7 @@ public final strictfp class ResampledGridCoverageTest extends TestCase
{
         lower[z] = upper[z] = gridZ = 7;            // Arbitrary non-zero position in the
grid.
         if (t < dim) {
             gridToCRS.setElement(t, dim, 48055);
-            lower[t] = upper[t] = gridT = 12;
+            lower[t] = upper[t] = 12;
         }
         if (flipY) {
             /*
@@ -403,7 +397,9 @@ public final strictfp class ResampledGridCoverageTest extends TestCase
{
          * Verify GridCoverage.render(GridExtent) contract: the origin of the returned image
          * shall be the lower-left corner of `sliceExtent`, which is (3,3) in this test.
          */
-        targetImage = result.render(new GridExtent(LX+3, LY+3, 2, 2));
+        targetImage = result.render(new GridExtent(null,
+                new long[] {LX+3, LY+3, gridZ},
+                new long[] {LX+4, LY+4, gridZ}, true));
         assertPixelsEqual(sourceImage, new Rectangle(3, 3, 2, 2),
                           targetImage, new Rectangle(0, 0, 2, 2));
     }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
index b999aa6..0200eb4 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Raster.java
@@ -40,7 +40,7 @@ import org.apache.sis.internal.coverage.j2d.BufferedGridCoverage;
  * but it is {@link ImageRenderer} responsibility to perform this substitution as an optimization.</p>
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   1.0
  * @module
  */
@@ -79,8 +79,8 @@ final class Raster extends BufferedGridCoverage {
      */
     @Override
     public RenderedImage render(final GridExtent target) {
+        final ImageRenderer renderer = new ImageRenderer(this, target);
         try {
-            final ImageRenderer renderer = new ImageRenderer(this, target);
             renderer.setData(data);
             if (bandOffsets != null) {
                 renderer.setInterleavedPixelOffsets(pixelStride, bandOffsets);


Mime
View raw message