use of javax.measure.UnitConverter in project sis by apache.
the class ConventionalUnit method getConverterTo.
/**
* Returns a converter of numeric values from this unit to another unit of same type.
*
* @param that the unit of same type to which to convert the numeric values.
* @return the converter from this unit to {@code that} unit.
* @throws UnconvertibleException if the converter can not be constructed.
*/
@Override
public UnitConverter getConverterTo(final Unit<Q> that) throws UnconvertibleException {
if (that == this) {
return LinearConverter.IDENTITY;
}
ArgumentChecks.ensureNonNull("that", that);
UnitConverter c = toTarget;
if (target != that) {
// Optimization for a common case.
final Unit<Q> step = that.getSystemUnit();
if (target != step && !target.isCompatible(step)) {
// Should never occur unless parameterized type has been compromised.
throw new UnconvertibleException(incompatible(that));
}
// Usually leave 'c' unchanged.
c = target.getConverterTo(step).concatenate(c);
c = step.getConverterTo(that).concatenate(c);
}
return c;
}
use of javax.measure.UnitConverter in project sis by apache.
the class ConventionalUnit method getConverterToAny.
/**
* Returns a converter from this unit to the specified unit of unknown type.
* This method can be used when the quantity type of the specified unit is unknown at compile-time
* or when dimensional analysis allows for conversion between units of different type.
*
* @param that the unit to which to convert the numeric values.
* @return the converter from this unit to {@code that} unit.
* @throws IncommensurableException if this unit is not {@linkplain #isCompatible(Unit) compatible} with {@code that} unit.
*
* @see #isCompatible(Unit)
*/
@Override
public UnitConverter getConverterToAny(final Unit<?> that) throws IncommensurableException {
if (that == this) {
return LinearConverter.IDENTITY;
}
ArgumentChecks.ensureNonNull("that", that);
UnitConverter c = toTarget;
if (target != that) {
// Optimization for a common case.
final Unit<?> step = that.getSystemUnit();
if (target != step && !target.isCompatible(step)) {
throw new IncommensurableException(incompatible(that));
}
// Usually leave 'c' unchanged.
c = target.getConverterToAny(step).concatenate(c);
c = step.getConverterToAny(that).concatenate(c);
}
return c;
}
use of javax.measure.UnitConverter in project sis by apache.
the class MetadataReader method addExtent.
/**
* Adds the extent declared in the current group. For more consistent results, the caller should restrict
* the {@linkplain Decoder#setSearchPath search path} to a single group before invoking this method.
*
* @return {@code true} if at least one numerical value has been added.
*/
private boolean addExtent() {
addExtent(stringValue(GEOGRAPHIC_IDENTIFIER));
/*
* If at least one geographic ordinates is available, add a GeographicBoundingBox.
*/
final Number xmin = decoder.numericValue(LONGITUDE.MINIMUM);
final Number xmax = decoder.numericValue(LONGITUDE.MAXIMUM);
final Number ymin = decoder.numericValue(LATITUDE.MINIMUM);
final Number ymax = decoder.numericValue(LATITUDE.MAXIMUM);
final Number zmin = decoder.numericValue(VERTICAL.MINIMUM);
final Number zmax = decoder.numericValue(VERTICAL.MAXIMUM);
boolean hasExtent = (xmin != null || xmax != null || ymin != null || ymax != null);
if (hasExtent) {
final UnitConverter xConv = getConverterTo(decoder.unitValue(LONGITUDE.UNITS), Units.DEGREE);
final UnitConverter yConv = getConverterTo(decoder.unitValue(LATITUDE.UNITS), Units.DEGREE);
addExtent(new double[] { valueOf(xmin, xConv), valueOf(xmax, xConv), valueOf(ymin, yConv), valueOf(ymax, yConv) }, 0);
}
/*
* If at least one vertical ordinates above is available, add a VerticalExtent.
*/
if (zmin != null || zmax != null) {
final UnitConverter c = getConverterTo(decoder.unitValue(VERTICAL.UNITS), Units.METRE);
double min = valueOf(zmin, c);
double max = valueOf(zmax, c);
if (CF.POSITIVE_DOWN.equals(stringValue(VERTICAL.POSITIVE))) {
final double tmp = min;
min = -max;
max = -tmp;
}
addVerticalExtent(min, max, VERTICAL_CRS);
hasExtent = true;
}
/*
* Get the start and end times as Date objects if available, or as numeric values otherwise.
* In the later case, the unit symbol tells how to convert to Date objects.
*/
Date startTime = decoder.dateValue(TIME.MINIMUM);
Date endTime = decoder.dateValue(TIME.MAXIMUM);
if (startTime == null && endTime == null) {
final Number tmin = decoder.numericValue(TIME.MINIMUM);
final Number tmax = decoder.numericValue(TIME.MAXIMUM);
if (tmin != null || tmax != null) {
final String symbol = stringValue(TIME.UNITS);
if (symbol != null) {
final Date[] dates = decoder.numberToDate(symbol, tmin, tmax);
startTime = dates[0];
endTime = dates[1];
}
}
}
/*
* If at least one time values above is available, add a temporal extent.
* This operation requires the the sis-temporal module. If not available,
* we will report a warning and leave the temporal extent missing.
*/
if (startTime != null || endTime != null)
try {
addTemporalExtent(startTime, endTime);
hasExtent = true;
} catch (UnsupportedOperationException e) {
warning(e);
}
return hasExtent;
}
use of javax.measure.UnitConverter in project sis by apache.
the class CRSBuilder method verify.
/**
* Verifies if the user-defined ellipsoid created from GeoTIFF values
* matches the given ellipsoid created from the EPSG geodetic dataset.
* This method does not verify the EPSG code.
*
* @param ellipsoid the ellipsoid created from the EPSG geodetic dataset.
* @param unit the unit of measurement declared in the GeoTIFF file.
*/
private void verify(final Ellipsoid ellipsoid, final Unit<Length> unit) {
final UnitConverter uc = ellipsoid.getAxisUnit().getConverterTo(unit);
verify(ellipsoid, uc.convert(ellipsoid.getSemiMajorAxis()), GeoKeys.SemiMajorAxis, unit);
verify(ellipsoid, uc.convert(ellipsoid.getSemiMinorAxis()), GeoKeys.SemiMinorAxis, unit);
verify(ellipsoid, ellipsoid.getInverseFlattening(), GeoKeys.InvFlattening, null);
}
use of javax.measure.UnitConverter in project com.revolsys.open by revolsys.
the class GeographicCoordinateSystem method getLengthUnit.
@Override
public Unit<Length> getLengthUnit() {
final Unit<Angle> unit = this.angularUnit.getUnit();
final UnitConverter radianConverter = unit.getConverterTo(Units.RADIAN);
final Ellipsoid ellipsoid = this.geodeticDatum.getEllipsoid();
final double radius = ellipsoid.getSemiMajorAxis();
final double radianFactor = radianConverter.convert(1);
return Units.METRE.multiply(radius).multiply(radianFactor);
}
Aggregations