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 865ea92  Bug fix: `PointOutsideCoverageException` was not correctly 
made silent when point is outside coverage.
865ea92 is described below

commit 865ea929de47ac0a446f8c5e9cc947bd7a22e991
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Thu Feb 3 20:31:00 2022 +0100

    Bug fix: `PointOutsideCoverageException` was not correctly made silent when 
point is outside coverage.
---
 .../sis/coverage/grid/FractionalGridCoordinates.java   | 18 +++++++++++++++++-
 .../org/apache/sis/coverage/grid/GridEvaluator.java    | 16 +++++++++-------
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/FractionalGridCoordinates.java
 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/FractionalGridCoordinates.java
index 0d732b9..accd273 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/FractionalGridCoordinates.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/FractionalGridCoordinates.java
@@ -48,7 +48,7 @@ import org.apache.sis.util.resources.Errors;
  * {@link ArithmeticException} is thrown.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.2
  *
  * @see GridEvaluator#toGridCoordinates(DirectPosition)
  *
@@ -231,6 +231,19 @@ public class FractionalGridCoordinates implements 
GridCoordinates, Serializable
      * @return a grid extent of the given size (if possible) containing those 
grid coordinates.
      */
     public GridExtent toExtent(final GridExtent bounds, final long... size) {
+        return toExtent(bounds, size, false);
+    }
+
+    /**
+     * Implementation of {@link #toExtent(GridExtent, long...)} with the 
option to replace
+     * {@link PointOutsideCoverageException} by null return value.
+     *
+     * @param  bounds         if the coordinates shall be contained inside a 
grid, that grid extent. Otherwise {@code null}.
+     * @param  size           the desired extent sizes as strictly positive 
numbers, or 0 for automatic sizes (1 or 2).
+     * @param  nullIfOutside  whether to return {@code null} instead of 
throwing an exception if given point is outside coverage bounds.
+     * @return a grid extent containing grid coordinates, or {@code null} if 
outside and {@code nullIfOutside} is {@code true}.
+     */
+    final GridExtent toExtent(final GridExtent bounds, final long[] size, 
final boolean nullIfOutside) {
         final int dimension = coordinates.length;
         if (bounds != null) {
             final int bd = bounds.getDimension();
@@ -291,6 +304,9 @@ public class FractionalGridCoordinates implements 
GridCoordinates, Serializable
                 final long validMin = bounds.getLow(i);
                 final long validMax = bounds.getHigh(i);
                 if (nearest > validMax || nearest < validMin) {
+                    if (nullIfOutside) {
+                        return null;
+                    }
                     final StringBuilder b = new StringBuilder();
                     writeCoordinates(b);
                     throw new PointOutsideCoverageException(
diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridEvaluator.java
 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridEvaluator.java
index 42f149e..b8ddb4b 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridEvaluator.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridEvaluator.java
@@ -46,7 +46,7 @@ import org.apache.sis.util.ArgumentChecks;
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.2
  *
  * @see GridCoverage#evaluator()
  *
@@ -181,14 +181,15 @@ public class GridEvaluator implements 
GridCoverage.Evaluator {
         try {
             final FractionalGridCoordinates gc = toGridPosition(point);
             try {
-                final GridExtent subExtent = gc.toExtent(gridGeometry.extent, 
size);
-                return evaluate(coverage.render(subExtent), 0, 0);
+                final GridExtent subExtent = gc.toExtent(gridGeometry.extent, 
size, nullIfOutside);
+                if (subExtent != null) {
+                    return evaluate(coverage.render(subExtent), 0, 0);
+                }
             } catch (ArithmeticException | IndexOutOfBoundsException | 
DisjointExtentException ex) {
-                if (nullIfOutside) {
-                    return null;
+                if (!nullIfOutside) {
+                    throw (PointOutsideCoverageException) new 
PointOutsideCoverageException(
+                            gc.pointOutsideCoverage(gridGeometry.extent), 
point).initCause(ex);
                 }
-                throw (PointOutsideCoverageException) new 
PointOutsideCoverageException(
-                        gc.pointOutsideCoverage(gridGeometry.extent), 
point).initCause(ex);
             }
         } catch (PointOutsideCoverageException ex) {
             ex.setOffendingLocation(point);
@@ -196,6 +197,7 @@ public class GridEvaluator implements 
GridCoverage.Evaluator {
         } catch (RuntimeException | TransformException ex) {
             throw new CannotEvaluateException(ex.getMessage(), ex);
         }
+        return null;        // May reach this point only if `nullIfOutside` is 
true.
     }
 
     /**

Reply via email to