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 705de4a756 Try harder to resolve ambiguity when axis type is X, Y or Z.
705de4a756 is described below

commit 705de4a756cd00416e335789476eef4c4c7555d1
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Fri Jul 8 17:23:37 2022 +0200

    Try harder to resolve ambiguity when axis type is X, Y or Z.
    
    https://issues.apache.org/jira/browse/SIS-552
---
 .../org/apache/sis/internal/netcdf/AxisType.java   | 48 +++++++++++++++-------
 .../org/apache/sis/internal/netcdf/Variable.java   |  5 +++
 2 files changed, 38 insertions(+), 15 deletions(-)

diff --git 
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/AxisType.java 
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/AxisType.java
index b6c3f4055e..300132d56d 100644
--- 
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/AxisType.java
+++ 
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/AxisType.java
@@ -32,7 +32,7 @@ import ucar.nc2.constants.CF;
  * Enumeration order is the desired order of coordinate values.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.3
  * @since   1.1
  * @module
  */
@@ -106,12 +106,26 @@ public enum AxisType {
         return (type != null) ? TYPES.get(type.toLowerCase(Locale.US)) : null;
     }
 
+    /**
+     * Returns {@code true} if the given abbreviation is null or is ambiguous.
+     * The latter happens when the {@code axis} attribute value or the 
variable name is "X", "Y" or "Z",
+     * which could be longitude, latitude or height as well as axes in any 
other coordinate system.
+     *
+     * @param  abbreviation  the axis abbreviation, or {@code null}.
+     * @return whether the given abbreviation is considered ambiguous.
+     */
+    private static boolean isNullOrAmbiguous(final Character abbreviation) {
+        return (abbreviation == null) || (abbreviation >= 'x' && abbreviation 
<= 'z');
+    }
+
     /**
      * Returns the axis type (identified by its abbreviation) for the given 
axis, or 0 if unknown.
      * The returned code is one of the controlled vocabulary documented in 
{@link Axis#abbreviation}.
      *
      * @param  axis  axis for which to get an abbreviation.
      * @return abbreviation for the given axis, or 0 if none.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/SIS-552";>SIS-552</a>
      */
     public static char abbreviation(final Variable axis) {
         /*
@@ -121,17 +135,20 @@ public enum AxisType {
          * are standardized to "longitude" and "latitude" among others.
          */
         Character abbreviation = abbreviation(axis.getAxisType());
-        if (abbreviation == null) {
+        if (isNullOrAmbiguous(abbreviation)) {
+            Character fallback = abbreviation;
             abbreviation = 
abbreviation(axis.getAttributeAsString(CF.STANDARD_NAME));    // No fallback on 
variable name.
+            if (fallback == null) fallback = abbreviation;
             /*
              * If the abbreviation is still unknown, look at the "long_name", 
"description" or "title" attribute. Those
              * attributes are not standardized, so they are less reliable than 
"standard_name". But they are still more
              * reliable that the variable name since the long name may be 
"Longitude" or "Latitude" while the variable
              * name is only "x" or "y".
              */
-            if (abbreviation == null) {
+            if (isNullOrAmbiguous(abbreviation)) {
                 abbreviation = abbreviation(axis.getDescription());
-                if (abbreviation == null) {
+                if (fallback == null) fallback = abbreviation;
+                if (isNullOrAmbiguous(abbreviation)) {
                     /*
                      * Actually the "degree_east" and "degree_north" units of 
measurement are the most reliable way to
                      * identify geographic system, but we nevertheless check 
them almost last because the direction is
@@ -150,17 +167,18 @@ public enum AxisType {
                      * We test the variable name last because that name is 
more at risk of being an uninformative "x" or "y" name.
                      * If even the variable name is not sufficient, we use 
some easy to recognize units.
                      */
-                    if (abbreviation == null) {
-                        abbreviation = abbreviation(axis.getName());
-                        if (abbreviation == null) {
-                            final Unit<?> unit = axis.getUnit();
-                            if (Units.isTemporal(unit)) {
-                                return 't';
-                            } else if (Units.isPressure(unit)) {
-                                return 'z';
-                            } else {
-                                return 0;
-                            }
+                    abbreviation = abbreviation(axis.getName());
+                    if (fallback == null) fallback = abbreviation;
+                    if (isNullOrAmbiguous(abbreviation)) {
+                        final Unit<?> unit = axis.getUnit();
+                        if (Units.isTemporal(unit)) {
+                            return 't';
+                        } else if (Units.isPressure(unit)) {
+                            return 'z';
+                        } else if (fallback != null) {
+                            return fallback;
+                        } else {
+                            return 0;
                         }
                     }
                 }
diff --git 
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java 
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
index e97358861a..6bbb896069 100644
--- 
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
+++ 
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
@@ -517,6 +517,11 @@ public abstract class Variable extends Node {
      * Returns the value of {@code "_CoordinateAxisType"} or {@code "axis"} 
attribute, or {@code null} if none.
      * Note that a {@code null} value does not mean that this variable is not 
an axis.
      *
+     * <p>Possible values for {@code _CoordinateAxisType} attribute include 
{@code "lat"}, {@code "lon"},
+     * {@code "GeoX"} and {@code "GeoY"}. But the possible values for {@code 
axis} attribute include only
+     * {@code "X"} and {@code "Y"}, which is more ambiguous. Caller should try 
to complete the information
+     * when the returned value is X or Y.</p>
+     *
      * @return {@code "_CoordinateAxisType"} or {@code "axis"} attribute 
value, or {@code null} if none.
      */
     protected abstract String getAxisType();

Reply via email to