sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/03: Move back reduce(…) in GridGeometry for simplifying GridDerivation and for consistency with GridExtent.reduce(…).
Date Tue, 29 Jan 2019 18:52:12 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

commit 17cdd81f3e3525af77d74e4b9b35b29cbef55bf5
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Jan 29 12:22:03 2019 +0100

    Move back reduce(…) in GridGeometry for simplifying GridDerivation and for consistency
with GridExtent.reduce(…).
---
 .../apache/sis/coverage/grid/GridDerivation.java   | 121 ++++++---------------
 .../org/apache/sis/coverage/grid/GridExtent.java   |   3 +-
 .../org/apache/sis/coverage/grid/GridGeometry.java |  41 ++++++-
 .../sis/coverage/grid/GridDerivationTest.java      |  54 ++++-----
 .../apache/sis/coverage/grid/GridGeometryTest.java |  52 +++++----
 5 files changed, 127 insertions(+), 144 deletions(-)

diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
index 4aeb15b..36991ed 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridDerivation.java
@@ -49,19 +49,23 @@ import org.opengis.coverage.PointOutsideCoverageException;
  * <ol>
  *   <li>{@link #rounding(GridRoundingMode)} and/or {@link #margin(int...)} in any
order</li>
  *   <li>{@link #subgrid(Envelope, double...)}</li>
- *   <li>{@link #slice(DirectPosition)}</li>
- *   <li>{@link #sliceByRatio(double, int...)}</li>
- *   <li>{@link #reduce(int...)}</li>
+ *   <li>{@link #slice(DirectPosition)} and/or {@link #sliceByRatio(double, int...)}</li>
  * </ol>
  *
  * Then the grid geometry is created by a call to {@link #build()}.
- * Alternatively, {@link #extent()} can be invoked if only the {@link GridExtent} is desired
+ * Alternatively, {@link #buildExtent()} can be invoked if only the {@link GridExtent} is
desired
  * instead than the full {@link GridGeometry}.
  *
+ * <p>All methods in this class preserve the number of dimensions. For example the
{@link #slice(DirectPosition)} method sets
+ * the {@linkplain GridExtent#getSize(int) grid size} to 1 in all dimensions specified by
the <cite>slice point</cite>,
+ * but does not remove those dimensions from the grid geometry.
+ * For dimensionality reduction, see {@link GridGeometry#reduce(int...)}.</p>
+ *
  * @author  Martin Desruisseaux (Geomatys)
  * @version 1.0
  *
  * @see GridGeometry#derive()
+ * @see GridGeometry#reduce(int...)
  *
  * @since 1.0
  * @module
@@ -73,9 +77,8 @@ public class GridDerivation {
     protected final GridGeometry base;
 
     /**
-     * If {@link #subgrid(Envelope, double...)},  {@link #slice(DirectPosition)} or {@link
#reduce(int...)} has been invoked,
-     * the method name. This is used for preventing those methods to be invoked twice or
out-of-order, which is currently not
-     * supported.
+     * If {@link #subgrid(Envelope, double...)} or {@link #slice(DirectPosition)} has been
invoked, the method name.
+     * This is used for preventing those methods to be invoked twice or out-of-order, which
is currently not supported.
      */
     private String subGridSetter;
 
@@ -101,12 +104,6 @@ public class GridDerivation {
     private int[] modifiedDimensions;
 
     /**
-     * The grid dimension to keep, or {@code null} if no filtering is applied.
-     * Those values are set by {@link #reduce(int...)}.
-     */
-    private int[] selectedDimensions;
-
-    /**
      * If non-null, the extent will be expanded by that amount of cells on each grid dimension.
      *
      * @see #margin(int...)
@@ -143,16 +140,6 @@ public class GridDerivation {
     }
 
     /**
-     * Verifies that {@link #reduce(int...)} has not yet been invoked.
-     * This method is invoked for enforcing the method call order defined in javadoc.
-     */
-    private void ensureReduceNotSet() {
-        if (selectedDimensions != null) {
-            throw new IllegalStateException(Resources.format(Resources.Keys.CanNotSetDerivedGridProperty_1,
"reduce"));
-        }
-    }
-
-    /**
      * Controls behavior of rounding from floating point values to integers.
      * This setting modifies computations performed by the following methods
      * (it has no effect on other methods in this {@code GridDerivation} class):
@@ -166,8 +153,8 @@ public class GridDerivation {
      *
      * @param  mode  the new rounding mode.
      * @return {@code this} for method call chaining.
-     * @throws IllegalStateException if {@link #slice(DirectPosition)}, {@link #subgrid(Envelope,
double...)} or
-     *         {@link #reduce(int...)} have already been invoked.
+     * @throws IllegalStateException if {@link #subgrid(Envelope, double...)} or {@link #slice(DirectPosition)}
+     *         has already been invoked.
      */
     public GridDerivation rounding(final GridRoundingMode mode) {
         ArgumentChecks.ensureNonNull("mode", mode);
@@ -201,8 +188,8 @@ public class GridDerivation {
      * @param  cellCounts  number of cells by which to expand the grid extent.
      * @return {@code this} for method call chaining.
      * @throws IllegalArgumentException if a value is negative.
-     * @throws IllegalStateException if {@link #slice(DirectPosition)}, {@link #subgrid(Envelope,
double...)} or
-     *         {@link #reduce(int...)} have already been invoked.
+     * @throws IllegalStateException if {@link #subgrid(Envelope, double...)} or {@link #slice(DirectPosition)}
+     *         has already been invoked.
      */
     public GridDerivation margin(final int... cellCounts) {
         ArgumentChecks.ensureNonNull("cellCounts", cellCounts);
@@ -238,7 +225,7 @@ public class GridDerivation {
      *   <li>If a non-default rounding mode is desired, it should be {@linkplain #rounding(GridRoundingMode)
specified}
      *       before to invoke this method.</li>
      *   <li>This method does not reduce the number of dimensions of the grid geometry.
-     *       For dimensionality reduction, see {@link #reduce(int...)}.</li>
+     *       For dimensionality reduction, see {@link GridGeometry#reduce(int...)}.</li>
      * </ul>
      *
      * @param  areaOfInterest  the desired spatiotemporal region in any CRS (transformations
will be applied as needed),
@@ -250,8 +237,8 @@ public class GridDerivation {
      * @return {@code this} for method call chaining.
      * @throws IncompleteGridGeometryException if the base grid geometry has no extent or
no "grid to CRS" transform.
      * @throws IllegalGridGeometryException if an error occurred while converting the envelope
coordinates to grid coordinates.
-     * @throws IllegalStateException if {@code subgrid(Envelope, double...)}, {@link #slice(DirectPosition)}
or
-     *         {@link #reduce(int...)} have already been invoked.
+     * @throws IllegalStateException if {@code subgrid(Envelope, double...)} or {@link #slice(DirectPosition)}
+     *         has already been invoked.
      *
      * @see GridExtent#subsample(int[])
      */
@@ -431,7 +418,7 @@ public class GridDerivation {
      *   <li>If a non-default rounding mode is desired, it should be {@linkplain #rounding(GridRoundingMode)
specified}
      *       before to invoke this method.</li>
      *   <li>This method does not reduce the number of dimensions of the grid geometry.
-     *       For dimensionality reduction, see {@link #reduce(int...)}.</li>
+     *       For dimensionality reduction, see {@link GridGeometry#reduce(int...)}.</li>
      * </ul>
      *
      * @param  slicePoint   the coordinates where to get a slice.
@@ -439,11 +426,9 @@ public class GridDerivation {
      * @throws IncompleteGridGeometryException if the base grid geometry has no extent or
no "grid to CRS" transform.
      * @throws IllegalGridGeometryException if an error occurred while converting the point
coordinates to grid coordinates.
      * @throws PointOutsideCoverageException if the given point is outside the grid extent.
-     * @throws IllegalStateException if {@link #reduce(int...)} has already been invoked.
      */
     public GridDerivation slice(final DirectPosition slicePoint) {
         ArgumentChecks.ensureNonNull("slicePoint", slicePoint);
-        ensureReduceNotSet();
         MathTransform cornerToCRS = base.requireGridToCRS();
         subGridSetter = "slice";
         try {
@@ -484,12 +469,10 @@ public class GridDerivation {
      * @return {@code this} for method call chaining.
      * @throws IncompleteGridGeometryException if the base grid geometry has no extent.
      * @throws IndexOutOfBoundsException if a {@code dimensionsToKeep} value is out of bounds.
-     * @throws IllegalStateException if {@link #reduce(int...)} has already been invoked.
      */
     public GridDerivation sliceByRatio(final double sliceRatio, final int... dimensionsToKeep)
{
         ArgumentChecks.ensureBetween("sliceRatio", 0, 1, sliceRatio);
         ArgumentChecks.ensureNonNull("dimensionsToKeep", dimensionsToKeep);
-        ensureReduceNotSet();
         final GridExtent extent = (subExtent != null) ? subExtent : base.getExtent();
         final GeneralDirectPosition slicePoint = new GeneralDirectPosition(extent.getDimension());
         for (int i=0; i<slicePoint.ordinates.length; i++) {
@@ -502,66 +485,34 @@ public class GridDerivation {
         return this;
     }
 
-    /**
-     * Requests a grid geometry that encompass only some dimensions of the grid extent.
-     * The specified dimensions will be copied into a new grid geometry.
-     * The selection is applied on {@linkplain GridGeometry#getExtent() grid extent} dimensions;
-     * they are not necessarily the same than the {@linkplain GridGeometry#getEnvelope()
envelope} dimensions.
-     * The given dimensions must be in strictly ascending order without duplicated values.
-     * The number of dimensions of the sub grid geometry will be {@code dimensions.length}.
-     *
-     * <p>This method performs a <cite>dimensionality reduction</cite>.
-     * This method can not be used for changing dimension order.</p>
-     *
-     * @param  dimensions  the grid (not CRS) dimensions to select, in strictly increasing
order.
-     * @return {@code this} for method call chaining.
-     * @throws IndexOutOfBoundsException if an index is out of bounds.
-     *
-     * @see GridExtent#getSubspaceDimensions(int)
-     * @see GridExtent#reduce(int...)
-     * @see org.apache.sis.referencing.CRS#reduce(CoordinateReferenceSystem, int...)
+    /*
+     * RATIONAL FOR NOT PROVIDING reduce(int... dimensions) METHOD HERE: that method would
need to be the last method invoked,
+     * otherwise it makes more complicated to implement other methods in this class.  Forcing
users to invoke 'build()' before
+     * (s)he can invoke GridGeometry.reduce(…) makes that clear and avoid the need for
more flags in this GridDerivation class.
+     * Furthermore declaring the 'reduce(…)' method in GridGeometry is more consistent
with 'GridExtent.reduce(…)'.
      */
-    public GridDerivation reduce(final int... dimensions) {
-        ensureReduceNotSet();
-        subGridSetter = "reduce";
-        selectedDimensions = GridExtent.verifyDimensions(dimensions, base.getDimension());
-        return this;
-    }
 
     /**
-     * Returns the extent of the modified grid geometry. This method is more efficient than
-     * {@link #build()} if only the grid extent is desired instead than the full grid geometry.
+     * Builds a grid geometry with the configuration specified by the other methods in this
{@code GridDerivation} class.
      *
-     * @return the modified grid geometry extent.
+     * @return the modified grid geometry. May be the {@linkplain #base} grid geometry if
no change apply.
      */
-    public GridExtent extent() {
-        GridExtent extent = (subExtent != null) ? subExtent : base.getExtent();
-        if (selectedDimensions != null) {
-            extent = extent.reduce(selectedDimensions);
+    public GridGeometry build() {
+        if (toBase != null || subExtent != base.extent) try {
+            return new GridGeometry(base, subExtent, toBase);
+        } catch (TransformException e) {
+            throw new IllegalGridGeometryException(e, "envelope");
         }
-        return extent;
+        return base;
     }
 
     /**
-     * Builds a grid geometry with the configuration specified by the other methods in this
{@code GridDerivation} class.
+     * Returns the extent of the modified grid geometry. This method is more efficient than
+     * {@link #build()} if only the grid extent is desired instead than the full grid geometry.
      *
-     * @return the modified grid geometry. May be the {@linkplain #base} grid geometry if
no change apply.
+     * @return the modified grid geometry extent.
      */
-    public GridGeometry build() {
-        GridGeometry grid = base;
-        String cause = null;
-        try {
-            if (toBase != null || subExtent != base.extent) {
-                cause = "subgrid";
-                grid = new GridGeometry(grid, subExtent, toBase);
-            }
-            if (selectedDimensions != null) {
-                cause = "dimensions";
-                grid = new GridGeometry(grid, selectedDimensions);
-            }
-        } catch (FactoryException | TransformException e) {
-            throw new IllegalGridGeometryException(e, cause);
-        }
-        return grid;
+    public GridExtent buildExtent() {
+        return (subExtent != null) ? subExtent : base.getExtent();
     }
 }
diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
index 7e26b07..0787073 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
@@ -895,7 +895,8 @@ public class GridExtent implements Serializable {
      * @return the sub-envelope, or {@code this} if the given array contains all dimensions
of this grid extent.
      * @throws IndexOutOfBoundsException if an index is out of bounds.
      *
-     * @see GridDerivation#reduce(int...)
+     * @see #getSubspaceDimensions(int)
+     * @see GridGeometry#reduce(int...)
      */
     public GridExtent reduce(int... dimensions) {
         final int sd = getDimension();
diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
index 5adf1b3..7f4241c 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
@@ -505,9 +505,9 @@ public class GridGeometry implements Serializable {
      * @param  dimensions  the dimensions to select, in strictly increasing order (this may
not be verified).
      * @throws FactoryException if an error occurred while separating the "grid to CRS" transform.
      *
-     * @see GridDerivation#reduce(int...)
+     * @see #reduce(int...)
      */
-    GridGeometry(final GridGeometry other, int[] dimensions) throws FactoryException {
+    private GridGeometry(final GridGeometry other, int[] dimensions) throws FactoryException
{
         extent = (other.extent != null) ? other.extent.reduce(dimensions) : null;
         final int n = dimensions.length;
         if (other.gridToCRS != null) {
@@ -976,9 +976,8 @@ public class GridGeometry implements Serializable {
 
     /**
      * Returns an object that can be used for creating a new grid geometry derived from this
grid geometry.
-     * Despite its name, {@code GridDerivation} does not change the state of this {@code
GridGeometry} but
-     * instead creates new instances as needed. Examples of modifications include clipping
to a sub-area,
-     * applying a sub-sampling, or selecting some grid dimensions.
+     * {@code GridDerivation} does not change the state of this {@code GridGeometry} but
instead creates
+     * new instances as needed. Examples of modifications include clipping to a sub-area
or applying a sub-sampling.
      *
      * <div class="note"><b>Example:</b>
      * for clipping this grid geometry to a sub-area, one can use:
@@ -992,6 +991,9 @@ public class GridGeometry implements Serializable {
      * </div>
      *
      * Each {@code GridDerivation} instance can be used only once and should be used in a
single thread.
+     * {@code GridDerivation} preserves the number of dimensions. For example {@linkplain
GridDerivation#slice slicing}
+     * sets the {@linkplain GridExtent#getSize(int) grid size} to 1 in all dimensions specified
by a <cite>slice point</cite>,
+     * but does not remove those dimensions from the grid geometry. For dimensionality reduction,
see {@link #reduce(int...)}.
      *
      * @return an object for deriving a grid geometry from {@code this}.
      */
@@ -1000,6 +1002,35 @@ public class GridGeometry implements Serializable {
     }
 
     /**
+     * Returns a grid geometry that encompass only some dimensions of the grid geometry.
+     * The specified dimensions will be copied into a new grid geometry.
+     * The selection is applied on {@linkplain #getExtent() grid extent} dimensions;
+     * they are not necessarily the same than the {@linkplain #getEnvelope() envelope} dimensions.
+     * The given dimensions must be in strictly ascending order without duplicated values.
+     * The number of dimensions of the sub grid geometry will be {@code dimensions.length}.
+     *
+     * <p>This method performs a <cite>dimensionality reduction</cite>.
+     * This method can not be used for changing dimension order.</p>
+     *
+     * @param  dimensions  the grid (not CRS) dimensions to select, in strictly increasing
order.
+     * @return the sub-grid geometry, or {@code this} if the given array contains all dimensions
of this grid geometry.
+     * @throws IndexOutOfBoundsException if an index is out of bounds.
+     *
+     * @see GridExtent#getSubspaceDimensions(int)
+     * @see GridExtent#reduce(int...)
+     * @see org.apache.sis.referencing.CRS#reduce(CoordinateReferenceSystem, int...)
+     */
+    public GridGeometry reduce(int... dimensions) {
+        dimensions = GridExtent.verifyDimensions(dimensions, getDimension());
+        if (dimensions != null) try {
+            return new GridGeometry(this, dimensions);
+        } catch (FactoryException e) {
+            throw new IllegalGridGeometryException(e, "dimensions");
+        }
+        return this;
+    }
+
+    /**
      * Returns a hash value for this grid geometry. This value need not remain
      * consistent between different implementations of the same class.
      */
diff --git a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
index 109d334..1f93d3b 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridDerivationTest.java
@@ -16,11 +16,11 @@
  */
 package org.apache.sis.coverage.grid;
 
+import org.opengis.geometry.Envelope;
 import org.opengis.metadata.spatial.DimensionNameType;
 import org.opengis.referencing.datum.PixelInCell;
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
-import org.apache.sis.referencing.operation.matrix.Matrix2;
 import org.apache.sis.referencing.operation.matrix.Matrix3;
 import org.apache.sis.referencing.operation.matrix.Matrix4;
 import org.apache.sis.referencing.operation.transform.MathTransforms;
@@ -32,6 +32,7 @@ import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
+import org.junit.Ignore;
 
 import static org.apache.sis.test.ReferencingAssert.*;
 import static org.apache.sis.coverage.grid.GridGeometryTest.assertExtentEquals;
@@ -73,7 +74,7 @@ public final strictfp class GridDerivationTest extends TestCase {
         envelope.setRange(0, -70.001, +80.002);
         envelope.setRange(1,   4.997,  15.003);
         assertExtentEquals(new long[] {370,  40,  4},
-                           new long[] {389, 339, 10}, grid.derive().subgrid(envelope).extent());
+                           new long[] {389, 339, 10}, grid.derive().subgrid(envelope).buildExtent());
     }
 
     /**
@@ -107,7 +108,7 @@ public final strictfp class GridDerivationTest extends TestCase {
         final GeneralEnvelope envelope = new GeneralEnvelope(HardCodedCRS.WGS84);
         envelope.setRange(0, -70.001, +80.002);
         envelope.setRange(1,  -4.997,  15.003);
-        final GridExtent actual = grid.derive().subgrid(envelope).extent();
+        final GridExtent actual = grid.derive().subgrid(envelope).buildExtent();
         assertEquals(extent.getAxisType(0), actual.getAxisType(0));
         assertExtentEquals(new long[] { 56, 69, 2},
                            new long[] {130, 73, 4}, actual);
@@ -199,39 +200,24 @@ public final strictfp class GridDerivationTest extends TestCase {
     }
 
     /**
-     * Tests {@link GridDerivation#reduce(int...)}.
+     * Tests deriving a grid geometry with an envelope crossing the antimeridian.
      */
     @Test
-    public void testReduce() {
+    @Ignore("TODO: not yet fixed.")
+    public void testSubGridCrossingAntiMeridian() {
         final GridGeometry grid = new GridGeometry(
-                new GridExtent(null, new long[] {336, 20, 4}, new long[] {401, 419, 10},
true),
-                PixelInCell.CELL_CORNER, MathTransforms.linear(new Matrix4(
-                        0,   0.5, 0,  -90,
-                        0.5, 0,   0, -180,
-                        0,   0,   2,    3,
-                        0,   0,   0,    1)), HardCodedCRS.GEOID_3D);
-        /*
-         * Tests on the two first dimensions.
-         */
-        GridGeometry reduced = grid.derive().reduce(0, 1).build();
-        assertNotSame(grid, reduced);
-        assertExtentEquals(new long[] {336, 20}, new long[] {401, 419}, reduced.getExtent());
-        assertSame("CRS", HardCodedCRS.WGS84, reduced.getCoordinateReferenceSystem());
-        assertArrayEquals("resolution", new double[] {0.5, 0.5}, reduced.getResolution(false),
STRICT);
-        assertMatrixEquals("gridToCRS", new Matrix3(
-                  0, 0.5,  -90,
-                  0.5, 0, -180,
-                  0,   0,    1), MathTransforms.getMatrix(reduced.getGridToCRS(PixelInCell.CELL_CORNER)),
STRICT);
-        /*
-         * Tests on the last dimension.
-         */
-        reduced = grid.derive().reduce(2).build();
-        assertNotSame(grid, reduced);
-        assertExtentEquals(new long[] {4}, new long[] {10}, reduced.getExtent());
-        assertSame("CRS", HardCodedCRS.GRAVITY_RELATED_HEIGHT, reduced.getCoordinateReferenceSystem());
-        assertArrayEquals("resolution", new double[] {2}, reduced.getResolution(false), STRICT);
-        assertMatrixEquals("gridToCRS", new Matrix2(
-                  2, 3,
-                  0, 1), MathTransforms.getMatrix(reduced.getGridToCRS(PixelInCell.CELL_CORNER)),
STRICT);
+                new GridExtent(200, 180), PixelInCell.CELL_CORNER,
+                MathTransforms.linear(new Matrix3(
+                1,  0, 80,
+                0, -1, 90,
+                0,  0,  1)), HardCodedCRS.WGS84);
+
+        final GeneralEnvelope aoi = new GeneralEnvelope(HardCodedCRS.WGS84);
+        aoi.setRange(0, 140, -179);
+        aoi.setRange(1, -90,   90);
+
+        final GridGeometry subgrid = grid.derive().subgrid(aoi).build();
+        Envelope subEnv = subgrid.getEnvelope();
+        assertEquals(aoi, subEnv);
     }
 }
diff --git a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridGeometryTest.java
b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridGeometryTest.java
index cb9bdf2..3a3ed67 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridGeometryTest.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridGeometryTest.java
@@ -20,21 +20,18 @@ import org.opengis.metadata.spatial.DimensionNameType;
 import org.opengis.referencing.datum.PixelInCell;
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.referencing.operation.matrix.Matrix2;
 import org.apache.sis.referencing.operation.matrix.Matrix3;
 import org.apache.sis.referencing.operation.matrix.Matrix4;
 import org.apache.sis.referencing.operation.matrix.Matrices;
 import org.apache.sis.referencing.operation.transform.MathTransforms;
 import org.apache.sis.referencing.crs.HardCodedCRS;
 import org.apache.sis.geometry.GeneralEnvelope;
-import org.apache.sis.internal.referencing.j2d.AffineTransform2D;
-import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.apache.sis.test.ReferencingAssert.*;
-import org.junit.Ignore;
-import org.opengis.geometry.Envelope;
 
 
 /**
@@ -239,22 +236,39 @@ public final strictfp class GridGeometryTest extends TestCase {
     }
 
     /**
-     * Test derivate grid geometry with an envelope crossing the antimeridian.
+     * Tests {@link GridGeometry#reduce(int...)}.
      */
     @Test
-    @Ignore
-    public void testSubGridCrossingAntiMeridian() {
-
-        final GridExtent extent = new GridExtent(200, 180);
-        final AffineTransform2D gridToCrs = new AffineTransform2D(1, 0, 0, -1, 80, 90);
-        final GridGeometry grid = new GridGeometry(extent, PixelInCell.CELL_CORNER, gridToCrs,
CommonCRS.WGS84.normalizedGeographic());
-
-        final GeneralEnvelope aoi = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
-        aoi.setRange(0, 140, -179);
-        aoi.setRange(1, -90, 90);
-
-        final GridGeometry subgrid = grid.derive().subgrid(aoi).build();
-        Envelope subEnv = subgrid.getEnvelope();
-        assertEquals(aoi, subEnv);
+    public void testReduce() {
+        final GridGeometry grid = new GridGeometry(
+                new GridExtent(null, new long[] {336, 20, 4}, new long[] {401, 419, 10},
true),
+                PixelInCell.CELL_CORNER, MathTransforms.linear(new Matrix4(
+                        0,   0.5, 0,  -90,
+                        0.5, 0,   0, -180,
+                        0,   0,   2,    3,
+                        0,   0,   0,    1)), HardCodedCRS.GEOID_3D);
+        /*
+         * Tests on the two first dimensions.
+         */
+        GridGeometry reduced = grid.reduce(0, 1);
+        assertNotSame(grid, reduced);
+        assertExtentEquals(new long[] {336, 20}, new long[] {401, 419}, reduced.getExtent());
+        assertSame("CRS", HardCodedCRS.WGS84, reduced.getCoordinateReferenceSystem());
+        assertArrayEquals("resolution", new double[] {0.5, 0.5}, reduced.getResolution(false),
STRICT);
+        assertMatrixEquals("gridToCRS", new Matrix3(
+                  0, 0.5,  -90,
+                  0.5, 0, -180,
+                  0,   0,    1), MathTransforms.getMatrix(reduced.getGridToCRS(PixelInCell.CELL_CORNER)),
STRICT);
+        /*
+         * Tests on the last dimension.
+         */
+        reduced = grid.reduce(2);
+        assertNotSame(grid, reduced);
+        assertExtentEquals(new long[] {4}, new long[] {10}, reduced.getExtent());
+        assertSame("CRS", HardCodedCRS.GRAVITY_RELATED_HEIGHT, reduced.getCoordinateReferenceSystem());
+        assertArrayEquals("resolution", new double[] {2}, reduced.getResolution(false), STRICT);
+        assertMatrixEquals("gridToCRS", new Matrix2(
+                  2, 3,
+                  0, 1), MathTransforms.getMatrix(reduced.getGridToCRS(PixelInCell.CELL_CORNER)),
STRICT);
     }
 }


Mime
View raw message