@itwin/measure-tools-react
Version:
Frontend framework and tools for measurements
115 lines • 6.04 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { IModelApp, QuantityType } from "@itwin/core-frontend";
import { MeasureTools } from "../MeasureTools.js";
export class FormatterUtils {
static removeUnitSuffixes(s) {
s = s.replace(/ m/g, "");
s = s.replace(/ ft/g, "");
return s;
}
static formatCoordinatesWithSpec(point, spec) {
const xStr = FormatterUtils.removeUnitSuffixes(IModelApp.quantityFormatter.formatQuantity(point.x, spec));
const yStr = FormatterUtils.removeUnitSuffixes(IModelApp.quantityFormatter.formatQuantity(point.y, spec));
const zStr = FormatterUtils.removeUnitSuffixes(IModelApp.quantityFormatter.formatQuantity(point.z, spec));
return `${xStr}, ${yStr}, ${zStr}`;
}
static formatCoordinatesXYWithSpec(point, spec) {
const xStr = FormatterUtils.removeUnitSuffixes(IModelApp.quantityFormatter.formatQuantity(point.x, spec));
const yStr = FormatterUtils.removeUnitSuffixes(IModelApp.quantityFormatter.formatQuantity(point.y, spec));
return `${xStr}, ${yStr}`;
}
static async formatCoordinates(point) {
const coordSpec = await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(QuantityType.Coordinate);
if (undefined === coordSpec)
return "";
return FormatterUtils.formatCoordinatesWithSpec(point, coordSpec);
}
static formatCoordinatesImmediate(point) {
const coordSpec = IModelApp.quantityFormatter.findFormatterSpecByQuantityType(QuantityType.Coordinate);
if (undefined === coordSpec)
return "";
return FormatterUtils.formatCoordinatesWithSpec(point, coordSpec);
}
static async formatCoordinatesXY(point) {
const coordSpec = await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(QuantityType.Coordinate);
if (undefined === coordSpec)
return "";
return FormatterUtils.formatCoordinatesXYWithSpec(point, coordSpec);
}
static formatCoordinatesXYImmediate(point) {
const coordSpec = IModelApp.quantityFormatter.findFormatterSpecByQuantityType(QuantityType.Coordinate);
if (undefined === coordSpec)
return "";
return FormatterUtils.formatCoordinatesXYWithSpec(point, coordSpec);
}
/** Formats the input angle into DD°MM'SS.SS" format.
* NOTE: uses the same symbols as the IModelApp's quantityFormatter for minute and second.
* The minute symbol is an apostrophe ' while it should be a prime (\u2032)
* The second symbol is a quotation mark " while it should be a double prime (\u2033)
*/
static formatAngleToDMS(angleInDegrees) {
const isNegative = angleInDegrees < 0;
angleInDegrees = Math.abs(angleInDegrees);
const d = Math.trunc(angleInDegrees);
const m = Math.abs(Math.trunc((angleInDegrees - d) * 60));
const s = Math.abs(((angleInDegrees - d) * 60 - m) * 60);
let str = isNegative ? "-" : "";
str += d;
str += "\xB0";
str += `00${m}`.slice(-2);
str += "\u0027";
str += `00000${s.toFixed(2)}`.slice(-5);
str += "\u0022";
return str;
}
static formatCartographicToLatLongDMS(c) {
const latSuffixKey = 0 < c.latitude
? "MeasureTools:Generic.latitudeNorthSuffix"
: "MeasureTools:Generic.latitudeSouthSuffix";
const longSuffixKey = 0 < c.longitude
? "MeasureTools:Generic.longitudeEastSuffix"
: "MeasureTools:Generic.longitudeWestSuffix";
let str = FormatterUtils.formatAngleToDMS(Math.abs(c.latitudeDegrees));
str += MeasureTools.localization.getLocalizedString(latSuffixKey);
str += ", ";
str += FormatterUtils.formatAngleToDMS(Math.abs(c.longitudeDegrees));
str += MeasureTools.localization.getLocalizedString(longSuffixKey);
return str;
}
static async formatCartographicToLatLong(c) {
const latLongSpec = await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(QuantityType.LatLong);
const latSuffixKey = 0 < c.latitude
? "MeasureTools:Generic.latitudeNorthSuffix"
: "MeasureTools:Generic.latitudeSouthSuffix";
const longSuffixKey = 0 < c.longitude
? "MeasureTools:Generic.longitudeEastSuffix"
: "MeasureTools:Generic.longitudeWestSuffix";
let str = IModelApp.quantityFormatter.formatQuantity(Math.abs(c.latitude), latLongSpec);
str += MeasureTools.localization.getLocalizedString(latSuffixKey);
str += ", ";
str += IModelApp.quantityFormatter.formatQuantity(Math.abs(c.longitude), latLongSpec);
str += MeasureTools.localization.getLocalizedString(longSuffixKey);
return str;
}
static formatSlope(slopeInPercent, withSlopeRatio) {
const fSlope = `${slopeInPercent.toFixed(2)}%`;
if (!withSlopeRatio || 0.0 === slopeInPercent)
return fSlope;
const oneOverSlope = 100.0 / Math.abs(slopeInPercent);
const sign = slopeInPercent < 0.0 ? "-" : "";
const fSlopeRatio = `${sign}1 : ${oneOverSlope.toFixed(3)}`;
return `${fSlope} (${fSlopeRatio})`;
}
static async formatStation(station) {
const spec = await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(QuantityType.Stationing);
return IModelApp.quantityFormatter.formatQuantity(station, spec);
}
static async formatLength(length) {
const spec = await IModelApp.quantityFormatter.getFormatterSpecByQuantityType(QuantityType.LengthEngineering);
return IModelApp.quantityFormatter.formatQuantity(length, spec);
}
}
//# sourceMappingURL=FormatterUtils.js.map