-
Notifications
You must be signed in to change notification settings - Fork 4
/
MapillaryModel.ts
377 lines (328 loc) · 12.3 KB
/
MapillaryModel.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
import Point from "@arcgis/core/geometry/Point";
import type { MapModel } from "@vertigis/web/mapping";
import type {
PropertyDefs,
ComponentModelProperties,
} from "@vertigis/web/models";
import {
ComponentModelBase,
serializable,
importModel,
} from "@vertigis/web/models";
import { throttle } from "@vertigis/web/ui";
import type { IViewer, ViewerImageEvent, LngLat } from "mapillary-js";
interface MapillaryModelProperties extends ComponentModelProperties {
mapillaryKey?: string;
searchRadius?: number;
defaultScale?: number;
startSynced?: boolean;
}
interface MapillaryCamera {
latitude: number;
longitude: number;
heading: number;
tilt: number;
fov: number;
}
/**
* Convert Mapillary bearing to a Scene's camera rotation.
* @param bearing Mapillary bearing in degrees (degrees relative to due north).
* @returns Scene camera rotation in degrees (degrees rotation of due north).
*/
function getCameraRotationFromBearing(bearing: number): number {
return 360 - bearing;
}
@serializable
export default class MapillaryModel extends ComponentModelBase<MapillaryModelProperties> {
mapillaryKey: string;
searchRadius: number;
defaultScale: number;
startSynced: boolean;
synchronizePosition: boolean;
readonly imageQueryUrl = "https://a.mapillary.com/v3/images";
// The latest location received from a locationmarker.update event
currentMarkerPosition: { latitude: number; longitude: number };
updating = false;
// The computed position of the current Mapillary image
private _currentImagePosition: LngLat;
private _awaitViewHandle: IHandle;
private _viewerUpdateHandle: IHandle;
private _handleMarkerUpdate = true;
private _synced = false;
/**
* Handles pov changes once the image position is known.
*/
private readonly _onPerspectiveChange = throttle(async () => {
if (!this.map || !this.mapillary || this.updating) {
return;
}
this.updating = true;
const { latitude, longitude, heading, tilt, fov } =
await this._getMapillaryCamera();
const centerPoint = new Point({
latitude,
longitude,
});
this._handleMarkerUpdate = false;
await Promise.all([
this.messages.commands.locationMarker.update.execute({
geometry: centerPoint,
heading,
tilt,
fov,
id: this.id,
maps: this.map,
}),
this.synchronizePosition
? this.messages.commands.map.zoomToViewpoint.execute({
maps: this.map,
viewpoint: {
rotation: getCameraRotationFromBearing(heading),
targetGeometry: centerPoint,
scale: this.defaultScale,
},
})
: undefined,
]).finally(() => (this.updating = false));
}, 128);
private _mapillary: IViewer | undefined;
get mapillary(): IViewer | undefined {
return this._mapillary;
}
set mapillary(instance: IViewer | undefined) {
if (instance === this._mapillary) {
return;
}
this._viewerUpdateHandle?.remove();
// If an instance already exists, clean it up first.
if (this._mapillary) {
// Clean up event handlers.
this.mapillary.off("image", this._onImageChange);
this.mapillary.off("pov", this._onPerspectiveChange);
// Activating the cover appears to be the best way to "clean up" Mapillary.
// https://github.com/mapillary/mapillary-js/blob/8b6fc2f36e3011218954d95d601062ff6aa41ad9/src/viewer/ComponentController.ts#L184-L192
this.mapillary.activateCover();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._unsyncMaps();
}
this._mapillary = instance;
// A new instance is being set - add the event handlers.
if (instance) {
// Listen for changes to the currently displayed mapillary image
this.mapillary.on("image", this._onImageChange);
// Change the current mapillary image when the location marker is moved.
this._viewerUpdateHandle =
this.messages.events.locationMarker.updated.subscribe((event) =>
this._handleViewerUpdate(event)
);
}
// We may need to sync if the map and initialized view have arrived first.
if (!this._synced && this.map.view) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._syncMaps();
}
}
private _map: MapModel | undefined;
get map(): MapModel | undefined {
return this._map;
}
@importModel("map-extension")
set map(instance: MapModel | undefined) {
if (instance === this._map) {
return;
}
// If an instance already exists, clean it up first.
if (this._map) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._unsyncMaps();
}
this._map = instance;
// We may need to wait for the view to arrive before proceeding.
this._awaitViewHandle = this.watch("map.view", (view) => {
if (view) {
this._awaitViewHandle.remove();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._syncMaps();
}
});
}
async recenter(): Promise<void> {
const { latitude, longitude, heading } =
await this._getMapillaryCamera();
const centerPoint = new Point({
latitude,
longitude,
});
await this.messages.commands.map.zoomToViewpoint.execute({
maps: this.map,
viewpoint: {
rotation: getCameraRotationFromBearing(heading),
targetGeometry: centerPoint,
scale: this.defaultScale,
},
});
}
// TODO: Bring back when CORS issue is resolved.
// https://forum.mapillary.com/t/web-app-blocked-by-cors-policy-mapillary/5357
// https://forum.mapillary.com/t/cors-error-when-requesting-coverage-vector-tiles/5303
// async moveCloseToPosition(latitude: number, longitude: number):
// Promise<void> {try {const url =
// `https://tiles.mapillary.com/maps/vtp/mly1_public/2/17/${latitude}/${longitude}?access_token=${this.mapillaryKey}`;
// const response = await fetch(url); const data = await response.json();
// const imgKey = data?.features?.[0]?.properties?.key;
// if (imgKey) {
// await this.mapillary.moveTo(imgKey);
// this.updating = false;
// } else {
// this.updating = false;
// this._activateCover();
// }
// } catch {
// this.updating = false;
// this._activateCover();
// }
protected override async _onDestroy(): Promise<void> {
await super._onDestroy();
this._viewerUpdateHandle?.remove();
this._awaitViewHandle?.remove();
}
protected override _getSerializableProperties(): PropertyDefs<MapillaryModelProperties> {
const props = super._getSerializableProperties();
return {
...props,
mapillaryKey: {
serializeModes: ["initial"],
default: "",
},
searchRadius: {
serializeModes: ["initial"],
default: 500,
},
defaultScale: {
serializeModes: ["initial"],
default: 3000,
},
startSynced: {
serializeModes: ["initial"],
default: true,
},
title: {
...this._toPropertyDef(props.title),
default: "language-web-incubator-mapillary-title",
},
icon: {
...this._toPropertyDef(props.icon),
default: "map-3rd-party",
},
};
}
/**
* Setup the initial state of the maps such as the location marker and map
* position.
*/
private async _syncMaps(): Promise<void> {
if (!this.map || !this.mapillary || this._synced) {
return;
}
this._synced = true;
this.synchronizePosition = this.startSynced ?? true;
// Set mapillary as close as possible to the center of the view
// await this.moveCloseToPosition(
// this.map.view.center.latitude,
// this.map.view.center.longitude
// );
// Create location marker based on current location from Mapillary and
// pan/zoom VertiGIS Studio map to the location.
const { latitude, longitude, heading, tilt, fov } =
await this._getMapillaryCamera();
const centerPoint = new Point({ latitude, longitude });
await Promise.all([
this.messages.commands.locationMarker.create.execute({
fov,
geometry: centerPoint,
heading,
tilt,
id: this.id,
maps: this.map,
// When the CORS issue above is resolved change this to `true`
userDraggable: false,
}),
this.synchronizePosition
? this.messages.commands.map.zoomToViewpoint.execute({
maps: this.map,
viewpoint: {
rotation: getCameraRotationFromBearing(heading),
targetGeometry: centerPoint,
scale: this.defaultScale,
},
})
: undefined,
]);
}
private async _unsyncMaps(): Promise<void> {
this._synced = false;
await this.messages.commands.locationMarker.remove.execute({
id: this.id,
maps: this.map,
});
}
private _handleViewerUpdate(event: any): void {
if (this._handleMarkerUpdate) {
const updatePoint = event.geometry as Point;
this.currentMarkerPosition = {
latitude: updatePoint.latitude,
longitude: updatePoint.longitude,
};
}
this._handleMarkerUpdate = true;
}
/**
* When the 'merged' property is set on the image we know that the position
* reported will be the computed location rather than a raw GPS value. We
* ignore all updates sent while the computed position is unknown as the raw
* GPS value can be inaccurate and will not exactly match the observed
* position of the camera. See:
* https://bl.ocks.org/oscarlorentzon/16946cb9eedfad2a64669cb1121e6c75
*/
private readonly _onImageChange = (event: ViewerImageEvent) => {
const { image } = event;
if (image.merged) {
this._currentImagePosition = image.lngLat;
// Set the initial marker position for this image.
this._onPerspectiveChange();
// Handle further pov changes.
this.mapillary.on("pov", this._onPerspectiveChange);
} else {
this._currentImagePosition = undefined;
this.mapillary.off("pov", this._onPerspectiveChange);
}
};
/**
* Gets the current POV of the mapillary camera
*/
private async _getMapillaryCamera(): Promise<MapillaryCamera | undefined> {
if (!this.mapillary) {
return undefined;
}
// Will return a raw GPS value if the image position has not yet been calculated.
const [{ lat, lng }, { bearing, tilt }, fov] = await Promise.all([
this._currentImagePosition ?? this.mapillary.getPosition(),
this.mapillary.getPointOfView() as Promise<{
bearing: number;
tilt: number;
}>,
this.mapillary.getFieldOfView(),
]);
return {
latitude: lat,
longitude: lng,
heading: bearing,
tilt: tilt + 90,
fov,
};
}
private _activateCover() {
this.updating = false;
this.mapillary.activateCover();
}
}