Search in sources :

Example 21 with UnitConverter

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

the class Normalizer method normalize.

/**
 * Returns a new axis with the same properties (except identifiers) than given axis,
 * but with normalized axis direction and unit of measurement.
 *
 * @param  axis     the axis to normalize.
 * @param  changes  the change to apply on axis direction and units.
 * @return an axis using normalized direction and units, or {@code axis} if there is no change.
 */
static CoordinateSystemAxis normalize(final CoordinateSystemAxis axis, final AxisFilter changes) {
    final Unit<?> unit = axis.getUnit();
    final AxisDirection direction = axis.getDirection();
    final Unit<?> newUnit = changes.getUnitReplacement(axis, unit);
    final AxisDirection newDir = changes.getDirectionReplacement(axis, direction);
    /*
         * Reuse some properties (name, remarks, etc.) from the existing axis. If the direction changed,
         * then the axis name may need change too (e.g. "Westing" → "Easting"). The new axis name may be
         * set to "Unnamed", but the caller will hopefully be able to replace the returned instance by
         * an instance from the EPSG database with appropriate name.
         */
    final boolean sameDirection = newDir.equals(direction);
    if (sameDirection && newUnit.equals(unit)) {
        return axis;
    }
    final String abbreviation = axis.getAbbreviation();
    final String newAbbr = sameDirection ? abbreviation : AxisDirections.suggestAbbreviation(axis.getName().getCode(), newDir, newUnit);
    final Map<String, Object> properties = new HashMap<>();
    if (newAbbr.equals(abbreviation)) {
        properties.putAll(IdentifiedObjects.getProperties(axis, EXCLUDES));
    } else {
        properties.put(NAME_KEY, UNNAMED);
    }
    /*
         * Convert the axis range and build the new axis. The axis range will be converted only if
         * the axis direction is the same or the opposite, otherwise we do not know what should be
         * the new values. In the particular case of opposite axis direction, we need to reverse the
         * sign of minimum and maximum values.
         */
    if (sameDirection || newDir.equals(AxisDirections.opposite(direction))) {
        final UnitConverter c;
        try {
            c = unit.getConverterToAny(newUnit);
        } catch (IncommensurableException e) {
            // Use IllegalStateException because the public API is an AbstractCS member method.
            throw new IllegalStateException(Resources.format(Resources.Keys.IllegalUnitFor_2, "axis", unit), e);
        }
        double minimum = c.convert(axis.getMinimumValue());
        double maximum = c.convert(axis.getMaximumValue());
        if (!sameDirection) {
            final double tmp = minimum;
            minimum = -maximum;
            maximum = -tmp;
        }
        properties.put(DefaultCoordinateSystemAxis.MINIMUM_VALUE_KEY, minimum);
        properties.put(DefaultCoordinateSystemAxis.MAXIMUM_VALUE_KEY, maximum);
        properties.put(DefaultCoordinateSystemAxis.RANGE_MEANING_KEY, axis.getRangeMeaning());
    }
    return new DefaultCoordinateSystemAxis(properties, newAbbr, newDir, newUnit);
}
Also used : HashMap(java.util.HashMap) IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter) AxisDirection(org.opengis.referencing.cs.AxisDirection)

Example 22 with UnitConverter

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

the class DefaultParameterValue method doubleValueList.

/**
 * Returns an ordered sequence of numeric values in the specified unit of measure.
 * This convenience method applies unit conversions on the fly as needed.
 *
 * <p>The default implementation invokes {@link #doubleValueList()} and {@link #getUnit()},
 * then converts the values to the given unit of measurement.</p>
 *
 * @param  unit  the unit of measure for the value to be returned.
 * @return the sequence of values represented by this parameter after conversion to type
 *         {@code double} and conversion to {@code unit}.
 * @throws IllegalArgumentException if the specified unit is invalid for this parameter.
 * @throws InvalidParameterTypeException if the value is not an array of {@code double}s.
 * @throws IllegalStateException if the value is not defined and there is no default value.
 *
 * @see #getUnit()
 * @see #setValue(double[],Unit)
 * @see #doubleValue(Unit)
 * @see Parameters#doubleValueList(ParameterDescriptor)
 */
@Override
public double[] doubleValueList(final Unit<?> unit) throws IllegalArgumentException, IllegalStateException {
    final UnitConverter converter = getConverterTo(unit);
    final double[] values = doubleValueList();
    for (int i = 0; i < values.length; i++) {
        values[i] = converter.convert(values[i]);
    }
    return values;
}
Also used : UnitConverter(javax.measure.UnitConverter)

Example 23 with UnitConverter

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

the class ConventionalUnitTest method testConvertAngle.

/**
 * Tests conversion of an angular value between two conventional units.
 * The use of angular units is of special interest because of rounding errors.
 */
@Test
public void testConvertAngle() {
    final UnitConverter c = Units.GRAD.getConverterTo(Units.DEGREE);
    assertEquals(180, c.convert(200), STRICT);
    assertEquals(2.33722917, c.convert(2.5969213), STRICT);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 24 with UnitConverter

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

the class ConventionalUnitTest method testConvertTemperature.

/**
 * Tests conversion of a temperature value between two conventional units.
 */
@Test
public void testConvertTemperature() {
    final UnitConverter c = Units.FAHRENHEIT.getConverterTo(Units.CELSIUS);
    assertEquals("50°F", 10, c.convert(50), STRICT);
    assertEquals("5°F", -15, c.convert(5), STRICT);
    assertEquals("0°C", 32, c.inverse().convert(0), STRICT);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 25 with UnitConverter

use of javax.measure.UnitConverter in project ddf by codice.

the class GeospatialUtil method createBufferedCircleFromPoint.

private static Geometry createBufferedCircleFromPoint(Measure distance, CoordinateReferenceSystem origCRS, Geometry point) {
    Geometry pointGeo = point;
    Unit<?> unit = distance.getUnit();
    UnitConverter unitConverter = null;
    if (!(origCRS instanceof ProjectedCRS)) {
        double x = point.getCoordinate().x;
        double y = point.getCoordinate().y;
        // CRS code for UTM
        String crsCode = "AUTO:42001," + x + "," + y;
        try {
            CoordinateReferenceSystem utmCrs = CRS.decode(crsCode);
            MathTransform toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, utmCrs);
            MathTransform fromTransform = CRS.findMathTransform(utmCrs, DefaultGeographicCRS.WGS84);
            pointGeo = JTS.transform(point, toTransform);
            return JTS.transform(pointGeo.buffer(distance.doubleValue()), fromTransform);
        } catch (MismatchedDimensionException | TransformException | FactoryException e) {
            LOGGER.debug("Unable to create buffered circle from point.", e);
        }
    } else {
        try {
            unitConverter = unit.getConverterToAny(origCRS.getCoordinateSystem().getAxis(0).getUnit());
        } catch (IncommensurableException e) {
            LOGGER.debug("Unable to create unit converter.", e);
        }
    }
    if (unitConverter != null) {
        return pointGeo.buffer(unitConverter.convert(distance.doubleValue()));
    }
    return pointGeo.buffer(distance.doubleValue());
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) IncommensurableException(javax.measure.IncommensurableException) TransformException(org.opengis.referencing.operation.TransformException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) Geometry(org.locationtech.jts.geom.Geometry) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) UnitConverter(javax.measure.UnitConverter) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

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