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 2023/05/25 15:22:09 UTC

[sis] 03/03: Add an `AbstractEnvelope.isFinite()` convenience method.

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

commit 3110d38dbaf5d25be54ecdef367323c13ec292d4
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu May 25 17:21:06 2023 +0200

    Add an `AbstractEnvelope.isFinite()` convenience method.
---
 .../org/apache/sis/geometry/AbstractEnvelope.java   | 21 ++++++++++++++++++++-
 .../org/apache/sis/geometry/ArrayEnvelopeTest.java  | 15 +++++++++++++--
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java b/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
index cb9726a691..ae6f1d59fc 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
@@ -118,7 +118,7 @@ import static org.apache.sis.math.MathFunctions.isNegativeZero;
  * </ul>
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.1
+ * @version 1.4
  * @since   0.3
  */
 @XmlTransient
@@ -675,6 +675,25 @@ public abstract class AbstractEnvelope extends FormattableObject implements Enve
         return envelopes;
     }
 
+    /**
+     * Determines whether or not this envelope contains only finite values.
+     * More specifically this method returns {@code false} if at least one
+     * coordinate value is NaN or infinity, and {@code true} otherwise.
+     * Note that a finite envelope may be {@linkplain #isEmpty() empty}.
+     *
+     * @return {@code true} if this envelope contains only finite coordinate values.
+     *
+     * @since 1.4
+     */
+    public boolean isFinite() {
+        for (int i = getDimension(); --i >= 0;) {
+            if (!Double.isFinite(getLower(i)) || !Double.isFinite(getUpper(i))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /**
      * Determines whether or not this envelope is empty. An envelope is empty if it has zero
      * {@linkplain #getDimension() dimension}, or if the {@linkplain #getSpan(int) span} of
diff --git a/core/sis-referencing/src/test/java/org/apache/sis/geometry/ArrayEnvelopeTest.java b/core/sis-referencing/src/test/java/org/apache/sis/geometry/ArrayEnvelopeTest.java
index f374565cc9..5e916b7c8d 100644
--- a/core/sis-referencing/src/test/java/org/apache/sis/geometry/ArrayEnvelopeTest.java
+++ b/core/sis-referencing/src/test/java/org/apache/sis/geometry/ArrayEnvelopeTest.java
@@ -28,14 +28,25 @@ import static org.apache.sis.referencing.crs.HardCodedCRS.WGS84;
 
 /**
  * Unit tests for class {@link ArrayEnvelope}.
- * This is the base class of {@link GeneralEnvelope} and {@link ImmutableEnvelope}.
+ * The latter is the base class of {@link GeneralEnvelope} and {@link ImmutableEnvelope}.
  *
  * @author  Michael Hausegger
- * @version 1.0
+ * @version 1.4
  * @since   0.8
  */
 @DependsOn(AbstractEnvelopeTest.class)
 public final class ArrayEnvelopeTest extends TestCase {
+    /**
+     * Tests {@link ArrayEnvelope#isFinite()}.
+     */
+    @Test
+    public void testIsFinite() {
+        ArrayEnvelope envelope = new ArrayEnvelope(new double[] {10, 356.683168});
+        assertTrue(envelope.isFinite());
+        envelope.coordinates[0] = Double.NaN;
+        assertFalse(envelope.isFinite());
+    }
+
     /**
      * Tests {@link ArrayEnvelope#isEmpty()}.
      */