Search in sources :

Example 1 with TemporalExtent

use of org.opengis.metadata.extent.TemporalExtent 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 TemporalExtent

use of org.opengis.metadata.extent.TemporalExtent 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)

Aggregations

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