use of javax.measure.Unit in project sis by apache.
the class EPSGDataAccess method createUnit.
/**
* Creates an unit of measurement from a code.
* Current implementation first checks if {@link Units#valueOfEPSG(int)} can provide a hard-coded unit
* for the given code before to try to parse the information found in the database. This is done that
* way for better support of non-straightforward units like <cite>sexagesimal degrees</cite>
* (EPSG:9110 and 9111).
*
* <div class="note"><b>Example:</b>
* some EPSG codes for units are:
*
* <table class="sis" summary="EPSG codes examples">
* <tr><th>Code</th> <th>Description</th></tr>
* <tr><td>9002</td> <td>decimal degree</td></tr>
* <tr><td>9001</td> <td>metre</td></tr>
* <tr><td>9030</td> <td>kilometre</td></tr>
* <tr><td>1040</td> <td>second</td></tr>
* <tr><td>1029</td> <td>year</td></tr>
* </table></div>
*
* @param code value allocated by EPSG.
* @return the unit of measurement for the given code.
* @throws NoSuchAuthorityCodeException if the specified {@code code} was not found.
* @throws FactoryException if the object creation failed for some other reason.
*/
@Override
public synchronized Unit<?> createUnit(final String code) throws NoSuchAuthorityCodeException, FactoryException {
ArgumentChecks.ensureNonNull("code", code);
Unit<?> returnValue = null;
try (ResultSet result = executeQuery("Unit of Measure", "UOM_CODE", "UNIT_OF_MEAS_NAME", "SELECT UOM_CODE," + " FACTOR_B," + " FACTOR_C," + " TARGET_UOM_CODE," + " UNIT_OF_MEAS_NAME" + " FROM [Unit of Measure]" + " WHERE UOM_CODE = ?", code)) {
while (result.next()) {
final int source = getInteger(code, result, 1);
final double b = getOptionalDouble(result, 2);
final double c = getOptionalDouble(result, 3);
final int target = getInteger(code, result, 4);
if (source == target) {
/*
* The unit is a base unit. Verify its consistency:
* conversion from 'source' to itself shall be the identity function.
*/
final boolean pb = (b != 1);
if (pb || c != 1) {
throw new FactoryDataException(error().getString(Errors.Keys.InconsistentAttribute_2, pb ? "FACTOR_B" : "FACTOR_C", pb ? b : c));
}
}
// Check in our list of hard-coded unit codes.
Unit<?> unit = Units.valueOfEPSG(source);
if (unit == null) {
final Unit<?> base = Units.valueOfEPSG(target);
if (base != null && !Double.isNaN(b) && !Double.isNaN(c)) {
// May be NaN if the conversion is non-linear.
unit = Units.multiply(base, b, c);
} else
try {
// Try parsing the unit symbol as a fallback.
unit = Units.valueOf(getString(code, result, 5));
} catch (ParserException e) {
throw new FactoryDataException(error().getString(Errors.Keys.UnknownUnit_1, code), e);
}
}
returnValue = ensureSingleton(unit, returnValue, code);
}
} catch (SQLException exception) {
throw databaseFailure(Unit.class, code, exception);
}
if (returnValue == null) {
throw noSuchAuthorityCode(Unit.class, code);
}
return returnValue;
}
use of javax.measure.Unit in project sis by apache.
the class CommonAuthorityFactory method createUnitFromEPSG.
/**
* Returns the unit of measurement for the given EPSG code.
* This is used only for codes in the legacy {@code "AUTO"} namespace.
*/
private static Unit<?> createUnitFromEPSG(final double code) throws NoSuchAuthorityCodeException {
// Error message to be used only in case of failure.
String message = null;
// The string representation of the code, to be used only in case of failure.
final String s;
final int c = (int) code;
if (c == code) {
final Unit<?> unit = Units.valueOfEPSG(c);
if (Units.isLinear(unit)) {
return unit;
} else if (unit != null) {
message = Errors.format(Errors.Keys.NonLinearUnit_1, unit);
}
s = String.valueOf(c);
} else {
s = String.valueOf(code);
}
if (message == null) {
message = Resources.format(Resources.Keys.NoSuchAuthorityCode_3, Constants.EPSG, Unit.class, s);
}
throw new NoSuchAuthorityCodeException(message, Constants.EPSG, s);
}
use of javax.measure.Unit in project uom-se by unitsofmeasurement.
the class DefaultQuantityFormat method parse.
@SuppressWarnings("unchecked")
@Override
public ComparableQuantity<?> parse(CharSequence csq, ParsePosition cursor) throws ParserException {
int startDecimal = cursor.getIndex();
while ((startDecimal < csq.length()) && Character.isWhitespace(csq.charAt(startDecimal))) {
startDecimal++;
}
int endDecimal = startDecimal + 1;
while ((endDecimal < csq.length()) && !Character.isWhitespace(csq.charAt(endDecimal))) {
endDecimal++;
}
BigDecimal decimal = new BigDecimal(csq.subSequence(startDecimal, endDecimal).toString());
cursor.setIndex(endDecimal + 1);
Unit unit = SimpleUnitFormat.getInstance().parse(csq, cursor);
return Quantities.getQuantity(decimal, unit);
}
use of javax.measure.Unit in project uom-se by unitsofmeasurement.
the class EBNFHelper method newUnitPrecedenceInternal.
private static int newUnitPrecedenceInternal(Unit<?> unit, Appendable buffer, SymbolMap symbolMap) throws IOException {
UnitConverter converter = null;
boolean printSeparator = false;
StringBuilder temp = new StringBuilder();
int unitPrecedence = NOOP_PRECEDENCE;
Unit<?> parentUnit = unit.getSystemUnit();
converter = ((AbstractUnit<?>) unit).getSystemConverter();
if (KILOGRAM.equals(parentUnit)) {
if (unit instanceof TransformedUnit<?>) {
// incosistency
if (unit.equals(GRAM)) {
return noopPrecedenceInternal(buffer, symbolMap.getSymbol(GRAM));
} else {
// parentUnit = GRAM;
// converter = unit.getConverterTo((Unit) KILOGRAM);
converter = ((TransformedUnit) unit).getConverter();
}
// parentUnit = GRAM;
} else {
converter = unit.getConverterTo((Unit) GRAM);
}
} else if (CUBIC_METRE.equals(parentUnit)) {
if (converter != null) {
parentUnit = LITRE;
}
}
// https://github.com/unitsofmeasurement/si-units/issues/4
if (unit instanceof TransformedUnit) {
TransformedUnit transUnit = (TransformedUnit) unit;
if (parentUnit == null)
parentUnit = transUnit.getParentUnit();
// String x = parentUnit.toString();
converter = transUnit.getConverter();
}
unitPrecedence = formatInternal(parentUnit, temp, symbolMap);
printSeparator = !parentUnit.equals(AbstractUnit.ONE);
int result = ConverterFormatter.formatConverter(converter, printSeparator, unitPrecedence, temp, symbolMap);
buffer.append(temp);
return result;
}
use of javax.measure.Unit in project uom-se by unitsofmeasurement.
the class NumberSpaceQuantityFormat method parse.
@Override
public ComparableQuantity<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException, ParserException {
String str = csq.toString();
Number number = numberFormat.parse(str, cursor);
if (number == null)
throw new IllegalArgumentException("Number cannot be parsed");
Unit unit = unitFormat.parse(csq);
return Quantities.getQuantity(number.longValue(), unit);
}
Aggregations