Search in sources :

Example 6 with Time

use of javax.measure.quantity.Time in project sis by apache.

the class Store method parseEnvelope.

/**
 * Parses the envelope described by the header line starting with {@code @stboundedby}.
 * The envelope returned by this method will be stored in the {@link #envelope} field.
 *
 * <p>Example:</p>
 * {@preformat text
 *   &#64;stboundedby, urn:ogc:def:crs:CRS:1.3:84, 2D, 50.23 9.23, 50.31 9.27, 2012-01-17T12:33:41Z, 2012-01-17T12:37:00Z, sec
 * }
 *
 * This method sets {@link #timeEncoding} and {@link #spatialDimensionCount} as a side-effect.
 *
 * @param  elements  the line elements. The first elements should be {@code "@stboundedby"}.
 * @return the envelope, or {@code null} if the given list does not contain enough elements.
 */
@SuppressWarnings("fallthrough")
private GeneralEnvelope parseEnvelope(final List<String> elements) throws DataStoreException, FactoryException {
    CoordinateReferenceSystem crs = null;
    int spatialDimensionCount = 2;
    boolean isDimExplicit = false;
    double[] lowerCorner = ArraysExt.EMPTY_DOUBLE;
    double[] upperCorner = ArraysExt.EMPTY_DOUBLE;
    Instant startTime = null;
    Instant endTime = null;
    Unit<Time> timeUnit = Units.SECOND;
    boolean isTimeAbsolute = false;
    int ordinal = -1;
    for (final String element : elements) {
        ordinal++;
        if (!element.isEmpty()) {
            switch(ordinal) {
                // The "@stboundedby" header.
                case 0:
                    continue;
                case 1:
                    crs = CRS.forCode(element);
                    continue;
                case 2:
                    if (element.length() == 2 && Character.toUpperCase(element.charAt(1)) == 'D') {
                        isDimExplicit = true;
                        spatialDimensionCount = element.charAt(0) - '0';
                        if (spatialDimensionCount < 1 || spatialDimensionCount > 3) {
                            throw new DataStoreReferencingException(errors().getString(Errors.Keys.IllegalCoordinateSystem_1, element));
                        }
                        continue;
                    }
                    /*
                             * According the Moving Feature specification, the [dim] element is optional.
                             * If we did not recognized the dimension, assume that we have the next element
                             * (i.e. the lower corner). Fall-through so we can process it.
                             */
                    // Fall through
                    ordinal++;
                case 3:
                    lowerCorner = CharSequences.parseDoubles(element, ORDINATE_SEPARATOR);
                    continue;
                case 4:
                    upperCorner = CharSequences.parseDoubles(element, ORDINATE_SEPARATOR);
                    continue;
                case 5:
                    startTime = Instant.parse(element);
                    continue;
                case 6:
                    endTime = Instant.parse(element);
                    continue;
                case 7:
                    switch(element.toLowerCase(Locale.US)) {
                        case "sec":
                        case "second":
                            /* Already SECOND. */
                            continue;
                        case "minute":
                            timeUnit = Units.MINUTE;
                            continue;
                        case "hour":
                            timeUnit = Units.HOUR;
                            continue;
                        case "day":
                            timeUnit = Units.DAY;
                            continue;
                        case "absolute":
                            isTimeAbsolute = true;
                            continue;
                        default:
                            throw new DataStoreReferencingException(errors().getString(Errors.Keys.UnknownUnit_1, element));
                    }
            }
            // If we reach this point, there is some remaining unknown elements. Ignore them.
            break;
        }
    }
    /*
         * Complete the CRS by adding a vertical component if needed, then a temporal component.
         * Only after the CRS has been completed we can create the envelope.
         *
         * Vertical component:
         *   Ideally, should be part of the CRS created from the authority code. But if the authority
         *   code is only for a two-dimensional CRS, we default to an arbitrary height component.
         *
         * Temporal component:
         *   Assumed never part of the authority code. We need to build the temporal component ourselves
         *   in order to set the origin to the start time.
         */
    final GeneralEnvelope envelope;
    if (crs != null) {
        int count = 0;
        final CoordinateReferenceSystem[] components = new CoordinateReferenceSystem[3];
        components[count++] = crs;
        /*
             * If the coordinates are three-dimensional but the CRS is 2D, add a vertical axis.
             * The vertical axis shall be the third one, however we do not enforce that rule
             * since Apache SIS should work correctly even if the vertical axis is elsewhere.
             */
        int dimension = crs.getCoordinateSystem().getDimension();
        if (isDimExplicit) {
            if (spatialDimensionCount > dimension) {
                components[count++] = CommonCRS.Vertical.MEAN_SEA_LEVEL.crs();
                dimension++;
            }
            if (dimension != spatialDimensionCount) {
                throw new DataStoreReferencingException(errors().getString(Errors.Keys.MismatchedDimension_3, "@stboundedby(CRS)", spatialDimensionCount, dimension));
            }
        }
        if (dimension > Short.MAX_VALUE) {
            throw new DataStoreReferencingException(errors().getString(Errors.Keys.ExcessiveNumberOfDimensions_1, dimension));
        }
        spatialDimensionCount = dimension;
        /*
             * Add a temporal axis if we have a start time (no need for end time).
             * This block presumes that the CRS does not already have a time axis.
             * If a time axis was already present, an exception will be thrown at
             * builder.createCompoundCRS(…) invocation time.
             */
        final GeodeticObjectBuilder builder = new GeodeticObjectBuilder();
        String name = crs.getName().getCode();
        if (startTime != null) {
            final TemporalCRS temporal;
            if (isTimeAbsolute) {
                temporal = TimeEncoding.DEFAULT.crs();
                timeEncoding = TimeEncoding.ABSOLUTE;
            } else {
                temporal = builder.createTemporalCRS(Date.from(startTime), timeUnit);
                timeEncoding = new TimeEncoding(temporal.getDatum(), timeUnit);
            }
            components[count++] = temporal;
            name = name + " + " + temporal.getName().getCode();
        }
        crs = builder.addName(name).createCompoundCRS(ArraysExt.resize(components, count));
        envelope = new GeneralEnvelope(crs);
    } else {
        /*
             * While illegal in principle, Apache SIS accepts missing CRS.
             * In such case, use only the number of dimensions.
             */
        int dim = spatialDimensionCount;
        // Same criterion than in above block.
        if (startTime != null)
            dim++;
        envelope = new GeneralEnvelope(dim);
    }
    /*
         * At this point we got the three- or four-dimensional spatio-temporal CRS.
         * We can now set the envelope coordinate values, including temporal values.
         */
    int dim;
    if ((dim = lowerCorner.length) != spatialDimensionCount || (dim = upperCorner.length) != spatialDimensionCount) {
        throw new DataStoreReferencingException(errors().getString(Errors.Keys.MismatchedDimension_3, "@stboundedby(BBOX)", spatialDimensionCount, dim));
    }
    for (int i = 0; i < spatialDimensionCount; i++) {
        envelope.setRange(i, lowerCorner[i], upperCorner[i]);
    }
    if (startTime != null) {
        envelope.setRange(spatialDimensionCount, timeEncoding.toCRS(startTime.toEpochMilli()), (endTime == null) ? Double.NaN : timeEncoding.toCRS(endTime.toEpochMilli()));
    }
    this.spatialDimensionCount = (short) spatialDimensionCount;
    return envelope;
}
Also used : Instant(java.time.Instant) Time(javax.measure.quantity.Time) TemporalCRS(org.opengis.referencing.crs.TemporalCRS) DataStoreReferencingException(org.apache.sis.storage.DataStoreReferencingException) GeodeticObjectBuilder(org.apache.sis.internal.referencing.GeodeticObjectBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 7 with Time

use of javax.measure.quantity.Time in project indriya by unitsofmeasurement.

the class TimeQuantitiesConcurrentTest method ofQuantityTest.

@Test
public void ofQuantityTest() {
    Quantity<Time> hour = Quantities.getQuantity(1, Units.HOUR);
    TimeUnitQuantity timeQuantity = TimeUnitQuantity.of(hour);
    assertEquals(TimeUnit.SECONDS, timeQuantity.getTimeUnit());
    assertEquals(SECOND, timeQuantity.toUnit());
    assertEquals(Integer.valueOf(3600), timeQuantity.getValue());
}
Also used : Time(javax.measure.quantity.Time) TimeUnitQuantity(tech.units.indriya.quantity.time.TimeUnitQuantity) Test(org.junit.jupiter.api.Test)

Example 8 with Time

use of javax.measure.quantity.Time in project indriya by unitsofmeasurement.

the class TimeQuantitiesTest method ofTemporalAdjustTest.

@Test
public void ofTemporalAdjustTest() {
    LocalDate a = Year.of(2015).atMonth(Month.JANUARY).atDay(9);
    Quantity<Time> time = TimeQuantities.getQuantity(a, () -> TemporalAdjusters.next(DayOfWeek.SUNDAY));
    assertEquals(Integer.valueOf(2), Integer.valueOf(time.getValue().intValue()));
    assertEquals(Units.DAY, time.getUnit());
}
Also used : LocalTime(java.time.LocalTime) Time(javax.measure.quantity.Time) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test)

Example 9 with Time

use of javax.measure.quantity.Time in project indriya by unitsofmeasurement.

the class TimeQuantitiesTest method ofQuantityTest.

@Test
public void ofQuantityTest() {
    Quantity<Time> hour = Quantities.getQuantity(1, Units.HOUR);
    TemporalQuantity timeQuantity = TemporalQuantity.of(hour);
    assertEquals(SECONDS, timeQuantity.getTemporalUnit());
    assertEquals(SECOND, timeQuantity.toUnit());
    assertEquals(Integer.valueOf(3600), timeQuantity.getValue());
}
Also used : LocalTime(java.time.LocalTime) Time(javax.measure.quantity.Time) TemporalQuantity(tech.units.indriya.quantity.time.TemporalQuantity) Test(org.junit.jupiter.api.Test)

Example 10 with Time

use of javax.measure.quantity.Time in project indriya by unitsofmeasurement.

the class TimeQuantitiesTest method ofLocalTimeTest.

@Test
public void ofLocalTimeTest() {
    LocalTime a = LocalTime.of(0, 0);
    LocalTime b = LocalTime.of(12, 0);
    Quantity<Time> time = TimeQuantities.getQuantity(a, b);
    assertEquals(Double.valueOf(12.0), Double.valueOf(time.getValue().doubleValue()));
    assertEquals(Units.HOUR, time.getUnit());
}
Also used : LocalTime(java.time.LocalTime) LocalTime(java.time.LocalTime) Time(javax.measure.quantity.Time) Test(org.junit.jupiter.api.Test)

Aggregations

Time (javax.measure.quantity.Time)15 LocalTime (java.time.LocalTime)10 Test (org.junit.Test)7 Test (org.junit.jupiter.api.Test)7 LocalDate (java.time.LocalDate)6 BigDecimal (java.math.BigDecimal)2 DayOfWeek (java.time.DayOfWeek)2 Month (java.time.Month)2 Year (java.time.Year)2 ChronoUnit (java.time.temporal.ChronoUnit)2 TemporalAdjuster (java.time.temporal.TemporalAdjuster)2 TemporalAdjusters (java.time.temporal.TemporalAdjusters)2 Quantity (javax.measure.Quantity)2 Unit (javax.measure.Unit)2 TemporalQuantity (tec.uom.se.quantity.time.TemporalQuantity)2 TemporalQuantity (tech.units.indriya.quantity.time.TemporalQuantity)2 TimeUnitQuantity (tech.units.indriya.quantity.time.TimeUnitQuantity)2 Instant (java.time.Instant)1 TimeUnit (java.util.concurrent.TimeUnit)1 Frequency (javax.measure.quantity.Frequency)1