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 da98b1fc8a431ef8ab2c53ba629f57d738e7e32b
Author: Alexis Manin <[email protected]>
AuthorDate: Thu Apr 4 11:37:15 2019 +0200

    chore(raster): Add tests for grid derivation: slicing on a corner, slicing 
with wrap-around.
---
 .../sis/coverage/grid/GridDerivationTest.java      | 102 +++++++++++++++++++++
 1 file changed, 102 insertions(+)

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 87bb0a5..9c57c9d 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,6 +16,13 @@
  */
 package org.apache.sis.coverage.grid;
 
+import org.apache.sis.geometry.Envelope2D;
+import org.apache.sis.internal.referencing.j2d.AffineTransform2D;
+import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.referencing.crs.DefaultCompoundCRS;
+import org.apache.sis.referencing.operation.matrix.Matrices;
+import org.junit.Assert;
+import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.Envelope;
 import org.opengis.metadata.spatial.DimensionNameType;
 import org.opengis.referencing.datum.PixelInCell;
@@ -33,6 +40,10 @@ import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
+import java.util.Collections;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+
 import static org.apache.sis.test.ReferencingAssert.*;
 import static org.apache.sis.coverage.grid.GridGeometryTest.assertExtentEquals;
 
@@ -253,6 +264,97 @@ public final strictfp class GridDerivationTest extends 
TestCase {
     }
 
     /**
+     * Check that wrap-around is well applied when using {@link 
GridDerivation#slice(DirectPosition)}.
+     */
+    @Test
+    public void testSliceWrapAround() {
+        final GridGeometry base = new GridGeometry(
+                PixelInCell.CELL_CORNER,
+                new AffineTransform2D(-0.02, 0, 0, 0.1, 55, 172),
+                new Envelope2D(CommonCRS.WGS84.geographic(), 42, 172, 13, 51),
+                GridRoundingMode.NEAREST
+        );
+
+        final GridGeometry expectedResult = base.derive()
+                .slice(new DirectPosition2D(51, 187))
+                .build();
+
+        final GridGeometry fromWrapAround = base.derive()
+                .slice(new DirectPosition2D(51, -173))
+                .build();
+
+        Assert.assertEquals("Slice with wrap-around", expectedResult, 
fromWrapAround);
+        assertBetween(
+                "Wrapped Y coordinate",
+                base.envelope.getMinimum(1),
+                base.envelope.getMaximum(1),
+                fromWrapAround.envelope.getMedian(1)
+        );
+    }
+
+    /**
+     * Ensure that slicing on a corner point does not fail, but gives back a 
geometry centered on a pixel corner.
+     * @throws TransformException If we cannot build our test point.
+     */
+    @Test
+    public void testSliceCorner() throws TransformException {
+        GridGeometry base = grid(-132, 327, 986, 597, 2, 3);
+
+        // First of all, we'll try by focusing on the last pixel.
+        final DirectPosition2D gridUpperCorner = new DirectPosition2D(
+                base.extent.getHigh(0),
+                base.extent.getLow(1)
+        );
+
+        final DirectPosition geoUpperCorner = 
base.getGridToCRS(PixelInCell.CELL_CENTER)
+                .transform(gridUpperCorner, null);
+
+        GridGeometry slice = base.derive()
+                .slice(geoUpperCorner)
+                .build();
+
+        long[] expectedGridPoint = {(long) gridUpperCorner.x, (long) 
gridUpperCorner.y};
+        assertExtentEquals(expectedGridPoint, expectedGridPoint, slice.extent);
+
+        /* We will now try to focus on a point near the envelope edge. Note 
that slicing ensures to return a valid grid
+         * for any point INSIDE the envelope, it's non-deterministic about 
points perfectly aligned on the edge. So,
+         * here we will test a point very near to the envelope edge, but still 
into it.
+         */
+        final GeneralEnvelope grid3d = new GeneralEnvelope(3);
+        grid3d.setEnvelope(0, 0, 0, 1920, 1080, 4);
+
+        final DefaultCompoundCRS crs3d = new DefaultCompoundCRS(
+                Collections.singletonMap("name", "geo3d"),
+                CommonCRS.defaultGeographic(),
+                CommonCRS.Temporal.JULIAN.crs()
+        );
+        final GeneralEnvelope geo3d = new GeneralEnvelope(crs3d);
+        geo3d.setEnvelope(-180, -90, 1865.128, 180, 90, 1865.256);
+        base = new GridGeometry(
+                PixelInCell.CELL_CORNER,
+                MathTransforms.linear(Matrices.createTransform(grid3d, geo3d)),
+                geo3d,
+                GridRoundingMode.NEAREST
+        );
+
+        final GeneralDirectPosition geo3dUpperCorner = new 
GeneralDirectPosition(geo3d.getUpperCorner());
+        IntStream.range(0, geo3dUpperCorner.getDimension())
+                .forEach(idx -> geo3dUpperCorner.ordinates[idx] -= 1e-7);
+
+        slice = base.derive()
+                .slice(geo3dUpperCorner)
+                .build();
+
+        // Build expected grid point focused after slicing. We expect it to be 
upper corner.
+        expectedGridPoint = 
DoubleStream.of(grid3d.getUpperCorner().getCoordinate())
+                .mapToLong(value -> (long) value)
+                .map(exclusiveValue -> exclusiveValue -1)// Exclusive to 
inclusive
+                .toArray();
+
+        assertExtentEquals(expectedGridPoint, expectedGridPoint, slice.extent);
+    }
+
+    /**
      * Tests deriving a grid geometry with an envelope crossing the 
antimeridian.
      */
     @Test

Reply via email to