Search in sources :

Example 1 with UnitConverter

use of javax.measure.UnitConverter in project unit-api by unitsofmeasurement.

the class TestUnit method getConverterTo.

public UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException {
    if ((this == that) || this.equals(that))
        // Shortcut.
        return TestConverter.IDENTITY;
    Unit<Q> thisSystemUnit = this.getSystemUnit();
    Unit<Q> thatSystemUnit = that.getSystemUnit();
    if (!thisSystemUnit.equals(thatSystemUnit))
        try {
            return getConverterToAny(that);
        } catch (IncommensurableException e) {
            throw new UnconvertibleException(e);
        }
    UnitConverter thisToSI = this.getSystemConverter();
    UnitConverter thatToSI = that.getConverterTo(thatSystemUnit);
    return thatToSI.inverse().concatenate(thisToSI);
}
Also used : IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException)

Example 2 with UnitConverter

use of javax.measure.UnitConverter in project unit-api by unitsofmeasurement.

the class UnitTest method testGetConverterTo.

@Test(expected = UnconvertibleException.class)
public void testGetConverterTo() {
    sut = DistanceUnit.m;
    UnitConverter converter = sut.getConverterTo(BaseUnit.ONE);
    assertNotNull(converter);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 3 with UnitConverter

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

the class ExtentsTest method testGetVerticalRange.

/**
 * Tests {@link Extents#getVerticalRange(Extent)}.
 *
 * @throws IncommensurableException if a conversion between incompatible units were attempted.
 */
@Test
@SuppressWarnings("null")
public void testGetVerticalRange() throws IncommensurableException {
    final List<DefaultVerticalExtent> extents = Arrays.asList(new DefaultVerticalExtent(-200, -100, VerticalCRSMock.HEIGHT), new DefaultVerticalExtent(150, 300, VerticalCRSMock.DEPTH), new DefaultVerticalExtent(0.1, 0.2, VerticalCRSMock.SIGMA_LEVEL), // [91.44 … 182.88] metres
    new DefaultVerticalExtent(-600, -300, VerticalCRSMock.HEIGHT_ft), new DefaultVerticalExtent(10130, 20260, VerticalCRSMock.BAROMETRIC_HEIGHT));
    Collections.shuffle(extents, TestUtilities.createRandomNumberGenerator());
    /*
         * Since we have shuffled the vertical extents in random order, the range that we will
         * test may be either in metres or in feet depending on which vertical extent is first.
         * So we need to check which linear unit is first.
         */
    Unit<?> unit = null;
    for (final DefaultVerticalExtent e : extents) {
        unit = e.getVerticalCRS().getCoordinateSystem().getAxis(0).getUnit();
        if (Units.isLinear(unit))
            break;
    }
    final UnitConverter c = unit.getConverterToAny(Units.METRE);
    /*
         * The actual test. Arbitrarily compare the heights in metres, converting them if needed.
         */
    final DefaultExtent extent = new DefaultExtent();
    extent.setVerticalElements(extents);
    final MeasurementRange<Double> range = Extents.getVerticalRange(extent);
    assertNotNull("getVerticalRange", range);
    assertSame("unit", unit, range.unit());
    assertEquals("minimum", -300, c.convert(range.getMinDouble()), 0.001);
    assertEquals("maximum", -91.44, c.convert(range.getMaxDouble()), 0.001);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 4 with UnitConverter

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

the class CoordinateFormat method format.

/**
 * Formats the given coordinate and appends the resulting text to the given stream or buffer.
 *
 * @param  position    the coordinate to format.
 * @param  toAppendTo  where the text is to be appended.
 * @throws IOException if an error occurred while writing to the given appendable.
 */
@Override
@SuppressWarnings("UnnecessaryBoxing")
public void format(final DirectPosition position, final Appendable toAppendTo) throws IOException {
    ArgumentChecks.ensureNonNull("position", position);
    ArgumentChecks.ensureNonNull("toAppendTo", toAppendTo);
    CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem();
    if (crs == null) {
        // May still be null.
        crs = defaultCRS;
    }
    if (crs != lastCRS) {
        initialize(crs);
    }
    /*
         * Standard java.text.Format API can only write into a StringBuffer. If the given Appendable is not a
         * StringBuffer, then we will need to format in a temporary buffer before to copy to the Appendable.
         */
    final StringBuffer destination;
    if (toAppendTo instanceof StringBuffer) {
        destination = (StringBuffer) toAppendTo;
    } else {
        if (buffer == null) {
            buffer = new StringBuffer();
        }
        destination = buffer;
        destination.setLength(0);
    }
    if (dummy == null) {
        dummy = new FieldPosition(0);
    }
    /*
         * The format to use for each ordinate has been computed by 'initialize'.  The format array length
         * should match the number of dimensions in the given position if the DirectPosition is consistent
         * with its CRS, but we will nevertheless verify has a paranoiac check.  If there is no CRS, or if
         * the DirectPosition dimension is (illegally) greater than the CRS dimension, then we will format
         * the ordinate as a number.
         */
    final int dimension = position.getDimension();
    for (int i = 0; i < dimension; i++) {
        double value = position.getOrdinate(i);
        final Object object;
        final Format f;
        if (formats != null && i < formats.length) {
            f = formats[i];
            if (isNegative(i)) {
                value = -value;
            }
            if (toFormatUnit != null) {
                final UnitConverter c = toFormatUnit[i];
                if (c != null) {
                    value = c.convert(value);
                }
            }
            switch(types[i]) {
                default:
                    object = Double.valueOf(value);
                    break;
                case LONGITUDE:
                    object = new Longitude(value);
                    break;
                case LATITUDE:
                    object = new Latitude(value);
                    break;
                case ANGLE:
                    object = new Angle(value);
                    break;
                case DATE:
                    object = new Date(Math.round(value) + epochs[i]);
                    break;
            }
        } else {
            object = value;
            f = getFormat(Number.class);
        }
        /*
             * At this point we got the value to format together with the Format instance to use.
             */
        if (i != 0) {
            toAppendTo.append(separator);
        }
        if (f.format(object, destination, dummy) != toAppendTo) {
            toAppendTo.append(destination);
            destination.setLength(0);
        }
        if (unitSymbols != null && i < unitSymbols.length) {
            final String symbol = unitSymbols[i];
            if (symbol != null) {
                toAppendTo.append(Characters.NO_BREAK_SPACE).append(symbol);
            }
        }
    }
}
Also used : Latitude(org.apache.sis.measure.Latitude) FieldPosition(java.text.FieldPosition) Longitude(org.apache.sis.measure.Longitude) Date(java.util.Date) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) NumberFormat(java.text.NumberFormat) DateFormat(java.text.DateFormat) AngleFormat(org.apache.sis.measure.AngleFormat) CompoundFormat(org.apache.sis.io.CompoundFormat) DecimalFormat(java.text.DecimalFormat) Angle(org.apache.sis.measure.Angle) UnitConverter(javax.measure.UnitConverter) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 5 with UnitConverter

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

the class DatumShiftTransform method setContextParameters.

/**
 * Sets the semi-axis length in the {@link #context} parameters.
 * This is a helper method for constructors in some (not all) subclasses.
 *
 * @param  semiMajor  the semi-major axis length of the source ellipsoid.
 * @param  semiMinor  the semi-minor axis length of the source ellipsoid.
 * @param  unit       the unit of measurement of source ellipsoid axes.
 * @param  target     the target ellipsoid.
 */
final void setContextParameters(final double semiMajor, final double semiMinor, final Unit<Length> unit, final Ellipsoid target) {
    final UnitConverter c = target.getAxisUnit().getConverterTo(unit);
    context.getOrCreate(Molodensky.SRC_SEMI_MAJOR).setValue(semiMajor, unit);
    context.getOrCreate(Molodensky.SRC_SEMI_MINOR).setValue(semiMinor, unit);
    context.getOrCreate(Molodensky.TGT_SEMI_MAJOR).setValue(c.convert(target.getSemiMajorAxis()), unit);
    context.getOrCreate(Molodensky.TGT_SEMI_MINOR).setValue(c.convert(target.getSemiMinorAxis()), unit);
}
Also used : 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