/** * Parses the text into an instance of unit * * @param name * @see UnitFormat#parse(CharSequence) * @throws ParserException if any problem occurs while parsing the specified character sequence * (e.g. illegal syntax). * @throws UnsupportedOperationException if the {@link UnitFormat} is unable to parse. * @return A unit instance */ public static Unit<?> parseUnit(String name) { return Units.autoCorrect(DefaultUnitParser.getInstance().parse(name)); } }
@Override public boolean equals(Object obj) { if (obj instanceof UnitWrapper) { return Units.equals(unit, ((UnitWrapper) obj).getUnit()); } return false; }
public EPSGFormat() { super(); initUnits(Units.getDefaultFormat()); epsgLabelsAndAliases(this); } }
UnitConverter converter = Units.getConverterToAny(candidate, unit); if (!converter.isLinear()) {
value = Units.getConverterToAny(unit, target).convert(value);
unit = Units.autoCorrect(unit); // auto-correct DEGREE_ANGLE and FOOT_SURVEY } else {
value = Units.getConverterToAny(unit, target).convert(value);
public ESRIFormat() { super(); initUnits(Units.getDefaultFormat()); esriLabelsAndAliases(this); } }
/** * Parses an "UNIT" element. This element has the following pattern: * * <blockquote> * * <code> * UNIT["<name>", <conversion factor> {,<authority>}] * </code> * * </blockquote> * * @param parent The parent element. * @param unit The contextual unit. Usually {@link SI#METRE} or {@link SI#RADIAN}. * @return The "UNIT" element as an {@link Unit} object. * @throws ParseException if the "UNIT" can't be parsed. * @todo Authority code is currently ignored. We may consider to create a subclass of {@link * Unit} which implements {@link IdentifiedObject} in a future version. */ @SuppressWarnings("unchecked") private <T extends Quantity<T>> Unit<T> parseUnit(final Element parent, final Unit<T> unit) throws ParseException { final Element element = parent.pullElement("UNIT"); final String name = element.pullString("name"); final double factor = element.pullDouble("factor"); parseAuthority(element, name); element.close(); Unit<T> finalUnit = (factor != 1) ? unit.multiply(factor) : unit; return Units.autoCorrect(finalUnit); }
/** * Set the parameter value as a floating point and its associated unit. * * @param value The parameter value. * @param unit The unit for the specified value. * @throws InvalidParameterValueException if the value is illegal for some reason (for example a * value out of range). */ public void setValue(double value, final Unit<?> unit) throws InvalidParameterValueException { ensureNonNull("unit", unit); @SuppressWarnings("unchecked") // Checked by constructor. final ParameterDescriptor<Double> descriptor = (ParameterDescriptor) this.descriptor; final Unit<?> thisUnit = descriptor.getUnit(); if (thisUnit == null) { throw unitlessParameter(descriptor); } final int expectedID = Parameter.getUnitMessageID(thisUnit); if (Parameter.getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } value = Units.getConverterToAny(unit, thisUnit).convert(value); this.value = Parameter.ensureValidValue(descriptor, Double.valueOf(value)); }
DefaultUnitParser() { initUnits(Units.getDefaultFormat()); esriLabelsAndAliases(this); // add epsg labels the latest, to override esri ones if they collide epsgLabelsAndAliases(this); }
throw new IllegalStateException("Unable to map model " + model + " for this unit"); unit = Units.autoCorrect(unit); // auto-correct DEGREE_ANGLE and FOOT_SURVEY
/** * Returns the numeric value of the coordinate operation parameter in the specified unit of * measure. This convenience method apply unit conversion on the fly as needed. * * @param unit The unit of measure for the value to be returned. * @return The numeric value represented by this parameter after conversion to type {@code * double} and conversion to {@code unit}. * @throws InvalidParameterTypeException if the value is not a numeric type. * @throws IllegalArgumentException if the specified unit is invalid for this parameter. * @see #getUnit * @see #setValue(double,Unit) * @see #doubleValueList(Unit) */ public double doubleValue(final Unit<?> unit) throws InvalidParameterTypeException { if (this.unit == null) { throw unitlessParameter(descriptor); } ensureNonNull("unit", unit); final int expectedID = getUnitMessageID(this.unit); if (getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } return Units.getConverterToAny(this.unit, unit).convert(doubleValue()); }
Unit<Q> factor = base.multiply(sz); return Units.autoCorrect(factor); } catch (NumberFormatException nfe) { final IOException ioe =
/** * Returns the envelope span along the specified dimension, in terms of the given units. * * @param dimension The dimension to query. * @param unit The unit for the return value. * @return The span in terms of the given unit. * @throws IndexOutOfBoundsException If the given index is out of bounds. * @throws ConversionException if the length can't be converted to the specified units. * @throws IllegalArgumentException if the target units are not compatible with the units of the * provided {@code dimension} * @since 2.5 */ public double getSpan(final int dimension, final Unit<?> unit) throws IndexOutOfBoundsException { double value = getSpan(dimension); if (crs != null) { final Unit<?> source = crs.getCoordinateSystem().getAxis(dimension).getUnit(); if (source != null) { value = Units.getConverterToAny(source, unit).convert(value); } } return value; }
@Test public void testUnitsMatch() { Unit<Angle> degree = Units.autoCorrect( new TransformedUnit<Angle>( SI.RADIAN, new MultiplyConverter((Math.PI * 2.0) / 360.0))); assertEquals("autocorrection of degree definition from jsr275", NonSI.DEGREE_ANGLE, degree); assertTrue("jsr275 deegree definition", isDegreeAngle(degree)); // UNIT["degree", 0.017453292519943295], degree = Units.autoCorrect( new TransformedUnit<Angle>( SI.RADIAN, new MultiplyConverter(0.017453292519943295))); assertEquals( "autocorrection of deegree definition from EsriLookupTest", NonSI.DEGREE_ANGLE, degree); assertTrue("deegree definition from EsriLookupTest", isDegreeAngle(degree)); Unit<Length> feet = Units.autoCorrect( new TransformedUnit<Length>( SI.METRE, new MultiplyConverter(0.3048006096012192))); assertEquals( "autocorrection of US Survey definition from EsriLookupTest", USCustomary.FOOT_SURVEY, feet); assertTrue("survey foot definition from EsriLookupTest", isUSSurveyFoot(feet)); }
/** * Returns the numeric value of the coordinate operation parameter in the specified unit of * measure. This convenience method apply unit conversion on the fly as needed. * * @param unit The unit of measure for the value to be returned. * @return The numeric value 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. */ public double doubleValue(final Unit<?> unit) throws IllegalArgumentException { ensureNonNull("unit", unit); final Unit<?> thisUnit = getUnit(); if (thisUnit == null) { throw unitlessParameter(descriptor); } final int expectedID = Parameter.getUnitMessageID(thisUnit); if (Parameter.getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } return Units.getConverterToAny(thisUnit, unit).convert(value); }
/** * Returns an ordered sequence of numeric values in the specified unit of measure. This * convenience method apply unit conversion on the fly as needed. * * @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 InvalidParameterTypeException if the value is not an array of {@code double}s. * @throws IllegalArgumentException if the specified unit is invalid for this parameter. * @see #getUnit * @see #setValue(double[],Unit) * @see #doubleValue(Unit) */ public double[] doubleValueList(final Unit<?> unit) throws InvalidParameterTypeException { if (this.unit == null) { throw unitlessParameter(descriptor); } ensureNonNull("unit", unit); final int expectedID = getUnitMessageID(this.unit); if (getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } UnitConverter converter = Units.getConverterToAny(this.unit, unit); final double[] values = doubleValueList().clone(); for (int i = 0; i < values.length; i++) { values[i] = converter.convert(values[i]); } return values; }
/** * Set the parameter value as an array of floating point and their associated unit. * * @param values The parameter values. * @param unit The unit for the specified value. * @throws InvalidParameterValueException if the floating point type is inappropriate for this * parameter, or if the value is illegal for some other reason (for example a value out of * range). */ public void setValue(double[] values, final Unit<?> unit) throws InvalidParameterValueException { ensureNonNull("unit", unit); @SuppressWarnings("unchecked") // Checked by constructor. final ParameterDescriptor<T> descriptor = (ParameterDescriptor) this.descriptor; final Unit<?> targetUnit = descriptor.getUnit(); if (targetUnit == null) { throw unitlessParameter(descriptor); } final int expectedID = getUnitMessageID(targetUnit); if (getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } final double[] converted = values.clone(); UnitConverter converter = Units.getConverterToAny(unit, targetUnit); for (int i = 0; i < converted.length; i++) { converted[i] = converter.convert(converted[i]); } this.value = ensureValidValue(descriptor, converted); this.unit = unit; }
Errors.format(expectedID, unit), descriptor.getName().getCode(), value); Double converted = Units.getConverterToAny(unit, targetUnit).convert(value); ensureValidValue(descriptor, converted);