/** * @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; } }
List<String> reverseConverted = null; try { double unitConverted = unit.getConverterTo(convertedUnit).convert(value); double diff = Math.abs(unitConverted - convertedValueInText); if (diff > DELTA) {
UnitConverter converter = mt.getUnit().asType(Length.class).getConverterTo(SI.METRE); double tolMeters = converter.convert(mt.doubleValue()); return tolMeters * OGC_METERS_TO_DEGREES; UnitConverter converter = mt.getUnit().getConverterTo(targetUnit); return converter.convert(mt.doubleValue()); } else {
/** * Computes a rescaling multiplier to be applied to an unscaled value. * * @param mapScale the mapScale in pixels per meter. * @param uom the unit of measure that will be used to scale. * @return the rescaling multiplier for the provided parameters. */ protected double computeRescaleMultiplier(Unit<Length> uom) { // no scaling to do if UOM is PIXEL (or null, which stands for PIXEL as well) if (uom == null || uom.equals(Units.PIXEL)) return 1; if (uom == SI.METRE) { return mapScale; } // converts value from meters to given UOM UnitConverter converter = uom.getConverterTo(SI.METRE); return converter.convert(mapScale); }
/** * Computes a rescaling multiplier to be applied to an unscaled value. * * @param mapScale the mapScale in pixels per meter. * @param uom the unit of measure that will be used to scale. * @return the rescaling multiplier for the provided parameters. */ double computeRescaleMultiplier(double mapScale, Unit<Length> uom) { // no scaling to do if UOM is PIXEL (or null, which stands for PIXEL as well) if (uom == null || uom.equals(Units.PIXEL)) return 1; if (uom == SI.METRE) { return mapScale; } // converts value from meters to given UOM UnitConverter converter = uom.getConverterTo(SI.METRE); return converter.convert(mapScale); } };
/** * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super() call in * constructors"). * * @throws IncommensurableException */ private static UnitConverter getConverter(final Unit<Time> source) throws IncommensurableException { if (source == null) { throw new UnconvertibleException(Errors.format(ErrorKeys.NO_UNIT)); } return source.getConverterTo(MILLISECOND); }
/** Initialize the fields required for {@link #toDate} and {@link #toValue} operations. */ @SuppressWarnings("unchecked") private void initializeConverter() { origin = ((TemporalDatum) datum).getOrigin().getTime(); Unit<Time> time = (Unit<Time>) coordinateSystem.getAxis(0).getUnit(); toMillis = time.getConverterTo(MILLISECOND); }
UnitConverter converter = unit.getConverterTo(USCustomary.KNOT); return converter.convert(speed); } catch (Exception e) {
/** * Returns the longitude value relative to the Greenwich Meridian, expressed in the specified * units. This convenience method makes it easier to obtain longitude in decimal degrees ({@code * getGreenwichLongitude(NonSI.DEGREE_ANGLE)}), regardless of the underlying angular units of * this prime meridian. * * @param targetUnit The unit in which to express longitude. * @return The Greenwich longitude in the given units. */ public double getGreenwichLongitude(final Unit<Angle> targetUnit) { return getAngularUnit().getConverterTo(targetUnit).convert(getGreenwichLongitude()); }
/** * Returns the longitude value relative to the Greenwich Meridian, expressed in the specified * units. */ private static double getGreenwichLongitude(final PrimeMeridian pm, final Unit<Angle> unit) { return pm.getAngularUnit().getConverterTo(unit).convert(pm.getGreenwichLongitude()); }
/** * Numeric representation of measure in the provided unit * * @param unit * @return numeric representation. */ public Number numericValue(Unit<Q> unit) { if (getUnit() == unit || getUnit().equals(unit)) { return getValue(); } else { UnitConverter converter = getUnit().getConverterTo(unit); return converter.convert(getValue()); } } /**
/** * Format the inner part of a <A * HREF="http://geoapi.sourceforge.net/snapshot/javadoc/org/opengis/referencing/doc-files/WKT.html"><cite>Well * Known Text</cite> (WKT)</A> element. * * @param formatter The formatter to use. * @return The WKT element name, which is "SPHEROID" */ @Override protected String formatWKT(final Formatter formatter) { final double ivf = getInverseFlattening(); formatter.append(getAxisUnit().getConverterTo(SI.METRE).convert(getSemiMajorAxis())); formatter.append(Double.isInfinite(ivf) ? 0 : ivf); return "SPHEROID"; } }
return size; return unit.getConverterTo(SI.METRE).convert(size);
metadata.addGeoDoubleParam( GeoTiffPCSCodes.ProjLinearUnitSizeGeoKey, linearUnit.getConverterTo(SI.METRE).convert(1)); } else if (USCustomary.FOOT.equals(linearUnit)) { metadata.addGeoShortParam( metadata.addGeoDoubleParam( GeoTiffPCSCodes.ProjLinearUnitSizeGeoKey, linearUnit.getConverterTo(SI.METRE).convert(1)); } else if (USCustomary.YARD.equals(linearUnit)) { metadata.addGeoShortParam( linearUnit.getConverterTo(SI.METRE).convert(1)); } else if (USCustomary.FOOT_SURVEY.equals(linearUnit)) { metadata.addGeoShortParam( linearUnit.getConverterTo(SI.METRE).convert(1));
/** * Constructs a transform from the specified parameters. * * @param semiMajor The semi-major axis length. * @param semiMinor The semi-minor axis length. * @param units The axis units. * @param hasHeight {@code true} if geographic coordinates include an ellipsoidal height (i.e. * are 3-D), or {@code false} if they are only 2-D. */ public GeocentricTransform( final double semiMajor, final double semiMinor, final Unit<Length> units, final boolean hasHeight) { this.hasHeight = hasHeight; final UnitConverter converter = units.getConverterTo(SI.METRE); a = converter.convert(semiMajor); b = converter.convert(semiMinor); a2 = a * a; b2 = b * b; e2 = (a2 - b2) / a2; ep2 = (a2 - b2) / b2; checkArgument("a", a, Double.MAX_VALUE); checkArgument("b", b, a); }
return distanceMeters; } else { UnitConverter converter = SI.METRE.getConverterTo(unit); return converter.convert(distanceMeters);
/** * @param tileMatrix * @param unit * @return */ private static double getPixelSpan(TileMatrix tileMatrix) { CoordinateSystem coordinateSystem = tileMatrix.getCrs().getCoordinateSystem(); Unit unit = coordinateSystem.getAxis(0).getUnit(); // now divide by meters per unit! double pixelSpan = tileMatrix.getDenominator() * PixelSizeMeters; if (unit.equals(NonSI.DEGREE_ANGLE)) { /* * use the length of a degree at the equator = 60 nautical miles! * unit = USCustomary.NAUTICAL_MILE; UnitConverter metersperunit = * unit.getConverterTo(SI.METRE); pixelSpan /= * metersperunit.convert(60.0); */ // constant value from // https://msi.nga.mil/MSISiteContent/StaticFiles/Calculators/degree.html // apparently - 60.10764611706782 NaMiles pixelSpan /= 111319; } else { UnitConverter metersperunit = unit.getConverterTo(SI.METRE); pixelSpan /= metersperunit.convert(1); } return pixelSpan; } }
/** Compares two values for equality. */ private static <Q extends Quantity<Q>> void checkConversion( final double expected, final Unit<Q> unitExpected, final double actual, final Unit<Q> unitActual) { UnitConverter converter = unitActual.getConverterTo(unitExpected); assertEquals(expected, converter.convert(actual), 1E-6); converter = converter.inverse(); assertEquals(actual, converter.convert(expected), 1E-6); }
/** * Compare this ellipsoid with the specified object for equality. * * @param object The object to compare to {@code this}. * @param compareMetadata {@code true} for performing a strict comparaison, or {@code false} for * comparing only properties relevant to transformations. * @return {@code true} if both objects are equal. */ @Override public boolean equals(final AbstractIdentifiedObject object, final boolean compareMetadata) { if (object == this) { return true; // Slight optimization. } if (super.equals(object, compareMetadata)) { final DefaultEllipsoid that = (DefaultEllipsoid) object; if (!compareMetadata) { UnitConverter converter = that.unit.getConverterTo(this.unit); return Utilities.equals(this.semiMajorAxis, converter.convert(that.semiMajorAxis)) && Utilities.equals( this.semiMinorAxis, converter.convert(that.semiMinorAxis)); } else { return (this.ivfDefinitive == that.ivfDefinitive) && Utilities.equals(this.semiMajorAxis, that.semiMajorAxis) && Utilities.equals(this.semiMinorAxis, that.semiMinorAxis) && Utilities.equals(this.inverseFlattening, that.inverseFlattening) && Utilities.equals(this.unit, that.unit); } } return false; }
final Unit targetUnit = targetCS.getAxis(0).getUnit(); double epochShift = sourceDatum.getOrigin().getTime() - targetDatum.getOrigin().getTime(); epochShift = MILLISECOND.getConverterTo(targetUnit).convert(epochShift);