You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2019/02/18 10:45:56 UTC
[sis] branch geoapi-4.0 updated: Move Variable.getRole()
implementation into Convention.roleOf(Variable).
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 13e0760 Move Variable.getRole() implementation into Convention.roleOf(Variable).
13e0760 is described below
commit 13e0760b4e58025224f58a9bc85f35dd89fae4b7
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Feb 18 11:45:22 2019 +0100
Move Variable.getRole() implementation into Convention.roleOf(Variable).
---
.../org/apache/sis/internal/netcdf/Convention.java | 43 +++++++++++++++++--
.../org/apache/sis/internal/netcdf/Variable.java | 49 ++++------------------
.../sis/internal/netcdf/impl/VariableInfo.java | 7 ++--
.../sis/internal/netcdf/ucar/VariableWrapper.java | 7 ++--
.../apache/sis/internal/netcdf/VariableTest.java | 5 ++-
5 files changed, 58 insertions(+), 53 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 76a80d6..b864ab5 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
@@ -17,6 +17,7 @@
package org.apache.sis.internal.netcdf;
import java.util.Iterator;
+import java.awt.image.DataBuffer;
import org.apache.sis.internal.referencing.LazySet;
import org.apache.sis.referencing.operation.transform.TransferFunction;
import org.apache.sis.measure.MeasurementRange;
@@ -35,6 +36,9 @@ import ucar.nc2.constants.CDM;
*
* <blockquote><pre>META-INF/services/org.apache.sis.internal.netcdf.Convention</pre></blockquote>
*
+ * This class does not encapsulate all conventions needed for understanding a netCDF file,
+ * but only conventions that are more likely to need to be overridden for some data producers.
+ *
* <p><b>This is an experimental class for internal usage only (for now).</b>
* The API of this class is likely to change in any future Apache SIS version.
* This class may become public (in a modified form) in the future if we gain
@@ -110,14 +114,47 @@ public class Convention {
}
/**
- * Returns the role of the given variable. In particular, this method shall return
- * {@link VariableRole#AXIS} if the given variable seems to be a coordinate system axis.
+ * Returns whether the given variable is used as a coordinate system axis, a coverage or something else.
+ * In particular this method shall return {@link VariableRole#AXIS} if the given variable seems to be a
+ * coordinate system axis instead than the actual data. By netCDF convention, coordinate system axes
+ * have the name of one of the dimensions defined in the netCDF header.
+ *
+ * <p>The default implementation returns {@link VariableRole#COVERAGE} if the given variable can be used
+ * for generating an image, by checking the following conditions:</p>
+ *
+ * <ul>
+ * <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 variable the variable for which to get the role, or {@code null}.
* @return role of the given variable, or {@code null} if the given variable was null.
*/
public VariableRole roleOf(final Variable variable) {
- return (variable != null) ? variable.getRole() : null;
+ if (variable == null) {
+ return null;
+ }
+ if (variable.isCoordinateSystemAxis()) {
+ return VariableRole.AXIS;
+ }
+ int numVectors = 0; // Number of dimension having more than 1 value.
+ for (final Dimension dimension : variable.getGridDimensions()) {
+ if (dimension.length() >= Grid.MIN_SPAN) {
+ numVectors++;
+ }
+ }
+ if (numVectors >= Grid.MIN_DIMENSION) {
+ final DataType dataType = variable.getDataType();
+ if (dataType.rasterDataType != DataBuffer.TYPE_UNDEFINED) {
+ return VariableRole.COVERAGE;
+ }
+ }
+ return VariableRole.OTHER;
}
/**
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 7b47574..c8b6b84 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
@@ -25,7 +25,6 @@ import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;
import java.io.IOException;
-import java.awt.image.DataBuffer;
import java.time.Instant;
import javax.measure.Unit;
import org.opengis.referencing.operation.Matrix;
@@ -282,48 +281,17 @@ public abstract class Variable extends NamedElement {
protected abstract boolean isUnlimited();
/**
- * Returns whether this variable is used as a coordinate system axis, a coverage or something else.
- * In particular this method shall return {@link VariableRole#AXIS} if this variable seems to be a
- * coordinate system axis instead than the actual data. By netCDF convention, coordinate system axes
- * have the name of one of the dimensions defined in the netCDF header.
+ * Returns whether this variable is used as a coordinate system axis.
+ * By netCDF convention, coordinate system axes have the name of one of the dimensions defined in the netCDF header.
*
- * <p>The default implementation returns {@link VariableRole#COVERAGE} if the given variable can be used
- * for generating an image, by checking the following conditions:</p>
+ * <p>This method has protected access because it should not be invoked directly. Code using variable role should
+ * invoke {@link Convention#roleOf(Variable)} instead, for allowing specialization by {@link Convention}.</p>
*
- * <ul>
- * <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>
- *
- * Subclasses shall override this method for checking the {@link VariableRole#AXIS} case before to delegate
- * to this method.
+ * @return whether this variable is a coordinate system axis.
*
- * <p>This method has protected access because it should not be invoked directly except in overridden methods.
- * Code using variable role should invoke {@link Convention#roleOf(Variable)} instead, for allowing specialization
- * by {@link Convention}.</p>
- *
- * @return the role of this variable.
+ * @see Convention#roleOf(Variable)
*/
- protected VariableRole getRole() {
- int numVectors = 0; // Number of dimension having more than 1 value.
- for (final Dimension dimension : getGridDimensions()) {
- if (dimension.length() >= Grid.MIN_SPAN) {
- numVectors++;
- }
- }
- if (numVectors >= Grid.MIN_DIMENSION) {
- final DataType dataType = getDataType();
- if (dataType.rasterDataType != DataBuffer.TYPE_UNDEFINED) {
- return VariableRole.COVERAGE;
- }
- }
- return VariableRole.OTHER;
- }
+ protected abstract boolean isCoordinateSystemAxis();
/**
* Returns the grid geometry for this variable, or {@code null} if this variable is not a data cube.
@@ -344,7 +312,8 @@ public abstract class Variable extends NamedElement {
* of the grid envelope plus one. The lower corner is always (0, 0, …, 0).
*
* <p>This information is used for completing ISO 19115 metadata, providing a default implementation of
- * {@link #getRole()} method, or for building string representation of this variable among others.</p>
+ * {@link Convention#roleOf(Variable)} method, or for building string representation of this variable
+ * among others.</p>
*
* @return all dimension of the grid, in netCDF order (reverse of "natural" order).
*
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
index 7057ebd..4c22f59 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
@@ -36,7 +36,6 @@ import org.apache.sis.internal.netcdf.DataType;
import org.apache.sis.internal.netcdf.Dimension;
import org.apache.sis.internal.netcdf.Grid;
import org.apache.sis.internal.netcdf.Variable;
-import org.apache.sis.internal.netcdf.VariableRole;
import org.apache.sis.internal.netcdf.Resources;
import org.apache.sis.internal.storage.io.ChannelDataInput;
import org.apache.sis.internal.storage.io.HyperRectangleReader;
@@ -442,13 +441,13 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
}
/**
- * Returns {@code AXIS} if this variable seems to be a coordinate system axis,
+ * Returns {@code true} if this variable seems to be a coordinate system axis,
* determined by comparing its name with the name of all dimensions in the netCDF file.
* Also determined by inspection of {@code "coordinates"} attribute on other variables.
*/
@Override
- protected VariableRole getRole() {
- return isCoordinateSystemAxis ? VariableRole.AXIS : super.getRole();
+ protected boolean isCoordinateSystemAxis() {
+ return isCoordinateSystemAxis;
}
/**
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 50c711f..6dad48e 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
@@ -41,7 +41,6 @@ import org.apache.sis.internal.netcdf.DataType;
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.netcdf.VariableRole;
import org.apache.sis.internal.util.UnmodifiableArrayList;
import org.apache.sis.util.logging.WarningListeners;
import org.apache.sis.storage.DataStoreException;
@@ -229,11 +228,11 @@ final class VariableWrapper extends Variable {
}
/**
- * Returns {@code AXIS} if this variable seems to be a coordinate system axis.
+ * Returns {@code true} if this variable seems to be a coordinate system axis.
*/
@Override
- protected VariableRole getRole() {
- return variable.isCoordinateVariable() ? VariableRole.AXIS : super.getRole();
+ protected boolean isCoordinateSystemAxis() {
+ return variable.isCoordinateVariable();
}
/**
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 e200ff0..b7710b8 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
@@ -93,7 +93,7 @@ public strictfp class VariableTest extends TestCase {
* <li>{@link Variable#getDescription()}</li>
* <li>{@link Variable#getDataType()}</li>
* <li>{@link Variable#getGridDimensions()} length</li>
- * <li>{@link Variable#getRole()}</li>
+ * <li>{@link Convention#roleOf(Variable)}</li>
* </ul>
*
* @throws IOException if an I/O error occurred while opening the file.
@@ -120,6 +120,7 @@ public strictfp class VariableTest extends TestCase {
* @param variables the variable for which to test properties.
*/
private static void assertBasicPropertiesEqual(final Object[] expected, final Variable[] variables) {
+ final Convention convention = new Convention();
int propertyIndex = 0;
for (final Variable variable : variables) {
final String name = variable.getName();
@@ -129,7 +130,7 @@ public strictfp class VariableTest extends TestCase {
assertEquals(name, expected[propertyIndex++], variable.getDescription());
assertEquals(name, expected[propertyIndex++], dataType);
assertEquals(name, expected[propertyIndex++], variable.getGridDimensions().size());
- assertEquals(name, expected[propertyIndex++], variable.getRole());
+ assertEquals(name, expected[propertyIndex++], convention.roleOf(variable));
assertEquals(0, propertyIndex % NUM_BASIC_PROPERTY_COLUMNS); // Sanity check for VariableTest itself.
}
assertEquals("Expected more variables.",