|
| 1 | +import { |
| 2 | + ChangeDetectionStrategy, |
| 3 | + ChangeDetectorRef, |
| 4 | + Component, |
| 5 | + Input, |
| 6 | + OnChanges, |
| 7 | + OnDestroy, |
| 8 | + OnInit, |
| 9 | + Output, |
| 10 | + SimpleChanges, |
| 11 | + input, |
| 12 | + effect |
| 13 | +} from '@angular/core'; |
| 14 | +import { Layer } from 'ol/layer'; |
| 15 | +import { Feature } from 'ol'; |
| 16 | +import VectorLayer from 'ol/layer/Vector'; |
| 17 | +import VectorSource from 'ol/source/Vector'; |
| 18 | +import { Style, Stroke } from 'ol/style'; |
| 19 | +import { MultiLineString } from 'ol/geom'; |
| 20 | +import { MapComponent } from '../map.component'; |
| 21 | +import { Extent, Coordinate } from '../models'; |
| 22 | +import { fromLonLatArray, splitAtAntimeridian, lineDashFor } from '../util'; |
| 23 | +import { AsyncSubject } from 'rxjs'; |
| 24 | +import { LineStyleConfig } from 'src/app/types'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Great-circle arc line component. |
| 28 | + * |
| 29 | + * Receives an array of [lon, lat] coordinates that are already densified |
| 30 | + * (many intermediate great-circle points). Renders them as a multi-vertex |
| 31 | + * LineString that visually curves on a Mercator map. |
| 32 | + */ |
| 33 | +@Component({ |
| 34 | + selector: 'ol-map > fb-great-circle', |
| 35 | + template: '<ng-content></ng-content>', |
| 36 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 37 | + standalone: false |
| 38 | +}) |
| 39 | +export class GreatCircleComponent implements OnInit, OnDestroy, OnChanges { |
| 40 | + protected layer: Layer; |
| 41 | + public source: VectorSource; |
| 42 | + protected features: Array<Feature> = []; |
| 43 | + |
| 44 | + @Output() layerReady: AsyncSubject<Layer> = new AsyncSubject(); |
| 45 | + |
| 46 | + protected coords = input<Coordinate[]>(); |
| 47 | + protected lineStyle = input<LineStyleConfig>(); |
| 48 | + |
| 49 | + @Input() opacity: number; |
| 50 | + @Input() visible: boolean; |
| 51 | + @Input() extent: Extent; |
| 52 | + @Input() zIndex: number; |
| 53 | + @Input() minResolution: number; |
| 54 | + @Input() maxResolution: number; |
| 55 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 56 | + @Input() layerProperties: { [index: string]: any }; |
| 57 | + |
| 58 | + constructor( |
| 59 | + protected changeDetectorRef: ChangeDetectorRef, |
| 60 | + protected mapComponent: MapComponent |
| 61 | + ) { |
| 62 | + this.changeDetectorRef.detach(); |
| 63 | + effect(() => { |
| 64 | + this.coords(); |
| 65 | + this.lineStyle(); |
| 66 | + this.parseInput(); |
| 67 | + if (this.source) { |
| 68 | + this.source.clear(); |
| 69 | + this.source.addFeatures(this.features); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + ngOnInit() { |
| 75 | + this.parseInput(); |
| 76 | + this.source = new VectorSource({ features: this.features }); |
| 77 | + this.layer = new VectorLayer( |
| 78 | + Object.assign(this, { ...this.layerProperties }) |
| 79 | + ); |
| 80 | + const map = this.mapComponent.getMap(); |
| 81 | + if (this.layer && map) { |
| 82 | + map.addLayer(this.layer); |
| 83 | + map.render(); |
| 84 | + this.layerReady.next(this.layer); |
| 85 | + this.layerReady.complete(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + ngOnChanges(changes: SimpleChanges) { |
| 90 | + if (this.layer) { |
| 91 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 92 | + const properties: { [index: string]: any } = {}; |
| 93 | + for (const key in changes) { |
| 94 | + if (key === 'layerProperties') { |
| 95 | + this.layer.setProperties(properties, false); |
| 96 | + } else { |
| 97 | + properties[key] = changes[key].currentValue; |
| 98 | + } |
| 99 | + } |
| 100 | + this.layer.setProperties(properties, false); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + ngOnDestroy() { |
| 105 | + const map = this.mapComponent.getMap(); |
| 106 | + if (this.layer && map) { |
| 107 | + map.removeLayer(this.layer); |
| 108 | + map.render(); |
| 109 | + this.layer = null; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + parseInput() { |
| 114 | + const fa: Feature[] = []; |
| 115 | + const c = this.coords(); |
| 116 | + if (Array.isArray(c) && c.length >= 2) { |
| 117 | + const projected = splitAtAntimeridian(c.map(p => [p[0], p[1]] as Coordinate)) |
| 118 | + .map(seg => fromLonLatArray(seg)); |
| 119 | + const feat = new Feature({ |
| 120 | + geometry: new MultiLineString(projected) |
| 121 | + }); |
| 122 | + feat.setId('greatCircleSelf'); |
| 123 | + const ls = this.lineStyle(); |
| 124 | + const color = ls?.color ?? 'rgba(0,128,255,0.7)'; |
| 125 | + const lineWidth = ls?.weight ?? 1; |
| 126 | + const lineDash = ls ? lineDashFor(ls.dash) : [8, 4]; |
| 127 | + feat.setStyle( |
| 128 | + new Style({ |
| 129 | + stroke: new Stroke({ color, width: lineWidth, lineDash }) |
| 130 | + }) |
| 131 | + ); |
| 132 | + fa.push(feat); |
| 133 | + } |
| 134 | + this.features = fa; |
| 135 | + } |
| 136 | +} |
0 commit comments