sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Better filtering of netCDF variable to consider as GridCoverage.
Date Wed, 12 Dec 2018 09:44:28 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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 2d186a8  Better filtering of netCDF variable to consider as GridCoverage.
2d186a8 is described below

commit 2d186a8bd2d88f5207acaf1fb67d9ec26ff0c17d
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Wed Dec 12 10:44:06 2018 +0100

    Better filtering of netCDF variable to consider as GridCoverage.
---
 .../java/org/apache/sis/internal/netcdf/Grid.java  | 10 +++++++
 .../org/apache/sis/internal/netcdf/Variable.java   | 31 ++++++++--------------
 .../apache/sis/storage/netcdf/GridResource.java    | 20 +-------------
 .../apache/sis/storage/netcdf/MetadataReader.java  |  6 ++---
 .../org/apache/sis/storage/netcdf/NetcdfStore.java | 19 ++++++++++---
 .../apache/sis/internal/netcdf/VariableTest.java   |  4 +--
 6 files changed, 42 insertions(+), 48 deletions(-)

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 129fa29..8368e87 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
@@ -55,6 +55,16 @@ import org.apache.sis.math.Vector;
  */
 public abstract class Grid extends NamedElement {
     /**
+     * Minimal number of dimension for accepting a variable as a coverage variable.
+     */
+    public static final int MIN_DIMENSION = 2;
+
+    /**
+     * Minimal number of cells along {@value #MIN_DIMENSION} dimensions for accepting a variable
as a coverage variable.
+     */
+    static final int MIN_SPAN = 2;
+
+    /**
      * The axes, created when first needed.
      * The ordering of axes is based on the order in which dimensions are declared for variables
using this grid.
      * This is not necessarily the same order than the order in which variables are listed
in the netCDF file.
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 087c651..9a2db4b 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
@@ -64,11 +64,6 @@ public abstract class Variable extends NamedElement {
     public static final Pattern TIME_UNIT_PATTERN = Pattern.compile("(.+)\\Wsince\\W(.+)",
Pattern.CASE_INSENSITIVE);
 
     /**
-     * Minimal number of dimension for accepting a variable as a coverage variable.
-     */
-    public static final int MIN_DIMENSION = 2;
-
-    /**
      * The unit of measurement, parsed from {@link #getUnitsString()} when first needed.
      * We do not try to parse the unit at construction time because this variable may be
      * never requested by the user.
@@ -231,29 +226,25 @@ public abstract class Variable extends NamedElement {
      * This method checks for the following conditions:
      *
      * <ul>
-     *   <li>Images require at least {@value #MIN_DIMENSION} dimensions of size equals
or greater
-     *       than {@code minLength}. They may have more dimensions, in which case a slice
will be
-     *       taken later.</li>
-     *   <li>Exclude axes. Axes are often already excluded by the above condition
-     *       because axis are usually 1-dimensional, but some axes are 2-dimensional
-     *       (e.g. a localization grid).</li>
-     *   <li>Excludes characters, strings and structures, which can not be easily
-     *       mapped to an image type. In addition, 2-dimensional character arrays
-     *       are often used for annotations and we don't want to confuse them
-     *       with images.</li>
+     *   <li>Images require at least {@value Grid#MIN_DIMENSION} dimensions of size
equals or greater than {@value Grid#MIN_SPAN}.
+     *       They may have more dimensions, in which case a slice will be taken later.</li>
+     *   <li>Exclude axes. Axes are often already excluded by the above condition because
axis are usually 1-dimensional,
+     *       but some axes are 2-dimensional (e.g. a localization grid).</li>
+     *   <li>Excludes characters, strings and structures, which can not be easily mapped
to an image type.
+     *       In addition, 2-dimensional character arrays are often used for annotations and
we do not want
+     *       to confuse them with images.</li>
      * </ul>
      *
-     * @param  minSpan  minimal span (in unit of grid cells) along the dimensions.
      * @return {@code true} if the variable can be considered a coverage.
      */
-    public final boolean isCoverage(final int minSpan) {
+    public final boolean isCoverage() {
         int numVectors = 0;                                     // Number of dimension having
more than 1 value.
         for (final int length : getShape()) {
-            if (Integer.toUnsignedLong(length) >= minSpan) {
+            if (Integer.toUnsignedLong(length) >= Grid.MIN_SPAN) {
                 numVectors++;
             }
         }
-        if (numVectors >= MIN_DIMENSION) {
+        if (numVectors >= Grid.MIN_DIMENSION) {
             final DataType dataType = getDataType();
             if (dataType.rasterDataType != DataBuffer.TYPE_UNDEFINED) {
                 return !isCoordinateSystemAxis();
@@ -297,7 +288,7 @@ public abstract class Variable extends NamedElement {
      * Values shall be handled as unsigned 32 bits integers.
      *
      * <p>In ISO 19123 terminology, this method returns the upper corner of the grid
envelope plus one.
-     * The lower corner is always (0, 0, …, 0). This method is used by {@link #isCoverage(int)}
method,
+     * The lower corner is always (0, 0, …, 0). This method is used by {@link #isCoverage()}
method,
      * or for building string representations of this variable.</p>
      *
      * @return the number of grid cells for each dimension, as unsigned integer in netCDF
order (reverse of "natural" order).
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
index 9d4f4bc..b59a7a8 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
@@ -17,7 +17,6 @@
 package org.apache.sis.storage.netcdf;
 
 import java.util.List;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.io.IOException;
 import java.nio.file.Path;
@@ -35,7 +34,6 @@ import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.coverage.grid.GridExtent;
 import org.apache.sis.coverage.grid.GridCoverage;
 import org.apache.sis.storage.DataStoreException;
-import org.apache.sis.storage.Resource;
 import org.apache.sis.measure.NumberRange;
 import org.apache.sis.util.Numbers;
 import org.apache.sis.util.resources.Vocabulary;
@@ -105,7 +103,7 @@ final class GridResource extends AbstractGridResource implements ResourceOnFileS
      * @param  grid     the grid geometry (size, CRS…) of the {@linkplain #data} cube.
      * @param  data     the variable providing actual data.
      */
-    private GridResource(final Decoder decoder, final Grid grid, final Variable data) throws
IOException, DataStoreException {
+    GridResource(final Decoder decoder, final Grid grid, final Variable data) throws IOException,
DataStoreException {
         super(decoder.listeners);
         this.data    = data;
         gridGeometry = grid.getGridGeometry(decoder);
@@ -114,22 +112,6 @@ final class GridResource extends AbstractGridResource implements ResourceOnFileS
     }
 
     /**
-     * Returns a list of all grid resources found in the netCDF file opened by the given
decoder.
-     * This method should be invoked only once and the result cached. The returned list is
modifiable;
-     * caller is free to add other elements.
-     */
-    static List<Resource> list(final Decoder decoder) throws IOException, DataStoreException
{
-        final List<Resource> resources = new ArrayList<>();
-        for (final Variable variable : decoder.getVariables()) {
-            final Grid grid = variable.getGridGeometry(decoder);
-            if (grid != null) {
-                resources.add(new GridResource(decoder, grid, variable));
-            }
-        }
-        return resources;
-    }
-
-    /**
      * Returns the variable name as an identifier of this resource.
      */
     @Override
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 4c13091..d8735a8 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
@@ -893,7 +893,7 @@ split:  while ((start = CharSequences.skipLeadingWhitespaces(value, start,
lengt
     private void addContentInfo() {
         final Map<List<String>, List<Variable>> contents = new HashMap<>(4);
         for (final Variable variable : decoder.getVariables()) {
-            if (variable.isCoverage(2)) {
+            if (variable.isCoverage()) {
                 final List<String> dimensions = Arrays.asList(variable.getGridDimensionNames());
                 CollectionsExt.addToMultiValuesMap(contents, dimensions, variable);
             }
@@ -1039,8 +1039,8 @@ split:  while ((start = CharSequences.skipLeadingWhitespaces(value,
start, lengt
          * is built from the netCDF CoordinateSystem objects.
          */
         for (final Grid cs : decoder.getGridGeometries()) {
-            if (cs.getSourceDimensions() >= Variable.MIN_DIMENSION &&
-                cs.getTargetDimensions() >= Variable.MIN_DIMENSION)
+            if (cs.getSourceDimensions() >= Grid.MIN_DIMENSION &&
+                cs.getTargetDimensions() >= Grid.MIN_DIMENSION)
             {
                 addSpatialRepresentationInfo(cs);
             }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
index e7838fa..2f18007 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
@@ -19,6 +19,7 @@ package org.apache.sis.storage.netcdf;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Collection;
 import org.opengis.util.NameSpace;
@@ -32,6 +33,8 @@ import org.apache.sis.storage.UnsupportedStorageException;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.Aggregate;
 import org.apache.sis.internal.netcdf.Decoder;
+import org.apache.sis.internal.netcdf.Grid;
+import org.apache.sis.internal.netcdf.Variable;
 import org.apache.sis.internal.storage.URIDataStore;
 import org.apache.sis.internal.util.UnmodifiableArrayList;
 import org.apache.sis.setup.OptionKey;
@@ -199,10 +202,18 @@ public class NetcdfStore extends DataStore implements Aggregate {
     public synchronized Collection<Resource> components() throws DataStoreException
{
         if (components == null) try {
             Resource[] resources = decoder.getDiscreteSampling();
-            final List<Resource> list = GridResource.list(decoder);
-            if (!list.isEmpty()) {
-                list.addAll(UnmodifiableArrayList.wrap(resources));
-                resources = list.toArray(new Resource[list.size()]);
+            final List<Resource> grids = new ArrayList<>();
+            for (final Variable variable : decoder.getVariables()) {
+                if (variable.isCoverage()) {
+                    final Grid grid = variable.getGridGeometry(decoder);
+                    if (grid != null) {
+                        grids.add(new GridResource(decoder, grid, variable));
+                    }
+                }
+            }
+            if (!grids.isEmpty()) {
+                grids.addAll(UnmodifiableArrayList.wrap(resources));
+                resources = grids.toArray(new Resource[grids.size()]);
             }
             components = UnmodifiableArrayList.wrap(resources);
         } catch (IOException e) {
diff --git a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
index 8ebdb20..39a602b 100644
--- a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
+++ b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
@@ -91,7 +91,7 @@ public strictfp class VariableTest extends TestCase {
      *   <li>{@link Variable#getDataType()}</li>
      *   <li>{@link Variable#getShape()} length</li>
      *   <li>{@link Variable#isCoordinateSystemAxis()}</li>
-     *   <li>{@link Variable#isCoverage(int)}</li>
+     *   <li>{@link Variable#isCoverage()}</li>
      * </ul>
      *
      * @throws IOException if an I/O error occurred while opening the file.
@@ -128,7 +128,7 @@ public strictfp class VariableTest extends TestCase {
             assertEquals(name, expected[propertyIndex++], dataType);
             assertEquals(name, expected[propertyIndex++], variable.getShape().length);
             assertEquals(name, expected[propertyIndex++], variable.isCoordinateSystemAxis());
-            assertEquals(name, expected[propertyIndex++], variable.isCoverage(2));
+            assertEquals(name, expected[propertyIndex++], variable.isCoverage());
             assertEquals(0, propertyIndex % NUM_BASIC_PROPERTY_COLUMNS);            // Sanity
check for VariableTest itself.
         }
         assertEquals("Expected more variables.",


Mime
View raw message