/** * @param value number to convert * @param unit unit used in text * @return suggestions of the given number converted into metric units, sorted by naturalness * or null if conversion is not necessary / was not possible */ @Nullable protected List<Map.Entry<Unit, Double>> getMetricEquivalent(double value, @NotNull Unit unit) { LinkedList<Map.Entry<Unit, Double>> conversions = new LinkedList<>(); for (Unit metric : metricUnits) { if (unit.equals(metric)) { // don't convert to itself return null; } if (unit.isCompatible(metric)) { Double converted = unit.getConverterTo(metric).convert(value); conversions.add(new AbstractMap.SimpleImmutableEntry<>(metric, converted)); } } sortByNaturalness(conversions); if (conversions.size() == 0) { return null; } else { return conversions; } }
/** * Returns {@code true} if the specified unit is compatible with {@linkplain SI#SECOND seconds}. * This method is invoked at construction time for checking units compatibility. * * @since 2.2 */ @Override protected boolean isCompatibleUnit(final AxisDirection direction, final Unit<?> unit) { return SI.SECOND.isCompatible(unit); }
/** * Returns {@code true} if the specified unit is compatible with {@linkplain SI#METER meters}. * In addition, this method also accepts {@link Unit#ONE}, which is used for coordinates in a * grid. This method is invoked at construction time for checking units compatibility. * * @since 2.2 */ @Override protected boolean isCompatibleUnit(final AxisDirection direction, final Unit<?> unit) { return SI.METRE.isCompatible(unit) || AbstractUnit.ONE.equals(unit); // Note: this condition is also coded in PredefinedCS.rightHanded(AffineCS). }
/** * Returns the unit type as one of error message code. Used for checking unit with a better * error message formatting if needed. * * <p>Note: It is difficult to differentiate scale and angular units, since both of them are * dimensionless. However, in EPSG database version 6.7, there is only 3 scale units and all of * them maps to {@link Unit#ONE} or {@link Units#PPM}. Consequently, they are hard-coded and * treated especially by this method. * * @todo Provides a better way to differentiate scale units (currently AbstractUnit.ONE) and * angular units. Both are dimensionless... */ static int getUnitMessageID(final Unit<?> unit) { // Note: ONE must be tested before RADIAN. if (AbstractUnit.ONE.equals(unit) || Units.PPM.equals(unit)) return ErrorKeys.NON_SCALE_UNIT_$1; if (SI.METRE.isCompatible(unit)) return ErrorKeys.NON_LINEAR_UNIT_$1; if (SI.SECOND.isCompatible(unit)) return ErrorKeys.NON_TEMPORAL_UNIT_$1; if (SI.RADIAN.isCompatible(unit)) return ErrorKeys.NON_ANGULAR_UNIT_$1; return ErrorKeys.INCOMPATIBLE_UNIT_$1; }
/** * Set the unit for formatting linear measures. * * @param unit The new unit, or {@code null}. */ public void setLinearUnit(final Unit<Length> unit) { if (unit != null && !SI.METRE.isCompatible(unit)) { throw new IllegalArgumentException(Errors.format(ErrorKeys.NON_LINEAR_UNIT_$1, unit)); } linearUnit = unit; }
/** * Makes sure that the specified unit is a linear one. This is a convenience method for subclass * constructors. * * @param unit Unit to check. * @throws IllegalArgumentException if {@code unit} is not a linear unit. */ protected static void ensureLinearUnit(final Unit<?> unit) throws IllegalArgumentException { if (!SI.METRE.isCompatible(unit)) { throw new IllegalArgumentException(Errors.format(ErrorKeys.NON_LINEAR_UNIT_$1, unit)); } }
/** * Set the angular unit for formatting measures. * * @param unit The new unit, or {@code null}. */ public void setAngularUnit(final Unit<Angle> unit) { if (unit != null && (!SI.RADIAN.isCompatible(unit) || AbstractUnit.ONE.equals(unit))) { throw new IllegalArgumentException(Errors.format(ErrorKeys.NON_ANGULAR_UNIT_$1, unit)); } angularUnit = unit; }
/** * Makes sure that the specified unit is a temporal one. This is a convenience method for * subclass constructors. * * @param unit Unit to check. * @throws IllegalArgumentException if {@code unit} is not a temporal unit. */ protected static void ensureTimeUnit(final Unit<?> unit) throws IllegalArgumentException { if (!SI.SECOND.isCompatible(unit)) { throw new IllegalArgumentException(Errors.format(ErrorKeys.NON_TEMPORAL_UNIT_$1, unit)); } }
/** * Makes sure that the specified unit is an angular one. This is a convenience method for * subclass constructors. * * @param unit Unit to check. * @throws IllegalArgumentException if {@code unit} is not an angular unit. */ protected static void ensureAngularUnit(final Unit<?> unit) throws IllegalArgumentException { if (!SI.RADIAN.isCompatible(unit) && !AbstractUnit.ONE.equals(unit)) { throw new IllegalArgumentException(Errors.format(ErrorKeys.NON_ANGULAR_UNIT_$1, unit)); } } }
protected Unit<?> replace(final Unit<?> units) { if (forceStandardUnits) { if (units.isCompatible(SI.METRE)) { return SI.METRE;
/** * Constructs a parameter from the specified name and value. This convenience constructor * creates a {@link DefaultParameterDescriptor} object. But if such an object is available, then * the preferred way to get a {@code ParameterValue} is to invoke {@link * ParameterDescriptor#createValue}. * * @param name The parameter name. * @param value The parameter value. * @param unit The unit for the parameter value. * @return A new parameter instance for the given name and value. * @since 2.5 */ public static Parameter<Double> create(final String name, final double value, Unit<?> unit) { // Normalizes the specified unit into one of "standard" units used in projections. if (unit != null) { if (SI.METRE.isCompatible(unit)) unit = SI.METRE; else if (SI.DAY.isCompatible(unit)) unit = SI.DAY; else if (NonSI.DEGREE_ANGLE.isCompatible(unit)) unit = NonSI.DEGREE_ANGLE; } final ParameterDescriptor<Double> descriptor = DefaultParameterDescriptor.create( name, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, unit); final Parameter<Double> parameter = new Parameter<Double>(descriptor); parameter.value = value; parameter.unit = unit; return parameter; }
/** * Returns {@code true} if the specified unit is compatible with {@linkplain NonSI#DEGREE_ANGLE * decimal degrees} (or {@linkplain SI#METER meters} in the special case of height). This method * is invoked at construction time for checking units compatibility. * * @since 2.2 */ @Override protected boolean isCompatibleUnit(AxisDirection direction, final Unit<?> unit) { direction = direction.absolute(); final Unit<?> expected = AxisDirection.UP.equals(direction) ? SI.METRE : NonSI.DEGREE_ANGLE; return expected.isCompatible(unit); }
double tolMeters = converter.convert(mt.doubleValue()); return tolMeters * OGC_METERS_TO_DEGREES; } else if (targetUnit != null && targetUnit.isCompatible(SI.METRE)) {
/** * Returns a new axis with the same properties than current axis except for the units. * * @param newUnit The unit for the new axis. * @return An axis using the specified unit. * @throws IllegalArgumentException If the specified unit is incompatible with the expected one. */ final DefaultCoordinateSystemAxis usingUnit(final Unit<?> newUnit) throws IllegalArgumentException { if (unit.equals(newUnit)) { return this; } if (unit.isCompatible(newUnit)) { return new DefaultCoordinateSystemAxis( getProperties(this, null), abbreviation, direction, newUnit, minimum, maximum, rangeMeaning); } throw new IllegalArgumentException(Errors.format(ErrorKeys.INCOMPATIBLE_UNIT_$1, newUnit)); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> max = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (max == null || (max.getUnit().isCompatible(itemState.getUnit()) && max.compareTo(itemState) < 0)) { max = itemState; } } } } return max != null ? max : UnDefType.UNDEF; }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> min = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (min == null || (min.getUnit().isCompatible(itemState.getUnit()) && min.compareTo(itemState) > 0)) { min = itemState; } } } } return min != null ? min : UnDefType.UNDEF; }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> sum = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (sum == null) { sum = itemState; // initialise the sum from the first item } else if (sum.getUnit().isCompatible(itemState.getUnit())) { sum = sum.add(itemState); } } } } return sum != null ? sum : UnDefType.UNDEF; }
@Override public int compareTo(QuantityType<T> o) { if (quantity.getUnit().isCompatible(o.quantity.getUnit())) { QuantityType<T> v1 = this.toUnit(getUnit().getSystemUnit()); QuantityType<?> v2 = o.toUnit(o.getUnit().getSystemUnit()); if (v1 != null && v2 != null) { return Double.compare(v1.doubleValue(), v2.doubleValue()); } else { throw new IllegalArgumentException("Unable to convert to system unit during compare."); } } else { throw new IllegalArgumentException("Can not compare incompatible units."); } }
/** * Returns the angular unit of the specified coordinate system. The preference will be given to * the longitude axis, if found. */ static Unit<Angle> getAngularUnit(final CoordinateSystem coordinateSystem) { Unit<Angle> unit = NonSI.DEGREE_ANGLE; for (int i = coordinateSystem.getDimension(); --i >= 0; ) { final CoordinateSystemAxis axis = coordinateSystem.getAxis(i); final Unit<?> candidate = axis.getUnit(); if (NonSI.DEGREE_ANGLE.isCompatible(candidate)) { unit = candidate.asType(Angle.class); if (AxisDirection.EAST.equals(axis.getDirection().absolute())) { break; // Found the longitude axis. } } } return unit; }
CoordinateReferenceSystem crs = source.getCoordinateReferenceSystem(); Unit<?> unit = crs.getCoordinateSystem().getAxis(0).getUnit(); boolean isMeter = unit.isCompatible(SI.METRE); double resX = af.getScaleX(); double resY = af.getScaleY();