-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlayer-vector-chart.component.ts
More file actions
132 lines (121 loc) · 3.74 KB
/
Copy pathlayer-vector-chart.component.ts
File metadata and controls
132 lines (121 loc) · 3.74 KB
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
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
effect,
inject,
input,
OnDestroy
} from '@angular/core';
import VectorTileLayer from 'ol/layer/VectorTile';
import VectorTileSource from 'ol/source/VectorTile';
import { MVT } from 'ol/format';
import { applyStyle } from 'ol-mapbox-style';
import { MapComponent } from '../map.component';
import { FBChart } from 'src/app/types';
import { initPMTilesVectorLayer } from './pmtiles-utils';
import { extentFromBounds, resolveLayerMaxZoom } from './chart-utils';
import { createAbortableVectorTileLoader } from './tile-loader-abort';
// ** Freeboard Vector TileLayer Chart **
@Component({
selector: 'ol-map > fb-tilelayer-vector',
template: '<ng-content></ng-content>',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: false
})
export class VectorChartLayerComponent implements OnDestroy {
protected chart = input<FBChart>();
protected zIndex = input<number>();
protected overZoomTiles = input<boolean>(true);
protected mapMaxZoom = input<number>();
private layer: VectorTileLayer;
private changeDetectorRef = inject(ChangeDetectorRef);
private mapComponent = inject(MapComponent);
constructor() {
this.changeDetectorRef.detach();
effect(() => {
this.chart();
this.zIndex();
this.overZoomTiles();
this.mapMaxZoom();
this.parseChart();
});
}
ngOnDestroy() {
const map = this.mapComponent.getMap();
if (this.layer) {
map.removeLayer(this.layer);
map.render();
}
}
private parseChart(chart: FBChart = this.chart()) {
const map = this.mapComponent.getMap();
if (!map) {
return;
}
if (!this.layer) {
const minZ =
chart[1].minZoom && chart[1].minZoom >= 0.1
? chart[1].minZoom - 0.1
: chart[1].minZoom;
const maxZ = chart[1].maxZoom;
const layerMaxZ = resolveLayerMaxZoom(
maxZ,
this.mapMaxZoom(),
this.overZoomTiles()
);
if (chart[1].url.indexOf('.pmtiles') !== -1) {
this.layer = initPMTilesVectorLayer(chart[1], this.zIndex());
} else {
this.layer = new VectorTileLayer({
source: new VectorTileSource({
url: chart[1].url,
format: new MVT({
layers:
Array.isArray(chart[1].layers) && chart[1].layers.length !== 0
? chart[1].layers
: null
}),
maxZoom: maxZ,
tileLoadFunction: createAbortableVectorTileLoader()
}),
preload: 0,
zIndex: this.zIndex(),
minZoom: minZ,
maxZoom: layerMaxZ,
opacity: chart[1].defaultOpacity ?? 1
});
}
if (this.layer) {
this.layer.setMinZoom(minZ);
this.layer.setMaxZoom(layerMaxZ);
this.layer.setExtent(extentFromBounds(chart[1].bounds));
if (chart[1].style) {
applyStyle(this.layer as any, chart[1].style);
}
this.layer.set('id', chart[0]);
this.layer.set('chartId', chart[0]);
this.layer.set('chartType', chart[1].type);
this.layer.set('chartFormat', chart[1].format);
map.addLayer(this.layer);
}
} else {
const minZ =
chart[1].minZoom && chart[1].minZoom >= 0.1
? chart[1].minZoom - 0.1
: chart[1].minZoom;
const maxZ = chart[1].maxZoom;
const layerMaxZ = resolveLayerMaxZoom(
maxZ,
this.mapMaxZoom(),
this.overZoomTiles()
);
this.layer.setZIndex(this.zIndex());
this.layer.setMinZoom(minZ);
this.layer.setMaxZoom(layerMaxZ);
this.layer.setOpacity(chart[1].defaultOpacity ?? 1);
this.layer.setExtent(extentFromBounds(chart[1].bounds));
}
map.render();
}
}