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/01/20 10:10:17 UTC

[sis] branch geoapi-4.0 updated: Make `GridGeometry.getEnvelope(CoordinateReferenceSystem)` a little bit more robust.

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 6b413e8  Make `GridGeometry.getEnvelope(CoordinateReferenceSystem)` a little bit more robust.
6b413e8 is described below

commit 6b413e8db06b719f0ef5e6a72d6dff5ab08851af
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Jan 20 11:07:19 2022 +0100

    Make `GridGeometry.getEnvelope(CoordinateReferenceSystem)` a little bit more robust.
---
 .../org/apache/sis/coverage/grid/GridExtent.java   |  2 ++
 .../org/apache/sis/coverage/grid/GridGeometry.java | 23 ++++++++++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)

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 866b38d..a754ff6 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
@@ -955,6 +955,8 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable
      * @throws TransformException if the envelope can not be computed with the given transform.
      *
      * @see #GridExtent(AbstractEnvelope, GridRoundingMode, int[], GridExtent, int[])
+     *
+     * @see GridGeometry#getEnvelope(CoordinateReferenceSystem)
      */
     final GeneralEnvelope toCRS(final MathTransform cornerToCRS, final MathTransform gridToCRS, final Envelope fallback)
             throws TransformException
diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
index 84b0402..0ef45e2 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
@@ -914,16 +914,31 @@ public class GridGeometry implements LenientComparable, Serializable {
             bitmask  = GRID_TO_CRS;
             errorKey = Resources.Keys.UnspecifiedTransform;
         } else try {
+            /*
+             * At this point the envelope should never be null because of invariants enforced by constructors.
+             * But we nevertheless perform some paranoiac checks. If we fail to transform the envelope, its okay.
+             * The main transform is the one operating on grid extent. The envelope transformation is for taking
+             * in account singularity points (mostly poles) and in case this grid geometry is a sub-grid geometry,
+             * in which case the envelope may have been clipped and we want to keep that clip.
+             */
+            final boolean onlyEnvelope = (extent == null || cornerToCRS == null);
             final CoordinateOperation op = findOperation(sourceCRS, crs, geographicBBox());
-            final Envelope clip = (envelope != null) ? Envelopes.transform(op, envelope) : null;
-            if (extent == null || cornerToCRS == null) {
-                return clip;
+            Envelope clip;
+            try {
+                clip = Envelopes.transform(op, envelope);
+                if (onlyEnvelope) return clip;
+            } catch (TransformException e) {
+                if (onlyEnvelope) throw e;
+                recoverableException("getEnvelope", e);
+                clip = null;
             }
             MathTransform tr = MathTransforms.concatenate(cornerToCRS, op.getMathTransform());
             final GeneralEnvelope env = extent.toCRS(tr, tr, clip);
             env.setCoordinateReferenceSystem(op.getTargetCRS());
             env.normalize();
-            env.intersect(clip);
+            if (clip != null) {
+                env.intersect(clip);
+            }
             return env;
         } catch (FactoryException e) {
             throw new TransformException(e);