import Feature from 'ol/Feature.js'; import { PanoramaMap, type Projection, type VcsMap, CesiumMap, GeometryType, getDefaultProjection, getTextFromOptions, ObliqueMap, OpenlayersMap, originalFeatureSymbol, } from '@vcmap/core'; import type { VcsUiApp } from '@vcmap/ui'; import type { ShallowRef } from 'vue'; import { shallowRef } from 'vue'; import type { Text } from 'ol/style.js'; import type { MeasurementPlugin } from '../index.js'; import type { MeasurementFeature, MeasurementManager, } from '../measurementManager.js'; import { MapNames } from '../util/configHelper.js'; import type { MeasurementConfig } from '../util/configHelper.js'; import { name } from '../../package.json'; export type MeasurementValues = { type: MeasurementType; area?: string; circumference?: string; distance?: string; height?: string; /** angle in vertical measurement */ alpha?: string; /** angle in vertical measurement */ beta?: string; /** horizontal distance in vertical measurement */ horizontal?: string; /** point measurement coordinates */ pointPosition?: PositionValue; /** positions of distance measurement coordinates */ vertexPositions?: Array; groundAltitude?: string; heightAltitude?: string; }; /** * @api */ type PositionValue = { id: string | undefined; name: string | undefined; /** point measurement coordinates */ x: number | undefined; /** point measurement coordinates */ y: number | undefined; /** point measurement coordinates */ z: number | undefined; }; export const measurementModeSymbol = Symbol('measurementModeSymbol'); export const doNotEditAndPersistent = Symbol('doNotEditAndPersistentSymbol'); export const measurementTypeProperty = 'vcs_measurement_type'; export enum MeasurementType { Position3D = 'Position3D', Position2D = 'Position2D', Distance2D = 'Distance2D', Area2D = 'Area2D', Distance3D = 'Distance3D', Area3D = 'Area3D', Height3D = 'Height3D', ObliqueHeight2D = 'ObliqueHeight2D', } export const measurementGeometryType = { [MeasurementType.Position3D]: GeometryType.Point, [MeasurementType.Position2D]: GeometryType.Point, [MeasurementType.Distance2D]: GeometryType.LineString, [MeasurementType.Area2D]: GeometryType.Polygon, [MeasurementType.Distance3D]: GeometryType.LineString, [MeasurementType.Area3D]: GeometryType.Polygon, [MeasurementType.Height3D]: GeometryType.LineString, [MeasurementType.ObliqueHeight2D]: GeometryType.LineString, }; export function getValues(feature: MeasurementFeature): MeasurementValues { if (feature[measurementModeSymbol]) { return feature[measurementModeSymbol].values.value; } return feature[originalFeatureSymbol]?.[measurementModeSymbol]?.values .value as MeasurementValues; } export function isSupportedMeasurement( feature: MeasurementFeature, map: VcsMap, ): boolean { const supportedMaps: string[] = feature[measurementModeSymbol]?.supportedMaps ?? feature[originalFeatureSymbol]?.[measurementModeSymbol]?.supportedMaps ?? []; const isSupported = supportedMaps.includes(map.className); if (map instanceof PanoramaMap) { return isSupported && !!map.currentPanoramaImage?.hasDepth; } return isSupported; } type MeasurementModeOptions = { app: VcsUiApp; manager: MeasurementManager; }; class MeasurementMode { app: VcsUiApp; manager: MeasurementManager; projection: Projection; decimalPlaces: number; decimalPlacesZ?: number; decimalPlacesAngle: number; templateFeature: Feature; values: ShallowRef; constructor(options: MeasurementModeOptions) { this.app = options.app; this.manager = options.manager; this.projection = getDefaultProjection(); this.templateFeature = this.createTemplateFeature(); this.values = shallowRef(this.defaultValues); const { config } = this.app.plugins.getByKey(name) as MeasurementPlugin; const activeMapClassName = this.app.maps.activeMap?.className; this.decimalPlaces = config[activeMapClassName as keyof MeasurementConfig].decimalPlaces; this.decimalPlacesAngle = config[MapNames.Cesium].decimalPlacesAngle; if ( activeMapClassName === MapNames.Cesium || activeMapClassName === MapNames.Oblique || activeMapClassName === MapNames.Panorama ) { this.decimalPlacesZ = config[activeMapClassName].decimalPlacesZ ?? this.decimalPlaces; } } get defaultValues(): MeasurementValues { return { type: this.type, area: '0', circumference: '0', distance: '0', height: '0', alpha: '0', beta: '0', horizontal: '0', pointPosition: undefined, vertexPositions: [], }; } // eslint-disable-next-line class-methods-use-this get type(): MeasurementType { return MeasurementType.Area2D; } // eslint-disable-next-line class-methods-use-this get geometryType(): GeometryType { return GeometryType.Polygon; } // eslint-disable-next-line class-methods-use-this get supportedMaps(): string[] { return [CesiumMap.className, OpenlayersMap.className, ObliqueMap.className]; } getValue(value: number, square = false, useDecimalPLacesZ = false): string { let abs = Math.abs(value); const potential = square ? 2 : 1; let unit = square ? 'm²' : 'm'; const cutoff = 1000 ** potential; if (abs > cutoff * (square ? 1 : 10)) { abs /= cutoff; unit = `k${unit}`; } const formattedValue = (value < 0 ? '-' : '') + abs.toFixed(useDecimalPLacesZ ? this.decimalPlacesZ : this.decimalPlaces); return `${formattedValue} ${unit}`; } // eslint-disable-next-line class-methods-use-this check(feature: Feature): boolean { const geometry = feature.getGeometry(); if (!geometry) { return false; } return geometry.getCoordinates().length !== 0; } calcMeasurementResult(feature: Feature): Promise { return Promise.resolve(this.check(feature)); } createTemplateFeature(): Feature { const feature = new Feature(); feature.set(measurementTypeProperty, this.type); return feature; } static getDefaultText(): Text { return getTextFromOptions({ font: 'bold 18px Arial', textBaseline: 'bottom', offsetY: -20, offsetX: 0, fill: { color: [3, 9, 53, 1] }, stroke: { color: [255, 255, 255, 1], width: 2 }, }); } setProjection(projection: Projection): void { if (projection.epsg !== this.projection.epsg) { this.projection = projection; } } } export default MeasurementMode;