sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Avoid an exception when axis units are not specified and "pixel units" can be assumed.
Date Fri, 31 May 2019 18:31:43 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 01b6a3c9f8ec806d57ed8e2ebe120a939c64855e
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri May 31 15:39:03 2019 +0200

    Avoid an exception when axis units are not specified and "pixel units" can be assumed.
---
 .../main/java/org/apache/sis/util/Exceptions.java  | 25 ++++++++++++++++-
 .../java/org/apache/sis/internal/netcdf/Axis.java  | 13 ++++++++-
 .../org/apache/sis/internal/netcdf/Decoder.java    |  3 +-
 .../java/org/apache/sis/internal/netcdf/Grid.java  | 32 ++++++++++++++++++----
 4 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java b/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
index e9209b5..850544b 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
@@ -19,6 +19,7 @@ package org.apache.sis.util;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Locale;
+import java.util.Objects;
 import java.sql.SQLException;
 import java.io.UncheckedIOException;
 import java.nio.file.DirectoryIteratorException;
@@ -32,7 +33,7 @@ import org.apache.sis.util.collection.BackingStoreException;
  * Static methods working with {@link Exception} instances.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.3
  * @module
  */
@@ -131,6 +132,28 @@ public final class Exceptions extends Static {
     }
 
     /**
+     * Returns {@code true} if the given exceptions are of the same class and contains the
same message.
+     * This method does not compare the {@linkplain Throwable#getStackTrace() stack trace},
+     * {@linkplain Throwable#getCause() cause} or {@linkplain Throwable#getSuppressed() suppressed
exceptions}.
+     *
+     * @param  first   the first exception, or {@code null}.
+     * @param  second  the second exception, or {@code null}.
+     * @return {@code true} if both exceptions are {@code null}, or both exceptions are non-null,
+     *         of the same class and with the same {@linkplain Throwable#getMessage() message}.
+     *
+     * @since 1.0
+     */
+    public static boolean messageEquals(final Throwable first, final Throwable second) {
+        if (first == second) {
+            return true;
+        }
+        if (first == null || second == null) {
+            return false;
+        }
+        return first.getClass() == second.getClass() && Objects.equals(first.getMessage(),
second.getMessage());
+    }
+
+    /**
      * Returns a string which contain the given message on the first line, followed by the
      * {@linkplain #getLocalizedMessage(Throwable, Locale) localized message} of the given
exception
      * on the next line. If the exception has a {@linkplain Throwable#getCause() causes},
then
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
index 2678593..68db514 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
@@ -493,7 +493,9 @@ public final class Axis extends NamedElement {
      * @param  order    0 if creating the first axis, 1 if creating the second axis, <i>etc</i>.
      * @return the ISO axis.
      */
-    final CoordinateSystemAxis toISO(final CSFactory factory, final int order) throws FactoryException
{
+    final CoordinateSystemAxis toISO(final CSFactory factory, final int order)
+            throws DataStoreException, FactoryException, IOException
+    {
         /*
          * The axis name is stored without namespace, because the variable name in a netCDF
file can be anything;
          * this is not controlled vocabulary. However the standard name, if any, is stored
with "NetCDF" namespace
@@ -545,6 +547,15 @@ public final class Axis extends NamedElement {
                 case 'H': case 'h':                                 // Gravity-related and
ellipsoidal height.
                 case 'E': case 'N': unit = Units.METRE;  break;     // Projected easting
and northing.
                 case 't':           unit = Units.SECOND; break;     // Time.
+                case 'x': case 'y': {
+                    final Vector values = coordinates.read();
+                    final Number increment = values.increment(0);
+                    if (increment != null && increment.doubleValue() == 1) {
+                        // Do not test values.doubleValue(0) since different conventions
exit (0-based, 1-based, etc).
+                        unit = Units.PIXEL;
+                    }
+                    break;
+                }
             }
         }
         AxisDirection dir = direction;
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
index 4977861..724ea61 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
@@ -377,8 +377,9 @@ public abstract class Decoder extends ReferencingFactoryContainer implements
Clo
          * Consequently if such information is present, grid CRS may be inaccurate.
          */
         if (list.isEmpty()) {
+            final List<Exception> warnings = new ArrayList<>();     // For internal
usage by Grid.
             for (final Grid grid : getGrids()) {
-                addIfNotPresent(list, grid.getCoordinateReferenceSystem(this));
+                addIfNotPresent(list, grid.getCoordinateReferenceSystem(this, warnings));
             }
         }
         return list;
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java
index 6ac15b4..fdaedb5 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java
@@ -41,6 +41,7 @@ import org.apache.sis.coverage.grid.GridGeometry;
 import org.apache.sis.coverage.grid.IllegalGridGeometryException;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.util.NullArgumentException;
+import org.apache.sis.util.Exceptions;
 import org.apache.sis.util.ArraysExt;
 
 
@@ -83,7 +84,7 @@ public abstract class Grid extends NamedElement {
      * The coordinate reference system, created when first needed.
      * May be {@code null} even after we attempted to create it.
      *
-     * @see #getCoordinateReferenceSystem(Decoder)
+     * @see #getCoordinateReferenceSystem(Decoder, List)
      */
     private CoordinateReferenceSystem crs;
 
@@ -270,12 +271,15 @@ public abstract class Grid extends NamedElement {
      * this method because this CRS will be used for adjusting axis order or for completion
if grid mapping does not include
      * information for all dimensions.</p>
      *
-     * @param   decoder  the decoder for which CRS are constructed.
+     * @param   decoder   the decoder for which CRS are constructed.
+     * @param   warnings  previous warnings, for avoiding to log the same message twice.
Can be null.
      * @return  the CRS for this grid geometry, or {@code null}.
      * @throws  IOException if an I/O operation was necessary but failed.
      * @throws  DataStoreException if the CRS can not be constructed.
      */
-    final CoordinateReferenceSystem getCoordinateReferenceSystem(final Decoder decoder) throws
IOException, DataStoreException {
+    final CoordinateReferenceSystem getCoordinateReferenceSystem(final Decoder decoder, final
List<Exception> warnings)
+            throws IOException, DataStoreException
+    {
         if (!isCRSDetermined) try {
             isCRSDetermined = true;                             // Set now for avoiding new
attempts if creation fail.
             final List<CRSBuilder<?,?>> builders = new ArrayList<>();
@@ -293,12 +297,30 @@ public abstract class Grid extends NamedElement {
                                         Collections.singletonMap(CoordinateSystem.NAME_KEY,
getName()), components);
             }
         } catch (FactoryException | NullArgumentException ex) {
-            canNotCreate(decoder, "getCoordinateReferenceSystem", Resources.Keys.CanNotCreateCRS_3,
ex);
+            if (isNewWarning(ex, warnings)) {
+                canNotCreate(decoder, "getCoordinateReferenceSystem", Resources.Keys.CanNotCreateCRS_3,
ex);
+            }
         }
         return crs;
     }
 
     /**
+     * Returns {@code true} if the given exception has not already been logged.
+     * If {@code true}, then this method add the given exception to the warnings list.
+     */
+    private static boolean isNewWarning(final Exception ex, final List<Exception> warnings)
{
+        if (warnings != null) {
+            for (final Exception previous : warnings) {
+                if (Exceptions.messageEquals(ex, previous)) {
+                    return false;
+                }
+            }
+            warnings.add(ex);
+        }
+        return true;
+    }
+
+    /**
      * Builds the grid extent if the shape is available. The shape may not be available
      * if a dimension has unlimited length. The dimension names are informative only.
      *
@@ -488,7 +510,7 @@ findFree:       for (int srcDim : axis.sourceDimensions) {
              * to be at the centers of the cells, but we do not require that in this standard".
We nevertheless check
              * if an axis thinks otherwise.
              */
-            final CoordinateReferenceSystem crs = getCoordinateReferenceSystem(decoder);
+            final CoordinateReferenceSystem crs = getCoordinateReferenceSystem(decoder, null);
             if (CRS.getHorizontalComponent(crs) instanceof GeographicCRS) {
                 for (final Axis axis : axes) {
                     if (axis.isCellCorner()) {


Mime
View raw message