-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathol-map-manager.ts
385 lines (357 loc) · 13.5 KB
/
ol-map-manager.ts
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
import type { Store } from '@ngrx/store';
import {
upperLeftCornerOf,
lowerRightCornerOf,
} from 'digital-fuesim-manv-shared';
import { Collection, View } from 'ol';
import type { Interaction } from 'ol/interaction';
import type VectorLayer from 'ol/layer/Vector';
import OlMap from 'ol/Map';
import type VectorSource from 'ol/source/Vector';
import { Subject, takeUntil } from 'rxjs';
import type { ExerciseService } from 'src/app/core/exercise.service';
import type { AppState } from 'src/app/state/app.state';
import {
selectSimulatedRegions,
selectViewports,
} from 'src/app/state/application/selectors/exercise.selectors';
import {
selectCurrentRole,
selectRestrictedViewport,
} from 'src/app/state/application/selectors/shared.selectors';
import { selectStateSnapshot } from 'src/app/state/get-state-snapshot';
import { fromLonLat } from 'ol/proj';
import type { TransferLinesService } from '../../core/transfer-lines.service';
import { startingPosition } from '../../starting-position';
import { CateringLinesFeatureManager } from '../feature-managers/catering-lines-feature-manager';
import { DeleteFeatureManager } from '../feature-managers/delete-feature-manager';
import { MapImageFeatureManager } from '../feature-managers/map-images-feature-manager';
import { MaterialFeatureManager } from '../feature-managers/material-feature-manager';
import { PatientFeatureManager } from '../feature-managers/patient-feature-manager';
import { PersonnelFeatureManager } from '../feature-managers/personnel-feature-manager';
import { SimulatedRegionFeatureManager } from '../feature-managers/simulated-region-feature-manager';
import { TransferLinesFeatureManager } from '../feature-managers/transfer-lines-feature-manager';
import { TransferPointFeatureManager } from '../feature-managers/transfer-point-feature-manager';
import { VehicleFeatureManager } from '../feature-managers/vehicle-feature-manager';
import {
isInViewport,
ViewportFeatureManager,
} from '../feature-managers/viewport-feature-manager';
import type { FeatureManager } from './feature-manager';
import type { PopupManager } from './popup-manager';
import { OlMapInteractionsManager } from './ol-map-interactions-manager';
import { SatelliteLayerManager } from './satellite-layer-manager';
import type { PopupService } from './popup.service';
export class OlMapManager {
private readonly _olMap: OlMap;
private featureManagers: FeatureManager<any>[];
private readonly mapInteractionsManager: OlMapInteractionsManager;
private static readonly defaultZoom = 20;
private readonly destroy$ = new Subject<void>();
/**
* key: the layer that is passed to the featureManager, that is saved in the value
* ```ts
* layerFeatureManagerDictionary.get(layer) === featureManager
* // means that:
* featureManager.layer === layer
* ```
*/
public readonly layerFeatureManagerDictionary = new Map<
VectorLayer<VectorSource>,
FeatureManager<any>
>();
public readonly featureNameFeatureManagerDictionary = new Map<
string,
FeatureManager<any>
>();
constructor(
private readonly store: Store<AppState>,
private readonly exerciseService: ExerciseService,
private readonly openLayersContainer: HTMLDivElement,
private readonly transferLinesService: TransferLinesService,
private readonly popupManager: PopupManager,
private readonly popupService: PopupService
) {
this._olMap = new OlMap({
interactions: new Collection<Interaction>(),
// We use Angular buttons instead
controls: [],
target: this.openLayersContainer,
// Note: The order of this array determines the order of the objects on the map.
// The most bottom objects must be at the top of the array.
layers: [],
overlays: [this.popupManager.popupOverlay],
view: new View({
center: [startingPosition.x, startingPosition.y],
zoom: OlMapManager.defaultZoom,
maxZoom: 23,
smoothExtentConstraint: false,
smoothResolutionConstraint: false,
constrainRotation: 1,
}),
});
this.featureManagers = [];
this.initializeFeatureManagers();
this.mapInteractionsManager = new OlMapInteractionsManager(
this.olMap.getInteractions(),
store,
popupManager,
this.olMap,
this.layerFeatureManagerDictionary,
this.destroy$
);
const satelliteLayerManager = new SatelliteLayerManager(
store,
this.destroy$
);
this.olMap.getLayers().clear();
this.olMap.addLayer(satelliteLayerManager.satelliteLayer);
// the mapInteractionsManager needs to be set and the satelliteLayer needs to be added before this is possible
this.registerFeatureManagers();
this.registerViewportRestriction();
popupManager.registerPopupTriggers(
this.olMap,
openLayersContainer,
this.layerFeatureManagerDictionary,
this.featureNameFeatureManagerDictionary
);
}
public get olMap(): OlMap {
return this._olMap;
}
private registerViewportRestriction() {
this.tryToFitViewForOverview(false);
this.store
.select(selectRestrictedViewport)
.pipe(takeUntil(this.destroy$))
.subscribe((viewport) => {
const view = this.olMap.getView();
view.set('extent', undefined);
view.setMinZoom(0);
if (!viewport) {
// We are no longer restricted to a viewport.
// Therefore, no new restrictions have to be set.
return;
}
const center = view.getCenter()!;
const previousZoom = view.getZoom()!;
const targetUpperLeftCorner = upperLeftCornerOf(viewport);
const targetLowerRightCorner = lowerRightCornerOf(viewport);
const targetExtent = [
targetUpperLeftCorner.x,
targetLowerRightCorner.y,
targetLowerRightCorner.x,
targetUpperLeftCorner.y,
];
view.fit(targetExtent);
const matchingZoom = view.getZoom()!;
if (isInViewport(center, viewport)) {
// We only want to change the zoom if necessary
view.setZoom(previousZoom);
view.setCenter(center);
}
view.set('extent', targetExtent);
const minZoom = Math.min(matchingZoom, view.getMaxZoom());
view.setMinZoom(minZoom);
});
}
/**
* Sets the map's view to see all viewports and simulated regions.
*/
public tryToFitViewForOverview(animate = true) {
if (
selectStateSnapshot(selectRestrictedViewport, this.store) !==
undefined
) {
// We are restricted to a viewport -> you can't fit the view
return;
}
const elements = [
...Object.values(selectStateSnapshot(selectViewports, this.store)),
...Object.values(
selectStateSnapshot(selectSimulatedRegions, this.store)
),
];
const view = this.olMap.getView();
if (elements.length === 0) {
view.setCenter([startingPosition.x, startingPosition.y]);
return;
}
const minX = Math.min(
...elements.map((element) => upperLeftCornerOf(element).x)
);
const minY = Math.min(
...elements.map((element) => lowerRightCornerOf(element).y)
);
const maxX = Math.max(
...elements.map((element) => lowerRightCornerOf(element).x)
);
const maxY = Math.max(
...elements.map((element) => upperLeftCornerOf(element).y)
);
const padding = 25;
view.fit([minX, minY, maxX, maxY], {
padding: [padding, padding, padding, padding],
duration: animate ? 1000 : undefined,
});
}
public getCoordinates() {
return this.olMap.getView().getCenter();
}
public tryGoToCoordinates(latitude: number, longitude: number) {
if (
selectStateSnapshot(selectCurrentRole, this.store) ===
'participant' &&
selectStateSnapshot(selectRestrictedViewport, this.store) !==
undefined
) {
// Participants or people restricted to viewports may not go to arbitrary coordinates
return;
}
const view = this.olMap.getView();
view.setCenter(fromLonLat([longitude, latitude]));
view.setZoom(OlMapManager.defaultZoom);
}
public changeZoom(mode: 'zoomIn' | 'zoomOut') {
const delta = mode === 'zoomIn' ? 1 : -1;
const view = this.olMap.getView();
view.animate({
zoom: (view.getZoom() ?? OlMapManager.defaultZoom) + delta,
duration: 200,
});
}
public destroy() {
this.destroy$.next();
this.olMap.dispose();
this.olMap.setTarget(undefined);
}
// This lets featureManagers register themselves and adds them to the layerFeatureManagerDictionary
private registerFeatureManagers() {
this.featureManagers.forEach((featureManager) => {
this.layerFeatureManagerDictionary.set(
featureManager.layer,
featureManager
);
featureManager.register(this.destroy$, this.mapInteractionsManager);
});
}
private initializeFeatureManagers() {
const transferLinesFeatureManager = new TransferLinesFeatureManager(
this.store,
this.transferLinesService,
this.olMap
);
const transferPointFeatureManager = new TransferPointFeatureManager(
this.olMap,
this.store,
this.exerciseService,
this.popupService
);
const patientFeatureManager = new PatientFeatureManager(
this.store,
this.olMap,
this.exerciseService,
this.popupService
);
const vehicleFeatureManager = new VehicleFeatureManager(
this.olMap,
this.store,
this.exerciseService,
this.popupService
);
const personnelFeatureManager = new PersonnelFeatureManager(
this.olMap,
this.store,
this.exerciseService,
this.popupService
);
const materialFeatureManager = new MaterialFeatureManager(
this.olMap,
this.store,
this.exerciseService,
this.popupService
);
const mapImageFeatureManager = new MapImageFeatureManager(
this.olMap,
this.exerciseService,
this.store,
this.popupService
);
const cateringLinesFeatureManager = new CateringLinesFeatureManager(
this.store,
this.olMap
);
const viewportFeatureManager = new ViewportFeatureManager(
this.olMap,
this.exerciseService,
this.store,
this.popupService
);
const simulatedRegionFeatureManager = new SimulatedRegionFeatureManager(
this.olMap,
this.exerciseService,
this.store,
this.popupService
);
const deleteFeatureManager = new DeleteFeatureManager(
this.store,
this.olMap,
this.exerciseService
);
// Register the Feature Managers in the correct Order
// The order represents the order of the layers on the map (last element is on top)
this.featureManagers = [
deleteFeatureManager,
transferLinesFeatureManager,
simulatedRegionFeatureManager,
mapImageFeatureManager,
transferPointFeatureManager,
vehicleFeatureManager,
cateringLinesFeatureManager,
patientFeatureManager,
personnelFeatureManager,
materialFeatureManager,
viewportFeatureManager,
];
this.featureNameFeatureManagerDictionary.set(
'vehicle',
vehicleFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'patient',
patientFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'personnel',
personnelFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'material',
materialFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'transferLine',
transferLinesFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'transferPoint',
transferPointFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'mapImage',
mapImageFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'cateringLine',
cateringLinesFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'viewport',
viewportFeatureManager
);
this.featureNameFeatureManagerDictionary.set(
'delete',
deleteFeatureManager
);
}
}