use of javax.measure.Unit in project unit-api by unitsofmeasurement.
the class DefaultTestQuantityFormat method parse.
@SuppressWarnings("unchecked")
@Override
public Quantity<?> parse(CharSequence csq, int index) throws MeasurementParseException {
// cursor.getIndex();
int startDecimal = index;
while ((startDecimal < csq.length()) && Character.isWhitespace(csq.charAt(startDecimal))) {
startDecimal++;
}
int endDecimal = startDecimal + 1;
while ((endDecimal < csq.length()) && !Character.isWhitespace(csq.charAt(endDecimal))) {
endDecimal++;
}
try {
final Double decimal = new Double(csq.subSequence(startDecimal, endDecimal).toString());
final String uStr = csq.subSequence(endDecimal + 1, csq.length()).toString();
Unit unit = SimpleTestUnitFormat.getInstance().parse(uStr);
return TestQuantities.getQuantity(decimal, unit);
} catch (NumberFormatException nfe) {
throw new MeasurementParseException(nfe);
}
}
use of javax.measure.Unit in project com.revolsys.open by revolsys.
the class ProjectedCoordinateSystem method getCoordinatesOperation.
@Override
public CoordinatesOperation getCoordinatesOperation(final CoordinateSystem coordinateSystem) {
if (coordinateSystem == null || this == coordinateSystem) {
return null;
} else {
final List<CoordinatesOperation> operations = new ArrayList<>();
final CoordinatesOperation inverseOperation = this.getInverseCoordinatesOperation();
if (inverseOperation == null) {
return null;
}
final Unit<Length> linearUnit1 = this.getLengthUnit();
if (!linearUnit1.equals(Units.METRE)) {
operations.add(new UnitConverstionOperation(linearUnit1, Units.METRE));
}
operations.add(inverseOperation);
if (coordinateSystem instanceof ProjectedCoordinateSystem) {
final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
final CoordinatesOperation projectOperation = projectedCoordinateSystem.getProjectCoordinatesOperation();
if (projectOperation != null) {
operations.add(projectOperation);
}
final Unit<Length> linearUnit2 = projectedCoordinateSystem.getLengthUnit();
if (!linearUnit2.equals(Units.METRE)) {
operations.add(new UnitConverstionOperation(Units.METRE, linearUnit2));
}
} else if (coordinateSystem instanceof GeographicCoordinateSystem) {
final GeographicCoordinateSystem geographicCoordinateSystem = (GeographicCoordinateSystem) coordinateSystem;
final Unit<Angle> angularUnit2 = geographicCoordinateSystem.getUnit();
if (!angularUnit2.equals(NonSI.DEGREE_ANGLE)) {
operations.add(new UnitConverstionOperation(NonSI.DEGREE_ANGLE, angularUnit2, 2));
}
} else {
return null;
}
switch(operations.size()) {
case 0:
return null;
case 1:
return operations.get(0);
default:
return new ChainedCoordinatesOperation(operations);
}
}
}
use of javax.measure.Unit in project com.revolsys.open by revolsys.
the class GeographicCoordinateSystem method getCoordinatesOperation.
@Override
public CoordinatesOperation getCoordinatesOperation(final CoordinateSystem coordinateSystem) {
if (coordinateSystem == null || this == coordinateSystem) {
return null;
} else if (coordinateSystem instanceof GeographicCoordinateSystem) {
final GeographicCoordinateSystem geographicCoordinateSystem = (GeographicCoordinateSystem) coordinateSystem;
final Unit<Angle> angularUnit1 = getUnit();
// TODO GeodeticDatum shift
final Unit<Angle> angularUnit2 = geographicCoordinateSystem.getUnit();
if (!angularUnit1.equals(angularUnit2)) {
return new UnitConverstionOperation(angularUnit1, angularUnit2, 2);
} else {
return null;
}
} else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
final List<CoordinatesOperation> operations = new ArrayList<>();
final Unit<Angle> angularUnit1 = getUnit();
if (!angularUnit1.equals(NonSI.DEGREE_ANGLE)) {
CoordinatesOperation converstionOperation;
if (angularUnit1.equals(Units.RADIAN)) {
converstionOperation = RadiansToDegreesOperation.INSTANCE;
} else {
converstionOperation = new UnitConverstionOperation(angularUnit1, NonSI.DEGREE_ANGLE, 2);
}
operations.add(converstionOperation);
}
// TODO geodeticDatum shift
final CoordinatesOperation projectOperation = projectedCoordinateSystem.getProjectCoordinatesOperation();
if (projectOperation != null) {
operations.add(projectOperation);
}
final Unit<Length> linearUnit2 = projectedCoordinateSystem.getLengthUnit();
if (!linearUnit2.equals(Units.METRE)) {
operations.add(new UnitConverstionOperation(Units.METRE, linearUnit2));
}
switch(operations.size()) {
case 0:
return null;
case 1:
return operations.get(0);
default:
return new ChainedCoordinatesOperation(operations);
}
} else {
return null;
}
}
use of javax.measure.Unit in project com.revolsys.open by revolsys.
the class LineString method getLength.
@Override
default double getLength(final Unit<Length> unit) {
double length = 0;
final CoordinateSystem coordinateSystem = getCoordinateSystem();
if (coordinateSystem instanceof GeographicCoordinateSystem) {
final int vertexCount = getVertexCount();
if (vertexCount > 1) {
double lon0 = getX(0);
double lat0 = getY(0);
for (int i = 1; i < vertexCount; i++) {
final double lon1 = getX(i);
final double lat1 = getY(i);
length += GeographicCoordinateSystem.distanceMetres(lon0, lat0, lon1, lat1);
lon0 = lon1;
lat0 = lat1;
}
}
final Quantity<Length> lengthMeasure = Quantities.getQuantity(length, Units.METRE);
length = QuantityType.doubleValue(lengthMeasure, unit);
} else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
final Unit<Length> lengthUnit = projectedCoordinateSystem.getLengthUnit();
length = getLength();
final Quantity<Length> lengthMeasure = Quantities.getQuantity(length, lengthUnit);
length = QuantityType.doubleValue(lengthMeasure, unit);
} else {
length = getLength();
}
return length;
}
use of javax.measure.Unit in project com.revolsys.open by revolsys.
the class LinearRing method getPolygonArea.
default double getPolygonArea(final Unit<Area> unit) {
double area = 0;
final CoordinateSystem coordinateSystem = getCoordinateSystem();
if (coordinateSystem instanceof GeographicCoordinateSystem) {
// TODO better algorithm than converting to world mercator
final GeometryFactory geometryFactory = GeometryFactory.worldMercator();
final LinearRing ring = as2d(geometryFactory);
return ring.getPolygonArea(unit);
} else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
final Unit<Length> lengthUnit = projectedCoordinateSystem.getLengthUnit();
@SuppressWarnings("unchecked") final Unit<Area> areaUnit = (Unit<Area>) lengthUnit.multiply(lengthUnit);
area = getPolygonArea();
final Quantity<Area> areaMeasure = Quantities.getQuantity(area, areaUnit);
area = QuantityType.doubleValue(areaMeasure, unit);
} else {
area = getPolygonArea();
}
return area;
}
Aggregations