Search in sources :

Example 31 with UnitConverter

use of javax.measure.UnitConverter in project sis by apache.

the class ConventionalUnit method getConverterTo.

/**
 * Returns a converter of numeric values from this unit to another unit of same type.
 *
 * @param  that  the unit of same type to which to convert the numeric values.
 * @return the converter from this unit to {@code that} unit.
 * @throws UnconvertibleException if the converter can not be constructed.
 */
@Override
public UnitConverter getConverterTo(final Unit<Q> that) throws UnconvertibleException {
    if (that == this) {
        return LinearConverter.IDENTITY;
    }
    ArgumentChecks.ensureNonNull("that", that);
    UnitConverter c = toTarget;
    if (target != that) {
        // Optimization for a common case.
        final Unit<Q> step = that.getSystemUnit();
        if (target != step && !target.isCompatible(step)) {
            // Should never occur unless parameterized type has been compromised.
            throw new UnconvertibleException(incompatible(that));
        }
        // Usually leave 'c' unchanged.
        c = target.getConverterTo(step).concatenate(c);
        c = step.getConverterTo(that).concatenate(c);
    }
    return c;
}
Also used : UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException)

Example 32 with UnitConverter

use of javax.measure.UnitConverter in project sis by apache.

the class ConventionalUnit method getConverterToAny.

/**
 * Returns a converter from this unit to the specified unit of unknown type.
 * This method can be used when the quantity type of the specified unit is unknown at compile-time
 * or when dimensional analysis allows for conversion between units of different type.
 *
 * @param  that  the unit to which to convert the numeric values.
 * @return the converter from this unit to {@code that} unit.
 * @throws IncommensurableException if this unit is not {@linkplain #isCompatible(Unit) compatible} with {@code that} unit.
 *
 * @see #isCompatible(Unit)
 */
@Override
public UnitConverter getConverterToAny(final Unit<?> that) throws IncommensurableException {
    if (that == this) {
        return LinearConverter.IDENTITY;
    }
    ArgumentChecks.ensureNonNull("that", that);
    UnitConverter c = toTarget;
    if (target != that) {
        // Optimization for a common case.
        final Unit<?> step = that.getSystemUnit();
        if (target != step && !target.isCompatible(step)) {
            throw new IncommensurableException(incompatible(that));
        }
        // Usually leave 'c' unchanged.
        c = target.getConverterToAny(step).concatenate(c);
        c = step.getConverterToAny(that).concatenate(c);
    }
    return c;
}
Also used : IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter)

Example 33 with UnitConverter

use of javax.measure.UnitConverter in project sis by apache.

the class MetadataReader method addExtent.

/**
 * Adds the extent declared in the current group. For more consistent results, the caller should restrict
 * the {@linkplain Decoder#setSearchPath search path} to a single group before invoking this method.
 *
 * @return {@code true} if at least one numerical value has been added.
 */
private boolean addExtent() {
    addExtent(stringValue(GEOGRAPHIC_IDENTIFIER));
    /*
         * If at least one geographic ordinates is available, add a GeographicBoundingBox.
         */
    final Number xmin = decoder.numericValue(LONGITUDE.MINIMUM);
    final Number xmax = decoder.numericValue(LONGITUDE.MAXIMUM);
    final Number ymin = decoder.numericValue(LATITUDE.MINIMUM);
    final Number ymax = decoder.numericValue(LATITUDE.MAXIMUM);
    final Number zmin = decoder.numericValue(VERTICAL.MINIMUM);
    final Number zmax = decoder.numericValue(VERTICAL.MAXIMUM);
    boolean hasExtent = (xmin != null || xmax != null || ymin != null || ymax != null);
    if (hasExtent) {
        final UnitConverter xConv = getConverterTo(decoder.unitValue(LONGITUDE.UNITS), Units.DEGREE);
        final UnitConverter yConv = getConverterTo(decoder.unitValue(LATITUDE.UNITS), Units.DEGREE);
        addExtent(new double[] { valueOf(xmin, xConv), valueOf(xmax, xConv), valueOf(ymin, yConv), valueOf(ymax, yConv) }, 0);
    }
    /*
         * If at least one vertical ordinates above is available, add a VerticalExtent.
         */
    if (zmin != null || zmax != null) {
        final UnitConverter c = getConverterTo(decoder.unitValue(VERTICAL.UNITS), Units.METRE);
        double min = valueOf(zmin, c);
        double max = valueOf(zmax, c);
        if (CF.POSITIVE_DOWN.equals(stringValue(VERTICAL.POSITIVE))) {
            final double tmp = min;
            min = -max;
            max = -tmp;
        }
        addVerticalExtent(min, max, VERTICAL_CRS);
        hasExtent = true;
    }
    /*
         * Get the start and end times as Date objects if available, or as numeric values otherwise.
         * In the later case, the unit symbol tells how to convert to Date objects.
         */
    Date startTime = decoder.dateValue(TIME.MINIMUM);
    Date endTime = decoder.dateValue(TIME.MAXIMUM);
    if (startTime == null && endTime == null) {
        final Number tmin = decoder.numericValue(TIME.MINIMUM);
        final Number tmax = decoder.numericValue(TIME.MAXIMUM);
        if (tmin != null || tmax != null) {
            final String symbol = stringValue(TIME.UNITS);
            if (symbol != null) {
                final Date[] dates = decoder.numberToDate(symbol, tmin, tmax);
                startTime = dates[0];
                endTime = dates[1];
            }
        }
    }
    /*
         * If at least one time values above is available, add a temporal extent.
         * This operation requires the the sis-temporal module. If not available,
         * we will report a warning and leave the temporal extent missing.
         */
    if (startTime != null || endTime != null)
        try {
            addTemporalExtent(startTime, endTime);
            hasExtent = true;
        } catch (UnsupportedOperationException e) {
            warning(e);
        }
    return hasExtent;
}
Also used : UnitConverter(javax.measure.UnitConverter) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) Date(java.util.Date)

Example 34 with UnitConverter

use of javax.measure.UnitConverter in project sis by apache.

the class CRSBuilder method verify.

/**
 * Verifies if the user-defined ellipsoid created from GeoTIFF values
 * matches the given ellipsoid created from the EPSG geodetic dataset.
 * This method does not verify the EPSG code.
 *
 * @param  ellipsoid  the ellipsoid created from the EPSG geodetic dataset.
 * @param  unit       the unit of measurement declared in the GeoTIFF file.
 */
private void verify(final Ellipsoid ellipsoid, final Unit<Length> unit) {
    final UnitConverter uc = ellipsoid.getAxisUnit().getConverterTo(unit);
    verify(ellipsoid, uc.convert(ellipsoid.getSemiMajorAxis()), GeoKeys.SemiMajorAxis, unit);
    verify(ellipsoid, uc.convert(ellipsoid.getSemiMinorAxis()), GeoKeys.SemiMinorAxis, unit);
    verify(ellipsoid, ellipsoid.getInverseFlattening(), GeoKeys.InvFlattening, null);
}
Also used : UnitConverter(javax.measure.UnitConverter)

Example 35 with UnitConverter

use of javax.measure.UnitConverter in project com.revolsys.open by revolsys.

the class GeographicCoordinateSystem method getLengthUnit.

@Override
public Unit<Length> getLengthUnit() {
    final Unit<Angle> unit = this.angularUnit.getUnit();
    final UnitConverter radianConverter = unit.getConverterTo(Units.RADIAN);
    final Ellipsoid ellipsoid = this.geodeticDatum.getEllipsoid();
    final double radius = ellipsoid.getSemiMajorAxis();
    final double radianFactor = radianConverter.convert(1);
    return Units.METRE.multiply(radius).multiply(radianFactor);
}
Also used : Angle(javax.measure.quantity.Angle) UnitConverter(javax.measure.UnitConverter)

Aggregations

UnitConverter (javax.measure.UnitConverter)54 IncommensurableException (javax.measure.IncommensurableException)18 UnconvertibleException (javax.measure.UnconvertibleException)13 Test (org.junit.jupiter.api.Test)11 Test (org.junit.Test)9 RationalConverter (tech.units.indriya.function.RationalConverter)7 Unit (javax.measure.Unit)5 List (java.util.List)4 Mass (javax.measure.quantity.Mass)4 Date (java.util.Date)3 DateFormat (java.text.DateFormat)2 DecimalFormat (java.text.DecimalFormat)2 Format (java.text.Format)2 NumberFormat (java.text.NumberFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 Dimension (javax.measure.Dimension)2 CompoundFormat (org.apache.sis.io.CompoundFormat)2 Angle (org.apache.sis.measure.Angle)2 AngleFormat (org.apache.sis.measure.AngleFormat)2