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: If users specified an envelope, use it for computing the sub-region to resample.
Date Tue, 31 Mar 2020 21:26:38 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 0bdce38  If users specified an envelope, use it for computing the sub-region to resample.
0bdce38 is described below

commit 0bdce384d7fb807d0679b60d259890aa7ed1ed19
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Mar 31 23:26:06 2020 +0200

    If users specified an envelope, use it for computing the sub-region to resample.
---
 .../sis/coverage/grid/GridCoverageProcessor.java    |  7 +++++++
 .../sis/coverage/grid/ResampledGridCoverage.java    | 21 ++++++++++++++++-----
 .../coverage/grid/ResampledGridCoverageTest.java    | 20 ++++++++++++++++++--
 3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
index e63f974..2f1b843 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
@@ -107,6 +107,13 @@ public class GridCoverageProcessor {
         ArgumentChecks.ensureNonNull("target", target);
         try {
             return ResampledGridCoverage.create(source, target, interpolation);
+        } catch (IllegalGridGeometryException e) {
+            final Throwable cause = e.getCause();
+            if (cause instanceof TransformException) {
+                throw (TransformException) cause;
+            } else {
+                throw e;
+            }
         } catch (FactoryException e) {
             throw new TransformException(e);
         }
diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
index 4e2a977..a05ddf2 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
@@ -170,7 +170,7 @@ final class ResampledGridCoverage extends GridCoverage {
      * @throws IncompleteGridGeometryException if the source grid geometry is missing an
information.
      * @throws TransformException if some coordinates can not be transformed to the specified
target.
      */
-    static GridCoverage create(final GridCoverage source, GridGeometry target, final Interpolation
interpolation)
+    static GridCoverage create(final GridCoverage source, final GridGeometry target, final
Interpolation interpolation)
             throws FactoryException, TransformException
     {
         final CoordinateReferenceSystem sourceCRS = source.getCoordinateReferenceSystem();
@@ -344,20 +344,31 @@ final class ResampledGridCoverage extends GridCoverage {
         /*
          * At this point all target grid geometry components are non-null.
          * Build the final target GridGeometry if any components were missing.
+         * If an envelope is defined, resample only that sub-region.
          */
+        GridGeometry resampled = target;
         ComparisonMode mode = ComparisonMode.IGNORE_METADATA;
         if (!target.isDefined(GridGeometry.EXTENT | GridGeometry.GRID_TO_CRS | GridGeometry.CRS))
{
-            target = new GridGeometry(targetExtent, PixelInCell.CELL_CENTER, targetCenterToCRS,
targetCRS);
+            resampled = new GridGeometry(targetExtent, PixelInCell.CELL_CENTER, targetCenterToCRS,
targetCRS);
             mode = ComparisonMode.APPROXIMATE;
+            if (target.isDefined(GridGeometry.ENVELOPE)) {
+                final MathTransform targetCornerToCRS = resampled.getGridToCRS(PixelInCell.CELL_CORNER);
+                GeneralEnvelope bounds = new GeneralEnvelope(resampled.getEnvelope());
+                bounds.intersect(target.getEnvelope());
+                bounds = Envelopes.transform(targetCornerToCRS.inverse(), bounds);
+                targetExtent = new GridExtent(bounds, GridRoundingMode.ENCLOSING, null, targetExtent,
null);
+                resampled = new GridGeometry(targetExtent, PixelInCell.CELL_CENTER, targetCenterToCRS,
targetCRS);
+                isGeometryExplicit = true;
+            }
         }
-        if (sourceGG.equals(target, mode)) {
+        if (sourceGG.equals(resampled, mode)) {
             return source;
         }
         /*
          * Complete the "target to source" transform.
          */
-        final MathTransform targetCornerToCRS = target.getGridToCRS(PixelInCell.CELL_CORNER);
-        return new ResampledGridCoverage(source, target,
+        final MathTransform targetCornerToCRS = resampled.getGridToCRS(PixelInCell.CELL_CORNER);
+        return new ResampledGridCoverage(source, resampled,
                 MathTransforms.concatenate(targetCornerToCRS, sourceCornerToCRS.inverse()),
                 MathTransforms.concatenate(targetCenterToCRS, sourceCenterToCRS.inverse()),
                 interpolation).specialize(isGeometryExplicit);
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
index 232608a..0417cc8 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/ResampledGridCoverageTest.java
@@ -145,6 +145,23 @@ public final strictfp class ResampledGridCoverageTest {
     }
 
     /**
+     * Tests resampling of a sub-region.
+     *
+     * @throws FactoryException if transformation between CRS can not be computed.
+     * @throws TransformException if some coordinates can not be transformed to the target
grid geometry.
+     */
+    @Test
+    public void testSubArea() throws FactoryException, TransformException {
+        final GridCoverage2D source = createGridCoverage();           // Envelope2D(20, 15,
60, 62)
+        GridGeometry gg = new GridGeometry(null, new Envelope2D(HardCodedCRS.WGS84, 18, 20,
17, 31));
+        final GridCoverage target = ResampledGridCoverage.create(source, gg, Interpolation.NEAREST);
+        final GridExtent sourceExtent = source.getGridGeometry().getExtent();
+        final GridExtent targetExtent = target.getGridGeometry().getExtent();
+        assertTrue(sourceExtent.getSize(0) > targetExtent.getSize(0));
+        assertTrue(sourceExtent.getSize(1) > targetExtent.getSize(1));
+    }
+
+    /**
      * Tests application of a reprojection.
      *
      * @throws FactoryException if transformation between CRS can not be computed.
@@ -153,8 +170,7 @@ public final strictfp class ResampledGridCoverageTest {
     @Test
     public void testReprojection() throws FactoryException, TransformException {
         final GridCoverage2D source = createGridCoverage();
-        GridGeometry gg = source.getGridGeometry();
-        gg = new GridGeometry(null, CELL_CENTER, null, HardCodedConversions.mercator());
+        GridGeometry gg = new GridGeometry(null, CELL_CENTER, null, HardCodedConversions.mercator());
         final GridCoverage target = ResampledGridCoverage.create(source, gg, Interpolation.NEAREST);
         assertTrue("GridExtent.startsAtZero", target.getGridGeometry().getExtent().startsAtZero());
         /*


Mime
View raw message