Java Code Examples for javax.measure.unit.Unit#getConverterTo()

The following examples show how to use javax.measure.unit.Unit#getConverterTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectionFactory.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
void computeTargetCRS(final IScope scope, final CoordinateReferenceSystem crs, final double longitude,
		final double latitude) {
	// If we already know in which CRS we project the data in GAMA, no need to recompute it. This information is
	// normally wiped when an experiment is disposed
	if (targetCRS != null) { return; }
	try {
		if (!GamaPreferences.External.LIB_TARGETED.getValue()) {
			targetCRS = computeDefaultCRS(scope, GamaPreferences.External.LIB_TARGET_CRS.getValue(), true);
		} else {
			if (crs != null && crs instanceof DefaultProjectedCRS) { // Temporary fix of issue 766... a better solution
				final CartesianCS ccs = ((DefaultProjectedCRS) crs).getCoordinateSystem();
				final Unit<?> unitX = ccs.getAxis(0).getUnit();
				if (unitX != null && !unitX.equals(SI.METER)) {
					unitConverter = unitX.getConverterTo(SI.METER);
				}
				targetCRS = crs;
			} else {
				final int index = (int) (0.5 + (longitude + 186.0) / 6);
				final boolean north = latitude > 0;
				final String newCode = EPSGPrefix + (32600 + index + (north ? 0 : 100));
				targetCRS = getCRS(scope, newCode);
			}
		}
	} catch (final GamaRuntimeException e) {
		e.addContext(
				"The cause could be that you try to re-project already projected data (see Gama > Preferences... > External for turning the option to true)");
		throw e;
	}
}
 
Example 2
Source File: NumericAlgorithmGenerator.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
String generateUnitConversionAlgorithm(Attribute targetAttribute, Attribute sourceAttribute) {
  String algorithm = null;

  Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute);

  Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute);

  if (sourceUnit != null) {
    if (targetUnit != null && !sourceUnit.equals(targetUnit)) {
      // if units are convertible, create convert algorithm
      UnitConverter unitConverter;
      try {
        unitConverter = sourceUnit.getConverterTo(targetUnit);
      } catch (ConversionException e) {
        unitConverter = null;
        // algorithm sets source unit and assigns source value to target
        algorithm =
            String.format(
                "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString());
      }

      if (unitConverter != null) {
        // algorithm sets source unit and assigns value converted to target unit to target
        algorithm =
            String.format(
                "$('%s').unit('%s').toUnit('%s').value();",
                sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString());
      }
    } else {
      // algorithm sets source unit and assigns source value to target
      algorithm =
          String.format(
              "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString());
    }
  }

  if (algorithm == null) {
    // algorithm assigns source value to target
    algorithm = String.format("$('%s').value();", sourceAttribute.getName());
  }

  return algorithm;
}