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 0826f4c45deb59281cd5fa69b4cac1fb6538a1e4
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Sat Apr 6 17:52:54 2019 +0200

    For parsing GDAL attributes, use GDAL flavor of WKT 1.
    Avoid to parse the same WKT many times.
---
 .../org/apache/sis/internal/netcdf/Decoder.java    |  5 ++-
 .../apache/sis/internal/netcdf/GridMapping.java    | 50 +++++++++++++++-------
 .../org/apache/sis/internal/netcdf/Variable.java   |  5 +++
 3 files changed, 44 insertions(+), 16 deletions(-)

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 1732715..75c575f 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
@@ -107,9 +107,12 @@ public abstract class Decoder extends 
ReferencingFactoryContainer implements Clo
      * {@code "spatial_ref_sys"} and {@code "GeoTransform"} attributes 
associated to a variable having the name
      * specified by the {@code "grid_mapping"} attribute.
      *
+     * <p>Keys are either {@link Variable} instance for which we found a grid 
mapping, or {@link String} instances
+     * if we found some variables with {@code "grid_mapping"} attribute 
values.</p>
+     *
      * @see GridMapping#forVariable(Variable)
      */
-    final Map<String,GridMapping> gridMapping;
+    final Map<Object,GridMapping> gridMapping;
 
     /**
      * Where to send the warnings.
diff --git 
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridMapping.java
 
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridMapping.java
index 09de0b2..ea7a691 100644
--- 
a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridMapping.java
+++ 
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridMapping.java
@@ -100,9 +100,9 @@ final class GridMapping {
      * @param  variable  the variable for which to create a grid geometry.
      */
     static GridMapping forVariable(final Variable variable) {
+        final Map<Object,GridMapping> gridMapping = 
variable.decoder.gridMapping;
         final String name = variable.getAttributeAsString(CF.GRID_MAPPING);
         if (name != null) {
-            final Map<String,GridMapping> gridMapping = 
variable.decoder.gridMapping;
             GridMapping gm = gridMapping.get(name);
             if (gm != null) {
                 return gm;
@@ -117,7 +117,18 @@ final class GridMapping {
                 }
             }
         }
-        return parseNonStandard(variable);
+        /*
+         * Found no "grid_mapping" attribute. The block below is not 
CF-compliant,
+         * but we find some use of this non-standard approach in practice.
+         */
+        GridMapping gm = gridMapping.get(variable);
+        if (gm == null) {
+            gm = parseNonStandard(variable);
+            if (gm != null) {
+                gridMapping.put(variable, gm);
+            }
+        }
+        return gm;
     }
 
     /**
@@ -145,7 +156,7 @@ final class GridMapping {
         MathTransform gridToCRS = null;
         try {
             if (wkt != null) {
-                crs = CRS.fromWKT(wkt);
+                crs = createFromWKT(mapping, wkt);
             }
             if (gtr != null) {
                 message = Resources.Keys.CanNotCreateGridGeometry_3;
@@ -158,7 +169,7 @@ final class GridMapping {
                                   
.getString(Errors.Keys.UnexpectedArrayLength_2, 6, c.length)));
                 }
             }
-        } catch (FactoryException | NumberFormatException e) {
+        } catch (ParseException | NumberFormatException e) {
             canNotCreate(mapping, message, e);
         }
         return new GridMapping(crs, gridToCRS, false);
@@ -193,17 +204,7 @@ final class GridMapping {
             if (isEPSG) {
                 crs = CRS.forCode(Constants.EPSG + ':' + isEPSG);
             } else {
-                final WKTFormat f = new WKTFormat(variable.getLocale(), 
variable.decoder.getTimeZone());
-                
f.setConvention(org.apache.sis.io.wkt.Convention.WKT1_COMMON_UNITS);
-                crs = (CoordinateReferenceSystem) f.parseObject(code);
-                final Warnings warnings = f.getWarnings();
-                if (warnings != null) {
-                    final LogRecord record = new LogRecord(Level.WARNING, 
warnings.toString());
-                    record.setLoggerName(Modules.NETCDF);
-                    
record.setSourceClassName(Variable.class.getCanonicalName());
-                    record.setSourceMethodName("getGridGeometry");
-                    variable.decoder.listeners.warning(record);
-                }
+                crs = createFromWKT(variable, code);
             }
         } catch (FactoryException | ParseException | ClassCastException e) {
             canNotCreate(variable, Resources.Keys.CanNotCreateCRS_3, e);
@@ -213,6 +214,25 @@ final class GridMapping {
     }
 
     /**
+     * Creates a coordinate reference system by parsing a Well Known Text 
(WKT) string. The WKT is presumed
+     * to use the GDAL flavor of WKT 1, and warnings are redirected to decoder 
listeners.
+     */
+    private static CoordinateReferenceSystem createFromWKT(final Variable 
variable, final String wkt) throws ParseException {
+        final WKTFormat f = new WKTFormat(variable.getLocale(), 
variable.decoder.getTimeZone());
+        f.setConvention(org.apache.sis.io.wkt.Convention.WKT1_COMMON_UNITS);
+        final CoordinateReferenceSystem crs = (CoordinateReferenceSystem) 
f.parseObject(wkt);
+        final Warnings warnings = f.getWarnings();
+        if (warnings != null) {
+            final LogRecord record = new LogRecord(Level.WARNING, 
warnings.toString());
+            record.setLoggerName(Modules.NETCDF);
+            record.setSourceClassName(Variable.class.getCanonicalName());
+            record.setSourceMethodName("getGridGeometry");
+            variable.decoder.listeners.warning(record);
+        }
+        return crs;
+    }
+
+    /**
      * Logs a warning about a CRS or grid geometry that can not be created.
      * This method presumes that {@link GridMapping} are invoked (indirectly) 
from {@link Variable#getGridGeometry()}.
      *
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 e81bc7c..d009902 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
@@ -1217,4 +1217,9 @@ public abstract class Variable extends NamedElement {
         }
         return buffer.toString();
     }
+
+    /*
+     * Do not override Object.equals(Object) and Object.hashCode(),
+     * because Variables are used as keys by GridMapping.forVariable(…).
+     */
 }

Reply via email to