All files / si-map/services map.service.ts

45.05% Statements 82/182
30.55% Branches 44/144
51.35% Functions 19/37
44.63% Lines 79/177

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660                                                                                              22x 22x 22x 22x         22x 22x     48x 48x 48x 48x 48x                                                                                                                                                                                                                                 21x       42x 42x 42x       30x 30x               29x   81059x                                                             20x                       12x                           24x                                                                                             12x 12x     12x 12x   12x       12x   12x 6x                                                   6x     12x                                                                                                                                                                                                                     5x 5x 5x 5x 5x       5x 5x 5x   5x           5x         5x       6x   6x                             6x             6x 6x 6x 6x   6x 10572x 10572x     10572x 15x 15x     15x     10557x 10539x   18x       6x 6x 6x     6x 18x       6x             20x 20x       12x   12x 33x     33x 18x     33x                                                                                                                                                     15x 3x   12x 9x   3x                                      
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
/// <reference types="./ol-ext.d.ts" />
import { Injectable } from '@angular/core';
import AnimatedCluster from 'ol-ext/layer/AnimatedCluster';
import Chart from 'ol-ext/style/Chart';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import Layer from 'ol/layer/Layer';
import VectorLayer from 'ol/layer/Vector';
import { fromLonLat } from 'ol/proj';
import Cluster from 'ol/source/Cluster';
import VectorSource from 'ol/source/Vector';
import { Circle, Fill, Icon, Stroke, Style, Text } from 'ol/style';
import { Options as IconOptions } from 'ol/style/Icon';
import ImageStyle from 'ol/style/Image';
 
import { ColorPalette } from '../models/color-palette.type';
import {
  ANIMATION_DURATION,
  CLUSTER_CHARTS_FRAGMENTS,
  CLUSTER_RADIUS_SIZE,
  DEFAULT_CLUSTER_TYPE,
  DEFAULT_DONUT_RATIO,
  DEFAULT_ICON,
  DEFAULT_ICON_SCALE,
  DEFAULT_OFFSET,
  DEFAULT_STROKE_WEIGHT,
  MAX_CLUSTER_BUILDINGS,
  SIEMENS_FONT,
  SIEMENS_FONT_LABELS
} from '../models/constants';
import { Grouping } from '../models/grouping.model';
import {
  IconMarkerOptions,
  LabelOptions,
  MapPoint,
  MarkerOptions
} from '../models/map-point.interface';
import { themeElement, ThemeType } from '../shared/themes/element';
import { getMarker } from '../shared/themes/marker';
 
@Injectable({
  providedIn: 'root'
})
export class MapService {
  private styleCache: Record<string, Style[]> = {};
  private clusterStyleCache: Record<string, Style[]> = {};
  private labelStyleCache: Record<string, Style[]> = {};
  private grouping?: Grouping;
 
  private _theme?: ThemeType;
 
  private wrappedTextCache: Record<string, string> = {};
  private labelTextStyleCache: Record<string, Text[]> = {};
 
  resetCaches(): void {
    this.styleCache = {};
    this.clusterStyleCache = {};
    this.labelStyleCache = {};
    this.wrappedTextCache = {};
    this.labelTextStyleCache = {};
  }
 
  private wrapLabelText(labelText: string, maxLength: number, maxLines: number): string {
    const cacheKey = `${labelText}_${maxLength}_${maxLines}`;
    if (this.wrappedTextCache[cacheKey]) {
      return this.wrappedTextCache[cacheKey];
    }
 
    let wrappedText = '';
    let currentLine = '';
    let lineCount = 0;
    const words = labelText.split(/[ \n]/g);
    if (maxLength > 0) {
      for (const word of words) {
        const strippedWord =
          word.length > maxLength ? word.substring(0, maxLength - 3) + '...' : word;
        if ((currentLine.length || -1) + 1 + strippedWord.length > maxLength) {
          if (lineCount + 1 >= maxLines) {
            const wrappedLine = currentLine.endsWith('...')
              ? currentLine
              : (currentLine.length > maxLength
                  ? currentLine.substring(0, maxLength - 3)
                  : currentLine) + '...';
            wrappedText += (wrappedText ? '\n' : '') + wrappedLine;
            return (this.wrappedTextCache[cacheKey] = wrappedText);
          } else {
            wrappedText += (wrappedText ? '\n' : '') + currentLine;
          }
          currentLine = '';
          lineCount++;
        }
        currentLine += (currentLine ? ' ' : '') + strippedWord;
      }
      if (currentLine) {
        wrappedText += (wrappedText ? '\n' : '') + currentLine;
      }
    } else {
      wrappedText = labelText;
    }
 
    return (this.wrappedTextCache[cacheKey] = wrappedText);
  }
 
  private getLabelTextStyle(
    text: string = '',
    maxLength: number,
    maxLines: number,
    markerSizes: [number, number],
    markerOffset: [number, number]
  ): Text[] {
    const cacheKey = `${text}_${maxLength}_${SIEMENS_FONT_LABELS}_${markerSizes[0]}_${markerSizes[1]}_${markerOffset[0]}_${markerOffset[1]}`;
    if (this.labelTextStyleCache[cacheKey]) {
      return this.labelTextStyleCache[cacheKey];
    }
 
    const wrappedText = this.wrapLabelText(text, maxLength, maxLines);
    const margin = 6;
    const extraPadding = 1;
    const halfWidth = markerSizes[0] / 2;
    const halfHeight = markerSizes[1] / 2;
    const placementOffsets = [
      {
        offsetX: markerOffset[0],
        offsetY: margin + markerOffset[1],
        align: { h: 'center', v: 'top' }
      },
      {
        offsetX: markerOffset[0],
        offsetY: -margin - markerSizes[1] + markerOffset[1],
        align: { h: 'center', v: 'bottom' }
      },
      {
        offsetX: -margin - halfWidth + markerOffset[0],
        offsetY: markerOffset[1] - halfHeight,
        align: { h: 'right', v: 'middle' }
      },
      {
        offsetX: margin + halfWidth - markerOffset[0],
        offsetY: markerOffset[1] - halfHeight,
        align: { h: 'left', v: 'middle' }
      }
    ] as const;
 
    return (this.labelTextStyleCache[cacheKey] = placementOffsets.map(
      offset =>
        new Text({
          text: wrappedText,
          font: SIEMENS_FONT_LABELS,
          fill: new Fill({ color: this.theme.textColor }),
          stroke: new Stroke({ color: this.theme.strokeColor, width: 4 }),
          offsetX: offset.offsetX,
          offsetY: offset.offsetY,
          padding: [
            offset.offsetY >= 0 ? offset.offsetY + extraPadding : 0,
            offset.offsetX < 0 ? -offset.offsetX - extraPadding : 0,
            offset.offsetY < 0 ? -offset.offsetY - extraPadding : 0,
            offset.offsetX >= 0 ? offset.offsetX + extraPadding : 0
          ],
          textAlign: offset.align.h,
          textBaseline: offset.align.v,
          overflow: true
        })
    ));
  }
 
  private zoomCallback: () => number | undefined = () => undefined;
 
  private getZoom(): number {
    return this.zoomCallback?.() ?? 3;
  }
 
  setZoomCallback(callback?: () => number | undefined): void {
    this.zoomCallback = callback ?? (() => undefined);
  }
 
  setTheme(groupColors: Record<number, string> | ColorPalette | undefined): void {
    this.resetCaches();
    this.grouping = new Grouping(groupColors);
    this._theme = undefined;
  }
 
  get theme(): ThemeType {
    this._theme ??= themeElement.style();
    return this._theme;
  }
 
  /**
   * Create feature with parameters
   * @param points - the list of points
   */
  createFeatures(points: MapPoint[], alwaysShowLabels = false): Feature[] {
    return points.map(
      point =>
        new Feature({
          geometry: new Point(fromLonLat([point.lon, point.lat])),
          // Append MapPointMetaData that will be available in getProperties()
          meta: point,
          name: point.name,
          description: point.description,
          marker: point.marker,
          label: alwaysShowLabels ? ({ text: point.name } as LabelOptions) : undefined,
          group: point.group,
          // TODO: Remove extraProps in v50
          // For backward compatibility, extraProps is still supported, but extraProperties should be used going forward
          extraProps: point.extraProperties,
          extraProperties: point.extraProperties,
          click: point.click
        })
    );
  }
 
  /**
   * Initialize animated cluster layer for points if clustering is enabled
   * @param features - for the source
   * @param markerAnimation - animation value for markers
   * @param clusterDistance - number distance between clusters
   * @param multiWorld - boolean if points should be repeated on multiple world maps
   */
  clusterLayer(
    features: Feature[],
    markerAnimation: boolean,
    clusterDistance: number,
    multiWorld: boolean
  ): Layer {
    return new AnimatedCluster({
      className: 'animated-cluster',
      zIndex: 1000,
      source: new Cluster({
        wrapX: multiWorld,
        distance: this.calculateClusteringDistance(clusterDistance, CLUSTER_RADIUS_SIZE),
        minDistance: CLUSTER_RADIUS_SIZE * 2,
        source: new VectorSource({
          features
        })
      }),
      animationDuration: markerAnimation ? ANIMATION_DURATION : 0,
      style: (feature: Feature) => this.getClusterStyle(feature)
    });
  }
 
  /**
   * Create vector layer when clustering is disabled
   * @param features - list of Features
   * @param multiWorld - boolean if points should be repeated on multiple world maps
   */
  vectorLayer(
    features: Feature[],
    multiWorld: boolean,
    defaultStyle: boolean = false
  ): VectorLayer<VectorSource> {
    return new VectorLayer({
      className: 'vector-layer',
      zIndex: 1000,
      source: new VectorSource({
        features,
        wrapX: multiWorld
      }),
      style: !defaultStyle
        ? feature => this.getFeatureStyle(feature as Feature, false, features)
        : undefined
    });
  }
 
  labelLayer(
    features: Feature[],
    multiWorld: boolean,
    maxLabelLength: number,
    maxLines: number,
    clusterLayer?: Layer
  ): VectorLayer<VectorSource> {
    const clusterSource = clusterLayer?.getSource() as Cluster<Feature<Point>>;
    return new VectorLayer({
      className: 'vector-layer',
      zIndex: 1200,
      source: new VectorSource({
        features,
        wrapX: multiWorld
      }),
      declutter: true,
      style: feature =>
        this.getLabelStyle(
          feature as Feature<Point>,
          features as Feature<Point>[],
          maxLabelLength,
          maxLines,
          clusterSource,
          false
        )
    });
  }
 
  /**
   * Cluster styling
   *
   * @param feature - Feature
   */
  getClusterStyle(feature: Feature): Style[] {
    const features = feature.getProperties().features;
    Iif (!features) {
      return this.getFeatureStyle(feature, true, features);
    }
    const uid = feature.getProperties().geometry.ol_uid;
    const size = features.length;
 
    Iif (size === 1) {
      return this.getFeatureStyle(feature, true, features);
    }
 
    let style: Style[] = this.clusterStyleCache[uid];
 
    if (!style) {
      style = [
        new Style({
          image: new Circle({
            radius: CLUSTER_RADIUS_SIZE,
            fill: new Fill({
              color: this.theme.fillColor
            }),
            stroke: new Stroke({
              color: this.theme.strokeColor,
              width: 6
            })
          })
        }),
        new Style({
          image: this.getClusterChart(features, this.grouping!),
          text: new Text({
            text: size > MAX_CLUSTER_BUILDINGS ? `${MAX_CLUSTER_BUILDINGS}+` : size.toString(),
            placement: 'point',
            textAlign: 'center',
            textBaseline: 'middle',
            fill: new Fill({ color: this.theme.textColor }),
            font: SIEMENS_FONT
          })
        })
      ];
 
      this.clusterStyleCache[uid] = style;
    }
 
    return style;
  }
 
  /**
   * Separate feature styling with icon
   * @param feature - to style
   * @param cluster - indication
   * @param features - list of features
   */
  getFeatureStyle(feature: Feature, cluster: boolean, features: Feature[], dummy = false): Style[] {
    const subFeatures = feature.getProperties()?.features;
    const feat = cluster && features ? subFeatures[0] : feature;
    const marker: MarkerOptions = feat.getProperties().marker;
 
    if (feat) {
      const uid = feat.getProperties().geometry.ol_uid + (dummy ? '~' : '');
      let style = this.styleCache[uid];
 
      if (!style) {
        style = [
          new Style(
            dummy
              ? {}
              : {
                  image: this.getMarkerStyle(feat, this.grouping!, marker)
                }
          )
        ];
 
        this.styleCache[uid] = style;
      }
 
      return style;
    }
 
    return [
      new Style({
        stroke: new Stroke({
          color: this.theme.strokeColor,
          width: DEFAULT_STROKE_WEIGHT
        })
      })
    ];
  }
 
  getLabelStyle(
    feature: Feature<Point>,
    features: Feature<Point>[],
    maxLabelLength: number,
    maxLines: number,
    clusterSource?: Cluster<Feature<Point>>,
    dummy = false
  ): Style[] {
    const featureStyle = this.getFeatureStyle(feature, false, features, true);
 
    // const marker: MarkerOptions = feature.getProperties().marker;
    const label: LabelOptions = feature.getProperties().label;
 
    if (dummy || !label?.text) {
      return featureStyle;
    }
 
    // Determine if the feature is clustered
    let isClustered = !!clusterSource;
 
    if (clusterSource) {
      let clusterFeatures = [];
 
      // Here we iterate over all clusters and check if the feature is among them
      clusterSource.getFeatures().forEach(cluster => {
        clusterFeatures = cluster.get('features');
        if (clusterFeatures?.length === 1 && clusterFeatures[0] === feature) {
          isClustered = false;
        }
      });
    }
 
    // Only show labels for features that are not clustered
    if (!isClustered) {
      const uid = feature.getProperties().geometry.ol_uid + '~' + this.getZoom();
      let style = this.labelStyleCache[uid];
 
      if (!style) {
        const labelStyles = this.getLabelTextStyle(
          label?.text,
          maxLabelLength,
          maxLines,
          this.getMarkerSize(feature),
          this.getMarkerOffset(feature)
        );
        style = labelStyles.map(
          labelStyle =>
            new Style({
              text: labelStyle
            })
        );
 
        this.labelStyleCache[uid] = style;
      }
 
      return [...featureStyle, ...style];
    }
 
    return featureStyle;
  }
 
  getMarkerOffset(feature: Feature): [number, number] {
    const marker = feature.getProperties()?.marker;
    const markerType = marker?.type;
    const [, height] = this.getMarkerSize(feature);
    const offsetVertical = !markerType || markerType === 'status' ? 0 : height / 2;
    return [0, offsetVertical];
  }
 
  getMarkerSize(feature: Feature): [number, number] {
    const marker = feature.getProperties()?.marker;
    const markerType = marker?.type;
    const scale = (markerType === 'icon' ? marker?.icon?.scale : 1) ?? 1;
    const height =
      !markerType || markerType === 'status'
        ? 32
        : markerType === 'dot'
          ? 28
          : DEFAULT_OFFSET * 2 * scale;
    const width =
      !markerType || markerType === 'status'
        ? 22
        : markerType === 'dot'
          ? 28
          : DEFAULT_OFFSET * 2 * scale;
    return [width, height];
  }
 
  private getClusterChart(features: Feature[], grouping: Grouping): ImageStyle {
    const { data, colors } = this.getClusterChartData(features, grouping);
 
    const chart = new Chart({
      type: DEFAULT_CLUSTER_TYPE,
      radius: CLUSTER_RADIUS_SIZE,
      donutRatio: DEFAULT_DONUT_RATIO,
      colors,
      data,
      rotateWithView: true,
      snapToPixel: true,
      stroke: new Stroke({
        color: colors.length > 1 ? this.theme.strokeColor : 'rgba(0,0,0,0)',
        width: colors.length > 1 ? DEFAULT_STROKE_WEIGHT : 0
      }),
      overflow: true
    } as any); // types are wrong here and don't match the actual API
 
    return chart;
  }
 
  private getClusterChartData(
    features: Feature[],
    grouping: Grouping
  ): { data: number[]; colors: string[] } {
    const theme = this.theme;
    const groups: Record<number, number> = {};
    const ungrouped: number[] = [];
    const ungroupedColors: string[] = [];
 
    features.forEach(feat => {
      const props = feat.getProperties();
      const groupId = props.group;
 
      // Ungrouped features are considered as they have group on their own.
      if (groupId == null) {
        ungrouped.push(1);
        ungroupedColors.push(
          this.getStatusMarkerColor(theme, props.marker) ?? theme.defaultMarkerColor
        );
        return;
      }
 
      if (groups[groupId]) {
        groups[groupId] = groups[groupId] + 1;
      } else {
        groups[groupId] = 1;
      }
    });
 
    const data: number[] = [];
    this.transformChartData(data, Object.values(groups), features.length);
    this.transformChartData(data, ungrouped, features.length);
 
    // Corresponding colors of groups are based on matching index
    const colors = [
      ...Object.keys(groups).map(groupId => grouping.getColor(Number(groupId))),
      ...ungroupedColors
    ];
 
    return { data, colors };
  }
 
  /**
   * Calculates minimal safe distance for clustering to avoid overlapping of clusters
   */
  private calculateClusteringDistance(distance: number, clusterRadius: number): number {
    const safeDistance = clusterRadius * 2;
    return Math.max(safeDistance, distance);
  }
 
  private transformChartData(data: number[], series: number[], size: number): void {
    const fragmentSize = size / CLUSTER_CHARTS_FRAGMENTS;
 
    for (const value of series) {
      let fragment = value / fragmentSize;
 
      // Any fragment smaller than 1 must be represented at least as one fragment in the chart
      if (fragment > 0 && fragment < 1) {
        fragment = 1;
      }
 
      data.push(fragment);
    }
  }
 
  private getMarkerStyle(
    feature: Feature,
    grouping: Grouping,
    marker?: MarkerOptions
  ): Icon | Circle | undefined {
    if (!marker || marker.type === 'status') {
      return this.getStatusMarker(marker ?? { type: 'status', status: 'default' });
    }
 
    const color = this.getMarkerColor(feature, grouping, marker);
 
    if (marker.type === 'dot') {
      return this.getDotMarker(color);
    }
 
    if (marker.type === 'icon') {
      return this.getIconMarker(color, marker.icon);
    }
 
    return undefined;
  }
 
  /**
   * Decides what color should Marker be painted with.
   *
   * Markers are painted with group colors if group id is present. If marker color is specified,
   * it takes precedence over the group color.
   */
  private getMarkerColor(feature: Feature, grouping: Grouping, marker?: MarkerOptions): string {
    let color = this.theme.defaultMarkerColor;
 
    const groupId = feature.getProperties().group;
    if (groupId) {
      color = grouping.getColor(groupId);
    }
 
    if (marker?.color) {
      color = marker.color;
    }
 
    if (marker?.status) {
      color = this.theme.status[marker.status];
    }
 
    return color;
  }
 
  private getDotMarker(color: string): Circle {
    return new Circle({
      radius: 10,
      fill: new Fill({
        color
      }),
      stroke: new Stroke({
        color: this.theme.strokeColor,
        width: 6
      })
    });
  }
 
  private getIconMarker(color: string, iconConf?: IconMarkerOptions): Icon {
    const defaultIconStyle: IconOptions = {
      color,
      src: DEFAULT_ICON,
      scale: DEFAULT_ICON_SCALE
    };
 
    return new Icon({ ...defaultIconStyle, ...iconConf });
  }
 
  private getStatusMarkerColor(theme: ThemeType, marker?: MarkerOptions): string | undefined {
    if (!marker) {
      return theme.status.default;
    }
    if (marker.type === 'status') {
      return theme.status[marker.status!];
    }
    return marker.color;
  }
 
  private getStatusMarker(marker: MarkerOptions): Icon {
    marker.color = this.getStatusMarkerColor(this.theme, marker);
 
    const svg = getMarker(marker.status!, this.theme);
    const svgb64 = 'data:image/svg+xml;base64,' + btoa(svg);
 
    return new Icon({
      size: [64, 64],
      color: 'transparent', // Note: must be 'transparent' or '#fff' for OL to keep the colors
      width: 42,
      height: 42,
      displacement: [0, 16],
      src: svgb64
    });
  }
}