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 2020/11/30 17:17:15 UTC
[sis] branch geoapi-4.0 updated (c0bf843 -> da00b49)
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a change to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git.
from c0bf843 Rename `getPropertyValue(String, Object)` as `getValueOrFallback(…)` for avoiding dangerous similarity with `setPropertyValue(…)`. Before this change, the two methods differed only by one letter ("g" versus "s"), which is a risk of introducing bugs by typos. The new name is also more readable and more consistent with standard Java practice such as `java.util.Map.getOrDefault(…)`.
new 84e6e56 Avoid duplication in netCDF variables and attributes when a lower-case name has been generated in addition to the original name.
new 98e07c2 More robust handling of map projection parameters in netCDF file: - If parameters are provided as strings instead than numbers, parse them. - Report unused values for allowing user to check if an important parameter has been ignored.
new da00b49 Always consider a variable as an axis if the "_CoordinateAxisType" attribute is present.
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../apache/sis/internal/util/CollectionsExt.java | 7 ++-
.../org/apache/sis/internal/netcdf/Convention.java | 7 ++-
.../apache/sis/internal/netcdf/GridMapping.java | 61 +++++++++++++++++--
.../org/apache/sis/internal/netcdf/Resources.java | 5 ++
.../sis/internal/netcdf/Resources.properties | 1 +
.../sis/internal/netcdf/Resources_fr.properties | 1 +
.../sis/internal/netcdf/impl/ChannelDecoder.java | 71 ++++++++++++++++------
.../sis/internal/netcdf/impl/VariableInfo.java | 45 ++++++++++----
.../org/apache/sis/internal/netcdf/GridTest.java | 21 ++++++-
.../apache/sis/internal/netcdf/VariableTest.java | 11 +---
.../sis/internal/netcdf/impl/GridInfoTest.java | 9 ++-
.../sis/internal/netcdf/impl/VariableInfoTest.java | 3 +-
12 files changed, 187 insertions(+), 55 deletions(-)
[sis] 02/03: More robust handling of map projection parameters in
netCDF file: - If parameters are provided as strings instead than numbers,
parse them. - Report unused values for allowing user to check if an
important parameter has been ignored.
Posted by de...@apache.org.
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 98e07c2b5e9817b81c86e295c1221762c5e6fd18
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Nov 30 16:58:02 2020 +0100
More robust handling of map projection parameters in netCDF file:
- If parameters are provided as strings instead than numbers, parse them.
- Report unused values for allowing user to check if an important parameter has been ignored.
---
.../org/apache/sis/internal/netcdf/Convention.java | 7 ++-
.../apache/sis/internal/netcdf/GridMapping.java | 61 +++++++++++++++++++---
.../org/apache/sis/internal/netcdf/Resources.java | 5 ++
.../sis/internal/netcdf/Resources.properties | 1 +
.../sis/internal/netcdf/Resources_fr.properties | 1 +
5 files changed, 68 insertions(+), 7 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 44b3fdf..3ece160 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
@@ -394,6 +394,11 @@ public class Convention {
protected static final String LONGITUDE_OF_PRIME_MERIDIAN = "longitude_of_prime_meridian";
/**
+ * Suffix of all attribute names used for CRS component names.
+ */
+ static final String NAME_SUFFIX = "_name";
+
+ /**
* The {@value} attribute name from CF-convention, defined here because not yet provided in {@link CF}.
* Associated value shall be an instance of {@link String}. This field may be removed in a future SIS
* version if this constant become defined in {@link ucar.nc2.constants}.
@@ -492,7 +497,7 @@ public class Convention {
continue;
}
default: {
- if (ln.endsWith("_name")) {
+ if (ln.endsWith(NAME_SUFFIX)) {
value = node.getAttributeAsString(name);
if (value == null) continue;
}
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 6c52aa5..5c7f2fe 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
@@ -19,11 +19,13 @@ package org.apache.sis.internal.netcdf;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.text.ParseException;
import org.opengis.util.FactoryException;
+import org.opengis.parameter.ParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.parameter.ParameterNotFoundException;
import org.opengis.referencing.IdentifiedObject;
@@ -64,6 +66,7 @@ import org.apache.sis.util.resources.Vocabulary;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.CharSequences;
import org.apache.sis.util.ArraysExt;
+import org.apache.sis.util.Numbers;
import org.apache.sis.io.wkt.WKTFormat;
import org.apache.sis.io.wkt.Warnings;
import org.apache.sis.measure.Units;
@@ -77,7 +80,7 @@ import ucar.nc2.constants.CF;
* which creates Coordinate Reference Systems by inspecting coordinate system axes.
*
* @author Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
*
* @see <a href="http://cfconventions.org/cf-conventions/cf-conventions.html#grid-mappings-and-projections">CF-conventions</a>
* @see <a href="https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/reference/StandardCoordinateTransforms.html">UCAR projections</a>
@@ -184,6 +187,8 @@ final class GridMapping {
* be something else (the data variable itself, or a group, <i>etc.</i>). That node, together with the attributes to
* be parsed, depends on the {@link Convention} instance.
*
+ * @param node the dummy variable on which attributes are defined for projection parameters.
+ *
* @see <a href="http://cfconventions.org/cf-conventions/cf-conventions.html#grid-mappings-and-projections">CF-conventions</a>
*/
private static GridMapping parseProjectionParameters(final Node node) {
@@ -204,12 +209,36 @@ final class GridMapping {
final CoordinateOperationFactory opFactory = node.decoder.getCoordinateOperationFactory();
final OperationMethod method = opFactory.getOperationMethod((String) definition.remove(CF.GRID_MAPPING_NAME));
final ParameterValueGroup parameters = method.getParameters().createValue();
- for (final Map.Entry<String,Object> entry : definition.entrySet()) {
+ for (final Iterator<Map.Entry<String,Object>> it = definition.entrySet().iterator(); it.hasNext();) {
+ final Map.Entry<String,Object> entry = it.next();
final String name = entry.getKey();
final Object value = entry.getValue();
- if (value instanceof Number || value instanceof double[] || value instanceof float[]) try {
- parameters.parameter(name).setValue(value);
- } catch (IllegalArgumentException ex) {
+ try {
+ if (value instanceof Number || value instanceof double[] || value instanceof float[]) {
+ it.remove();
+ parameters.parameter(name).setValue(value);
+ } else if (value instanceof String && !name.endsWith(Convention.NAME_SUFFIX)) {
+ /*
+ * In principle we should ignore non-numeric parameters. But in practice, some badly encoded
+ * netCDF files store parameters as strings instead than numbers. If the parameter name is
+ * known to the projection method, try to parse the character string.
+ */
+ final ParameterValue<?> parameter;
+ try {
+ parameter = parameters.parameter(name);
+ } catch (IllegalArgumentException e) {
+ continue;
+ }
+ final Class<?> type = parameter.getDescriptor().getValueClass();
+ if (Numbers.isNumber(type)) {
+ it.remove();
+ parameter.setValue(Double.parseDouble((String) value));
+ } else if (Numbers.isNumber(type.getComponentType())) {
+ it.remove();
+ parameter.setValue(parseDoubles((String) value), null);
+ }
+ }
+ } catch (IllegalArgumentException ex) { // Includes NumberFormatException.
warning(node, ex, Resources.Keys.CanNotSetProjectionParameter_5, node.decoder.getFilename(),
node.getName(), name, value, ex.getLocalizedMessage());
}
@@ -236,6 +265,14 @@ final class GridMapping {
crs = p;
}
/*
+ * Report all projection parameters that have not been used. If the map is not rendered
+ * at expected location, it may be because we have ignored some important parameters.
+ */
+ if (!definition.isEmpty()) {
+ warning(node, null, Resources.Keys.UnknownProjectionParameters_2,
+ node.decoder.getFilename(), String.join(", ", definition.keySet()));
+ }
+ /*
* Build the "grid to CRS" if present. This is not defined by CF-convention,
* but may be present in some non-CF conventions.
*/
@@ -250,6 +287,9 @@ final class GridMapping {
/**
* Creates the geographic CRS from axis length specified in the given map projection parameters.
* The returned CRS will always have (latitude, longitude) axes in that order and in degrees.
+ *
+ * @param parameters parameters from which to get ellipsoid axis lengths. Will not be modified.
+ * @param definition map from which to get element names. Elements used will be removed.
*/
private static GeographicCRS createBaseCRS(final Decoder decoder, final ParameterValueGroup parameters,
final Map<String,Object> definition, final Object greenwichLongitude) throws FactoryException
@@ -369,7 +409,7 @@ final class GridMapping {
}
if (gtr != null) {
message = Resources.Keys.CanNotCreateGridGeometry_3;
- final double[] c = CharSequences.parseDoubles(gtr, ' ');
+ final double[] c = parseDoubles(gtr);
if (c.length == 6) {
gridToCRS = new AffineTransform2D(c[1], c[4], c[2], c[5], c[0], c[3]); // X_DIMENSION, Y_DIMENSION
} else {
@@ -385,6 +425,15 @@ final class GridMapping {
}
/**
+ * Parses a comma-separated or space-separated array of numbers.
+ *
+ * @throws NumberFormatException if at least one number can not be parsed.
+ */
+ private static double[] parseDoubles(final String values) {
+ return CharSequences.parseDoubles(values.replace(',', ' '), ' ');
+ }
+
+ /**
* Tries to parse the Coordinate Reference System using ESRI conventions or other non-CF conventions.
* This method is invoked as a fallback if {@link #parseGeoTransform(Node)} found no grid geometry.
*
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
index 55e9ba0..4a8bfa8 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
@@ -175,6 +175,11 @@ public final class Resources extends IndexedResourceBundle {
public static final short UnexpectedDimensionForVariable_4 = 2;
/**
+ * Unknown projection parameters in file “{0}”: {1}
+ */
+ public static final short UnknownProjectionParameters_2 = 25;
+
+ /**
* Variable “{1}” in file “{0}” has {2,number} dimensions but only {3,number} can be associated
* to a coordinate reference system.
*/
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
index df38cdb..931f669 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
@@ -41,5 +41,6 @@ MissingVariableAttribute_3 = Missing attribute \u201c{2}\u201d on the \u2
ResamplingIntervalNotFound_2 = Variable \u201c{1}\u201d or netCDF file \u201c{0}\u201d has a different size than its coordinate system, but no resampling interval is specified.
UnexpectedAxisCount_4 = Reference system of type \u2018{1}\u2019 can not have {2}\u00a0axes. The axes found in the \u201c{0}\u201d netCDF file are: {3}.
UnexpectedDimensionForVariable_4 = Variable \u201c{1}\u201d in file \u201c{0}\u201d has a dimension \u201c{3}\u201d while we expected \u201c{2}\u201d.
+UnknownProjectionParameters_2 = Unknown projection parameters in file \u201c{0}\u201d: {1}
UnmappedDimensions_4 = Variable \u201c{1}\u201d in file \u201c{0}\u201d has {2,number} dimensions but only {3,number} can be associated to a coordinate reference system.
UnsupportedDataType_3 = NetCDF file \u201c{0}\u201d uses unsupported data type {2} for variable \u201c{1}\u201d.
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
index ef58d0b..dfb79b6 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
@@ -46,5 +46,6 @@ MissingVariableAttribute_3 = Il manque l\u2019attribut \u201c{2}\u201d su
ResamplingIntervalNotFound_2 = La variable \u00ab\u202f{1}\u202f\u00bb du fichier netCDF \u00ab\u202f{0}\u202f\u00bb a une taille diff\u00e9rente de celle de son syst\u00e8me de coordonn\u00e9es, mais l\u2019intervalle d\u2019\u00e9chantillonnage n\u2019a pas \u00e9t\u00e9 sp\u00e9cifi\u00e9.
UnexpectedAxisCount_4 = Les syst\u00e8mes de r\u00e9f\u00e9rence de type \u2018{1}\u2019 ne peuvent pas avoir {2}\u00a0axes. Les axes trouv\u00e9s dans le fichier netCDF \u00ab\u202f{0}\u202f\u00bb sont\u00a0: {3}.
UnexpectedDimensionForVariable_4 = La variable \u00ab\u202f{1}\u202f\u00bb dans le fichier \u00ab\u202f{0}\u202f\u00bb a une dimension \u00ab\u202f{3}\u202f\u00bb alors qu\u2019on attendait \u00ab\u202f{2}\u202f\u00bb.
+UnknownProjectionParameters_2 = Param\u00e8tres de projection inconnus dans le fichier \u00ab\u202f{0}\u202f\u00bb\u00a0: {1}
UnmappedDimensions_4 = La variable \u00ab\u202f{1}\u202f\u00bb dans le fichier \u00ab\u202f{0}\u202f\u00bb a {2,number} dimensions mais seulement {3,number} peuvent \u00eatre associ\u00e9es \u00e0 un syst\u00e8me de r\u00e9f\u00e9rence des coordonn\u00e9es.
UnsupportedDataType_3 = Le fichier netCDF \u00ab\u202f{0}\u202f\u00bb utilise un type de donn\u00e9es non-support\u00e9 {2} pour la variable \u00ab\u202f{1}\u202f\u00bb.
[sis] 01/03: Avoid duplication in netCDF variables and attributes
when a lower-case name has been generated in addition to the original name.
Posted by de...@apache.org.
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 84e6e5626a5e7340eb05e85e040c293c1cd71959
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Nov 30 13:35:17 2020 +0100
Avoid duplication in netCDF variables and attributes when a lower-case name has been generated in addition to the original name.
---
.../apache/sis/internal/util/CollectionsExt.java | 7 ++-
.../sis/internal/netcdf/impl/ChannelDecoder.java | 65 +++++++++++++++++-----
.../sis/internal/netcdf/impl/VariableInfo.java | 37 ++++++++----
3 files changed, 81 insertions(+), 28 deletions(-)
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
index 7a8a982..3d4810c 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
@@ -896,6 +896,9 @@ public final class CollectionsExt extends Static {
* <p>Code searching in the returned map shall ask for the original (non lower-case) name
* <strong>before</strong> to ask for the lower-cases version of that name.</p>
*
+ * <p>Iteration order in map entries is the same than iteration order in the given collection.
+ * If lower-case names have been generated, they appear immediately after the original names.</p>
+ *
* @param <E> the type of elements.
* @param entries the entries to store in the map, or {@code null} if none.
* @param namesLocale the locale to use for creating the "all lower cases" names.
@@ -905,10 +908,10 @@ public final class CollectionsExt extends Static {
public static <E> Map<String,E> toCaseInsensitiveNameMap(
final Collection<Map.Entry<String,E>> entries, final Locale namesLocale)
{
- if (entries == null) {
+ if (entries == null || entries.isEmpty()) {
return Collections.emptyMap();
}
- final Map<String,E> map = new HashMap<>(hashMapCapacity(entries.size()));
+ final Map<String,E> map = new LinkedHashMap<>(hashMapCapacity(entries.size()));
final Set<String> generated = new HashSet<>();
for (final Map.Entry<String, ? extends E> entry : entries) {
final String name = entry.getKey();
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
index 9f7d529..5570ebf 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
@@ -59,6 +59,7 @@ import org.apache.sis.storage.event.StoreListeners;
import org.apache.sis.util.ArraysExt;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.resources.Vocabulary;
+import org.apache.sis.util.collection.Containers;
import org.apache.sis.util.collection.TreeTable;
import org.apache.sis.util.collection.TableColumn;
import org.apache.sis.setup.GeometryLibrary;
@@ -73,7 +74,7 @@ import org.apache.sis.math.Vector;
*
* @author Johann Sorel (Geomatys)
* @author Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
*
* @see <a href="http://portal.opengeospatial.org/files/?artifact_id=43734">NetCDF Classic and 64-bit Offset Format (1.0)</a>
*
@@ -179,7 +180,11 @@ public final class ChannelDecoder extends Decoder {
final VariableInfo[] variables;
/**
- * Same as {@link #variables}, but as a map for faster search.
+ * Contains all {@link #variables}, but as a map for faster lookup by name. The same {@link VariableInfo}
+ * instance may be repeated in two entries if the original variable name contains upper case letters.
+ * In such case, the value is repeated and associated to a key in all lower case key letters.
+ *
+ * @see #findVariable(String)
*/
private final Map<String,VariableInfo> variableMap;
@@ -193,6 +198,15 @@ public final class ChannelDecoder extends Decoder {
private final Map<String,Object> attributeMap;
/**
+ * Names of attributes. This is {@code attributeMap.keySet()} unless some attributes have a name
+ * containing upper case letters. In such case a separated set is created for avoiding duplicated
+ * names (the name with upper case letters + the name in all lower case letters).
+ *
+ * @see #getAttributeNames()
+ */
+ private final Set<String> attributeNames;
+
+ /**
* All dimensions in the netCDF files.
*
* @see #readDimensions(int)
@@ -257,9 +271,9 @@ public final class ChannelDecoder extends Decoder {
* Read the dimension, attribute and variable declarations. We expect exactly 3 lists,
* where any of them can be flagged as absent by a long (64 bits) 0.
*/
- DimensionInfo[] dimensions = null;
- VariableInfo[] variables = null;
- Map<String,Object> attributes = Collections.emptyMap();
+ DimensionInfo[] dimensions = null;
+ VariableInfo[] variables = null;
+ List<Map.Entry<String,Object>> attributes = Collections.emptyList();
for (int i=0; i<3; i++) {
final long tn = input.readLong(); // Combination of tag and nelems
if (tn != 0) {
@@ -278,7 +292,8 @@ public final class ChannelDecoder extends Decoder {
}
}
}
- this.attributeMap = attributes;
+ attributeMap = CollectionsExt.toCaseInsensitiveNameMap(attributes, NAME_LOCALE);
+ attributeNames = attributeNames(attributes, attributeMap);
if (variables != null) {
this.variables = variables;
this.variableMap = toCaseInsensitiveNameMap(variables);
@@ -541,7 +556,7 @@ public final class ChannelDecoder extends Decoder {
*
* @param nelems the number of attributes to read.
*/
- private Map<String,Object> readAttributes(int nelems) throws IOException, DataStoreException {
+ private List<Map.Entry<String,Object>> readAttributes(int nelems) throws IOException, DataStoreException {
final List<Map.Entry<String,Object>> attributes = new ArrayList<>(nelems);
while (--nelems >= 0) {
final String name = readName();
@@ -555,7 +570,7 @@ public final class ChannelDecoder extends Decoder {
}
}
}
- return CollectionsExt.toCaseInsensitiveNameMap(attributes, NAME_LOCALE);
+ return attributes;
}
/**
@@ -602,7 +617,7 @@ public final class ChannelDecoder extends Decoder {
* Following block is almost a copy-and-paste of similar block in the contructor,
* but with less cases in the "switch" statements.
*/
- Map<String,Object> attributes = Collections.emptyMap();
+ List<Map.Entry<String,Object>> attributes = Collections.emptyList();
final long tn = input.readLong();
if (tn != 0) {
final int tag = (int) (tn >>> Integer.SIZE);
@@ -619,7 +634,8 @@ public final class ChannelDecoder extends Decoder {
default: throw malformedHeader();
}
}
- variables[j] = new VariableInfo(this, input, name, varDims, attributes,
+ final Map<String,Object> map = CollectionsExt.toCaseInsensitiveNameMap(attributes, NAME_LOCALE);
+ variables[j] = new VariableInfo(this, input, name, varDims, map, attributeNames(attributes, map),
DataType.valueOf(input.readInt()), input.readInt(), readOffset());
}
/*
@@ -632,6 +648,24 @@ public final class ChannelDecoder extends Decoder {
return variables;
}
+ /**
+ * Returns the keys of {@code attributeMap} without the duplicated values caused by the change of name case.
+ * For example if an attribute {@code "Foo"} exists and a {@code "foo"} key has been generated for enabling
+ * case-insensitive search, only the {@code "Foo"} name is added in the returned set.
+ *
+ * @param attributes the attributes returned by {@link #readAttributes(int)}.
+ * @param attributeMap the map created by {@link CollectionsExt#toCaseInsensitiveNameMap(Collection, Locale)}.
+ * @return {@code attributes.keySet()} without duplicated keys.
+ */
+ private static Set<String> attributeNames(final List<Map.Entry<String,Object>> attributes, final Map<String,?> attributeMap) {
+ if (attributes.size() >= attributeMap.size()) {
+ return Collections.unmodifiableSet(attributeMap.keySet());
+ }
+ final Set<String> attributeNames = new LinkedHashSet<>(Containers.hashMapCapacity(attributes.size()));
+ attributes.forEach((e) -> attributeNames.add(e.getKey()));
+ return attributeNames;
+ }
+
// --------------------------------------------------------------------------------------------
@@ -788,12 +822,13 @@ public final class ChannelDecoder extends Decoder {
/**
* Returns the names of all global attributes found in the file.
+ * The returned set is unmodifiable.
*
* @return names of all global attributes in the file.
*/
@Override
public Collection<String> getAttributeNames() {
- return Collections.unmodifiableSet(attributeMap.keySet());
+ return Collections.unmodifiableSet(attributeNames);
}
/**
@@ -1027,12 +1062,12 @@ nextVar: for (final VariableInfo variable : variables) {
*/
@Override
public void addAttributesTo(final TreeTable.Node root) {
- for (final Map.Entry<String,VariableInfo> entry : variableMap.entrySet()) {
+ for (final VariableInfo variable : variables) {
final TreeTable.Node node = root.newChild();
- node.setValue(TableColumn.NAME, entry.getKey());
- entry.getValue().addAttributesTo(node);
+ node.setValue(TableColumn.NAME, variable.getName());
+ variable.addAttributesTo(node);
}
- VariableInfo.addAttributesTo(root, attributeMap);
+ VariableInfo.addAttributesTo(root, attributeNames, attributeMap);
}
/**
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 fc30c71..bd06b4c 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
@@ -141,6 +141,15 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
private final Map<String,Object> attributes;
/**
+ * Names of attributes. This is {@code attributeMap.keySet()} unless some attributes have a name
+ * containing upper case letters. In such case a separated set is used for avoiding duplicated
+ * names (the name with upper case letters + the name in all lower case letters).
+ *
+ * @see #getAttributeNames()
+ */
+ private final Set<String> attributeNames;
+
+ /**
* The netCDF type of data, or {@code null} if unknown.
*
* @see #getDataType()
@@ -183,14 +192,16 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
final String name,
final DimensionInfo[] dimensions,
final Map<String,Object> attributes,
+ final Set<String> attributeNames,
DataType dataType,
final int size,
final long offset) throws DataStoreContentException
{
super(decoder);
- this.name = name;
- this.dimensions = dimensions;
- this.attributes = attributes;
+ this.name = name;
+ this.dimensions = dimensions;
+ this.attributes = attributes;
+ this.attributeNames = attributeNames;
final Object isUnsigned = getAttributeValue(CDM.UNSIGNED, "_unsigned");
if (isUnsigned instanceof String) {
dataType = dataType.unsigned(Boolean.valueOf((String) isUnsigned));
@@ -501,12 +512,13 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
/**
* Returns the names of all attributes associated to this variable.
+ * The returned set is unmodifiable.
*
* @return names of all attributes associated to this variable.
*/
@Override
public Collection<String> getAttributeNames() {
- return Collections.unmodifiableSet(attributes.keySet());
+ return Collections.unmodifiableSet(attributeNames);
}
/**
@@ -560,7 +572,7 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
* @param branch where to add new nodes for the attributes of this variable.
*/
final void addAttributesTo(final TreeTable.Node branch) {
- addAttributesTo(branch, attributes);
+ addAttributesTo(branch, attributeNames, attributes);
}
/**
@@ -568,14 +580,17 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
* returned by {@link org.apache.sis.storage.netcdf.NetcdfStore#getNativeMetadata()}.
* This tree is for information purpose only.
*
- * @param branch where to add new nodes for the given attributes.
- * @param attributes the attributes to add to the specified branch.
+ * @param branch where to add new nodes for the given attributes.
+ * @param attributeNames name of attribute to add to the specified branch.
+ * @param attributes the attributes to add to the specified branch.
*/
- static void addAttributesTo(final TreeTable.Node branch, final Map<String,Object> attributes) {
- for (final Map.Entry<String,Object> entry : attributes.entrySet()) {
+ static void addAttributesTo(final TreeTable.Node branch,
+ final Set<String> attributeNames, final Map<String,Object> attributes)
+ {
+ for (final String name : attributeNames) {
final TreeTable.Node node = branch.newChild();
- node.setValue(TableColumn.NAME, entry.getKey());
- Object value = entry.getValue();
+ node.setValue(TableColumn.NAME, name);
+ Object value = attributes.get(name);
if (value != null) {
if (value instanceof Vector) {
value = ((Vector) value).toArray();
[sis] 03/03: Always consider a variable as an axis if the
"_CoordinateAxisType" attribute is present.
Posted by de...@apache.org.
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 da00b49791b40f8400e47ba12032e9b85165642d
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Nov 30 18:15:28 2020 +0100
Always consider a variable as an axis if the "_CoordinateAxisType" attribute is present.
---
.../sis/internal/netcdf/impl/ChannelDecoder.java | 6 +++---
.../sis/internal/netcdf/impl/VariableInfo.java | 8 +++++++-
.../org/apache/sis/internal/netcdf/GridTest.java | 21 ++++++++++++++++++---
.../apache/sis/internal/netcdf/VariableTest.java | 11 +----------
.../sis/internal/netcdf/impl/GridInfoTest.java | 9 ++++++++-
.../sis/internal/netcdf/impl/VariableInfoTest.java | 3 +--
6 files changed, 38 insertions(+), 20 deletions(-)
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
index 5570ebf..6452e53 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
@@ -936,10 +936,10 @@ public final class ChannelDecoder extends Decoder {
* @param names names of variables containing axis data, or {@code null} if none.
* @param axes where to add named variables.
* @param dimensions where to report all dimensions used by added axes.
- * @return whether {@code names} was non-null.
+ * @return whether {@code names} was non-null and non-empty.
*/
private boolean listAxes(final CharSequence[] names, final Set<VariableInfo> axes, final Set<DimensionInfo> dimensions) {
- if (names == null) {
+ if (names == null || names.length == 0) {
return false;
}
for (final CharSequence name : names) {
@@ -1002,7 +1002,7 @@ nextVar: for (final VariableInfo variable : variables) {
/*
* The axes can be inferred in two ways: if the variable contains a "coordinates" attribute,
* that attribute lists explicitly the variables to use as axes. Otherwise we have to infer
- * the axes from the variable dimensions, using the 'dimToVars' map computed at the beginning
+ * the axes from the variable dimensions, using the `dimToAxes` map computed at the beginning
* of this method. If and only if we can find all axes, we create the GridGeometryInfo.
* This is a "all or nothing" operation.
*/
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 bd06b4c..f6d4545 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
@@ -245,11 +245,17 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
}
}
/*
+ * According CF conventions, a variable is considered a coordinate system axis if it has the same name
+ * as its dimension. But the "_CoordinateAxisType" attribute is often used for making explicit that a
+ * variable is an axis. We check that case before to check variable name.
+ */
+ isCoordinateSystemAxis = (dimensions.length == 1 || dimensions.length == 2) && (getAxisType() != null);
+ /*
* If the "_CoordinateAliasForDimension" attribute is defined, then its value will be used
* instead of the variable name when determining if the variable is a coordinate system axis.
* "_CoordinateVariableAlias" seems to be a legacy attribute name for the same purpose.
*/
- if (dimensions.length == 1) {
+ if (!isCoordinateSystemAxis && dimensions.length == 1) {
Object value = getAttributeValue(_Coordinate.AliasForDimension, "_coordinatealiasfordimension");
if (value == null) {
value = getAttributeValue("_CoordinateVariableAlias", "_coordinatevariablealias");
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 ed35f9b..e681a20 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
@@ -36,13 +36,20 @@ import static org.apache.sis.test.TestUtilities.getSingleton;
* {@link #createDecoder(TestData)} method in order to test a different implementation.
*
* @author Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
* @since 0.3
* @module
*/
@DependsOn(VariableTest.class)
public strictfp class GridTest extends TestCase {
/**
+ * Whether the {@code "runtime"} variable in {@link TestData#NETCDF_4D_PROJECTED} is used as a target dimension
+ * for the {@code gridToCRS} transform. The UCAR library and Apache SIS implementation have different behavior
+ * regarding this dimension.
+ */
+ protected boolean includeRuntimeDimension;
+
+ /**
* Optionally filters out some grid geometries that shall be ignored by the tests.
* The default implementation returns the given array unmodified. This method is overridden by
* {@code GridGeometryInfoTest} in order to ignore one-dimensional coordinate systems created
@@ -67,9 +74,10 @@ public strictfp class GridTest extends TestCase {
assertEquals("getSourceDimensions()", 2, geometry.getSourceDimensions());
assertEquals("getTargetDimensions()", 2, geometry.getTargetDimensions());
+ final int n = includeRuntimeDimension ? 5 : 4;
geometry = getSingleton(filter(selectDataset(TestData.NETCDF_4D_PROJECTED).getGrids()));
assertEquals("getSourceDimensions()", 4, geometry.getSourceDimensions());
- assertEquals("getTargetDimensions()", 4, geometry.getTargetDimensions());
+ assertEquals("getTargetDimensions()", n, geometry.getTargetDimensions());
}
/**
@@ -106,7 +114,7 @@ public strictfp class GridTest extends TestCase {
@DependsOnMethod("testDimensions")
public void testAxes4D() throws IOException, DataStoreException {
final Axis[] axes = getSingleton(filter(selectDataset(TestData.NETCDF_4D_PROJECTED).getGrids())).getAxes(decoder());
- assertEquals(4, axes.length);
+ assertEquals(includeRuntimeDimension ? 5 : 4, axes.length);
final Axis x = axes[0];
final Axis y = axes[1];
final Axis z = axes[2];
@@ -126,6 +134,13 @@ public strictfp class GridTest extends TestCase {
assertEquals(19, y.getMainSize().getAsLong());
assertEquals( 4, z.getMainSize().getAsLong());
assertEquals( 1, t.getMainSize().getAsLong());
+
+ if (includeRuntimeDimension) {
+ final Axis r = axes[4];
+ assertEquals('t', r.abbreviation);
+ assertArrayEquals(new int[] {0}, r.gridDimensionIndices);
+ assertEquals(1, r.getMainSize().getAsLong());
+ }
}
/**
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 3b13e45..d05cf6b 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
@@ -52,18 +52,9 @@ public strictfp class VariableTest extends TestCase {
private static final int NUM_BASIC_PROPERTY_COLUMNS = 5;
/**
- * Whether the {@code "runtime"} variable in {@link TestData#NETCDF_4D_PROJECTED} is considered an axis or not.
- * The UCAR library considers it as an axis because it has an {@code "_CoordinateAxisType"} attribute.
- * Apache SIS does not consider it as an axis because that variable does not match any dimension and is not used
- * in any other variable.
- */
- protected boolean isRuntimeAnAxis;
-
- /**
* Creates a new test.
*/
public VariableTest() {
- isRuntimeAnAxis = true;
}
/**
@@ -109,7 +100,7 @@ public strictfp class VariableTest extends TestCase {
"y0", "projection_y_coordinate", DataType.FLOAT, 1, VariableRole.AXIS,
"z0", "Flight levels in 100s of feet", DataType.FLOAT, 1, VariableRole.AXIS,
"time", "Data time", DataType.DOUBLE, 1, VariableRole.AXIS,
- "runtime", "Data generation time", DataType.DOUBLE, 1, isRuntimeAnAxis ? VariableRole.AXIS : VariableRole.OTHER,
+ "runtime", "Data generation time", DataType.DOUBLE, 1, VariableRole.AXIS,
"CIP", "Current Icing Product", DataType.FLOAT, 4, VariableRole.COVERAGE
}, getVariablesCIP(selectDataset(TestData.NETCDF_4D_PROJECTED)));
}
diff --git a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/GridInfoTest.java b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/GridInfoTest.java
index 1ae3598..3ed8762 100644
--- a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/GridInfoTest.java
+++ b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/GridInfoTest.java
@@ -34,13 +34,20 @@ import org.opengis.test.dataset.TestData;
* passed.
*
* @author Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
* @since 0.3
* @module
*/
@DependsOn({VariableInfoTest.class, GridTest.class})
public final strictfp class GridInfoTest extends GridTest {
/**
+ * Creates a new test.
+ */
+ public GridInfoTest() {
+ includeRuntimeDimension = true;
+ }
+
+ /**
* Creates a new decoder for the specified dataset.
*
* @return the decoder for the specified dataset.
diff --git a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
index 4bc4e4d..759ea96 100644
--- a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
+++ b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
@@ -30,7 +30,7 @@ import org.opengis.test.dataset.TestData;
* passed.
*
* @author Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
* @since 0.3
* @module
*/
@@ -40,7 +40,6 @@ public final strictfp class VariableInfoTest extends VariableTest {
* Creates a new test.
*/
public VariableInfoTest() {
- isRuntimeAnAxis = false;
}
/**