You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2022/12/08 20:47:02 UTC

[sis] branch geoapi-4.0 updated: Extent.intersect(…) should not return a value when there is no intersection.

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 32bdc7b3ea Extent.intersect(…) should not return a value when there is no intersection.
32bdc7b3ea is described below

commit 32bdc7b3ea9cd4a59f77e16a5db2301c45ce48ef
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Dec 8 21:44:34 2022 +0100

    Extent.intersect(…) should not return a value when there is no intersection.
---
 .../apache/sis/coverage/grid/GridDerivation.java   |  6 +++---
 .../org/apache/sis/coverage/grid/GridExtent.java   | 22 ++++++++++++++++++----
 .../apache/sis/coverage/grid/GridExtentTest.java   |  7 ++++---
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
index 26ab0221c2..7364fbb9ef 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
@@ -482,7 +482,7 @@ public class GridDerivation {
         final double[] scales;
         if (areaOfInterest.isExtentOnly()) {
             if (baseExtent != null) {
-                baseExtent = baseExtent.intersect(areaOfInterest.extent);
+                baseExtent = baseExtent.intersect(areaOfInterest.extent).orElseThrow(DisjointExtentException::new);
                 subGridSetter = "subgrid";
             }
             scales = areaOfInterest.resolution;
@@ -879,7 +879,7 @@ public class GridDerivation {
             }
         }
         if (areaOfInterest != null && baseExtent != null) {
-            baseExtent = baseExtent.intersect(areaOfInterest);
+            baseExtent = baseExtent.intersect(areaOfInterest).orElseThrow(DisjointExtentException::new);
             subGridSetter = "subgrid";
         }
         if (subsampling == null) {
@@ -1175,7 +1175,7 @@ public class GridDerivation {
                     resized = resized.forChunkSize(chunkSize);
                 }
                 if (clipping == GridClippingMode.STRICT) {
-                    resized = resized.intersect(base.extent);
+                    resized = resized.intersect(base.extent).orElseThrow(DisjointExtentException::new);
                 }
                 if (!resized.equals(baseExtent)) {
                     baseExtent = resized;
diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
index 62ab58b1a0..ea210a710d 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
@@ -245,6 +245,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
      * The axis types are {@link DimensionNameType#COLUMN} and {@link DimensionNameType#ROW ROW} in that order.
      *
      * @param  bounds  the bounds to copy in the new grid extent.
+     * @throws IllegalArgumentException if the rectangle is empty.
      *
      * @since 1.1
      */
@@ -260,6 +261,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
      *
      * @param  width   number of pixels in each row.
      * @param  height  number of pixels in each column.
+     * @throws IllegalArgumentException if the width or the height is not greater than zero.
      */
     public GridExtent(final long width, final long height) {
         ArgumentChecks.ensureStrictlyPositive("width",  width);
@@ -1748,7 +1750,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
     }
 
     /**
-     * Returns the intersection of this grid extent with to the given grid extent.
+     * Returns the intersection of this grid extent with the given grid extent.
      * The given extent shall have the same number of dimensions than this extent.
      * The {@linkplain #getAxisType(int) axis types} (vertical, temporal, …) must
      * be the same in all dimensions, ignoring types that are absent.
@@ -1760,12 +1762,12 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
      *
      * @since 1.3
      */
-    public GridExtent intersect(final GridExtent other) {
-        return combine(other, false);
+    public Optional<GridExtent> intersect(final GridExtent other) {
+        return Optional.ofNullable(combine(other, false));
     }
 
     /**
-     * Returns the union of this grid extent with to the given grid extent.
+     * Returns the union of this grid extent with the given grid extent.
      * The given extent shall have the same number of dimensions than this extent.
      * The {@linkplain #getAxisType(int) axis types} (vertical, temporal, …) must
      * be the same in all dimensions, ignoring types that are absent.
@@ -1783,6 +1785,11 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
 
     /**
      * Implementation of {@link #union(GridExtent)} and {@link #intersect(GridExtent)}
+     *
+     * @param  other  the grid to combine with.
+     * @return the union or intersection result, or {@code null} if the intersection gave an empty result.
+     * @throws MismatchedDimensionException if the two extents do not have the same number of dimensions.
+     * @throws IllegalArgumentException if axis types are specified but inconsistent in at least one dimension.
      */
     private GridExtent combine(final GridExtent other, final boolean union) {
         final int n = coordinates.length;
@@ -1809,6 +1816,13 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
         while (i < n) {clipped[i] = extremum(coordinates[i], other.coordinates[i],  union); i++;}
         if (Arrays.equals(clipped,  this.coordinates)) return this;
         if (Arrays.equals(clipped, other.coordinates)) return other;
+        if (!union) {
+            for (i=0; i<m; i++) {
+                if (clipped[i] > clipped[i+m]) {
+                    return null;                    // No intersection.
+                }
+            }
+        }
         return new GridExtent(this, clipped);
     }
 
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
index d0e1869669..cb71f51b95 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
@@ -277,11 +277,12 @@ public final strictfp class GridExtentTest extends TestCase {
     @Test
     public void testIntersect() {
         final GridExtent domain = createOther();
-        final GridExtent extent = create3D().intersect(domain);
+        final GridExtent extent = create3D().intersect(domain).get();
         assertExtentEquals(extent, 0, 150, 399);
         assertExtentEquals(extent, 1, 220, 799);
         assertExtentEquals(extent, 2, 40,  46);
-        assertSame(extent.intersect(domain), extent);
+        assertSame(extent.intersect(domain).get(), extent);
+        assertFalse(extent.intersect(domain.translate(1000)).isPresent());
     }
 
     /**
@@ -308,7 +309,7 @@ public final strictfp class GridExtentTest extends TestCase {
                 new DimensionNameType[] {DimensionNameType.COLUMN, DimensionNameType.TRACK, DimensionNameType.TIME},
                 new long[] {100, 200, 40}, new long[] {500, 800, 50}, false);
         try {
-            domain.intersect(other);
+            domain.intersect(other).get();
             fail("Should not be allowed");
         } catch (IllegalArgumentException e) {
             assertNotNull(e.getMessage());