sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Fix an IndexOutOfBoundsException in Convention.nodataValues(…) and minor improvements in the handling of netCDF attributes.
Date Tue, 28 May 2019 21:09:13 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 ba7cec2f96402dab9f55677485ba1ccd51f05f0d
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue May 28 23:06:53 2019 +0200

    Fix an IndexOutOfBoundsException in Convention.nodataValues(…) and minor improvements
in the handling of netCDF attributes.
---
 .../org/apache/sis/internal/netcdf/Convention.java | 12 +++----
 .../sis/internal/netcdf/ucar/GroupWrapper.java     |  2 +-
 .../sis/internal/netcdf/ucar/VariableWrapper.java  | 42 +++++++++++++---------
 .../org/apache/sis/internal/netcdf/GridTest.java   |  2 +-
 4 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
index 699db49..7937f53 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
@@ -474,12 +474,10 @@ public class Convention {
                      * Assume that all map projection parameters in netCDF files are numbers
or array of numbers.
                      * If values are array, then they are converted to an array of {@code
double[]} type.
                      */
-                    final Vector data = node.getAttributeAsVector(name);
-                    if (data == null) continue;
-                    switch (data.size()) {
-                        case 0:  continue;
-                        case 1:  value = data.get(0); break;
-                        default: value = data.doubleValues(); break;
+                    value = node.getAttributeValue(name);
+                    if (value == null) continue;
+                    if (value instanceof Vector) {
+                        value = ((Vector) value).doubleValues();
                     }
                     break;
                 }
@@ -654,7 +652,7 @@ public class Convention {
             if (values != null) {
                 final int length = values.size();
                 for (int j=0; j<length; j++) try {
-                    pads.merge(values.get(i), 1 << i, (v1, v2) -> ((Integer) v1)
| ((Integer) v2));
+                    pads.merge(values.get(j), 1 << i, (v1, v2) -> ((Integer) v1)
| ((Integer) v2));
                 } catch (NumberFormatException e) {
                     data.decoder.illegalAttributeValue(name, values.stringValue(i), e);
                 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GroupWrapper.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GroupWrapper.java
index 6b5664d..605beb4 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GroupWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GroupWrapper.java
@@ -75,6 +75,6 @@ final class GroupWrapper extends Node {
      */
     @Override
     protected Object getAttributeValue(final String attributeName) {
-        return VariableWrapper.getAttributeValues(group.findAttributeIgnoreCase(attributeName));
+        return VariableWrapper.getAttributeValue(group.findAttributeIgnoreCase(attributeName));
     }
 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
index 953a15e..e0a3b60 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
@@ -337,31 +337,41 @@ final class VariableWrapper extends Variable {
      */
     @Override
     protected Object getAttributeValue(final String attributeName) {
-        return getAttributeValues(raw.findAttributeIgnoreCase(attributeName));
+        return getAttributeValue(raw.findAttributeIgnoreCase(attributeName));
     }
 
     /**
      * Implementation of {@link #getAttributeValue(String)} shared with {@link GroupWrapper}.
      */
-    static Object getAttributeValues(final Attribute attribute) {
+    static Object getAttributeValue(final Attribute attribute) {
         if (attribute != null) {
             final int length = attribute.getLength();
-            if (length != 0) {
-                boolean hasValues = false;
-                final boolean isString = attribute.isString();
-                final Object[] values = isString ? new String[length] : new Number[length];
-                for (int i=0; i<values.length; i++) {
-                    final Object value;
-                    if (isString) {
-                        value = Utils.nonEmpty(attribute.getStringValue(i));
-                    } else {
-                        value = Utils.fixSign(attribute.getNumericValue(i), attribute.isUnsigned());
+            switch (length) {
+                case 0: break;
+                case 1: {
+                    final Object value = attribute.getValue(0);
+                    if (value instanceof String) {
+                        return Utils.nonEmpty((String) value);
+                    } else if (value instanceof Number) {
+                        return Utils.fixSign((Number) value, attribute.isUnsigned());
                     }
-                    values[i] = value;
-                    hasValues |= (value != null);
+                    break;
                 }
-                if (hasValues) {
-                    return (values.length == 1) ? values[0] : Vector.create(values, attribute.isUnsigned());
+                default: {
+                    if (attribute.isString()) {
+                        boolean hasValues = false;
+                        final String[] values = new String[length];
+                        for (int i=0; i<length; i++) {
+                            values[i] = Utils.nonEmpty(attribute.getStringValue(i));
+                            hasValues |= (values[i] != null);
+                        }
+                        if (hasValues) {
+                            return values;
+                        }
+                    } else {
+                        final Array array = attribute.getValues();
+                        return createDecimalVector(array.get1DJavaArray(array.getElementType()),
attribute.isUnsigned());
+                    }
                 }
             }
         }
diff --git a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/GridTest.java
b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/GridTest.java
index 60639fe..f9781f6 100644
--- a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/GridTest.java
+++ b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/GridTest.java
@@ -144,6 +144,6 @@ public strictfp class GridTest extends TestCase {
         assertEquals("Latitude of false origin",           25,    pg.parameter("Latitude
of false origin")         .doubleValue(), STRICT);
         assertEquals("Longitude of false origin",         -95,    pg.parameter("Longitude
of false origin")        .doubleValue(), STRICT);
         assertEquals("Latitude of 1st standard parallel",  25,    pg.parameter("Latitude
of 1st standard parallel").doubleValue(), STRICT);
-        assertEquals("Latitude of 2nd standard parallel",  25.05, pg.parameter("Latitude
of 2nd standard parallel").doubleValue(), 1E-6);
+        assertEquals("Latitude of 2nd standard parallel",  25.05, pg.parameter("Latitude
of 2nd standard parallel").doubleValue(), STRICT);
     }
 }


Mime
View raw message