Search in sources :

Example 1 with TemporalPrimitive

use of org.opengis.temporal.TemporalPrimitive in project sis by apache.

the class Extents method getDate.

/**
 * Returns an instant in the {@linkplain Extent#getTemporalElements() temporal elements} of the given extent,
 * or {@code null} if none. First, this method computes the union of all temporal elements. Then this method
 * computes the linear interpolation between the start and end time as in the following pseudo-code:
 *
 * {@preformat java
 *     return new Date(startTime + (endTime - startTime) * location);
 * }
 *
 * Special cases:
 * <ul>
 *   <li>If {@code location} is 0, then this method returns the {@linkplain DefaultTemporalExtent#getStartTime() start time}.</li>
 *   <li>If {@code location} is 1, then this method returns the {@linkplain DefaultTemporalExtent#getEndTime() end time}.</li>
 *   <li>If {@code location} is 0.5, then this method returns the average of start time and end time.</li>
 *   <li>If {@code location} is outside the [0 … 1] range, then the result will be outside the temporal extent.</li>
 * </ul>
 *
 * @param  extent    the extent from which to get an instant, or {@code null}.
 * @param  location  0 for the start time, 1 for the end time, 0.5 for the average time, or the
 *                   coefficient (usually in the [0 … 1] range) for interpolating an instant.
 * @return an instant interpolated at the given location, or {@code null} if none.
 *
 * @since 0.4
 */
public static Date getDate(final Extent extent, final double location) {
    ArgumentChecks.ensureFinite("location", location);
    Date min = null;
    Date max = null;
    if (extent != null) {
        for (final TemporalExtent t : extent.getTemporalElements()) {
            Date startTime = null;
            Date endTime = null;
            if (t instanceof DefaultTemporalExtent) {
                final DefaultTemporalExtent dt = (DefaultTemporalExtent) t;
                // Maybe user has overridden those methods.
                if (location != 1)
                    startTime = dt.getStartTime();
                if (location != 0)
                    endTime = dt.getEndTime();
            } else {
                final TemporalPrimitive p = t.getExtent();
                if (location != 1)
                    startTime = DefaultTemporalExtent.getTime(p, true);
                if (location != 0)
                    endTime = DefaultTemporalExtent.getTime(p, false);
            }
            if (startTime != null && (min == null || startTime.before(min)))
                min = startTime;
            if (endTime != null && (max == null || endTime.after(max)))
                max = endTime;
        }
    }
    if (min == null)
        return max;
    if (max == null)
        return min;
    final long startTime = min.getTime();
    return new Date(Math.addExact(startTime, Math.round((max.getTime() - startTime) * location)));
}
Also used : TemporalExtent(org.opengis.metadata.extent.TemporalExtent) TemporalPrimitive(org.opengis.temporal.TemporalPrimitive) Date(java.util.Date)

Example 2 with TemporalPrimitive

use of org.opengis.temporal.TemporalPrimitive in project sis by apache.

the class DefaultTemporalExtent method intersect.

/**
 * Sets this temporal extent to the intersection of this extent with the specified one.
 * If there is no intersection between the two extents, then this method sets the temporal primitive to nil.
 * If either this extent or the specified extent has nil primitive, then the intersection result will also be nil.
 *
 * @param  other  the temporal extent to intersect with this extent.
 * @throws UnsupportedOperationException if no implementation of {@code TemporalFactory} has been found
 *         on the classpath.
 *
 * @see Extents#intersection(TemporalExtent, TemporalExtent)
 * @see org.apache.sis.geometry.GeneralEnvelope#intersect(Envelope)
 *
 * @since 0.8
 */
public void intersect(final TemporalExtent other) {
    checkWritePermission();
    final TemporalPrimitive ot = other.getExtent();
    if (ot != null && !(extent instanceof NilObject)) {
        if (extent == null || (ot instanceof NilObject)) {
            extent = ot;
        } else {
            Date t0 = getTime(extent, true);
            Date t1 = getTime(extent, false);
            Date h0 = getTime(ot, true);
            Date h1 = getTime(ot, false);
            boolean changed = false;
            if (h0 != null && (t0 == null || h0.after(t0))) {
                t0 = h0;
                changed = true;
            }
            if (h1 != null && (t1 == null || h1.before(t1))) {
                t1 = h1;
                changed = true;
            }
            if (changed) {
                if (t0 != null && t1 != null && t0.after(t1)) {
                    extent = NilReason.MISSING.createNilObject(TemporalPrimitive.class);
                } else {
                    setBounds(t0, t1);
                }
            }
        }
    }
}
Also used : TemporalPrimitive(org.opengis.temporal.TemporalPrimitive) NilObject(org.apache.sis.xml.NilObject) Date(java.util.Date)

Example 3 with TemporalPrimitive

use of org.opengis.temporal.TemporalPrimitive in project sis by apache.

the class Extents method getTimeRange.

/**
 * Returns the union of all time ranges found in the given extent, or {@code null} if none.
 *
 * @param  extent  the extent to convert to a time range, or {@code null}.
 * @return a time range created from the given extent, or {@code null} if none.
 *
 * @since 0.4
 */
public static Range<Date> getTimeRange(final Extent extent) {
    Date min = null;
    Date max = null;
    if (extent != null) {
        for (final TemporalExtent t : extent.getTemporalElements()) {
            final Date startTime, endTime;
            if (t instanceof DefaultTemporalExtent) {
                final DefaultTemporalExtent dt = (DefaultTemporalExtent) t;
                // Maybe user has overridden those methods.
                startTime = dt.getStartTime();
                endTime = dt.getEndTime();
            } else {
                final TemporalPrimitive p = t.getExtent();
                startTime = DefaultTemporalExtent.getTime(p, true);
                endTime = DefaultTemporalExtent.getTime(p, false);
            }
            if (startTime != null && (min == null || startTime.before(min)))
                min = startTime;
            if (endTime != null && (max == null || endTime.after(max)))
                max = endTime;
        }
    }
    if (min == null && max == null) {
        return null;
    }
    return new Range<>(Date.class, min, true, max, true);
}
Also used : TemporalExtent(org.opengis.metadata.extent.TemporalExtent) TemporalPrimitive(org.opengis.temporal.TemporalPrimitive) Range(org.apache.sis.measure.Range) MeasurementRange(org.apache.sis.measure.MeasurementRange) Date(java.util.Date)

Example 4 with TemporalPrimitive

use of org.opengis.temporal.TemporalPrimitive in project sis by apache.

the class DefaultTemporalExtent method setBounds.

/**
 * Sets the temporal extent to the specified values. This convenience method creates a temporal
 * primitive for the given dates, then invokes {@link #setExtent(TemporalPrimitive)}.
 *
 * <p><b>Note:</b> this method is available only if the {@code sis-temporal} module is available on the classpath,
 * or any other module providing an implementation of the {@code TemporalFactory} interface.</p>
 *
 * @param  startTime  the start date and time for the content of the dataset, or {@code null} if none.
 * @param  endTime    the end date and time for the content of the dataset, or {@code null} if none.
 * @throws UnsupportedOperationException if no implementation of {@code TemporalFactory} has been found
 *         on the classpath.
 */
public void setBounds(final Date startTime, final Date endTime) throws UnsupportedOperationException {
    TemporalPrimitive value = null;
    if (startTime != null || endTime != null) {
        if (endTime == null || endTime.equals(startTime)) {
            value = TemporalUtilities.createInstant(startTime);
        } else if (startTime == null) {
            value = TemporalUtilities.createInstant(endTime);
        } else {
            value = TemporalUtilities.createPeriod(startTime, endTime);
        }
    }
    setExtent(value);
}
Also used : TemporalPrimitive(org.opengis.temporal.TemporalPrimitive)

Aggregations

TemporalPrimitive (org.opengis.temporal.TemporalPrimitive)4 Date (java.util.Date)3 TemporalExtent (org.opengis.metadata.extent.TemporalExtent)2 MeasurementRange (org.apache.sis.measure.MeasurementRange)1 Range (org.apache.sis.measure.Range)1 NilObject (org.apache.sis.xml.NilObject)1