sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Add an Envelope.findOperation(Envelope, Envelope) which take in a coount the area of interest. This is an incomplete feature - see https://issues.apache.org/jira/browse/SIS-442
Date Fri, 14 Dec 2018 19:12:47 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 91ad710114344a63c1d623ec022589bc6962d0b6
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Dec 14 20:06:57 2018 +0100

    Add an Envelope.findOperation(Envelope, Envelope) which take in a coount the area of interest.
    This is an incomplete feature - see https://issues.apache.org/jira/browse/SIS-442
---
 .../org/apache/sis/coverage/grid/GridGeometry.java | 28 +++++-----
 .../apache/sis/coverage/grid/GridGeometryTest.java |  2 +-
 .../java/org/apache/sis/geometry/Envelopes.java    | 62 ++++++++++++++++++++--
 .../main/java/org/apache/sis/referencing/CRS.java  |  7 +--
 ide-project/NetBeans/nbproject/genfiles.properties |  2 +-
 ide-project/NetBeans/nbproject/project.xml         |  2 +
 .../sis/internal/storage/AbstractGridResource.java | 25 +++++----
 7 files changed, 90 insertions(+), 38 deletions(-)

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 b3f3c44..97c8eeb 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
@@ -30,6 +30,7 @@ import org.opengis.referencing.datum.PixelInCell;
 import org.opengis.referencing.operation.Matrix;
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
+import org.opengis.referencing.operation.CoordinateOperation;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.cs.CoordinateSystemAxis;
 import org.opengis.referencing.cs.CoordinateSystem;
@@ -43,7 +44,6 @@ import org.apache.sis.referencing.operation.matrix.Matrices;
 import org.apache.sis.referencing.operation.transform.MathTransforms;
 import org.apache.sis.referencing.operation.transform.PassThroughTransform;
 import org.apache.sis.referencing.operation.transform.TransformSeparator;
-import org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.raster.Resources;
 import org.apache.sis.util.resources.Vocabulary;
@@ -51,12 +51,9 @@ import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.CharSequences;
-import org.apache.sis.util.Utilities;
 import org.apache.sis.util.Debug;
 import org.apache.sis.io.TableAppender;
 
-import static org.apache.sis.referencing.CRS.findOperation;
-
 
 /**
  * Valid extent of grid coordinates together with the transform from those grid coordinates
@@ -423,9 +420,11 @@ public class GridGeometry implements Serializable {
     /**
      * Invoked when a recoverable exception occurred. Those exceptions must be minor enough
      * that they can be silently ignored in most cases.
+     *
+     * @param  exception  the exception that occurred.
      */
     static void recoverableException(final Exception exception) {
-        Logging.recoverableException(Logging.getLogger(Modules.RASTER), GridGeometry.class,
"transform", exception);
+        Logging.recoverableException(Logging.getLogger(Modules.RASTER), GridGeometry.class,
"<init>", exception);
     }
 
     /**
@@ -580,16 +579,9 @@ public class GridGeometry implements Serializable {
              * to the 'gridToCRS' transform.  We should not transform the envelope here -
only concatenate the
              * transforms - because transforming envelopes twice add errors.
              */
-            if (envelope != null) {
-                final CoordinateReferenceSystem sourceCRS = envelope.getCoordinateReferenceSystem();
-                if (sourceCRS != null) {
-                    final CoordinateReferenceSystem targetCRS = areaOfInterest.getCoordinateReferenceSystem();
-                    if (targetCRS != null && !Utilities.equalsIgnoreMetadata(sourceCRS,
targetCRS)) {
-                        final DefaultGeographicBoundingBox bbox = new DefaultGeographicBoundingBox();
-                        bbox.setBounds(areaOfInterest);
-                        gridToAOI = MathTransforms.concatenate(gridToAOI, findOperation(sourceCRS,
targetCRS, bbox).getMathTransform());
-                    }
-                }
+            final CoordinateOperation operation = Envelopes.findOperation(envelope, areaOfInterest);
+            if (operation != null) {
+                gridToAOI = MathTransforms.concatenate(gridToAOI, operation.getMathTransform());
             }
             /*
              * If the envelope dimensions does not encompass all grid dimensions, the envelope
is probably non-invertible.
@@ -871,6 +863,12 @@ public class GridGeometry implements Serializable {
      * @return {@code true} if all specified attributes are defined (i.e. invoking the
      *         corresponding method will not thrown an {@link IncompleteGridGeometryException}).
      * @throws IllegalArgumentException if the specified bitmask is not a combination of
known masks.
+     *
+     * @see #getCoordinateReferenceSystem()
+     * @see #getEnvelope()
+     * @see #getExtent()
+     * @see #getResolution(boolean)
+     * @see #getGridToCRS(PixelInCell)
      */
     public boolean isDefined(final int bitmask) throws IllegalArgumentException {
         if ((bitmask & ~(CRS | ENVELOPE | EXTENT | GRID_TO_CRS | RESOLUTION)) != 0) {
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 28ebd88..47148f5 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
@@ -258,7 +258,7 @@ public final strictfp class GridGeometryTest extends TestCase {
      * @throws TransformException if an error occurred while using the "grid to CRS" transform.
      */
     @Test
-    @DependsOnMethod("testNonLinear")
+    @DependsOnMethod({"testNonLinear", "testGetExtent"})
     public void testGetExtentNonLinear() throws TransformException {
         final GridExtent extent = new GridExtent(
                 new DimensionNameType[] {
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 f27739a..e854b90 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
@@ -33,13 +33,11 @@ import org.opengis.referencing.operation.CoordinateOperation;
 import org.opengis.referencing.operation.Matrix;
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
+import org.opengis.referencing.operation.OperationNotFoundException;
 import org.opengis.referencing.operation.NoninvertibleTransformException;
 import org.opengis.util.FactoryException;
-import org.apache.sis.util.Static;
-import org.apache.sis.util.Utilities;
-import org.apache.sis.util.ComparisonMode;
-import org.apache.sis.util.logging.Logging;
-import org.apache.sis.util.resources.Errors;
+import org.opengis.metadata.extent.GeographicBoundingBox;
+import org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox;
 import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.operation.AbstractCoordinateOperation;
 import org.apache.sis.referencing.operation.transform.AbstractMathTransform;
@@ -47,7 +45,12 @@ 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.logging.Logging;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.ComparisonMode;
+import org.apache.sis.util.Utilities;
+import org.apache.sis.util.Static;
 
 import static org.apache.sis.util.StringBuilders.trimFractionalPart;
 
@@ -176,6 +179,55 @@ public final class Envelopes extends Static {
     }
 
     /**
+     * Finds a mathematical operation from the CRS of the given source envelope to the CRS
of the given target envelope.
+     * For non-null georeferenced envelopes, this method is equivalent to the following code
with {@code areaOfInterest}
+     * computed as the union of the two envelopes:
+     *
+     * <blockquote><code>return {@linkplain CRS#findOperation(CoordinateReferenceSystem,
CoordinateReferenceSystem,
+     * GeographicBoundingBox)} CRS.findOperation(source.getCoordinateReferenceSystem(), target.getCoordinateReferenceSystem(),
+     * <var>areaOfInterest</var>)</code></blockquote>
+     *
+     * If at least one envelope is null or has no CRS, then this method returns {@code null}.
+     *
+     * @param  source  the source envelope, or {@code null}.
+     * @param  target  the target envelope, or {@code null}.
+     * @return the mathematical operation from {@code source} CRS to {@code target} CRS,
+     *         or {@code null} if at least one argument is null or has no CRS.
+     * @throws OperationNotFoundException if no operation was found between the given pair
of CRS.
+     * @throws FactoryException if the operation can not be created for another reason.
+     *
+     * @see CRS#findOperation(CoordinateReferenceSystem, CoordinateReferenceSystem, GeographicBoundingBox)
+     *
+     * @since 1.0
+     */
+    public static CoordinateOperation findOperation(final Envelope source, final Envelope
target) throws FactoryException {
+        if (source != null && target != null) {
+            final CoordinateReferenceSystem sourceCRS, targetCRS;
+            if ((sourceCRS = source.getCoordinateReferenceSystem()) != null &&
+                (targetCRS = target.getCoordinateReferenceSystem()) != null)
+            {
+                DefaultGeographicBoundingBox areaOfInterest = null;
+                if (!sourceCRS.equals(targetCRS)) try {
+                    DefaultGeographicBoundingBox bbox = new DefaultGeographicBoundingBox();
+                    bbox.setBounds(source);
+                    areaOfInterest = bbox;                          // Set only on success.
+                    bbox = new DefaultGeographicBoundingBox();
+                    bbox.setBounds(target);
+                    areaOfInterest.add(bbox);
+                } catch (TransformException e) {
+                    /*
+                     * Note: we may succeed to transform 'source' and fail to transform 'target'
to geographic bounding box,
+                     * but the opposite is unlikely because 'source' should not have less
dimensions than 'target'.
+                     */
+                    Logging.recoverableException(Logging.getLogger(Loggers.GEOMETRY), Envelopes.class,
"findOperation", e);
+                }
+                return CRS.findOperation(sourceCRS, targetCRS, areaOfInterest);
+            }
+        }
+        return null;
+    }
+
+    /**
      * Computes the intersection of all given envelopes, transforming them to a common CRS
if necessary.
      * If all envelopes use the same CRS ({@link ComparisonMode#IGNORE_METADATA ignoring
metadata})
      * or if the CRS of all envelopes is {@code null}, then the {@linkplain GeneralEnvelope#intersect(Envelope)
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 0c4697f..62ea42e 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
@@ -616,6 +616,7 @@ public final class CRS extends Static {
      * @throws OperationNotFoundException if no operation was found between the given pair
of CRS.
      * @throws FactoryException if the operation can not be created for another reason.
      *
+     * @see Envelopes#findOperation(Envelope, Envelope)
      * @see DefaultCoordinateOperationFactory#createOperation(CoordinateReferenceSystem,
CoordinateReferenceSystem, CoordinateOperationContext)
      *
      * @since 0.7
@@ -629,9 +630,9 @@ public final class CRS extends Static {
         ArgumentChecks.ensureNonNull("targetCRS", targetCRS);
         final CoordinateOperationContext context = CoordinateOperationContext.fromBoundingBox(areaOfInterest);
         /*
-         * In principle we should just delegate to factory.createOperation(…). However
this operation may fail
-         * if a connection to the EPSG database has been found, but the EPSG tables do not
yet exist in that
-         * database and
+         * In principle following code should just delegate to factory.createOperation(…).
However that operation
+         * may fail if a connection to the EPSG database has been found, but the EPSG tables
do not yet exist in
+         * that database and we do not have the SQL scripts for creating them.
          */
         final DefaultCoordinateOperationFactory factory = CoordinateOperations.factory();
         try {
diff --git a/ide-project/NetBeans/nbproject/genfiles.properties b/ide-project/NetBeans/nbproject/genfiles.properties
index 59cb803..8afab47 100644
--- a/ide-project/NetBeans/nbproject/genfiles.properties
+++ b/ide-project/NetBeans/nbproject/genfiles.properties
@@ -3,6 +3,6 @@
 build.xml.data.CRC32=58e6b21c
 build.xml.script.CRC32=462eaba0
 build.xml.stylesheet.CRC32=28e38971@1.53.1.46
-nbproject/build-impl.xml.data.CRC32=7824aa7b
+nbproject/build-impl.xml.data.CRC32=fa87eb46
 nbproject/build-impl.xml.script.CRC32=a7689f96
 nbproject/build-impl.xml.stylesheet.CRC32=3a2fa800@1.89.1.48
diff --git a/ide-project/NetBeans/nbproject/project.xml b/ide-project/NetBeans/nbproject/project.xml
index 7f0d6bf..e5c4c7f 100644
--- a/ide-project/NetBeans/nbproject/project.xml
+++ b/ide-project/NetBeans/nbproject/project.xml
@@ -90,6 +90,8 @@
             <word>deserialized</word>
             <word>endianness</word>
             <word>geoidal</word>
+            <word>georeferenced</word>
+            <word>georeferencing</word>
             <word>geospatial</word>
             <word>grayscale</word>
             <word>hectopascals</word>
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
index 8971b9e..d5f1e78 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
@@ -99,20 +99,19 @@ public abstract class AbstractGridResource extends AbstractResource implements
G
      * @throws DataStoreException if an error occurred while validating the grid geometry.
      */
     protected final GridGeometry validateReadArgument(GridGeometry domain) throws DataStoreException
{
-        final GridGeometry gridGeometry = getGridGeometry();
-        final int dimension = gridGeometry.getDimension();
+        final GridGeometry stored = getGridGeometry();
         if (domain == null) {
-            domain = gridGeometry;
-        } else {
-            final int ad = domain.getDimension();
-            if (ad != gridGeometry.getDimension()) {
-                throw new MismatchedDimensionException(Errors.format(Errors.Keys.MismatchedDimension_3,
"domain", dimension, ad));
-            }
-            if (domain.isDefined(GridGeometry.GRID_TO_CRS) &&
-                    !gridGeometry.getGridToCRS(PixelInCell.CELL_CENTER).equals(domain.getGridToCRS(PixelInCell.CELL_CENTER)))
-            {
-                throw new IllegalArgumentException("Mismatched grid to CRS transform.");
-            }
+            return stored;
+        }
+        final int dimension = stored.getDimension();
+        final int ad = domain.getDimension();
+        if (ad != stored.getDimension()) {
+            throw new MismatchedDimensionException(Errors.format(Errors.Keys.MismatchedDimension_3,
"domain", dimension, ad));
+        }
+        if (domain.isDefined(GridGeometry.GRID_TO_CRS) &&
+                !stored.getGridToCRS(PixelInCell.CELL_CENTER).equals(domain.getGridToCRS(PixelInCell.CELL_CENTER)))
+        {
+            throw new IllegalArgumentException("Mismatched grid to CRS transform.");
         }
         return domain;
     }


Mime
View raw message