This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 3f28f21  Add `AbstractEnvelope.getTimeRange()` method as a complement 
of `GeneralEnvelope.setTimeRange(…)`. Also available as a static method in 
`Envelopes`.
3f28f21 is described below

commit 3f28f2140fc2b8f3220386d3e780a3824cf1247d
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Tue Mar 16 12:14:52 2021 +0100

    Add `AbstractEnvelope.getTimeRange()` method as a complement of 
`GeneralEnvelope.setTimeRange(…)`.
    Also available as a static method in `Envelopes`.
---
 .../org/apache/sis/coverage/grid/GridGeometry.java |  2 +-
 .../org/apache/sis/geometry/AbstractEnvelope.java  | 21 ++++++++++
 .../java/org/apache/sis/geometry/Envelopes.java    | 27 +++++++++++++
 .../org/apache/sis/geometry/GeneralEnvelope.java   |  3 ++
 .../internal/referencing/ServicesForMetadata.java  | 47 ++++++----------------
 .../sis/internal/referencing/TemporalAccessor.java | 37 +++++++++++++++--
 6 files changed, 97 insertions(+), 40 deletions(-)

diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
index 31d2195..833740f 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridGeometry.java
@@ -944,7 +944,7 @@ public class GridGeometry implements LenientComparable, 
Serializable {
         Instant[] times = timeRange;
         if (times == null) {
             final TemporalAccessor t = 
TemporalAccessor.of(getCoordinateReferenceSystem(envelope), 0);
-            times = (t != null) ? t.getTimeRange(envelope) : 
TemporalAccessor.EMPTY;
+            times = (t != null) ? t.getTimeBounds(envelope) : 
TemporalAccessor.EMPTY;
             timeRange = times;
         }
         return times;
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
 
b/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
index ef8847a..26496b8 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
@@ -22,6 +22,8 @@ package org.apache.sis.geometry;
  * force installation of the Java2D module (e.g. JavaFX/SWT).
  */
 import java.util.Objects;
+import java.util.Optional;
+import java.time.Instant;
 import java.io.Serializable;
 import javax.measure.Unit;
 import javax.measure.IncommensurableException;
@@ -42,9 +44,11 @@ import org.apache.sis.io.wkt.Formatter;
 import org.apache.sis.io.wkt.FormattableObject;
 import org.apache.sis.internal.referencing.WKTUtilities;
 import org.apache.sis.referencing.IdentifiedObjects;
+import org.apache.sis.measure.Range;
 import org.apache.sis.math.Vector;
 
 import static java.lang.Double.doubleToLongBits;
+import org.apache.sis.internal.referencing.TemporalAccessor;
 import static org.apache.sis.internal.util.Numerics.SIGN_BIT_MASK;
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
 import static org.apache.sis.util.ArgumentChecks.ensureDimensionMatches;
@@ -548,6 +552,23 @@ public abstract class AbstractEnvelope extends 
FormattableObject implements Enve
     }
 
     /**
+     * Returns the time range of the first dimension associated to a temporal 
CRS.
+     * This convenience method converts floating point values to instants using
+     * {@link 
org.apache.sis.referencing.crs.DefaultTemporalCRS#toInstant(double)}.
+     *
+     * @return time range in this given envelope.
+     *
+     * @see Envelopes#toTimeRange(Envelope)
+     * @see GeneralEnvelope#setTimeRange(Instant, Instant)
+     *
+     * @since 1.1
+     */
+    public Optional<Range<Instant>> getTimeRange() {
+        final TemporalAccessor t = 
TemporalAccessor.of(getCoordinateReferenceSystem(), 0);
+        return (t != null) ? Optional.of(t.getTimeRange(this)) : 
Optional.empty();
+    }
+
+    /**
      * Returns this envelope as an array of simple (without wraparound) 
envelopes.
      * The length of the returned array depends on the number of dimensions 
where a
      * {@linkplain org.opengis.referencing.cs.RangeMeaning#WRAPAROUND 
wraparound} range is found.
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java 
b/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java
index 63e59c6..a65100e 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/geometry/Envelopes.java
@@ -22,7 +22,9 @@ package org.apache.sis.geometry;
  * force installation of the Java2D module (e.g. JavaFX/SWT).
  */
 import java.util.Set;
+import java.util.Optional;
 import java.util.ConcurrentModificationException;
+import java.time.Instant;
 import org.opengis.geometry.Envelope;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.MismatchedDimensionException;
@@ -44,6 +46,7 @@ import 
org.apache.sis.referencing.operation.transform.AbstractMathTransform;
 import org.apache.sis.internal.metadata.ReferencingServices;
 import org.apache.sis.internal.referencing.CoordinateOperations;
 import org.apache.sis.internal.referencing.DirectPositionView;
+import org.apache.sis.internal.referencing.TemporalAccessor;
 import org.apache.sis.internal.system.Loggers;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.resources.Errors;
@@ -51,6 +54,7 @@ import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.Utilities;
 import org.apache.sis.util.Static;
+import org.apache.sis.measure.Range;
 import org.apache.sis.math.MathFunctions;
 
 import static org.apache.sis.util.StringBuilders.trimFractionalPart;
@@ -1026,4 +1030,27 @@ poles:  for (int i=0; i<dimension; i++, dimensionBitMask 
<<= 1) {
         true,  false,
         false, false
     };
+
+    /**
+     * Returns the time range of the first dimension associated to a temporal 
CRS.
+     * This convenience method converts floating point values to instants using
+     * {@link 
org.apache.sis.referencing.crs.DefaultTemporalCRS#toInstant(double)}.
+     *
+     * @param  envelope  envelope from which to extract time range, or {@code 
null} if none.
+     * @return time range in the given envelope.
+     *
+     * @see AbstractEnvelope#getTimeRange()
+     * @see GeneralEnvelope#setTimeRange(Instant, Instant)
+     *
+     * @since 1.1
+     */
+    public static Optional<Range<Instant>> toTimeRange(final Envelope 
envelope) {
+        if (envelope != null) {
+            final TemporalAccessor t = 
TemporalAccessor.of(envelope.getCoordinateReferenceSystem(), 0);
+            if (t != null) {
+                return Optional.of(t.getTimeRange(envelope));
+            }
+        }
+        return Optional.empty();
+    }
 }
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
 
b/core/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
index e37aeb3..423d8e2 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
@@ -482,6 +482,9 @@ public class GeneralEnvelope extends ArrayEnvelope 
implements Cloneable, Seriali
      *         if no temporal dimension has been found in this envelope.
      *
      * @since 1.0
+     *
+     * @see #getTimeRange()
+     * @see Envelopes#toTimeRange(Envelope)
      */
     public boolean setTimeRange(final Instant startTime, final Instant 
endTime) {
         final TemporalAccessor t = TemporalAccessor.of(crs, 0);
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ServicesForMetadata.java
 
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ServicesForMetadata.java
index 8bcf465..737fed1 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ServicesForMetadata.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ServicesForMetadata.java
@@ -27,7 +27,6 @@ import org.opengis.util.InternationalString;
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.referencing.IdentifiedObject;
 import org.opengis.referencing.crs.SingleCRS;
-import org.opengis.referencing.crs.TemporalCRS;
 import org.opengis.referencing.crs.VerticalCRS;
 import org.opengis.referencing.crs.GeographicCRS;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
@@ -52,7 +51,6 @@ import org.apache.sis.geometry.CoordinateFormat;
 import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.referencing.IdentifiedObjects;
-import org.apache.sis.referencing.crs.DefaultTemporalCRS;
 import org.apache.sis.parameter.DefaultParameterDescriptor;
 import org.apache.sis.metadata.iso.extent.DefaultExtent;
 import org.apache.sis.metadata.iso.extent.DefaultVerticalExtent;
@@ -64,7 +62,6 @@ import org.apache.sis.measure.Longitude;
 import org.apache.sis.internal.metadata.ReferencingServices;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.util.Constants;
-import org.apache.sis.internal.util.TemporalUtilities;
 import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.logging.Logging;
@@ -76,7 +73,7 @@ import org.apache.sis.util.Utilities;
  * Implements the referencing services needed by the {@code "sis-metadata"} 
module.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   0.5
  * @module
  */
@@ -239,26 +236,6 @@ public final class ServicesForMetadata extends 
ReferencingServices {
     }
 
     /**
-     * Implementation of the public {@code setBounds} methods for the temporal 
extent.
-     *
-     * @param  envelope     the source envelope.
-     * @param  target       the target temporal extent.
-     * @param  crs          the envelope CRS (mandatory, can not be {@code 
null}).
-     * @param  temporalCRS  the temporal component of the given CRS 
(mandatory).
-     * @throws UnsupportedOperationException if no implementation of {@code 
TemporalFactory} has been found
-     *         on the classpath.
-     */
-    private static void setTemporalExtent(final Envelope envelope, final 
DefaultTemporalExtent target,
-            final CoordinateReferenceSystem crs, final TemporalCRS temporalCRS)
-    {
-        final int dim = 
AxisDirections.indexOfColinear(crs.getCoordinateSystem(), 
temporalCRS.getCoordinateSystem());
-        assert dim >= 0 : crs;      // Should not fail since 'temporalCRS' has 
been extracted from 'crs' by the caller.
-        final DefaultTemporalCRS converter = 
DefaultTemporalCRS.castOrCopy(temporalCRS);
-        
target.setBounds(TemporalUtilities.toDate(converter.toInstant(envelope.getMinimum(dim))),
-                         
TemporalUtilities.toDate(converter.toInstant(envelope.getMaximum(dim))));
-    }
-
-    /**
      * Sets a geographic bounding box from the specified envelope.
      * If the envelope has no CRS, then (<var>longitude</var>, 
<var>latitude</var>) axis order is assumed.
      * If the envelope CRS is not geographic, then the envelope will be 
transformed to a geographic CRS.
@@ -318,11 +295,11 @@ public final class ServicesForMetadata extends 
ReferencingServices {
     @Override
     public void setBounds(final Envelope envelope, final DefaultTemporalExtent 
target) throws TransformException {
         final CoordinateReferenceSystem crs = 
envelope.getCoordinateReferenceSystem();
-        final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
-        if (temporalCRS == null) {                          // Mandatory for 
the conversion from numbers to dates.
+        final TemporalAccessor accessor = TemporalAccessor.of(crs, 0);
+        if (accessor == null) {                     // Mandatory for the 
conversion from numbers to dates.
             throw new 
TransformException(dimensionNotFound(Resources.Keys.MissingTemporalDimension_1, 
crs));
         }
-        setTemporalExtent(envelope, target, crs, temporalCRS);
+        accessor.setTemporalExtent(envelope, target);
     }
 
     /**
@@ -339,8 +316,8 @@ public final class ServicesForMetadata extends 
ReferencingServices {
         final CoordinateReferenceSystem crs = 
envelope.getCoordinateReferenceSystem();
         final SingleCRS horizontalCRS = CRS.getHorizontalComponent(crs);
         final VerticalCRS verticalCRS = CRS.getVerticalComponent(crs, true);
-        final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
-        if (horizontalCRS == null && verticalCRS == null && temporalCRS == 
null) {
+        final TemporalAccessor accessor = TemporalAccessor.of(crs, 0);
+        if (horizontalCRS == null && verticalCRS == null && accessor == null) {
             throw new 
TransformException(dimensionNotFound(Resources.Keys.MissingSpatioTemporalDimension_1,
 crs));
         }
         /*
@@ -387,8 +364,8 @@ public final class ServicesForMetadata extends 
ReferencingServices {
         } else {
             target.setVerticalExtent(null);
         }
-        if (temporalCRS != null) {
-            setTemporalExtent(envelope, target, crs, temporalCRS);
+        if (accessor != null) {
+            accessor.setTemporalExtent(envelope, target);
         } else {
             target.setExtent(null);
         }
@@ -408,8 +385,8 @@ public final class ServicesForMetadata extends 
ReferencingServices {
         final CoordinateReferenceSystem crs = 
envelope.getCoordinateReferenceSystem();
         final SingleCRS horizontalCRS = CRS.getHorizontalComponent(crs);
         final VerticalCRS verticalCRS = CRS.getVerticalComponent(crs, true);
-        final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
-        if (horizontalCRS == null && verticalCRS == null && temporalCRS == 
null) {
+        final TemporalAccessor accessor = TemporalAccessor.of(crs, 0);
+        if (horizontalCRS == null && verticalCRS == null && accessor == null) {
             throw new 
TransformException(dimensionNotFound(Resources.Keys.MissingSpatioTemporalDimension_1,
 crs));
         }
         if (horizontalCRS != null) {
@@ -420,9 +397,9 @@ public final class ServicesForMetadata extends 
ReferencingServices {
             setVerticalExtent(envelope, extent, crs, verticalCRS);
             target.getVerticalElements().add(extent);
         }
-        if (temporalCRS != null) {
+        if (accessor != null) {
             final DefaultTemporalExtent extent = new DefaultTemporalExtent();
-            setTemporalExtent(envelope, extent, crs, temporalCRS);
+            accessor.setTemporalExtent(envelope, extent);
             target.getTemporalElements().add(extent);
         }
     }
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/TemporalAccessor.java
 
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/TemporalAccessor.java
index 0a80be8..19581ce 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/TemporalAccessor.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/TemporalAccessor.java
@@ -17,11 +17,14 @@
 package org.apache.sis.internal.referencing;
 
 import java.time.Instant;
+import org.opengis.geometry.Envelope;
 import org.opengis.referencing.crs.CompoundCRS;
 import org.opengis.referencing.crs.TemporalCRS;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.referencing.crs.DefaultTemporalCRS;
+import org.apache.sis.metadata.iso.extent.DefaultTemporalExtent;
 import org.apache.sis.geometry.AbstractEnvelope;
+import org.apache.sis.measure.Range;
 
 
 /**
@@ -61,7 +64,7 @@ public final class TemporalAccessor {
 
     /**
      * Creates a new temporal accessor for elements at the given dimensions.
-     * This method search for a temporal component in the given CRS.
+     * This method searches for a temporal component in the given CRS.
      *
      * @param  crs  the coordinate reference system which may contain a 
temporal component, or {@code null}.
      * @param  dim  offset to add to the dimension indices. This is usually 
zero.
@@ -84,14 +87,14 @@ public final class TemporalAccessor {
     }
 
     /**
-     * Returns the lower and upper values in the given envelope. It is 
caller's responsibility to ensure that
-     * the envelope CRS is the same than the one used for creating this {@code 
TemporalAccessor}.
+     * Returns the lower and upper values in the given envelope. It is 
caller's responsibility to ensure
+     * that the envelope CRS is the same than the one used for creating this 
{@code TemporalAccessor}.
      *
      * @param  envelope  the envelope from which to get the start time end end 
time.
      * @return the start time and end time in an array of length 1 or 2, or an 
empty array if none.
      */
     @SuppressWarnings({"fallthrough", "ReturnOfCollectionOrArrayField"})
-    public Instant[] getTimeRange(final AbstractEnvelope envelope) {
+    public Instant[] getTimeBounds(final AbstractEnvelope envelope) {
         Instant startTime = timeCRS.toInstant(envelope.getLower(dimension));
         Instant endTime   = timeCRS.toInstant(envelope.getUpper(dimension));
         if (startTime == null) {
@@ -109,4 +112,30 @@ public final class TemporalAccessor {
         }
         return times;
     }
+
+    /**
+     * Returns the temporal range of given envelope. It is caller's 
responsibility to ensure that
+     * the envelope CRS is the same than the one used for creating this {@code 
TemporalAccessor}.
+     *
+     * @param  envelope  the envelope from which to get the start time end end 
time.
+     * @return the start time and end time.
+     *
+     * @see org.apache.sis.geometry.Envelopes#toTimeRange(Envelope)
+     */
+    public Range<Instant> getTimeRange(final Envelope envelope) {
+        return new Range<>(Instant.class,
+                timeCRS.toInstant(envelope.getMinimum(dimension)), true,
+                timeCRS.toInstant(envelope.getMaximum(dimension)), true);
+    }
+
+    /**
+     * Copies the temporal extent from an envelope to a metadata object.
+     *
+     * @param  envelope  the source envelope.
+     * @param  target    the target temporal extent.
+     */
+    void setTemporalExtent(final Envelope envelope, final 
DefaultTemporalExtent target) {
+        target.setBounds(timeCRS.toDate(envelope.getMinimum(dimension)),
+                         timeCRS.toDate(envelope.getMaximum(dimension)));
+    }
 }

Reply via email to