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 9d35fc2  Coordinate Reference System parsed from "grid_mapping" 
attributes should appear in metadata.
9d35fc2 is described below

commit 9d35fc254b121c734b5de69edcfd0fdb4b8791c2
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Fri Apr 5 21:00:08 2019 +0200

    Coordinate Reference System parsed from "grid_mapping" attributes should 
appear in metadata.
---
 .../org/apache/sis/internal/netcdf/Decoder.java    | 45 ++++++++++++++++++++++
 .../java/org/apache/sis/internal/netcdf/Grid.java  |  2 +-
 .../apache/sis/internal/netcdf/GridMapping.java    |  2 +-
 .../apache/sis/storage/netcdf/MetadataReader.java  |  3 +-
 4 files changed, 48 insertions(+), 4 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 2b0b80a..3084826 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
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.internal.netcdf;
 
+import java.util.List;
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Collection;
@@ -28,9 +30,12 @@ import java.nio.file.Path;
 import org.opengis.util.NameSpace;
 import org.opengis.util.NameFactory;
 import org.opengis.referencing.datum.Datum;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.setup.GeometryLibrary;
 import org.apache.sis.storage.DataStore;
 import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.util.Utilities;
+import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.logging.WarningListeners;
 import org.apache.sis.internal.util.StandardDateFormat;
 import org.apache.sis.internal.system.DefaultFactories;
@@ -324,4 +329,44 @@ public abstract class Decoder extends 
ReferencingFactoryContainer implements Clo
      * @throws DataStoreException if a logical error occurred.
      */
     public abstract Grid[] getGrids() throws IOException, DataStoreException;
+
+    /**
+     * Returns for information purpose only the Coordinate Reference Systems 
present in this file.
+     * The CRS returned by this method may not be exactly the CRS to be used 
by variables.
+     * This method is provided for metadata purposes.
+     *
+     * @return coordinate reference systems present in this file.
+     * @throws IOException if an I/O operation was necessary but failed.
+     * @throws DataStoreException if a logical error occurred.
+     */
+    public final List<CoordinateReferenceSystem> getReferenceSystemInfo() 
throws IOException, DataStoreException {
+        final List<CoordinateReferenceSystem> list = new ArrayList<>();
+        for (final Variable variable : getVariables()) {
+            final GridMapping m = GridMapping.forVariable(variable);
+            if (m != null) {
+                addIfNotPresent(list, m.crs);
+            }
+        }
+        if (list.isEmpty()) {
+            for (final Grid grid : getGrids()) {
+                addIfNotPresent(list, grid.getCoordinateReferenceSystem(this));
+            }
+        }
+        return list;
+    }
+
+    /**
+     * Adds the given coordinate reference system to the given list, provided 
that an equivalent CRS
+     * (ignoring axes) is not already present.
+     */
+    private static void addIfNotPresent(final List<CoordinateReferenceSystem> 
list, final CoordinateReferenceSystem crs) {
+        if (crs != null) {
+            for (int i=list.size(); --i >= 0;) {
+                if (Utilities.deepEquals(crs, list.get(i), 
ComparisonMode.ALLOW_VARIANT)) {
+                    return;
+                }
+            }
+            list.add(crs);
+        }
+    }
 }
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 6495ccd..7cc2401 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
@@ -268,7 +268,7 @@ public abstract class Grid extends NamedElement {
      * @throws  IOException if an I/O operation was necessary but failed.
      * @throws  DataStoreException if the CRS can not be constructed.
      */
-    public final CoordinateReferenceSystem getCoordinateReferenceSystem(final 
Decoder decoder) throws IOException, DataStoreException {
+    final CoordinateReferenceSystem getCoordinateReferenceSystem(final Decoder 
decoder) throws IOException, DataStoreException {
         if (!isCRSDetermined) try {
             isCRSDetermined = true;                             // Set now for 
avoiding new attempts if creation fail.
             final List<CRSBuilder<?,?>> builders = new ArrayList<>();
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 e5a82a8..3a0b235 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
@@ -69,7 +69,7 @@ final class GridMapping {
      * <div class="note"><b>Note:</b> this come from different information 
than the one used by {@link CRSBuilder},
      * which creates CRS by inspection of coordinate system axes.</div>
      */
-    private final CoordinateReferenceSystem crs;
+    final CoordinateReferenceSystem crs;
 
     /**
      * The <cite>grid to CRS</cite> transform, or {@code null} if none. This 
information is usually not specified
diff --git 
a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
 
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
index 4b43741..6d34580 100644
--- 
a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
+++ 
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
@@ -1025,8 +1025,7 @@ split:  while ((start = 
CharSequences.skipLeadingWhitespaces(value, start, lengt
      * @throws ArithmeticException if the size of an axis exceeds {@link 
Integer#MAX_VALUE}, or other overflow occurs.
      */
     public Metadata read() throws IOException, DataStoreException {
-        for (final Grid cs : decoder.getGrids()) {
-            final CoordinateReferenceSystem crs = 
cs.getCoordinateReferenceSystem(decoder);
+        for (final CoordinateReferenceSystem crs : 
decoder.getReferenceSystemInfo()) {
             addReferenceSystem(crs);
             if (verticalCRS == null) {
                 verticalCRS = CRS.getVerticalComponent(crs, false);

Reply via email to