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 an Envelopes.compound(Envelope...) method.
Date Mon, 24 Sep 2018 16:27:51 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 a2fefb1  Add an Envelopes.compound(Envelope...) method.
a2fefb1 is described below

commit a2fefb1ea734b7395afe0bd64c9a889ab2556a26
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Sep 24 12:27:28 2018 -0400

    Add an Envelopes.compound(Envelope...) method.
---
 .../java/org/apache/sis/geometry/Envelopes.java    | 64 ++++++++++++++++++++--
 .../main/java/org/apache/sis/referencing/CRS.java  |  3 +
 .../operation/transform/MathTransforms.java        | 23 ++++----
 .../org/apache/sis/geometry/EnvelopesTest.java     | 38 ++++++++++++-
 4 files changed, 111 insertions(+), 17 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java b/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java
index d6688c1..c96adbe 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java
@@ -22,6 +22,7 @@ package org.apache.sis.geometry;
  * force installation of the Java2D module (e.g. JavaFX/SWT).
  */
 import java.util.Set;
+import java.util.ConcurrentModificationException;
 import org.opengis.geometry.Envelope;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.MismatchedDimensionException;
@@ -46,8 +47,8 @@ import org.apache.sis.internal.referencing.CoordinateOperations;
 import org.apache.sis.internal.referencing.DirectPositionView;
 import org.apache.sis.internal.referencing.Formulas;
 import org.apache.sis.internal.system.Loggers;
+import org.apache.sis.util.ArgumentChecks;
 
-import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
 import static org.apache.sis.util.StringBuilders.trimFractionalPart;
 
 
@@ -87,7 +88,7 @@ import static org.apache.sis.util.StringBuilders.trimFractionalPart;
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @author  Johann Sorel (Geomatys)
- * @version 0.8
+ * @version 1.0
  *
  * @see org.apache.sis.metadata.iso.extent.Extents
  * @see CRS
@@ -103,6 +104,58 @@ public final class Envelopes extends Static {
     }
 
     /**
+     * Puts together a list of envelopes, each of them using an independent coordinate reference
system.
+     * The dimension of the returned envelope is the sum of the dimension of all components.
+     * If all components have a coordinate reference system, then the returned envelope will
+     * have a compound coordinate reference system.
+     *
+     * @param  components  the envelopes to aggregate in a single envelope, in the given
order.
+     * @return the aggregation of all given envelopes.
+     * @throws FactoryException if the geodetic factory failed to create the compound CRS.
+     *
+     * @see org.apache.sis.referencing.CRS#compound(CoordinateReferenceSystem...)
+     * @see org.apache.sis.referencing.operation.transform.MathTransforms#compound(MathTransform...)
+     *
+     * @since 1.0
+     */
+    public static Envelope compound(final Envelope... components) throws FactoryException
{
+        ArgumentChecks.ensureNonNull("components", components);
+        int sum = 0;
+        for (int i=0; i<components.length; i++) {
+            final Envelope env = components[i];
+            ArgumentChecks.ensureNonNullElement("components", i, env);
+            sum += env.getDimension();
+        }
+        final GeneralEnvelope compound = new GeneralEnvelope(sum);
+        CoordinateReferenceSystem[] crsComponents = null;
+        int firstAffectedOrdinate = 0;
+        for (int i=0; i<components.length; i++) {
+            final Envelope env = components[i];
+            final int dim = env.getDimension();
+            compound.subEnvelope(firstAffectedOrdinate, firstAffectedOrdinate += dim).setEnvelope(env);
+            if (i == 0) {
+                final CoordinateReferenceSystem crs = env.getCoordinateReferenceSystem();
+                if (crs != null) {
+                    crsComponents = new CoordinateReferenceSystem[components.length];
+                    crsComponents[0] = crs;
+                }
+            } else if (crsComponents != null) {
+                if ((crsComponents[i] = env.getCoordinateReferenceSystem()) == null) {
+                    crsComponents = null;               // Not all CRS are non-null.
+                }
+            }
+        }
+        if (firstAffectedOrdinate != sum) {
+            // Should never happen unless the number of dimensions of an envelope changed
during iteration.
+            throw new ConcurrentModificationException();
+        }
+        if (crsComponents != null) {
+            compound.setCoordinateReferenceSystem(CRS.compound(crsComponents));
+        }
+        return compound;
+    }
+
+    /**
      * Invoked when a recoverable exception occurred. Those exceptions must be minor enough
      * that they can be silently ignored in most cases.
      */
@@ -124,6 +177,7 @@ public final class Envelopes extends Static {
      * @throws TransformException if the point can not be transformed
      *         or if a problem occurred while calculating the derivative.
      */
+    @SuppressWarnings("null")
     static Matrix derivativeAndTransform(final MathTransform transform, final double[] srcPts,
             final double[] dstPts, final int dstOff, final boolean derivate) throws TransformException
     {
@@ -203,7 +257,7 @@ public final class Envelopes extends Static {
     public static GeneralEnvelope transform(final MathTransform transform, final Envelope
envelope)
             throws TransformException
     {
-        ensureNonNull("transform", transform);
+        ArgumentChecks.ensureNonNull("transform", transform);
         return (envelope != null) ? transform(transform, envelope, null) : null;
     }
 
@@ -436,7 +490,7 @@ public final class Envelopes extends Static {
     public static GeneralEnvelope transform(final CoordinateOperation operation, Envelope
envelope)
             throws TransformException
     {
-        ensureNonNull("operation", operation);
+        ArgumentChecks.ensureNonNull("operation", operation);
         if (envelope == null) {
             return null;
         }
@@ -765,7 +819,7 @@ poles:  for (int i=0; i<dimension; i++, dimensionBitMask <<=
1) {
      * @see org.apache.sis.io.wkt
      */
     public static Envelope fromWKT(final CharSequence wkt) throws FactoryException {
-        ensureNonNull("wkt", wkt);
+        ArgumentChecks.ensureNonNull("wkt", wkt);
         try {
             return new GeneralEnvelope(wkt);
         } catch (IllegalArgumentException e) {
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
index cc3ef56..30dd2f1 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
@@ -898,7 +898,10 @@ public final class CRS extends Static {
      *
      * @since 0.8
      *
+     * @see org.apache.sis.referencing.crs.DefaultCompoundCRS
      * @see GeodeticObjectFactory#createCompoundCRS(Map, CoordinateReferenceSystem...)
+     * @see org.apache.sis.geometry.Envelopes#compound(Envelope...)
+     * @see org.apache.sis.referencing.operation.transform.MathTransforms#compound(MathTransform...)
      */
     public static CoordinateReferenceSystem compound(final CoordinateReferenceSystem... components)
throws FactoryException {
         ArgumentChecks.ensureNonNull("components", components);
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
index 8322ba6..bd6bfb1 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
@@ -278,27 +278,28 @@ public final class MathTransforms extends Static {
      *       is equals to the sum of the target dimensions of all given transforms.</li>
      * </ul>
      *
-     * @param  transforms  the transforms to aggregate in a single transform, in the given
order.
-     * @return the aggregation of all given transforms, or {@code null} if the given {@code
transforms} array was empty.
+     * @param  components  the transforms to aggregate in a single transform, in the given
order.
+     * @return the aggregation of all given transforms, or {@code null} if the given {@code
components} array was empty.
      *
      * @see PassThroughTransform
-     * @see org.apache.sis.referencing.crs.DefaultCompoundCRS
+     * @see org.apache.sis.referencing.CRS#compound(CoordinateReferenceSystem...)
+     * @see org.apache.sis.geometry.Envelopes#compound(Envelope...)
      *
      * @since 0.6
      */
-    public static MathTransform compound(final MathTransform... transforms) {
-        ArgumentChecks.ensureNonNull("transforms", transforms);
+    public static MathTransform compound(final MathTransform... components) {
+        ArgumentChecks.ensureNonNull("components", components);
         int sum = 0;
-        final int[] dimensions = new int[transforms.length];
-        for (int i=0; i<transforms.length; i++) {
-            final MathTransform tr = transforms[i];
-            ArgumentChecks.ensureNonNullElement("transforms", i, tr);
+        final int[] dimensions = new int[components.length];
+        for (int i=0; i<components.length; i++) {
+            final MathTransform tr = components[i];
+            ArgumentChecks.ensureNonNullElement("components", i, tr);
             sum += (dimensions[i] = tr.getSourceDimensions());
         }
         MathTransform compound = null;
         int firstAffectedOrdinate = 0;
-        for (int i=0; i<transforms.length; i++) {
-            MathTransform tr = transforms[i];
+        for (int i=0; i<components.length; i++) {
+            MathTransform tr = components[i];
             tr = passThrough(firstAffectedOrdinate, tr, sum - (firstAffectedOrdinate += dimensions[i]));
             if (compound == null) {
                 compound = tr;
diff --git a/core/sis-referencing/src/test/java/org/apache/sis/geometry/EnvelopesTest.java
b/core/sis-referencing/src/test/java/org/apache/sis/geometry/EnvelopesTest.java
index e5ec867..b7ee17e 100644
--- a/core/sis-referencing/src/test/java/org/apache/sis/geometry/EnvelopesTest.java
+++ b/core/sis-referencing/src/test/java/org/apache/sis/geometry/EnvelopesTest.java
@@ -16,9 +16,11 @@
  */
 package org.apache.sis.geometry;
 
+import java.util.List;
 import java.util.Collections;
 import org.opengis.geometry.Envelope;
 import org.opengis.util.FactoryException;
+import org.opengis.referencing.crs.SingleCRS;
 import org.opengis.referencing.crs.GeographicCRS;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.operation.CoordinateOperation;
@@ -42,7 +44,7 @@ import static org.opengis.test.Validators.validate;
  * This class inherits the test methods defined in {@link TransformTestCase}.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.3
  * @module
  */
@@ -227,4 +229,38 @@ public final strictfp class EnvelopesTest extends TransformTestCase<GeneralEnvel
         expected.setRange(0, -180, 180);
         assertEnvelopeEquals(expected, Envelopes.transform(envelope, targetCRS), STRICT,
STRICT);
     }
+
+    /**
+     * Test {@link Envelopes#compound(Envelope...)} method.
+     *
+     * @throws FactoryException if an error occurred while creating the compound CRS.
+     *
+     * @since 1.0
+     */
+    @Test
+    public void testCompound() throws FactoryException {
+        final GeneralEnvelope element0 = new GeneralEnvelope(HardCodedCRS.WGS84);
+        final GeneralEnvelope element1 = new GeneralEnvelope(HardCodedCRS.TIME);
+        final GeneralEnvelope expected = new GeneralEnvelope(3);
+        element0.setRange(0,   20,   30);
+        expected.setRange(0,   20,   30);
+        element0.setRange(1,   40,   45);
+        expected.setRange(1,   40,   45);
+        element1.setRange(0, 1000, 1010);
+        expected.setRange(2, 1000, 1010);
+        Envelope env = Envelopes.compound(element0, element1);
+        final List<SingleCRS> crs = CRS.getSingleComponents(env.getCoordinateReferenceSystem());
+        assertEnvelopeEquals(expected, env);
+        assertEquals("crs.components.count", 2, crs.size());
+        assertSame("crs.components[0]", HardCodedCRS.WGS84, crs.get(0));
+        assertSame("crs.components[1]", HardCodedCRS.TIME, crs.get(1));
+        /*
+         * Try again without CRS in the second component.
+         * The compound envelope shall not have CRS anymore.
+         */
+        element1.setCoordinateReferenceSystem(null);
+        env = Envelopes.compound(element0, element1);
+        assertEnvelopeEquals(expected, env);
+        assertNull("crs.components", env.getCoordinateReferenceSystem());
+    }
 }


Mime
View raw message