Search in sources :

Example 1 with TemporalDatum

use of org.opengis.referencing.datum.TemporalDatum in project sis by apache.

the class GeodeticObjectBuilder method createTemporalCRS.

/**
 * Creates a temporal CRS from the given origin and temporal unit. For this method, the CRS name is optional:
 * if no {@code addName(…)} method has been invoked, then a default name will be used.
 *
 * @param  origin  the epoch in milliseconds since January 1st, 1970 at midnight UTC.
 * @param  unit    the unit of measurement.
 * @return a temporal CRS using the given origin and units.
 * @throws FactoryException if an error occurred while building the temporal CRS.
 */
public TemporalCRS createTemporalCRS(final Date origin, final Unit<Time> unit) throws FactoryException {
    /*
         * Try to use one of the pre-defined datum and coordinate system if possible.
         * This not only saves a little bit of memory, but also provides better names.
         */
    TimeCS cs = null;
    TemporalDatum datum = null;
    for (final CommonCRS.Temporal c : CommonCRS.Temporal.values()) {
        if (datum == null) {
            final TemporalDatum candidate = c.datum();
            if (origin.equals(candidate.getOrigin())) {
                datum = candidate;
            }
        }
        if (cs == null) {
            final TemporalCRS crs = c.crs();
            final TimeCS candidate = crs.getCoordinateSystem();
            if (unit.equals(candidate.getAxis(0).getUnit())) {
                if (datum == candidate && properties.isEmpty()) {
                    return crs;
                }
                cs = candidate;
            }
        }
    }
    /*
         * Create the datum and coordinate system before the CRS if we were not able to use a pre-defined object.
         * In the datum case, we will use the same metadata than the CRS (domain of validity, scope, etc.) except
         * the identifier and the remark.
         */
    onCreate(false);
    try {
        if (cs == null) {
            final CSFactory csFactory = getCSFactory();
            // To be used as a template, except for units.
            cs = CommonCRS.Temporal.JAVA.crs().getCoordinateSystem();
            cs = csFactory.createTimeCS(name(cs), csFactory.createCoordinateSystemAxis(name(cs.getAxis(0)), "t", AxisDirection.FUTURE, unit));
        }
        if (properties.get(TemporalCRS.NAME_KEY) == null) {
            properties.putAll(name(cs));
        }
        if (datum == null) {
            final Object remarks = properties.remove(TemporalCRS.REMARKS_KEY);
            final Object identifier = properties.remove(TemporalCRS.IDENTIFIERS_KEY);
            datum = getDatumFactory().createTemporalDatum(properties, origin);
            properties.put(TemporalCRS.IDENTIFIERS_KEY, identifier);
            properties.put(TemporalCRS.REMARKS_KEY, remarks);
            // Share the Identifier instance.
            properties.put(TemporalCRS.NAME_KEY, datum.getName());
        }
        return getCRSFactory().createTemporalCRS(properties, datum, cs);
    } finally {
        onCreate(true);
    }
}
Also used : TemporalDatum(org.opengis.referencing.datum.TemporalDatum) TemporalCRS(org.opengis.referencing.crs.TemporalCRS) CSFactory(org.opengis.referencing.cs.CSFactory) IdentifiedObject(org.opengis.referencing.IdentifiedObject) CommonCRS(org.apache.sis.referencing.CommonCRS) TimeCS(org.opengis.referencing.cs.TimeCS)

Example 2 with TemporalDatum

use of org.opengis.referencing.datum.TemporalDatum in project sis by apache.

the class CommonCRSTest method testTemporal.

/**
 * Verifies the epoch values of temporal enumeration compared to the Julian epoch.
 *
 * @see <a href="http://en.wikipedia.org/wiki/Julian_day">Wikipedia: Julian day</a>
 */
@Test
public void testTemporal() {
    final double julianEpoch = CommonCRS.Temporal.JULIAN.datum().getOrigin().getTime() / DAY_LENGTH;
    assertTrue(julianEpoch < 0);
    for (final CommonCRS.Temporal e : CommonCRS.Temporal.values()) {
        final String epoch;
        final double days;
        switch(e) {
            // Fall through
            case JAVA:
            case UNIX:
                epoch = "1970-01-01 00:00:00";
                days = 2440587.5;
                break;
            case TRUNCATED_JULIAN:
                epoch = "1968-05-24 00:00:00";
                days = 2440000.5;
                break;
            case DUBLIN_JULIAN:
                epoch = "1899-12-31 12:00:00";
                days = 2415020.0;
                break;
            case MODIFIED_JULIAN:
                epoch = "1858-11-17 00:00:00";
                days = 2400000.5;
                break;
            case JULIAN:
                epoch = "4713-01-01 12:00:00";
                days = 0;
                break;
            default:
                throw new AssertionError(e);
        }
        final String name = e.name();
        final TemporalDatum datum = e.datum();
        final TemporalCRS crs = e.crs();
        final Date origin = datum.getOrigin();
        Validators.validate(crs);
        // Datum before CRS creation.
        assertSame(name, datum, e.datum());
        // Datum after CRS creation.
        assertSame(name, crs.getDatum(), e.datum());
        assertEquals(name, epoch, format(origin));
        assertEquals(name, days, origin.getTime() / DAY_LENGTH - julianEpoch, 0);
    }
}
Also used : TemporalDatum(org.opengis.referencing.datum.TemporalDatum) TemporalCRS(org.opengis.referencing.crs.TemporalCRS) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TemporalCRS (org.opengis.referencing.crs.TemporalCRS)2 TemporalDatum (org.opengis.referencing.datum.TemporalDatum)2 Date (java.util.Date)1 CommonCRS (org.apache.sis.referencing.CommonCRS)1 Test (org.junit.Test)1 IdentifiedObject (org.opengis.referencing.IdentifiedObject)1 CSFactory (org.opengis.referencing.cs.CSFactory)1 TimeCS (org.opengis.referencing.cs.TimeCS)1