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: Add error message, add documentation, add test, and remove calls to Math.min/max that became useless because of the new exception.
Date Wed, 20 Mar 2019 12:06:38 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 9c98824  Add error message, add documentation, add test, and remove calls to Math.min/max
that became useless because of the new exception.
9c98824 is described below

commit 9c98824f60a3e3ed8d721f8a5de64b4b13d3762f
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Wed Mar 20 13:04:39 2019 +0100

    Add error message, add documentation, add test, and remove calls to Math.min/max that
became useless because of the new exception.
---
 .../sis/coverage/grid/DisjointExtentException.java | 19 ++++++++++++++-
 .../apache/sis/coverage/grid/GridDerivation.java   |  3 +++
 .../org/apache/sis/coverage/grid/GridExtent.java   | 12 ++++++----
 .../org/apache/sis/internal/raster/Resources.java  | 13 +++++++----
 .../sis/internal/raster/Resources.properties       |  4 ++--
 .../sis/internal/raster/Resources_fr.properties    |  3 ++-
 .../sis/coverage/grid/GridDerivationTest.java      | 27 +++++++++++++++++++++-
 7 files changed, 67 insertions(+), 14 deletions(-)

diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/DisjointExtentException.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/DisjointExtentException.java
index a3d72ff..9860c05 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/DisjointExtentException.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/DisjointExtentException.java
@@ -16,6 +16,9 @@
  */
 package org.apache.sis.coverage.grid;
 
+import org.apache.sis.internal.raster.Resources;
+
+
 /**
  * Thrown when operations on a {@link GridGeometry} result in an area which
  * do not intersect anymore the {@link GridExtent} of the {@link GridGeometry}.
@@ -26,7 +29,6 @@ package org.apache.sis.coverage.grid;
  * @module
  */
 public class DisjointExtentException extends IllegalGridGeometryException {
-
     /**
      * Serial number for inter-operability with different versions.
      */
@@ -56,4 +58,19 @@ public class DisjointExtentException extends IllegalGridGeometryException
{
     public DisjointExtentException(final String message, final Throwable cause) {
         super(message, cause);
     }
+
+    /**
+     * Creates an exception with an error message built from the given argument.
+     * Current implementation creates the error message immediately, but we may
+     * change to deferred creation later if it is a performance issue.
+     *
+     * @param dim    identification of the dimension having an invalid value.
+     * @param min    the lower bound of valid area.
+     * @param max    the upper bound of valid area.
+     * @param lower  the lower bound specified by user, which is invalid.
+     * @param upper  the upper bound specified by user, which is invalid.
+     */
+    DisjointExtentException(final Object dim, final long min, final long max, final long
lower, final long upper) {
+        super(Resources.format(Resources.Keys.GridEnvelopeOutsideCoverage_5, new Object[]
{dim, min, max, lower, upper}));
+    }
 }
diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
index 3633e3d..1521e52 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
@@ -383,6 +383,7 @@ public class GridDerivation {
      *
      * @param  gridOfInterest  the area of interest and desired resolution as a grid geometry.
      * @return {@code this} for method call chaining.
+     * @throws DisjointExtentException if the given grid of interest does not intersect the
grid extent.
      * @throws IncompleteGridGeometryException if a mandatory property of a grid geometry
is absent.
      * @throws IllegalGridGeometryException if an error occurred while converting the envelope
coordinates to grid coordinates.
      * @throws IllegalStateException if a {@link #subgrid(Envelope, double...) subgrid(…)}
or {@link #slice(DirectPosition) slice(…)}
@@ -466,6 +467,7 @@ public class GridDerivation {
      *                         be equal to the {@code areaOfInterest} dimension, but this
is not mandatory
      *                         (zero or missing values mean no sub-sampling, extraneous values
are ignored).
      * @return {@code this} for method call chaining.
+     * @throws DisjointExtentException if the given area of interest does not intersect the
grid extent.
      * @throws IncompleteGridGeometryException if the base grid geometry has no extent or
no "grid to CRS" transform.
      * @throws IllegalGridGeometryException if an error occurred while converting the envelope
coordinates to grid coordinates.
      * @throws IllegalStateException if a {@link #subgrid(GridGeometry) subgrid(…)} or
{@link #slice(DirectPosition) slice(…)}
@@ -621,6 +623,7 @@ public class GridDerivation {
      * This method shall be invoked for clipping only, without any subsampling applied.
      *
      * @param  indices  the envelope to intersect in units of {@link #base} grid coordinates.
+     * @throws DisjointExtentException if the given envelope does not intersect the grid
extent.
      */
     private void clipExtent(final GeneralEnvelope indices) {
         final GridExtent sub = new GridExtent(indices, rounding, margin, baseExtent, modifiedDimensions);
diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
index 2ccc779..722623c 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
@@ -308,6 +308,7 @@ public class GridExtent implements Serializable {
      * @param  modifiedDimensions  if {@code enclosing} is non-null, the grid dimensions
to set from the envelope.
      *                             The length of this array shall be equal to the {@code
envelope} dimension.
      *                             This argument is ignored if {@code enclosing} is null.
+     * @throws DisjointExtentException if the given envelope does not intersect the enclosing
grid extent.
      *
      * @see #toCRS(MathTransform, MathTransform)
      * @see #slice(DirectPosition, int[])
@@ -405,10 +406,13 @@ public class GridExtent implements Serializable {
             if (enclosing != null) {
                 final int lo = (modifiedDimensions != null) ? modifiedDimensions[i] : i;
                 final int hi = lo + m;
-                if (lower > coordinates[hi]) throw new DisjointExtentException();
-                if (upper < coordinates[lo]) throw new DisjointExtentException();
-                if (lower > coordinates[lo]) coordinates[lo] = Math.min(coordinates[hi],
lower);
-                if (upper < coordinates[hi]) coordinates[hi] = Math.max(coordinates[lo],
upper);
+                final long validMin = coordinates[lo];
+                final long validMax = coordinates[hi];
+                if (lower > validMin) coordinates[lo] = lower;
+                if (upper < validMax) coordinates[hi] = upper;
+                if (lower > validMax || upper < validMin) {
+                    throw new DisjointExtentException(enclosing.getAxisIdentification(lo,
i), validMin, validMax, lower, upper);
+                }
             } else {
                 coordinates[i]   = lower;
                 coordinates[i+m] = upper;
diff --git a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.java b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.java
index 36da41b..cc995c7 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.java
@@ -84,7 +84,7 @@ public final class Resources extends IndexedResourceBundle {
         public static final short CategoryRangeOverlap_4 = 13;
 
         /**
-         * Indices ({3}) are outside grid coverage. The value at dimension {0} shall be between
+         * Indices ({3}) are outside grid coverage. The value in dimension {0} shall be between
          * {1,number} and {2,number} inclusive.
          */
         public static final short GridCoordinateOutsideCoverage_4 = 21;
@@ -95,6 +95,12 @@ public final class Resources extends IndexedResourceBundle {
         public static final short GridEnvelopeMustBeNDimensional_1 = 25;
 
         /**
+         * Envelope is outside grid coverage. Indices [{3,number} … {4,number}] in dimension
{0} do not
+         * intersect the [{1,number} … {2,number}] grid extent.
+         */
+        public static final short GridEnvelopeOutsideCoverage_5 = 22;
+
+        /**
          * Sample value range {1} for “{0}” category is illegal.
          */
         public static final short IllegalCategoryRange_2 = 15;
@@ -180,10 +186,7 @@ public final class Resources extends IndexedResourceBundle {
          */
         public static final short OutOfIteratorDomain_2 = 1;
 
-        /**
-         * Point ({0}) is outside the coverage domain.
-         */
-        public static final short PointOutsideCoverageDomain_1 = 22;
+        public static final short PointOutsideCoverageDomain_1 = 33;
 
         /**
          * Too many qualitative categories.
diff --git a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.properties
b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.properties
index b2e3fc3..6abdf17 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.properties
+++ b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources.properties
@@ -24,7 +24,8 @@ CanNotMapToGridDimensions         = Some envelope dimensions can not be
mapped t
 CanNotSetDerivedGridProperty_1    = Can not set this derived grid property after a call to
\u201c{0}\u201d method.
 CanNotSimplifyTransferFunction_1  = Can not simplify transfer function of sample dimension
\u201c{0}\u201d.
 CategoryRangeOverlap_4            = The two categories \u201c{0}\u201d and \u201c{2}\u201d
have overlapping ranges: {1} and {3} respectively.
-GridCoordinateOutsideCoverage_4   = Indices ({3}) are outside grid coverage. The value at
dimension {0} shall be between {1,number} and {2,number} inclusive.
+GridCoordinateOutsideCoverage_4   = Indices ({3}) are outside grid coverage. The value in
dimension {0} shall be between {1,number} and {2,number} inclusive.
+GridEnvelopeOutsideCoverage_5     = Envelope is outside grid coverage. Indices [{3,number}
\u2026 {4,number}] in dimension {0} do not intersect the [{1,number} \u2026 {2,number}] grid
extent.
 GridEnvelopeMustBeNDimensional_1  = The grid envelope must have at least {0} dimensions.
 IllegalCategoryRange_2            = Sample value range {1} for \u201c{0}\u201d category is
illegal.
 IllegalGridEnvelope_3             = Illegal grid envelope [{1,number} \u2026 {2,number}]
for dimension {0}.
@@ -43,7 +44,6 @@ NoNDimensionalSlice_3             = Can not infer a {0}-dimensional slice
from t
 NonLinearInDimensions_1           = non-linear in {0} dimension{0,choice,1#|2#s}:
 NotStrictlyOrderedDimensions      = The specified dimensions are not in strictly ascending
order.
 OutOfIteratorDomain_2             = The ({0,number}, {1,number}) pixel coordinate is outside
iterator domain.
-PointOutsideCoverageDomain_1      = Point ({0}) is outside the coverage domain.
 TooManyQualitatives               = Too many qualitative categories.
 UnexpectedNumberOfBands_2         = Expected {0} bands but got {1}.
 UnknownDataType_1                 = Raster data type \u2018{0}\u2019 is unknown or unsupported.
diff --git a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources_fr.properties
b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources_fr.properties
index 1fcfe9b..8d58206 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources_fr.properties
+++ b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/Resources_fr.properties
@@ -29,7 +29,8 @@ CanNotMapToGridDimensions         = Certaines dimensions de l\u2019enveloppe
ne
 CanNotSetDerivedGridProperty_1    = Ne peut pas d\u00e9finir cette propri\u00e9t\u00e9 de
la grille d\u00e9riv\u00e9e apr\u00e8s un appel \u00e0 la m\u00e9thode \u00ab\u202f{0}\u202f\u00bb.
 CanNotSimplifyTransferFunction_1  = Ne peut pas simplifier la fonction de transfert de la
dimension d\u2019\u00e9chantillonnage \u00ab\u202f{0}\u202f\u00bb.
 CategoryRangeOverlap_4            = Les deux cat\u00e9gories \u00ab\u202f{0}\u202f\u00bb
et \u00ab\u202f{2}\u202f\u00bb ont des plages de valeurs qui se chevauchent\u2008: {1} et
{3} respectivement.
-GridCoordinateOutsideCoverage_4   = Les indices ({3}) sont en dehors du domaine de la grille.
La valeur \u00e0 la dimension {0} doit \u00eatre entre {1,number} et {2,number} inclusivement.
+GridCoordinateOutsideCoverage_4   = Les indices ({3}) sont en dehors du domaine de la grille.
La valeur dans la dimension {0} doit \u00eatre entre {1,number} et {2,number} inclusivement.
+GridEnvelopeOutsideCoverage_5     = L\u2019enveloppe est en dehors du domaine de la grille.
Les indices [{3,number} \u2026 {4,number}] dans la dimension {0} n\u2019interceptent pas l\u2019\u00e9tendue
[{1,number} \u2026 {2,number}] de la grille.
 GridEnvelopeMustBeNDimensional_1  = L\u2019enveloppe de la grille doit avoir au moins {0}
dimensions.
 IllegalCategoryRange_2            = La plage de valeurs {1} pour la cat\u00e9gorie \u00ab\u202f{0}\u202f\u00bb
est ill\u00e9gale.
 IllegalGridEnvelope_3             = La plage d\u2019index [{1,number} \u2026 {2,number}]
de la dimension {0} n\u2019est pas valide.
diff --git a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
index 9edd551..87bb0a5 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
@@ -48,7 +48,8 @@ import static org.apache.sis.coverage.grid.GridGeometryTest.assertExtentEquals;
 @DependsOn(GridGeometryTest.class)
 public final strictfp class GridDerivationTest extends TestCase {
     /**
-     * Tests {@link GridDerivation#subgrid(Envelope, double...)}.
+     * Tests {@link GridDerivation#subgrid(Envelope, double...)} using only the
+     * {@link GridExtent} result provided by {@link GridDerivation#getIntersection()}.
      */
     @Test
     public void testSubExtent() {
@@ -167,6 +168,7 @@ public final strictfp class GridDerivationTest extends TestCase {
 
     /**
      * Tests {@link GridDerivation#subgrid(Envelope, double...)}.
+     * Contrarily to {@link #testSubExtent()}, this method checks the full {@link GridGeometry}.
      *
      * @throws TransformException if an error occurred during computation.
      */
@@ -340,4 +342,27 @@ public final strictfp class GridDerivationTest extends TestCase {
         GridGeometry subgrid = grid.derive().subgrid(areaOfInterest).build();
         assertEnvelopeEquals(expected, subgrid.getEnvelope());
     }
+
+    /**
+     * Verifies the exception thrown when we specify an envelope outside the grid extent.
+     */
+    @Test
+    public void testOutsideDomain() {
+        final GridGeometry grid = new GridGeometry(
+                new GridExtent(10, 20), PixelInCell.CELL_CORNER,
+                MathTransforms.linear(new Matrix3(
+                        2, 0, 0,
+                        0, 2, 0,
+                        0, 0, 1)), HardCodedCRS.WGS84);
+
+        final GeneralEnvelope areaOfInterest = new GeneralEnvelope(HardCodedCRS.WGS84);
+        areaOfInterest.setRange(0, 60, 85);
+        areaOfInterest.setRange(1, 15, 30);
+        try {
+            grid.derive().subgrid(areaOfInterest);
+            fail("Should not have accepted the given AOI.");
+        } catch (DisjointExtentException e) {
+            assertNotNull(e.getMessage());
+        }
+    }
 }


Mime
View raw message