sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Add unit of measurements in the FeatureType built from a netCDF file.
Date Fri, 23 Oct 2020 22:23:47 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

commit 7cfb52240db4acf880a5450871eeaa56f41f6b52
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Oct 23 23:31:35 2020 +0200

    Add unit of measurements in the FeatureType built from a netCDF file.
---
 .../sis/feature/builder/AttributeTypeBuilder.java  | 31 +++++++++++++++++++---
 .../sis/internal/feature/AttributeConvention.java  | 17 ++++++++++++
 .../org/apache/sis/internal/netcdf/FeatureSet.java |  5 ++++
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java
b/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java
index 3b5f450..d77ca7e 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java
@@ -24,6 +24,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Objects;
 import java.lang.reflect.Array;
+import javax.measure.Unit;
 import org.opengis.util.GenericName;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.feature.DefaultAttributeType;
@@ -357,7 +358,6 @@ public final class AttributeTypeBuilder<V> extends PropertyTypeBuilder
{
      * @return {@code this} for allowing method calls chaining.
      *
      * @see #characteristics()
-     * @see AttributeConvention#VALID_VALUES_CHARACTERISTIC
      */
     @SafeVarargs
     public final AttributeTypeBuilder<V> setValidValues(final V... values) {
@@ -387,13 +387,39 @@ public final class AttributeTypeBuilder<V> extends PropertyTypeBuilder
{
      * @return {@code this} for allowing method calls chaining.
      *
      * @see #characteristics()
-     * @see AttributeConvention#MAXIMAL_LENGTH_CHARACTERISTIC
      */
     public AttributeTypeBuilder<V> setMaximalLength(final Integer length) {
         return setCharacteristic(AttributeConvention.MAXIMAL_LENGTH_CHARACTERISTIC, Integer.class,
length);
     }
 
     /**
+     * Returns the unit of measurement associated to attribute values.
+     * This convenience method returns the value of the characteristic set by {@link #setUnit(Unit)}.
+     *
+     * @return the unit of measurement associated to attribute values, or {@code null}.
+     */
+    public Unit<?> getUnit() {
+        return (Unit<?>) getCharacteristic(AttributeConvention.UNIT_CHARACTERISTIC);
+    }
+
+    /**
+     * Sets the unit of measurement that characterizes the values of this attribute.
+     * While this characteristic can be applied to any kind of attribute, it is meaningful
+     * only with numeric values.
+     *
+     * <p>This is a convenience method for {@link #addCharacteristic(Class)} with a
value
+     * of type {@link Unit} and a conventional name.</p>
+     *
+     * @param  unit  unit of measurement associated to attribute values, or {@code null}.
+     * @return {@code this} for allowing method calls chaining.
+     *
+     * @see #characteristics()
+     */
+    public AttributeTypeBuilder<V> setUnit(final Unit<?> unit) {
+        return setCharacteristic(AttributeConvention.UNIT_CHARACTERISTIC, Unit.class, unit);
+    }
+
+    /**
      * Returns the coordinate reference system associated to attribute values.
      * This convenience method returns the value of the characteristic set by {@link #setCRS(CoordinateReferenceSystem)}.
      *
@@ -415,7 +441,6 @@ public final class AttributeTypeBuilder<V> extends PropertyTypeBuilder
{
      * @return {@code this} for allowing method calls chaining.
      *
      * @see #characteristics()
-     * @see AttributeConvention#CRS_CHARACTERISTIC
      */
     public AttributeTypeBuilder<V> setCRS(final CoordinateReferenceSystem crs) {
         return setCharacteristic(AttributeConvention.CRS_CHARACTERISTIC, CoordinateReferenceSystem.class,
crs);
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
index 9756f50..5efadc1 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
@@ -145,6 +145,22 @@ public final class AttributeConvention extends Static {
     public static final ScopedName CRS_CHARACTERISTIC;
 
     /**
+     * Conventional name for fetching the unit of measurement of a property.
+     * This characteristic is typically an entry in the map returned by a call to the
+     * {@link org.apache.sis.feature.DefaultAttributeType#characteristics()} method
+     * on any attribute of numeric type.
+     *
+     * <p>While it is technically possible to have different units of measurement for
the same property
+     * on different feature instances, in most cases the unit is the same for all feature
instances.
+     * In such cases, the unit can be specified only once as the
+     * {@linkplain org.apache.sis.feature.DefaultAttributeType#getDefaultValue() default
value}.</p>
+     *
+     * <p>The {@linkplain org.apache.sis.feature.DefaultAttributeType#getValueClass()
value class} should be
+     * {@link javax.measure.Unit}.</p>
+     */
+    public static final ScopedName UNIT_CHARACTERISTIC;
+
+    /**
      * Conventional name for fetching the maximal length of string values.
      * The maximal length is stored as the
      * {@linkplain org.apache.sis.feature.DefaultAttributeType#getDefaultValue() default
value} of the
@@ -172,6 +188,7 @@ public final class AttributeConvention extends Static {
         GEOMETRY_PROPERTY             = Names.createScopedName(SCOPE, null, "geometry");
         ENVELOPE_PROPERTY             = Names.createScopedName(SCOPE, null, "envelope");
         CRS_CHARACTERISTIC            = Names.createScopedName(SCOPE, null, "crs");
+        UNIT_CHARACTERISTIC           = Names.createScopedName(SCOPE, null, "unit");
         MAXIMAL_LENGTH_CHARACTERISTIC = Names.createScopedName(SCOPE, null, "maximalLength");
         VALID_VALUES_CHARACTERISTIC   = Names.createScopedName(SCOPE, null, "validValues");
     }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/FeatureSet.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/FeatureSet.java
index 655e5c0..cdb3a00 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/FeatureSet.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/FeatureSet.java
@@ -30,6 +30,7 @@ import java.util.function.Consumer;
 import java.util.OptionalLong;
 import java.io.IOException;
 import java.io.UncheckedIOException;
+import javax.measure.Unit;
 import org.opengis.metadata.acquisition.GeometryType;
 import org.apache.sis.coverage.grid.GridExtent;
 import org.apache.sis.internal.feature.MovingFeatures;
@@ -244,6 +245,10 @@ final class FeatureSet extends DiscreteSampling {
         if (desc != null && !desc.equals(name)) {
             attribute.setDefinition(desc);
         }
+        final Unit<?> unit = variable.getUnit();
+        if (unit != null) {
+            attribute.setUnit(unit);
+        }
         if (hasTime) {
             attribute.addCharacteristic(MovingFeatures.TIME);
         }


Mime
View raw message