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/10/22 14:14:10 UTC
[sis] 02/03: Add methods or objects needed for completion of
`FeatureSet`:
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 24c869bf02442e27b9343431d99b8e1ddbef729d
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Oct 22 12:00:45 2020 +0200
Add methods or objects needed for completion of `FeatureSet`:
- Vector.isEmptyOrNaN()
- Geometries.createPoint(…) single-precision variant
- Synchronization lock netCDF feature set.
`FeatureSet` is modified for reading property values by chuncks of 512 values.
This is necessary when the netCDF file to read contains millions of features.
---
.../apache/sis/internal/feature/Geometries.java | 14 +
.../apache/sis/internal/feature/j2d/Factory.java | 8 +
.../main/java/org/apache/sis/math/ArrayVector.java | 42 +-
.../org/apache/sis/math/ConcatenatedVector.java | 10 +-
.../org/apache/sis/math/LinearlyDerivedVector.java | 10 +
.../java/org/apache/sis/math/RepeatedVector.java | 1 +
.../java/org/apache/sis/math/SequenceVector.java | 10 +-
.../src/main/java/org/apache/sis/math/Vector.java | 17 +
.../src/main/java/org/apache/sis/util/Numbers.java | 5 +-
.../org/apache/sis/internal/netcdf/Decoder.java | 5 +-
.../sis/internal/netcdf/DiscreteSampling.java | 12 +-
.../org/apache/sis/internal/netcdf/FeatureSet.java | 623 ++++++++++++++-------
.../apache/sis/internal/netcdf/RasterResource.java | 2 +
.../sis/internal/netcdf/ucar/DecoderWrapper.java | 11 +-
.../sis/internal/netcdf/ucar/FeaturesWrapper.java | 9 +-
.../org/apache/sis/storage/netcdf/NetcdfStore.java | 2 +-
.../apache/sis/internal/netcdf/FeatureSetTest.java | 6 +-
17 files changed, 562 insertions(+), 225 deletions(-)
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
index 071cae3..8e3c978 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
@@ -276,6 +276,20 @@ public abstract class Geometries<G> {
}
/**
+ * Single-precision variant of {@link #createPoint(double, double)}.
+ * Default implementation delegates to the double-precision variant.
+ *
+ * @param x the first coordinate value.
+ * @param y the second coordinate value.
+ * @return the point for the given coordinate values.
+ *
+ * @see #supportSinglePrecision()
+ */
+ public Object createPoint(float x, float y) {
+ return createPoint((double) x, (double) y);
+ }
+
+ /**
* Creates a two-dimensional point from the given coordinate. If the CRS is geographic, then the
* (x,y) values should be (longitude, latitude) for compliance with usage in ESRI and JTS libraries.
* The returned object will be an instance of {@link #pointClass}.
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Factory.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Factory.java
index fa3cce2..4672b7b 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Factory.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Factory.java
@@ -80,6 +80,14 @@ public final class Factory extends Geometries<Shape> {
* Creates a two-dimensional point from the given coordinate.
*/
@Override
+ public Object createPoint(float x, float y) {
+ return new Point2D.Float(x, y);
+ }
+
+ /**
+ * Creates a two-dimensional point from the given coordinate.
+ */
+ @Override
public Object createPoint(final double x, final double y) {
return new Point2D.Double(x, y);
}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
index bab44ed..f7d021e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
@@ -261,6 +261,14 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
return index;
}
+ /** Returns {@code true} if this vector is empty or contains only {@code NaN} values. */
+ @Override public boolean isEmptyOrNaN() {
+ for (final double v : array) {
+ if (!Double.isNaN(v)) return false;
+ }
+ return true;
+ }
+
/** Returns whether this vector in the given range is equals to the specified vector. */
@Override boolean equals(final int lower, final int upper, final Vector other, final int otherOffset) {
if (other instanceof Doubles) {
@@ -367,6 +375,14 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
return index;
}
+ /** Returns {@code true} if this vector is empty or contains only {@code NaN} values. */
+ @Override public final boolean isEmptyOrNaN() {
+ for (final float v : array) {
+ if (!Float.isNaN(v)) return false;
+ }
+ return true;
+ }
+
/** Returns whether this vector in the given range is equals to the specified vector. */
@Override final boolean equals(final int lower, final int upper, final Vector other, final int otherOffset) {
if (other.getClass() == getClass()) {
@@ -480,6 +496,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
return Long.class;
}
+ /** Returns whether this vector is empty. */
+ @Override public final boolean isEmptyOrNaN() {
+ return array.length == 0;
+ }
+
/** Returns whether values are convertible to {@code float} type. */
@Override public final boolean isSinglePrecision() {
return false;
@@ -597,6 +618,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
return Integer.class;
}
+ /** Returns whether this vector is empty. */
+ @Override public final boolean isEmptyOrNaN() {
+ return array.length == 0;
+ }
+
/** Returns whether values are convertible to {@code float} type. */
@Override public final boolean isSinglePrecision() {
return false;
@@ -718,6 +744,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
return Short.class;
}
+ /** Returns whether this vector is empty. */
+ @Override public final boolean isEmptyOrNaN() {
+ return array.length == 0;
+ }
+
/** Returns whether values are convertible to {@code float} type. */
@Override public final boolean isSinglePrecision() {
return true;
@@ -814,6 +845,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
return Byte.class;
}
+ /** Returns whether this vector is empty. */
+ @Override public final boolean isEmptyOrNaN() {
+ return array.length == 0;
+ }
+
/** Returns whether values are convertible to {@code float} type. */
@Override public final boolean isSinglePrecision() {
return true;
@@ -1194,11 +1230,7 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo
/** Returns {@code true} if the element at the given index is null or NaN. */
@Override public boolean isNaN(final int index) {
- Number value = array[index];
- if (value == null) return true;
- if (value instanceof Float) return ((Float) value).isNaN();
- if (value instanceof Double) return ((Double) value).isNaN();
- return false;
+ return Numbers.isNaN(array[index]);
}
@Override public int size() {return array.length;}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
index 5cdb748..e23cc5f 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
@@ -26,7 +26,7 @@ import org.apache.sis.util.Numbers;
* A vector which is the concatenation of two other vectors.
*
* @author Martin Desruisseaux (MPO, Geomatys)
- * @version 0.8
+ * @version 1.1
* @since 0.8
* @module
*/
@@ -91,6 +91,14 @@ final class ConcatenatedVector extends Vector implements Serializable {
}
/**
+ * Returns {@code true} if this vector is empty or contains only {@code NaN} values.
+ */
+ @Override
+ public boolean isEmptyOrNaN() {
+ return first.isEmptyOrNaN() && second.isEmptyOrNaN();
+ }
+
+ /**
* Returns {@code true} if the value at the given index is {@code NaN}.
*/
@Override
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
index 30aa85e..383087e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java
@@ -110,6 +110,16 @@ final class LinearlyDerivedVector extends Vector implements Serializable {
}
/**
+ * Returns {@code true} if this vector is empty or contains only {@code NaN} values.
+ * The implementation delegates to the {@linkplain #base} vector since linear relationship
+ * does not change whether values are NaN.
+ */
+ @Override
+ public boolean isEmptyOrNaN() {
+ return base.isEmptyOrNaN();
+ }
+
+ /**
* Returns {@code true} if the value at the given index is {@code null} or {@code NaN}.
* The implementation delegates to the {@linkplain #base} vector since linear relationship
* does not change whether a value is NaN.
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/RepeatedVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/RepeatedVector.java
index 92440bd..0646c44 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/RepeatedVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/RepeatedVector.java
@@ -136,6 +136,7 @@ final class RepeatedVector extends Vector implements Serializable {
}
/** Forwards to the base vector. */
+ @Override public final boolean isEmptyOrNaN() {return base.isEmptyOrNaN();}
@Override public final boolean isSinglePrecision() {return base.isSinglePrecision();}
@Override public final boolean isInteger() {return base.isInteger();}
@Override public final boolean isUnsigned() {return base.isUnsigned();}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
index a24bbfb..8ceab96 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
@@ -30,7 +30,7 @@ import org.apache.sis.internal.util.Numerics;
* Values may be {@code long} or {@code double} types.
*
* @author Martin Desruisseaux (MPO, Geomatys)
- * @version 1.0
+ * @version 1.1
* @since 0.8
* @module
*/
@@ -90,6 +90,14 @@ abstract class SequenceVector extends Vector implements Serializable {
}
/**
+ * Returns {@code true} if this vector is empty or contains only {@code NaN} values.
+ */
+ @Override
+ public final boolean isEmptyOrNaN() {
+ return (length == 0) || isNaN(0);
+ }
+
+ /**
* Unsupported operation since this vector is not modifiable.
*/
@Override
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
index 8e8b3ed..d20bbf9 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
@@ -336,6 +336,23 @@ public abstract class Vector extends AbstractList<Number> implements RandomAcces
public abstract int size();
/**
+ * Returns {@code true} if this vector is empty or contains only {@code NaN} values.
+ *
+ * @return whether this vector is empty or contains only {@code NaN} values.
+ *
+ * @since 1.1
+ */
+ public boolean isEmptyOrNaN() {
+ final int n = size();
+ for (int i=0; i<n; i++) {
+ if (!isNaN(i)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
* Returns {@code true} if the value at the given index is {@code null} or {@code NaN}.
*
* @param index the index in the [0 … {@linkplain #size() size}-1] range.
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java b/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
index bab3a28..a3f4712 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
@@ -199,11 +199,11 @@ public final class Numbers extends Static {
}
/**
- * Returns {@code true} if the given number is NaN.
+ * Returns {@code true} if the given number is null or NaN.
* Current implementation recognizes {@link Float} and {@link Double} types.
*
* @param value the number to test (may be {@code null}).
- * @return {@code true} if the given number if non-null and NaN.
+ * @return {@code true} if the given number is null or NaN.
*
* @see Float#isNaN()
* @see Double#isNaN()
@@ -211,6 +211,7 @@ public final class Numbers extends Static {
* @since 1.1
*/
public static boolean isNaN(final Number value) {
+ if (value == null) return true;
if (value instanceof Double) return ((Double) value).isNaN();
if (value instanceof Float) return ((Float) value).isNaN();
return false;
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 cc3a60b..4d2b69a 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
@@ -379,14 +379,15 @@ public abstract class Decoder extends ReferencingFactoryContainer implements Clo
* returns objects for handling them. This method does not need to cache the returned array, because
* it will be invoked only once by {@link org.apache.sis.storage.netcdf.NetcdfStore#components()}.
*
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
* @return a handler for the features, or an empty array if none.
* @throws IOException if an I/O operation was necessary but failed.
* @throws DataStoreException if a logical error occurred.
*/
- public DiscreteSampling[] getDiscreteSampling() throws IOException, DataStoreException {
+ public DiscreteSampling[] getDiscreteSampling(final Object lock) throws IOException, DataStoreException {
final String type = stringValue(CF.FEATURE_TYPE);
if (type == null || type.equalsIgnoreCase(FeatureSet.TRAJECTORY)) try {
- return FeatureSet.create(this);
+ return FeatureSet.create(this, lock);
} catch (IllegalArgumentException | ArithmeticException e) {
// Illegal argument is not a problem with content, but rather with configuration.
throw new DataStoreException(e.getLocalizedMessage(), e);
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/DiscreteSampling.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/DiscreteSampling.java
index 0e55aee..bbac501 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/DiscreteSampling.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/DiscreteSampling.java
@@ -41,15 +41,25 @@ public abstract class DiscreteSampling extends AbstractFeatureSet {
protected final Geometries<?> factory;
/**
+ * The object to use for synchronization. For now we use a {@code synchronized} statement,
+ * but it may be changed to {@link java.util.concurrent.locks.Lock} in a future version.
+ *
+ * @see RasterResource#lock
+ */
+ final Object lock;
+
+ /**
* Creates a new discrete sampling parser.
*
* @param library the library for geometric objects, or {@code null} for the default.
* @param listeners the set of registered warning listeners for the data store.
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
* @throws IllegalArgumentException if the given library is non-null but not available.
*/
- protected DiscreteSampling(final GeometryLibrary library, final StoreListeners listeners) {
+ protected DiscreteSampling(final GeometryLibrary library, final StoreListeners listeners, final Object lock) {
super(listeners);
factory = Geometries.implementation(library);
+ this.lock = lock;
}
/**
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 e404128..655e5c0 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
@@ -20,7 +20,6 @@ import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
@@ -67,15 +66,22 @@ final class FeatureSet extends DiscreteSampling {
/**
* Value of {@code "featureType"} global attribute for netCDF files that this class can handle.
* Also used as property name for the geometry object.
+ *
+ * @see #isTrajectory
*/
static final String TRAJECTORY = "trajectory";
/**
+ * Number of features to get in one read operation. We do not read features one-by-one because it may be slow.
+ * We do not read all features at once neither because it consumes a lot of memory if the netCDF file is large.
+ * This value is a compromise between reducing I/O operations and reducing memory consumption.
+ */
+ private static final int PAGE_SIZE = 512;
+
+ /**
* The number of instances for each feature, or {@code null} if none. If non-null, then the number of features
* is the length of this vector and each {@link Feature} instance has multi-valued properties with a number of
- * elements given by this count.
- *
- * If null, then the number of features is determined by the length of other variables.
+ * elements given by this count. If null, the number of features is determined by the length of other variables.
*
* @see #getFeatureCount()
*/
@@ -84,42 +90,62 @@ final class FeatureSet extends DiscreteSampling {
/**
* The singleton properties (for which there is only one value per feature instance), or an empty array if none.
* In the case of trajectories, this array usually contains a single variable for the moving feature identifiers
- * ("mfIdRef"). If {@link #counts} is non-null, then the length of {@code identifiers[i]} variables shall be the
+ * ("mfIdRef"). If {@link #counts} is non-null, then the length of all {@code properties} variables shall be the
* same than the length of the {@link #counts} vector.
*/
private final Variable[] properties;
/**
- * The kind of geometry described by {@linkplain #coordinates} vectors.
- * Current implementation supports only two types:
- *
- * <ul>
- * <li>{@link GeometryType#POINT} (specified by {@code isTrajectory = false}).</li>
- * <li>{@link GeometryType#LINEAR} (specified by {@code isTrajectory = true}).</li>
- * </ul>
+ * The time-varying properties (for which there is many values per feature instance), or an empty array if none.
+ * The length of all {@code dynamicProperties} variables shall be the sum of all {@link #counts} values.
+ * If {@code #count} is {@code null}, then this array is empty.
*/
- private final boolean isTrajectory;
+ private final Variable[] dynamicProperties;
/**
- * Whether the {@link #coordinates} array contains a temporal variable.
- * If {@code true}, then the time variable shall be last.
+ * Number of variables storing the coordinates of all geometries (trajectories or points). Those variables are
+ * stored at the beginning of either the {@link #properties} array or the {@link #dynamicProperties} array.
+ * The array where to find coordinates is determined by {@link #isTrajectory}:
+ *
+ * <ul>
+ * <li>If {@code isTrajectory = false}, coordinates are at the beginning of {@link #properties}.</li>
+ * <li>If {@code isTrajectory = true}, coordinates are at the beginning of {@link #dynamicProperties}.</li>
+ * </ul>
+ *
+ * The coordinates are <var>x</var>, <var>y</var> and potentially <var>z</var> or <var>t</var>, in that order.
+ * The <var>x</var> and <var>y</var> coordinates are typically longitudes and latitudes, but not necessarily.
+ * If temporal coordinates exist, the time variable must be last and {@link #hasTime} shall be {@code true}.
+ * Ordering is defined by the {@link AxisType} enumeration.
+ *
+ * <p>Note that referencing dimension is not necessarily equal to geometry dimension,
+ * because temporal coordinates are not stored in the geometry object.</p>
+ *
+ * @see AxisType
+ * @see #getReferencingDimension(boolean)
+ * @see Iter#geometryDimension
*/
- private final boolean hasTime;
+ private final int referencingDimension;
/**
- * The variables for <var>x</var>, <var>y</var> and potentially <var>z</var> or <var>t</var> coordinate values.
- * The <var>x</var> and <var>y</var> coordinates are typically longitudes and latitudes, but not necessarily.
- * If temporal coordinates exist, the time variable must be last and {@link #hasTime} shall be {@code true}.
- * All variables in this array shall have the same length. If {@link #counts} is non-null, then the length
- * of {@code coordinates[i]} variables shall be the sum of all {@link #counts} values.
+ * The kind of geometry described by coordinates. Current implementation supports only two types,
+ * identified by the {@code false} and {@code true} values respectively:
+ *
+ * <ul>
+ * <li>{@link GeometryType#POINT} with coordinates stored in {@link #properties}.</li>
+ * <li>{@link GeometryType#LINEAR} with coordinates stored in {@link #dynamicProperties}.</li>
+ * </ul>
+ *
+ * @see #TRAJECTORY
+ * @see #getReferencingDimension(boolean)
*/
- private final Variable[] coordinates;
+ private final boolean isTrajectory;
/**
- * Any time-varying properties other than coordinate values, or an empty array if none.
- * All variables in this array shall have the same length than {@link #coordinates} variables.
+ * Whether coordinates include a temporal variable.
+ * If {@code true}, then the time variable shall be last.
+ * If {@code false}, then {@link #referencingDimension} is equal to geometry dimension.
*/
- private final Variable[] dynamicProperties;
+ private final boolean hasTime;
/**
* The type of all features to be read by this {@code FeatureSet}.
@@ -130,28 +156,33 @@ final class FeatureSet extends DiscreteSampling {
* Creates a new discrete sampling parser for features identified by the given variable.
* All arrays given to this method are stored by direct reference (they are not cloned).
*
- * <p>The {@code name} argument can be anything. A not-too-bad choice (when nothing better is available)
- * is the name of the first dimension of {@code coordinates} and {@code properties} variables. All those
- * variables should have that first dimension in common, because {@code create(…)} uses that criterion.</p>
+ * <p>The {@code name} argument can be anything. A not-too-bad choice (when nothing better is available) is
+ * the name of the first dimension of {@code dynamicProperties} (preferred) or {@code properties} (fallback)
+ * variables. Variables in the same array should have that first dimension in common because {@code create(…)}
+ * uses that criterion.</p>
*
- * @param decoder the source of the features to create.
- * @param name name to give to the feature type.
- * @param counts the count of instances per feature, or {@code null} if none.
- * @param properties variables providing a single value per feature instance (e.g. "mfIdRef").
- * @param coordinates <var>x</var>, <var>y</var> and potentially <var>z</var> or <var>t</var> values.
- * @param hasTime whether the {@code coordinates} array contains a temporal variable.
- * @param dynamicProperties variables that contain time-varying properties other than coordinates.
+ * @param decoder the source of the features to create.
+ * @param name name to give to the feature type.
+ * @param counts the count of instances per feature, or {@code null} if none.
+ * @param properties variables providing a single value per feature instance (e.g. "mfIdRef").
+ * @param dynamicProperties variables that contain time-varying properties other than coordinates.
+ * @param referencingDimension number of coordinate variables.
+ * @param isTrajectory whether coordinates are stored in {@code properties} or {@code dynamicProperties}.
+ * @param hasTime whether coordinates include a temporal variable.
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
* @throws IllegalArgumentException if the given library is non-null but not available.
*/
private FeatureSet(final Decoder decoder, String name, final Vector counts, final Variable[] properties,
- final Variable[] coordinates, final boolean hasTime, final Variable[] dynamicProperties)
+ final Variable[] dynamicProperties, final int referencingDimension, final boolean isTrajectory,
+ final boolean hasTime, final Object lock)
{
- super(decoder.geomlib, decoder.listeners);
- this.counts = counts;
- this.properties = properties;
- this.coordinates = coordinates;
- this.dynamicProperties = dynamicProperties;
- this.hasTime = hasTime;
+ super(decoder.geomlib, decoder.listeners, lock);
+ this.counts = counts;
+ this.properties = properties;
+ this.dynamicProperties = dynamicProperties;
+ this.referencingDimension = referencingDimension;
+ this.hasTime = hasTime;
+ this.isTrajectory = isTrajectory;
/*
* Creates a description of the features to be read with following properties:
*
@@ -161,24 +192,31 @@ final class FeatureSet extends DiscreteSampling {
*/
final FeatureTypeBuilder builder = new FeatureTypeBuilder(
decoder.nameFactory, decoder.geomlib, decoder.listeners.getLocale());
- for (final Variable v : properties) {
- final Class<?> type = v.getDataType().getClass(v.getNumDimensions() > 1);
+
+ for (int i = getReferencingDimension(false); i < properties.length; i++) {
+ final Variable v = properties[i];
+ final Class<?> type;
+ if (v.isEnumeration()) {
+ type = String.class;
+ } else {
+ type = v.getDataType().getClass(v.getNumDimensions() > 1);
+ }
describe(v, builder.addAttribute(type), false);
}
- isTrajectory = (counts != null);
- if (coordinates.length > (hasTime ? 1 : 0)) {
- final AttributeTypeBuilder<?> geometry =
- builder.addAttribute(isTrajectory ? GeometryType.LINEAR : GeometryType.POINT);
+ if (referencingDimension != 0) {
+ final AttributeTypeBuilder<?> geometry;
+ geometry = builder.addAttribute(isTrajectory ? GeometryType.LINEAR : GeometryType.POINT);
geometry.setName(TRAJECTORY).addRole(AttributeRole.DEFAULT_GEOMETRY);
if (hasTime) {
geometry.addCharacteristic(MovingFeatures.TIME);
}
}
- for (final Variable v : dynamicProperties) {
+ for (int i = getReferencingDimension(true); i < dynamicProperties.length; i++) {
/*
* Use `Number` type instead than a more specialized subclass because values
* will be stored in `Vector` objects and that class implements `List<Number>`.
*/
+ final Variable v = dynamicProperties[i];
final Class<?> type = (v.isEnumeration() || v.isString()) ? String.class : Number.class;
describe(v, builder.addAttribute(type).setMaximumOccurs(Integer.MAX_VALUE), hasTime);
}
@@ -192,7 +230,7 @@ final class FeatureSet extends DiscreteSampling {
/**
* Sets the attribute name, and potentially its definition, from the given variable.
- * If the variable has a {@code "cf_role"} attribute set {@code "trajectory_id"},
+ * If the variable has a {@code "cf_role"} attribute set to {@code "trajectory_id"},
* then the attribute will also be declared as an identifier.
*
* @param variable the variable from which to get metadata.
@@ -216,12 +254,15 @@ final class FeatureSet extends DiscreteSampling {
/**
* Creates new discrete sampling parsers from the attribute values found in the given decoder.
+ * This method shall be invoked in a method synchronized on {@link #lock}.
*
* @param decoder the source of the features to create.
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
* @throws IllegalArgumentException if the geometric object library is not available.
* @throws ArithmeticException if the size of a variable exceeds {@link Integer#MAX_VALUE}, or other overflow occurs.
*/
- static FeatureSet[] create(final Decoder decoder) throws IOException, DataStoreException {
+ static FeatureSet[] create(final Decoder decoder, final Object lock) throws IOException, DataStoreException {
+ assert Thread.holdsLock(lock);
final List<FeatureSet> features = new ArrayList<>(3); // Will usually contain at most one element.
final Map<Dimension,Boolean> done = new HashMap<>(); // Whether a dimension has already been used.
for (final Variable v : decoder.getVariables()) {
@@ -251,7 +292,7 @@ final class FeatureSet extends DiscreteSampling {
final Dimension featureDimension = v.getGridDimensions().get(0);
final Dimension sampleDimension = decoder.findDimension(sampleDimName);
if (sampleDimension != null) {
- addFeatureSet(features, decoder, v, featureDimension, sampleDimension);
+ addFeatureSet(features, decoder, v, featureDimension, sampleDimension, lock);
done.put(sampleDimension, Boolean.TRUE);
} else {
decoder.listeners.warning(decoder.resources().getString(Resources.Keys.DimensionNotFound_3,
@@ -272,7 +313,7 @@ final class FeatureSet extends DiscreteSampling {
for (final Map.Entry<Dimension,Boolean> entry : done.entrySet()) {
if (!entry.getValue()) {
final Dimension dimension = entry.getKey();
- addFeatureSet(features, decoder, null, dimension, dimension);
+ addFeatureSet(features, decoder, null, dimension, dimension, lock);
}
}
return features.toArray(new FeatureSet[features.size()]);
@@ -304,15 +345,22 @@ final class FeatureSet extends DiscreteSampling {
* @param counts the count of instances per feature, or {@code null} if none.
* @param featureDimension dimension of properties having a single value per feature instance.
* @param sampleDimension dimension of properties having multiple values per feature instance.
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
*/
- private static void addFeatureSet(final List<FeatureSet> features, final Decoder decoder, final Variable counts,
- final Dimension featureDimension, final Dimension sampleDimension) throws IOException, DataStoreException
+ private static void addFeatureSet(final List<FeatureSet> features, final Decoder decoder,
+ final Variable counts, final Dimension featureDimension, final Dimension sampleDimension,
+ final Object lock) throws IOException, DataStoreException
{
- final String featureName = featureDimension.getName();
- final boolean isPointSet = sampleDimension.equals(featureDimension);
- final List<Variable> properties = isPointSet ? Collections.emptyList() : new ArrayList<>();
- final List<Variable> dynamicProperties = new ArrayList<>();
- final Map<AxisType,Variable> coordinates = new EnumMap<>(AxisType.class);
+ final String featureName = featureDimension.getName();
+ if (featureName == null) {
+ // May happen with HDF5 file read using UCAR library.
+ return;
+ }
+ final boolean isTrajectory = !sampleDimension.equals(featureDimension);
+ final List<Variable> properties = new ArrayList<>();
+ final List<Variable> dynamicProperties = isTrajectory ? new ArrayList<>() : Collections.emptyList();
+ final EnumMap<AxisType,Variable> coordinates = new EnumMap<>(AxisType.class);
+ final EnumMap<AxisType,Variable> trajectory = new EnumMap<>(AxisType.class);
for (final Variable data : decoder.getVariables()) {
if (data.equals(counts)) {
continue;
@@ -323,45 +371,42 @@ final class FeatureSet extends DiscreteSampling {
* the expected dimension. According CF convention that variable should also have a
* "cf_role" attribute set to "trajectory_id", but this is not required by SIS.
*/
+ final boolean dynamic;
if (featureName.equalsIgnoreCase(data.getName())) {
if (isScalarOrString(data, featureDimension, decoder)) {
- (isPointSet ? dynamicProperties : properties).add(data);
+ properties.add(data);
}
- } else if (!isPointSet && isScalarOrString(data, featureDimension, null)) {
- /*
- * Feature property other than identifiers. Should rarely happen.
- */
+ continue;
+ } else if (isScalarOrString(data, featureDimension, null)) {
properties.add(data);
- } else if (isScalarOrString(data, sampleDimension, null)) {
- /*
- * All other sample property (i.e. property having a value for each temporal value).
- * If `isPointSet` is true, then sample properties are actually feature properties.
- */
- final AxisType axisType = AxisType.valueOf(data);
- if (axisType != null) {
- final Variable previous = coordinates.putIfAbsent(axisType, data);
- if (previous != null) {
- // Duplicated axis type. Keep the first axis in declaration order.
- decoder.listeners.warning(decoder.resources().getString(Resources.Keys.DuplicatedAxisType_4,
- decoder.getFilename(), axisType, previous.getName(), data.getName()));
- }
- } else {
- dynamicProperties.add(data);
+ dynamic = false;
+ } else if (isTrajectory && isScalarOrString(data, sampleDimension, null)) {
+ dynamicProperties.add(data);
+ dynamic = true;
+ } else {
+ continue;
+ }
+ /*
+ * Check if the property that we just added is a coordinate system axis.
+ * We handle separately the axes having coordinates provided by static and dynamic properties.
+ * We will decide at the end of this loop which one of those two groups to use.
+ */
+ final AxisType axisType = AxisType.valueOf(data);
+ if (axisType != null) {
+ final Variable previous = (dynamic ? trajectory : coordinates).putIfAbsent(axisType, data);
+ if (previous != null) {
+ // Duplicated axis type. Keep the first axis in declaration order.
+ decoder.listeners.warning(decoder.resources().getString(Resources.Keys.DuplicatedAxisType_4,
+ decoder.getFilename(), axisType, previous.getName(), data.getName()));
}
}
}
+ final Reorder r = new Reorder();
features.add(new FeatureSet(decoder, featureName,
(counts != null) ? counts.read() : null,
- toArray(properties),
- toArray(coordinates.values()), coordinates.containsKey(AxisType.T),
- toArray(dynamicProperties)));
- }
-
- /**
- * Returns the content of given collection as an array.
- */
- private static Variable[] toArray(final Collection<Variable> variables) {
- return variables.toArray(new Variable[variables.size()]);
+ r.toArray(properties, coordinates, false),
+ r.toArray(dynamicProperties, trajectory, true),
+ r.referencingDimension, r.isTrajectory, r.hasTime, lock)); // Those arguments must be last.
}
/**
@@ -414,6 +459,69 @@ final class FeatureSet extends DiscreteSampling {
}
/**
+ * Returns the content of a property list as an array, potentially with coordinates moved at the array beginning.
+ * At most one call to the {@link #toArray(List, EnumMap, boolean)} will reorder the properties; other calls will
+ * return properties in the order they appear in the list.
+ */
+ private static final class Reorder {
+ /**
+ * Number of variables storing the coordinates of all geometries (trajectories or points).
+ * This is the value to assign to {@link FeatureSet#referencingDimension}.
+ */
+ int referencingDimension;
+
+ /**
+ * The kind of geometry described by coordinates.
+ * This is the value to assign to {@link FeatureSet#isTrajectory}.
+ */
+ boolean isTrajectory;
+
+ /**
+ * Whether coordinates include a temporal variable.
+ * This is the value to assign to {@link FeatureSet#hasTime}.
+ */
+ boolean hasTime;
+
+ /**
+ * Returns the content of given property list as an array, potentially with coordinate variables first.
+ *
+ * @param properties the list to return as an array, not necessarily with elements in same order.
+ * @param coordinates {@code properties} variables to consider as coordinate values.
+ * @param dynamic value to assign to {@link #isTrajectory} if coordinate axes have been found.
+ */
+ Variable[] toArray(final List<Variable> properties, final EnumMap<AxisType,Variable> coordinates, final boolean dynamic) {
+ Variable[] array = new Variable[properties.size()];
+ if (referencingDimension == 0 && coordinates.containsKey(AxisType.X) && coordinates.containsKey(AxisType.Y)) {
+ isTrajectory = dynamic;
+ hasTime = coordinates.containsKey(AxisType.T);
+ array = coordinates.values().toArray(array); // Put coordinates at array beginning.
+ int n = referencingDimension = coordinates.size();
+skip: for (final Variable v : properties) {
+ for (int i=referencingDimension; --i >= 0;) {
+ if (array[i] == v) continue skip; // Skip already added coordinates.
+ }
+ array[n++] = v; // Add property after coordinates.
+ }
+ assert n == array.length;
+ } else {
+ array = properties.toArray(array);
+ }
+ return array;
+ }
+ }
+
+ /**
+ * Returns the number of variables that are storing coordinate values.
+ * If present, those variables are always at the lowest array indices.
+ *
+ * @param dynamic {@code true} for searching in dynamic properties, or {@code false} for static properties.
+ * @return number of coordinate variables in the specified group of properties.
+ */
+ final int getReferencingDimension(final boolean dynamic) {
+ return (isTrajectory ^ dynamic) ? 0 : referencingDimension;
+ }
+
+ /**
* Returns the type of all features to be read by this {@code FeatureSet}.
*/
@Override
@@ -431,19 +539,43 @@ final class FeatureSet extends DiscreteSampling {
if (counts != null) {
return OptionalLong.of(counts.size());
}
- for (int i=0; ; i++) {
- final Variable[] data;
- switch (i) {
- case 0: data = properties; break;
- case 1: data = coordinates; break;
- case 2: data = dynamicProperties; break;
- default: return OptionalLong.empty();
- }
- if (data.length != 0) {
- final long length = data[0].getGridDimensions().get(0).length();
- if (length >= 0) return OptionalLong.of(length);
+ if (properties.length != 0) {
+ final long length = properties[0].getGridDimensions().get(0).length();
+ if (length >= 0) {
+ return OptionalLong.of(length);
}
}
+ return OptionalLong.empty();
+ }
+
+ /**
+ * Returns {@code true} if the given value is null, an empty string or a NaN value.
+ */
+ static boolean isEmpty(final Object value) {
+ return (value == null) || "".equals(value) ||
+ (value instanceof Float && ((Float) value).isNaN()) ||
+ (value instanceof Double && ((Double) value).isNaN());
+ }
+
+ /**
+ * Prepares indices of a sub-region to read in a vector of the given number of dimensions.
+ * This is a helper method for {@link Iter}, defined here because we can not put static methods
+ * in a non-static inner class.
+ *
+ * @param dimensions dimensions of the vector to read. Can be {@code null} if {@code numDim} is 1.
+ * @param numDim number of dimensions in the vector to read: {@code dimensions.size()}.
+ * @param position position of the first value to read in the netCDF variables.
+ * @param length number of property values to read.
+ */
+ static GridExtent extent(final List<Dimension> dimensions, int numDim, final long position, final int length) {
+ final long[] lower = new long[numDim];
+ final long[] upper = new long[numDim];
+ lower[--numDim] = position;
+ upper[ numDim] = Math.addExact(position, length);
+ for (int i=0; i<numDim; i++) {
+ upper[i] = dimensions.get(numDim-i).length();
+ }
+ return new GridExtent(null, lower, upper, false);
}
/**
@@ -472,16 +604,30 @@ final class FeatureSet extends DiscreteSampling {
/**
* Index of the next feature to read.
*/
- private int nextIndex;
+ private int featureIndex;
/**
- * Position in the data vectors of the next feature to read.
- * This is the sum of the length of data in all previous features.
+ * Index of the first feature for which the {@link #propertyValues} contains data.
+ * Call to {@code propertyValues[p].get(i)} provides value of property <var>p</var>
+ * for the feature at index {@code currentLowerIndex + i}.
*/
- private long position;
+ private int currentLowerIndex;
+
+ /**
+ * Index after the last feature for which the {@link #propertyValues} contains data.
+ * This is {@link #currentLowerIndex} + {@code propertyValues[any].size()}.
+ */
+ private int currentUpperIndex;
+
+ /**
+ * Index of the first {@link #propertyValues} list which is not a coordinate vector.
+ * Lists before that index will be stored in the geometry instead than as feature property.
+ */
+ private final int propertyIndexOffset;
/**
* Values of all simple properties (having a single value per feature instance).
+ * The list size should not exceed {@value FeatureSet#PAGE_SIZE} elements.
*
* @see FeatureSet#properties
*/
@@ -493,25 +639,49 @@ final class FeatureSet extends DiscreteSampling {
private final String[] propertyNames;
/**
- * Creates a new iterator.
+ * Index where to start reading dynamic property values for the next feature.
+ * This is the sum of the length of data in all previous features.
+ */
+ private long dynamicPropertyPosition;
+
+ /**
+ * Dimension of geometry objects.
+ * This is {@link #referencingDimension} minus the temporal dimension if any.
+ */
+ private final int geometryDimension;
+
+ /**
+ * Creates a new iterator. This constructor reads immediately data for the first
+ * {@value #PAGE_SIZE} feature instances as a way to detect problem early.
*/
Iter() throws IOException, DataStoreException {
size = (int) Math.min(getFeatureCount().orElse(0), Integer.MAX_VALUE);
- final int n = properties.length;
- propertyValues = new List<?>[n];
- propertyNames = new String[n];
+ geometryDimension = referencingDimension - (hasTime ? 1 : 0);
+ propertyIndexOffset = getReferencingDimension(false);
+ int n = properties.length;
+ propertyValues = new List<?>[n]; // Including coordinate vectors.
+ propertyNames = new String[n -= propertyIndexOffset]; // Excluding coordinate vectors.
for (int i=0; i<n; i++) {
- // Efficiency should be okay because those lists are cached.
- propertyValues[i] = properties[i].readAnyType();
- propertyNames [i] = properties[i].getName();
+ propertyNames[i] = properties[i + propertyIndexOffset].getName();
}
+ readNextPage(); // See constructor javadoc.
+ }
+
+ /**
+ * Reads static property values for the next {@value #PAGE_SIZE} feature instances.
+ * Reading starts at the feature at index given by {@link #currentLowerIndex}.
+ */
+ private void readNextPage() throws IOException, DataStoreException {
+ final int length = Math.min(size - currentLowerIndex, PAGE_SIZE);
+ read(properties, currentLowerIndex, length, propertyValues);
+ currentUpperIndex = currentLowerIndex + length;
}
/**
* Executes the given action only on the next feature, if any.
*
* <h4>Limitations</h4>
- * Current implementation performs a lot of seek operations, which may be inefficient
+ * Current implementation may perform a lot of seek operations, which may be inefficient
* unless the {@link Decoder} uses a {@code ChannelDataInput} backed by a direct buffer.
*
* @throws ArithmeticException if the size of a variable exceeds {@link Integer#MAX_VALUE}, or other overflow occurs.
@@ -520,67 +690,64 @@ final class FeatureSet extends DiscreteSampling {
@Override
public boolean tryAdvance(final Consumer<? super Feature> action) {
final Feature feature = type.newInstance();
- boolean isEmpty;
- long length;
- do {
- if (nextIndex >= size) {
- return false;
- }
- length = (counts != null) ? counts.longValue(nextIndex) : 1;
- isEmpty = (length == 0);
- for (int i=0; i < propertyNames.length; i++) {
- final Object value = propertyValues[i].get(nextIndex);
- feature.setPropertyValue(propertyNames[i], value);
- if (isEmpty) {
- isEmpty = (value == null) || "".equals(value) ||
- (value instanceof Float && ((Float) value).isNaN()) ||
- (value instanceof Double && ((Double) value).isNaN());
- }
- }
- nextIndex++;
- } while (isEmpty);
- /*
- * At this point we found that there is some data we can put in a feature instance.
- * Above loop has set the static properties (those having one value per feature).
- * The loop below sets the dynamic properties (those having time-varying values).
- */
- final Vector[] coordinateValues = new Vector[coordinates.length];
- final GridExtent extent = extent(null, 1, length);
- List<Dimension> textDimensions = null;
- GridExtent textExtent = null;
- boolean isSinglePrecision = factory.supportSinglePrecision();
+ final Vector[] coordinateValues;
+ int offset, length;
try {
- for (int i=0; i<coordinateValues.length; i++) {
- coordinateValues[i] = coordinates[i].read(extent, null);
- if (isSinglePrecision) {
- isSinglePrecision = coordinateValues[i].isSinglePrecision();
- }
- }
- for (int i=0; i<dynamicProperties.length; i++) {
- final Variable p = dynamicProperties[i];
- Object value;
- if (p.getNumDimensions() > 1) {
- final List<Dimension> dimensions = p.getGridDimensions();
- if (textExtent == null || !dimensions.equals(textDimensions)) {
- textExtent = extent(dimensions, dimensions.size(), length);
- textDimensions = dimensions;
+ /*
+ * Read the properties having one value per feature instance, for example the feature identifier.
+ * Those values are read by chunks, for example 512 values read in advance for each property.
+ * Features where all values are null, empty string or NaN are skipped.
+ */
+ List<?>[] values = propertyValues;
+ boolean isEmpty;
+ do {
+ if (featureIndex >= currentUpperIndex) {
+ if (featureIndex >= size) {
+ return false;
}
- value = p.readAnyType(textExtent, null);
- } else {
- value = p.readAnyType(extent, null);
+ currentLowerIndex = featureIndex;
+ readNextPage();
}
- if (p.isEnumeration() && value instanceof Vector) {
- final Vector data = (Vector) value;
- final String[] meanings = new String[data.size()];
- for (int j=0; j<meanings.length; j++) {
- String m = p.meaning(data.intValue(j));
- meanings[j] = (m != null) ? m : "";
+ offset = featureIndex - currentLowerIndex;
+ length = (counts != null) ? counts.intValue(featureIndex) : 1;
+ isEmpty = (length == 0);
+ for (int i=0; i < propertyNames.length; i++) {
+ final Object value = values[i + propertyIndexOffset].get(offset);
+ if (!isEmpty(value)) {
+ isEmpty = false;
+ feature.setPropertyValue(propertyNames[i], value);
}
- value = Arrays.asList(meanings);
}
- feature.setPropertyValue(dynamicProperties[i].getName(), value);
- // TODO: set time characteristic.
+ featureIndex++;
+ } while (isEmpty);
+ /*
+ * At this point we found that there is some data we can put in a feature instance.
+ * Above loop has set the static properties (those having one value per feature).
+ * The block below sets the dynamic properties (those having time-varying values).
+ */
+ final int n = dynamicProperties.length;
+ if (n != 0) {
+ final List<?>[] target = new List<?>[n];
+ read(dynamicProperties, dynamicPropertyPosition, length, target);
+ for (int i=getReferencingDimension(true); i<n; i++) {
+ feature.setPropertyValue(dynamicProperties[i].getName(), target[i]);
+ // TODO: set time characteristic.
+ }
+ if (isTrajectory) {
+ values = target;
+ }
}
+ /*
+ * The coordinate vectors are provided by the first variables in either `properties` or
+ * `dynamicProperties`, depending on whether the geometries are points or trajectories.
+ * Above code has set `values` to the correct source of coordinate data.
+ *
+ * The following `System.arraycopy(…)` call writes `List<?>` references into a `Vector[]` array,
+ * which seems unsafe. But it should not cause an ArrayStoreException because the elements that
+ * we copy should be `Vector` instances, even if the remaining elements are not.
+ */
+ coordinateValues = new Vector[geometryDimension];
+ System.arraycopy(values, 0, coordinateValues, 0, coordinateValues.length);
} catch (IOException e) {
throw new UncheckedIOException(canNotReadFile(), e);
} catch (DataStoreException e) {
@@ -588,54 +755,104 @@ final class FeatureSet extends DiscreteSampling {
}
/*
* Create the geometry, which may be a trajectory or a single point. Some geometry libraries
- * (e.g. Java2D) provides different implementations for single-precision or double-precision.
- * For that reason we
+ * (e.g. Java2D) provide different implementations for single-precision or double-precision.
+ * The use of single-precision when possible can help to reduce memory usage.
*/
- final Object geometry;
- if (isTrajectory) {
- final int dimension = coordinateValues.length - (hasTime ? 1 : 0);
- final int n = Math.toIntExact(length * dimension);
- final Vector vc;
- if (isSinglePrecision) {
- final float[] c = new float[n];
- for (int i=0; i<n; i++) {
- c[i] = coordinateValues[i % dimension].floatValue(i / dimension);
+ boolean isEmpty = isTrajectory; // Skip `isEmptyOrNaN()` test for points.
+ boolean isFloat = factory.supportSinglePrecision();
+ for (final Vector vc : coordinateValues) {
+ if (isEmpty) isEmpty = vc.isEmptyOrNaN();
+ if (isFloat) isFloat = vc.isSinglePrecision();
+ }
+makeGeom: if (!isEmpty) {
+ final Object geometry;
+ if (isTrajectory) {
+ /*
+ * Case when the geometry can have an arbitrary amount of points.
+ * Coordinates are taken from `dynamicProperties` variable, which
+ * are read every time that a feature instance is created.
+ */
+ final int n = Math.multiplyExact(length, geometryDimension);
+ final Vector vc;
+ if (isFloat) {
+ final float[] c = new float[n];
+ for (int i=0; i<n; i++) {
+ c[i] = coordinateValues[i % geometryDimension].floatValue(i / geometryDimension);
+ }
+ vc = Vector.create(c, false);
+ } else {
+ final double[] c = new double[n];
+ for (int i=0; i<n; i++) {
+ c[i] = coordinateValues[i % geometryDimension].doubleValue(i / geometryDimension);
+ }
+ vc = Vector.create(c);
}
- vc = Vector.create(c, false);
+ geometry = factory.createPolyline(false, geometryDimension, vc);
} else {
- final double[] c = new double[n];
- for (int i=0; i<n; i++) {
- c[i] = coordinateValues[i % dimension].doubleValue(i / dimension);
+ /*
+ * Case when the geometry is a single point. Note that the X and Y coordinates
+ * are guaranteed to be present because of the check done by `Reorder.toArray(…)`.
+ */
+ if (isFloat) {
+ final float x = coordinateValues[0].floatValue(offset);
+ final float y = coordinateValues[0].floatValue(offset);
+ if (Float.isNaN(x) && Float.isNaN(y)) break makeGeom;
+ geometry = factory.createPoint(x, y);
+ } else {
+ final double x = coordinateValues[0].doubleValue(offset);
+ final double y = coordinateValues[1].doubleValue(offset);
+ if (Double.isNaN(x) && Double.isNaN(y)) break makeGeom;
+ geometry = factory.createPoint(x, y);
}
- vc = Vector.create(c);
}
- geometry = factory.createPolyline(false, dimension, vc);
- } else {
- geometry = factory.createPoint(coordinateValues[0].doubleValue(0),
- coordinateValues[1].doubleValue(0));
+ feature.setPropertyValue(TRAJECTORY, geometry);
}
- feature.setPropertyValue(TRAJECTORY, geometry);
action.accept(feature);
- position += length; // Check for ArithmeticException is already done by `extent(…)` call.
+ dynamicPropertyPosition += length; // Check for ArithmeticException is already done by `extent(…)` call.
return true;
}
/**
- * Creates a grid extent for a sub-region to read in a vector of the given number of dimensions.
+ * Reads property values starting at the given position.
+ * The same sub-region is read for all variables.
*
- * @param dimensions dimensions of the vector to read. Can be {@code null} if {@code n} is 1.
- * @param n number of dimensions in the vector to read.
- * @param length number of values to read.
+ * @param variables the variables to read.
+ * @param position position of the first value to read in the netCDF variables.
+ * @param length number of property values to read.
+ * @param target where to store the results of read operations.
*/
- private GridExtent extent(final List<Dimension> dimensions, int n, final long length) {
- final long[] lower = new long[n];
- final long[] upper = new long[n];
- lower[--n] = position;
- upper[ n] = Math.addExact(position, length);
- for (int i=0; i<n; i++) {
- upper[i] = dimensions.get(n-i).length();
+ private void read(final Variable[] variables, final long position, final int length, final List<?>[] target)
+ throws IOException, DataStoreException
+ {
+ final GridExtent extent = extent(null, 1, position, length);
+ List<Dimension> textDimensions = null;
+ GridExtent textExtent = null;
+ synchronized (lock) {
+ for (int i=0; i < variables.length; i++) {
+ final Variable p = variables[i];
+ List<?> value;
+ if (p.getNumDimensions() > 1) {
+ final List<Dimension> dimensions = p.getGridDimensions();
+ if (textExtent == null || !dimensions.equals(textDimensions)) {
+ textExtent = extent(dimensions, dimensions.size(), position, length);
+ textDimensions = dimensions;
+ }
+ value = p.readAnyType(textExtent, null);
+ } else {
+ value = p.readAnyType(extent, null);
+ }
+ if (p.isEnumeration() && value instanceof Vector) {
+ final Vector data = (Vector) value;
+ final String[] meanings = new String[data.size()];
+ for (int j=0; j<meanings.length; j++) {
+ String m = p.meaning(data.intValue(j));
+ meanings[j] = (m != null) ? m : "";
+ }
+ value = Arrays.asList(meanings);
+ }
+ target[i] = value;
+ }
}
- return new GridExtent(null, lower, upper, false);
}
/**
@@ -651,7 +868,7 @@ final class FeatureSet extends DiscreteSampling {
*/
@Override
public long estimateSize() {
- return size - nextIndex;
+ return size - featureIndex;
}
/**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
index 75f93b4..e02059c 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/RasterResource.java
@@ -163,6 +163,8 @@ public final class RasterResource extends AbstractGridResource implements Resour
/**
* The object to use for synchronization. For now we use a {@code synchronized} statement,
* but it may be changed to {@link java.util.concurrent.locks.Lock} in a future version.
+ *
+ * @see DiscreteSampling#lock
*/
private final Object lock;
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
index 36a6bda..fda8c82 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
@@ -93,7 +93,7 @@ public final class DecoderWrapper extends Decoder implements CancelTask {
* The discrete sampling features, or {@code null} if none.
* This reference is kept for making possible to close it in {@link #close()}.
*
- * @see #getDiscreteSampling()
+ * @see #getDiscreteSampling(Object)
*/
private transient FeatureDataset features;
@@ -418,13 +418,14 @@ public final class DecoderWrapper extends Decoder implements CancelTask {
/**
* If this decoder can handle the file content as features, returns handlers for them.
*
- * @return {@inheritDoc}
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
+ * @return a handler for the features, or an empty array if none.
* @throws IOException if an I/O operation was necessary but failed.
* @throws DataStoreException if the library of geometric objects is not available.
*/
@Override
@SuppressWarnings("null")
- public DiscreteSampling[] getDiscreteSampling() throws IOException, DataStoreException {
+ public DiscreteSampling[] getDiscreteSampling(final Object lock) throws IOException, DataStoreException {
if (features == null && file instanceof NetcdfDataset) {
features = FeatureDatasetFactoryManager.wrap(null, (NetcdfDataset) file, this,
new Formatter(new LogAdapter(listeners), listeners.getLocale()));
@@ -435,7 +436,7 @@ public final class DecoderWrapper extends Decoder implements CancelTask {
final FeaturesWrapper[] wrappers = new FeaturesWrapper[fc.size()];
try {
for (int i=0; i<wrappers.length; i++) {
- wrappers[i] = new FeaturesWrapper(fc.get(i), geomlib, listeners);
+ wrappers[i] = new FeaturesWrapper(fc.get(i), geomlib, listeners, lock);
}
} catch (IllegalArgumentException e) {
throw new DataStoreException(e.getLocalizedMessage(), e);
@@ -446,7 +447,7 @@ public final class DecoderWrapper extends Decoder implements CancelTask {
/*
* If the UCAR library did not recognized the features in this file, ask to SIS.
*/
- return super.getDiscreteSampling();
+ return super.getDiscreteSampling(lock);
}
/**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/FeaturesWrapper.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/FeaturesWrapper.java
index 236060f..3daa109 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/FeaturesWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/FeaturesWrapper.java
@@ -29,7 +29,7 @@ import org.opengis.feature.FeatureType;
/**
* A wrapper around the UCAR {@code ucar.nc2.ft} package.
- * Created by {@link DecoderWrapper#getDiscreteSampling()}.
+ * Created by {@link DecoderWrapper#getDiscreteSampling(Object)}.
*
* @author Martin Desruisseaux (Geomatys)
* @version 1.0
@@ -50,10 +50,13 @@ final class FeaturesWrapper extends DiscreteSampling {
*
* @param factory the library for geometric objects, or {@code null} for the default.
* @param listeners the set of registered warning listeners for the data store.
+ * @param lock the lock to use in {@code synchronized(lock)} statements.
* @throws IllegalArgumentException if the given library is non-null but not available.
*/
- FeaturesWrapper(final FeatureCollection features, final GeometryLibrary factory, final StoreListeners listeners) {
- super(factory, listeners);
+ FeaturesWrapper(final FeatureCollection features, final GeometryLibrary factory, final StoreListeners listeners,
+ final Object lock)
+ {
+ super(factory, listeners, lock);
this.features = features;
}
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 f8d802a..3f6f801 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
@@ -225,7 +225,7 @@ public class NetcdfStore extends DataStore implements Aggregate {
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public synchronized Collection<Resource> components() throws DataStoreException {
if (components == null) try {
- Resource[] resources = decoder.getDiscreteSampling();
+ Resource[] resources = decoder.getDiscreteSampling(this);
final List<Resource> grids = RasterResource.create(decoder, this);
if (!grids.isEmpty()) {
grids.addAll(UnmodifiableArrayList.wrap(resources));
diff --git a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/FeatureSetTest.java b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/FeatureSetTest.java
index c4db0fa..1a57fcf 100644
--- a/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/FeatureSetTest.java
+++ b/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/FeatureSetTest.java
@@ -65,7 +65,11 @@ public strictfp class FeatureSetTest extends TestCase {
*/
@Test
public void testMovingFeatures() throws IOException, DataStoreException {
- final FeatureSet[] features = FeatureSet.create(selectDataset(TestData.MOVING_FEATURES));
+ final Object lock = new Object();
+ final FeatureSet[] features;
+ synchronized (lock) {
+ features = FeatureSet.create(selectDataset(TestData.MOVING_FEATURES), lock);
+ }
assertEquals(1, features.length);
type = features[0].getType();
verifyType(type.getProperties(false).iterator());