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 2023/04/14 18:28:08 UTC

[sis] 01/02: Complete API with a few convenience methods: - `CoverageAggregator.add(GridCoverage)` method in addition to existing methods working on resources. - `GridCoverageProcessor.selectSampleDimensions(…)` in complement to `selectGridDimensions(…)`.

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 58df212721234f7f8df65e5e35bb6319af40094d
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Apr 13 13:00:10 2023 +0200

    Complete API with a few convenience methods:
    - `CoverageAggregator.add(GridCoverage)` method in addition to existing methods working on resources.
    - `GridCoverageProcessor.selectSampleDimensions(…)` in complement to `selectGridDimensions(…)`.
---
 .../sis/coverage/grid/GridCoverageProcessor.java   | 27 +++++++++++++++++++++-
 .../sis/storage/aggregate/CoverageAggregator.java  | 26 ++++++++++++++++++++-
 .../apache/sis/storage/aggregate/GridSlice.java    | 15 +++++++++++-
 .../sis/storage/aggregate/GroupByTransform.java    |  2 +-
 4 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
index b56fd29e0f..9394a7c603 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageProcessor.java
@@ -594,6 +594,7 @@ public class GridCoverageProcessor implements Cloneable {
      * @since 1.3
      */
     public GridCoverage resample(final GridCoverage source, final CoordinateReferenceSystem target) throws TransformException {
+        ArgumentChecks.ensureNonNull("source", source);
         ArgumentChecks.ensureNonNull("target", target);
         return resample(source, new GridGeometry(null, PixelInCell.CELL_CENTER, null, target));
     }
@@ -610,6 +611,7 @@ public class GridCoverageProcessor implements Cloneable {
      * @since 1.4
      */
     public GridCoverage reduceDimensionality(final GridCoverage source) {
+        ArgumentChecks.ensureNonNull("source", source);
         return DimensionalityReduction.reduce(source.getGridGeometry()).apply(source);
     }
 
@@ -654,6 +656,7 @@ public class GridCoverageProcessor implements Cloneable {
      * @since 1.4
      */
     public GridCoverage removeGridDimensions(final GridCoverage source, final int... gridAxesToRemove) {
+        ArgumentChecks.ensureNonNull("source", source);
         var reduction = DimensionalityReduction.remove(source.getGridGeometry(), gridAxesToRemove);
         reduction.ensureIsSlice();
         return reduction.apply(source);
@@ -704,15 +707,37 @@ public class GridCoverageProcessor implements Cloneable {
      * @since 1.4
      */
     public GridCoverage selectGridDimensions(final GridCoverage source, final int... gridAxesToPass) {
+        ArgumentChecks.ensureNonNull("source", source);
         var reduction = DimensionalityReduction.select(source.getGridGeometry(), gridAxesToPass);
         reduction.ensureIsSlice();
         return reduction.apply(source);
     }
 
+    /**
+     * Selects a subset of sample dimensions (bands) in the given coverage.
+     * This method can also be used for changing sample dimension order or
+     * for repeating the same sample dimension from the source coverage.
+     * If the specified {@code bands} indices select all sample dimensions
+     * in the same order, then {@code source} is returned directly.
+     *
+     * @param  source  the coverage in which to select sample dimensions.
+     * @param  bands   indices of sample dimensions to retain.
+     * @return coverage width selected sample dimensions.
+     * @throws IllegalArgumentException if a sample dimension index is invalid.
+     *
+     * @see ImageProcessor#selectBands(RenderedImage, int...)
+     *
+     * @since 1.4
+     */
+    public GridCoverage selectSampleDimensions(final GridCoverage source, final int... bands) {
+        ArgumentChecks.ensureNonNull("source", source);
+        return aggregateRanges(new GridCoverage[] {source}, new int[][] {bands});
+    }
+
     /**
      * Aggregates in a single coverage the ranges of all specified coverages, in order.
      * The {@linkplain GridCoverage#getSampleDimensions() list of sample dimensions} of
-     * the aggregated coverage will be the concatenation of the lists of all sources.
+     * the aggregated coverage will be the concatenation of the lists from all sources.
      *
      * <p>This convenience method delegates to {@link #aggregateRanges(GridCoverage[], int[][])}.
      * See that method for more information on restrictions.</p>
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/CoverageAggregator.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/CoverageAggregator.java
index 4b353fce0e..cdba29f5e0 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/CoverageAggregator.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/CoverageAggregator.java
@@ -36,6 +36,7 @@ import org.apache.sis.storage.DataStoreContentException;
 import org.apache.sis.storage.GridCoverageResource;
 import org.apache.sis.storage.event.StoreListeners;
 import org.apache.sis.coverage.grid.GridCoverage;
+import org.apache.sis.coverage.grid.IllegalGridGeometryException;
 import org.apache.sis.coverage.SubspaceNotSpecifiedException;
 import org.apache.sis.util.collection.BackingStoreException;
 
@@ -89,7 +90,7 @@ import org.apache.sis.util.collection.BackingStoreException;
  * and no more addition are in progress.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.3
+ * @version 1.4
  * @since   1.3
  */
 public final class CoverageAggregator extends Group<GroupBySample> {
@@ -141,6 +142,8 @@ public final class CoverageAggregator extends Group<GroupBySample> {
      *
      * @param  resources  resources to add.
      * @throws DataStoreException if a resource cannot be used.
+     *
+     * @see #add(GridCoverageResource)
      */
     public void addAll(final Stream<? extends GridCoverageResource> resources) throws DataStoreException {
         try {
@@ -156,6 +159,27 @@ public final class CoverageAggregator extends Group<GroupBySample> {
         }
     }
 
+    /**
+     * Adds the given coverage. This method can be invoked from any thread.
+     *
+     * @param  coverage  coverage to add.
+     *
+     * @since 1.4
+     */
+    public void add(final GridCoverage coverage) {
+        final GroupBySample bySample = GroupBySample.getOrAdd(members, coverage.getSampleDimensions());
+        final GridSlice slice = new GridSlice(coverage);
+        final List<GridSlice> slices;
+        try {
+            slices = slice.getList(bySample.members, strategy).members;
+        } catch (NoninvertibleTransformException e) {
+            throw new IllegalGridGeometryException(e);
+        }
+        synchronized (slices) {
+            slices.add(slice);
+        }
+    }
+
     /**
      * Adds the given resource. This method can be invoked from any thread.
      * This method does <em>not</em> recursively decomposes an {@link Aggregate} into its component.
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GridSlice.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GridSlice.java
index 489af18d74..cc8bff034f 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GridSlice.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GridSlice.java
@@ -26,8 +26,10 @@ import org.opengis.referencing.operation.NoninvertibleTransformException;
 import org.apache.sis.referencing.operation.matrix.MatrixSIS;
 import org.apache.sis.storage.GridCoverageResource;
 import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.coverage.grid.GridCoverage;
 import org.apache.sis.coverage.grid.GridGeometry;
 import org.apache.sis.coverage.grid.GridExtent;
+import org.apache.sis.internal.storage.MemoryGridResource;
 import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.internal.util.Strings;
 import org.apache.sis.util.Numbers;
@@ -42,7 +44,7 @@ import org.apache.sis.util.Numbers;
  * are grouped by "grid to CRS" transform in the {@link GroupByTransform#members} list.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.3
+ * @version 1.4
  * @since   1.3
  */
 final class GridSlice {
@@ -63,6 +65,17 @@ final class GridSlice {
      */
     private final long[] offset;
 
+    /**
+     * Creates a new slice for the specified coverage.
+     *
+     * @param  slice  coverage associated to this slice.
+     */
+    GridSlice(final GridCoverage slice) {
+        resource = new MemoryGridResource(null, slice);
+        geometry = slice.getGridGeometry();
+        offset   = new long[geometry.getDimension()];
+    }
+
     /**
      * Creates a new slice for the specified resource.
      *
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GroupByTransform.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GroupByTransform.java
index aef7c80bd9..540483160e 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GroupByTransform.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/aggregate/GroupByTransform.java
@@ -112,7 +112,7 @@ final class GroupByTransform extends Group<GridSlice> {
     }
 
     /**
-     * Returns dimensions to aggregate, in order of recommendation.
+     * Returns grid dimensions to aggregate, in order of recommendation.
      * Aggregations should use the first dimension in the returned list.
      *
      * @todo A future version should add {@code findMosaicDimensions()}, which should be tested first.