mobility-toolbox-js 2.0.0-beta.30 → 2.0.0-beta.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/api/RealtimeAPI.d.ts +268 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +626 -343
  4. package/api/RoutingAPI.d.ts +33 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +65 -15
  7. package/api/StopsAPI.d.ts +36 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +70 -12
  10. package/api/index.d.ts +5 -0
  11. package/api/index.d.ts.map +1 -0
  12. package/api/index.js +10 -3
  13. package/api/typedefs.d.ts +105 -0
  14. package/api/typedefs.d.ts.map +1 -0
  15. package/api/typedefs.js +72 -0
  16. package/common/api/HttpAPI.d.ts +31 -0
  17. package/common/api/HttpAPI.d.ts.map +1 -0
  18. package/common/api/HttpAPI.js +82 -30
  19. package/common/api/WebSocketAPI.d.ts +95 -0
  20. package/common/api/WebSocketAPI.d.ts.map +1 -0
  21. package/common/api/WebSocketAPI.js +313 -168
  22. package/common/controls/Control.d.ts +74 -0
  23. package/common/controls/Control.d.ts.map +1 -0
  24. package/common/controls/Control.js +167 -78
  25. package/common/index.d.ts +3 -0
  26. package/common/index.d.ts.map +1 -0
  27. package/common/index.js +18 -2
  28. package/common/layers/Layer.d.ts +80 -0
  29. package/common/layers/Layer.d.ts.map +1 -0
  30. package/common/layers/Layer.js +251 -134
  31. package/common/mixins/CopyrightMixin.d.ts +22 -0
  32. package/common/mixins/CopyrightMixin.d.ts.map +1 -0
  33. package/common/mixins/CopyrightMixin.js +70 -22
  34. package/common/mixins/MapboxLayerMixin.d.ts +27 -0
  35. package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
  36. package/common/mixins/MapboxLayerMixin.js +240 -0
  37. package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
  38. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  39. package/common/mixins/RealtimeLayerMixin.js +698 -429
  40. package/common/mixins/StopFinderMixin.d.ts +40 -0
  41. package/common/mixins/StopFinderMixin.d.ts.map +1 -0
  42. package/common/mixins/StopFinderMixin.js +195 -107
  43. package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
  44. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  45. package/common/mixins/UserInteractionsLayerMixin.js +222 -121
  46. package/common/styles/index.d.ts +5 -0
  47. package/common/styles/index.d.ts.map +1 -0
  48. package/common/styles/index.js +24 -4
  49. package/common/styles/realtimeDefaultStyle.d.ts +15 -0
  50. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  51. package/common/styles/realtimeDefaultStyle.js +236 -190
  52. package/common/styles/realtimeDelayStyle.d.ts +11 -0
  53. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  54. package/common/styles/realtimeDelayStyle.js +25 -7
  55. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  56. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  57. package/common/styles/realtimeSimpleStyle.js +23 -17
  58. package/common/typedefs.d.ts +50 -0
  59. package/common/typedefs.d.ts.map +1 -0
  60. package/common/typedefs.js +21 -0
  61. package/common/utils/cleanStopTime.d.ts +7 -0
  62. package/common/utils/cleanStopTime.d.ts.map +1 -0
  63. package/common/utils/cleanStopTime.js +28 -17
  64. package/common/utils/compareDepartures.d.ts +9 -0
  65. package/common/utils/compareDepartures.d.ts.map +1 -0
  66. package/common/utils/compareDepartures.js +34 -22
  67. package/common/utils/createCanvas.d.ts +9 -0
  68. package/common/utils/createCanvas.d.ts.map +1 -0
  69. package/common/utils/createCanvas.js +28 -16
  70. package/common/utils/createTrackerFilters.d.ts +12 -0
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -0
  72. package/common/utils/createTrackerFilters.js +75 -54
  73. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  74. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  75. package/common/utils/getLayersAsFlatArray.js +15 -13
  76. package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
  77. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  78. package/common/utils/getMapboxMapCopyrights.js +24 -17
  79. package/common/utils/getMapboxRender.d.ts +5 -0
  80. package/common/utils/getMapboxRender.d.ts.map +1 -0
  81. package/common/utils/getMapboxRender.js +77 -0
  82. package/common/utils/getMaplibreRender.d.ts +5 -0
  83. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  84. package/common/utils/getMaplibreRender.js +38 -0
  85. package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
  86. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  87. package/common/utils/getRealtimeModeSuffix.js +11 -2
  88. package/common/utils/getUrlWithParams.d.ts +9 -0
  89. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  90. package/common/utils/getUrlWithParams.js +20 -10
  91. package/common/utils/getVehiclePosition.d.ts +14 -0
  92. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  93. package/common/utils/getVehiclePosition.js +63 -39
  94. package/common/utils/index.d.ts +12 -0
  95. package/common/utils/index.d.ts.map +1 -0
  96. package/common/utils/index.js +37 -9
  97. package/common/utils/removeDuplicate.d.ts +10 -0
  98. package/common/utils/removeDuplicate.d.ts.map +1 -0
  99. package/common/utils/removeDuplicate.js +29 -7
  100. package/common/utils/renderTrajectories.d.ts +20 -0
  101. package/common/utils/renderTrajectories.d.ts.map +1 -0
  102. package/common/utils/renderTrajectories.js +111 -78
  103. package/common/utils/sortByDelay.d.ts +3 -0
  104. package/common/utils/sortByDelay.d.ts.map +1 -0
  105. package/common/utils/sortByDelay.js +21 -17
  106. package/common/utils/timeUtils.d.ts +5 -0
  107. package/common/utils/timeUtils.d.ts.map +1 -0
  108. package/common/utils/timeUtils.js +47 -18
  109. package/common/utils/trackerConfig.d.ts +24 -0
  110. package/common/utils/trackerConfig.d.ts.map +1 -0
  111. package/common/utils/trackerConfig.js +171 -118
  112. package/iife.d.ts +3 -0
  113. package/iife.d.ts.map +1 -0
  114. package/iife.js +7 -0
  115. package/index.d.ts +6 -0
  116. package/index.d.ts.map +1 -0
  117. package/index.js +10 -7
  118. package/mapbox/controls/CopyrightControl.d.ts +32 -0
  119. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  120. package/mapbox/controls/CopyrightControl.js +72 -28
  121. package/mapbox/controls/index.d.ts +2 -0
  122. package/mapbox/controls/index.d.ts.map +1 -0
  123. package/mapbox/controls/index.js +6 -1
  124. package/mapbox/index.d.ts +5 -0
  125. package/mapbox/index.d.ts.map +1 -0
  126. package/mapbox/index.js +20 -4
  127. package/mapbox/layers/Layer.d.ts +47 -0
  128. package/mapbox/layers/Layer.d.ts.map +1 -0
  129. package/mapbox/layers/Layer.js +137 -54
  130. package/mapbox/layers/RealtimeLayer.d.ts +118 -0
  131. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  132. package/mapbox/layers/RealtimeLayer.js +310 -183
  133. package/mapbox/layers/index.d.ts +3 -0
  134. package/mapbox/layers/index.d.ts.map +1 -0
  135. package/mapbox/layers/index.js +7 -2
  136. package/mapbox/utils.d.ts +8 -0
  137. package/mapbox/utils.d.ts.map +1 -0
  138. package/mapbox/utils.js +54 -29
  139. package/mbt.js +2052 -2109
  140. package/mbt.js.map +3 -3
  141. package/mbt.min.js +18 -18
  142. package/mbt.min.js.map +3 -3
  143. package/ol/controls/CopyrightControl.d.ts +31 -0
  144. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  145. package/ol/controls/CopyrightControl.js +89 -41
  146. package/ol/controls/RoutingControl.d.ts +180 -0
  147. package/ol/controls/RoutingControl.d.ts.map +1 -0
  148. package/ol/controls/RoutingControl.js +666 -371
  149. package/ol/controls/StopFinderControl.d.ts +32 -0
  150. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  151. package/ol/controls/StopFinderControl.js +59 -10
  152. package/ol/controls/index.d.ts +4 -0
  153. package/ol/controls/index.d.ts.map +1 -0
  154. package/ol/controls/index.js +9 -3
  155. package/ol/index.d.ts +6 -0
  156. package/ol/index.d.ts.map +1 -0
  157. package/ol/index.js +21 -5
  158. package/ol/layers/Layer.d.ts +49 -0
  159. package/ol/layers/Layer.d.ts.map +1 -0
  160. package/ol/layers/Layer.js +178 -81
  161. package/ol/layers/MapboxLayer.d.ts +42 -0
  162. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  163. package/ol/layers/MapboxLayer.js +131 -198
  164. package/ol/layers/MapboxStyleLayer.d.ts +146 -0
  165. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  166. package/ol/layers/MapboxStyleLayer.js +382 -186
  167. package/ol/layers/MaplibreLayer.d.ts +27 -0
  168. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  169. package/ol/layers/MaplibreLayer.js +67 -138
  170. package/ol/layers/RealtimeLayer.d.ts +119 -0
  171. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  172. package/ol/layers/RealtimeLayer.js +327 -184
  173. package/ol/layers/RoutingLayer.d.ts +24 -0
  174. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  175. package/ol/layers/RoutingLayer.js +111 -56
  176. package/ol/layers/VectorLayer.d.ts +23 -0
  177. package/ol/layers/VectorLayer.d.ts.map +1 -0
  178. package/ol/layers/VectorLayer.js +71 -21
  179. package/ol/layers/WMSLayer.d.ts +38 -0
  180. package/ol/layers/WMSLayer.d.ts.map +1 -0
  181. package/ol/layers/WMSLayer.js +105 -37
  182. package/ol/layers/index.d.ts +9 -0
  183. package/ol/layers/index.d.ts.map +1 -0
  184. package/ol/layers/index.js +19 -8
  185. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  186. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  187. package/ol/styles/fullTrajectoryDelayStyle.js +32 -28
  188. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  189. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  190. package/ol/styles/fullTrajectoryStyle.js +44 -40
  191. package/ol/styles/index.d.ts +3 -0
  192. package/ol/styles/index.d.ts.map +1 -0
  193. package/ol/styles/index.js +7 -2
  194. package/package.json +14 -36
  195. package/setupTests.d.ts +2 -0
  196. package/setupTests.d.ts.map +1 -0
  197. package/setupTests.js +15 -0
  198. package/types/index.d.ts +7 -6
  199. package/types/routing.d.ts +25 -25
  200. package/types/stops.d.ts +19 -19
  201. package/api/RealtimeAPI.test.js +0 -67
  202. package/api/RoutingAPI.test.js +0 -25
  203. package/api/StopsAPI.test.js +0 -22
  204. package/common/api/HttpAPI.test.js +0 -50
  205. package/common/api/WebSocketAPI.test.js +0 -311
  206. package/common/controls/Control.test.js +0 -87
  207. package/common/layers/Layer.test.js +0 -134
  208. package/common/mixins/UserInteractionsLayerMixin.test.js +0 -199
  209. package/common/utils/createTrackerFilters.test.js +0 -79
  210. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  211. package/common/utils/removeDuplicate.test.js +0 -19
  212. package/common/utils/timeUtils.test.js +0 -10
  213. package/common/utils/trackerConfig.test.js +0 -23
  214. package/mapbox/layers/Layer.test.js +0 -182
  215. package/mapbox/layers/RealtimeLayer.test.js +0 -10
  216. package/ol/controls/CopyrightControl.test.js +0 -165
  217. package/ol/controls/RoutingControl.test.js +0 -146
  218. package/ol/controls/StopFinderControl.test.js +0 -48
  219. package/ol/layers/Layer.test.js +0 -175
  220. package/ol/layers/MapboxLayer.test.js +0 -160
  221. package/ol/layers/MapboxStyleLayer.test.js +0 -226
  222. package/ol/layers/RealtimeLayer.test.js +0 -67
  223. package/ol/layers/RoutingLayer.test.js +0 -39
  224. package/ol/layers/VectorLayer.test.js +0 -76
  225. package/ol/layers/WMSLayer.test.js +0 -57
@@ -1,436 +1,705 @@
1
- import { buffer, containsCoordinate, intersects } from "ol/extent";
2
- import { unByKey } from "ol/Observable";
3
- import GeoJSON from "ol/format/GeoJSON";
4
- import debounce from "lodash.debounce";
5
- import throttle from "lodash.throttle";
6
- import { fromLonLat } from "ol/proj";
7
- import realtimeDefaultStyle from "../styles/realtimeDefaultStyle";
8
- import { RealtimeAPI, RealtimeModes } from "../../api";
9
- import renderTrajectories from "../utils/renderTrajectories";
10
- import * as trackerConfig from "../utils/trackerConfig";
11
- export class RealtimeLayerInterface {
12
- constructor(options = {}) {
13
- }
14
- attachToMap(map) {
15
- }
16
- detachFromMap() {
17
- }
18
- start() {
19
- }
20
- stop() {
21
- }
22
- setBbox(extent, zoom) {
23
- }
24
- setMode(mode) {
25
- }
26
- getTrajectoryInfos(id, mode) {
27
- }
28
- }
29
- const RealtimeLayerMixin = (Base) => class extends Base {
30
- constructor(options = {}) {
31
- super({
32
- hitTolerance: 10,
33
- ...options
34
- });
35
- this.debug = options.debug;
36
- this.mode = options.mode || RealtimeModes.TOPOGRAPHIC;
37
- this.api = options.api || new RealtimeAPI(options);
38
- this.tenant = options.tenant || "";
39
- this.minZoomNonTrain = options.minZoomNonTrain || 9;
40
- this.minZoomInterpolation = options.minZoomInterpolation || 8;
41
- this.format = new GeoJSON();
42
- this.generalizationLevelByZoom = options.generalizationLevelByZoom || [
43
- 5,
44
- 5,
45
- 5,
46
- 5,
47
- 5,
48
- 5,
49
- 5,
50
- 5,
51
- 10,
52
- 30,
53
- 30,
54
- 100,
55
- 100,
56
- 100
57
- ];
58
- this.getGeneralizationLevelByZoom = (zoom) => {
59
- return options.getGeneralizationLevelByZoom && options.getGeneralizationLevelByZoom(zoom, this.generalizationLevelByZoom) || this.generalizationLevelByZoom[zoom];
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
60
8
  };
61
- this.renderTimeIntervalByZoom = options.renderTimeIntervalByZoom || [
62
- 1e5,
63
- 5e4,
64
- 4e4,
65
- 3e4,
66
- 2e4,
67
- 15e3,
68
- 1e4,
69
- 5e3,
70
- 2e3,
71
- 1e3,
72
- 400,
73
- 300,
74
- 250,
75
- 180,
76
- 90,
77
- 60,
78
- 50,
79
- 50,
80
- 50,
81
- 50,
82
- 50
83
- ];
84
- this.getRenderTimeIntervalByZoom = (zoom) => {
85
- return options.getRenderTimeIntervalByZoom && options.getRenderTimeIntervalByZoom(zoom, this.renderTimeIntervalByZoom) || this.renderTimeIntervalByZoom[zoom];
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
86
15
  };
87
- this.isUpdateBboxOnMoveEnd = options.isUpdateBboxOnMoveEnd !== false;
88
- this.throttleRenderTrajectories = throttle(this.renderTrajectoriesInternal, 50, { leading: false, trailing: true });
89
- this.debounceRenderTrajectories = debounce(this.renderTrajectoriesInternal, 50, { leading: true, trailing: true, maxWait: 5e3 });
90
- this.onFeatureHover = this.onFeatureHover.bind(this);
91
- this.onFeatureClick = this.onFeatureClick.bind(this);
92
- this.renderTrajectoriesInternal = this.renderTrajectoriesInternal.bind(this);
93
- this.onTrajectoryMessage = this.onTrajectoryMessage.bind(this);
94
- this.onDeleteTrajectoryMessage = this.onDeleteTrajectoryMessage.bind(this);
95
- this.onDocumentVisibilityChange = this.onDocumentVisibilityChange.bind(this);
96
- }
97
- defineProperties(options) {
98
- const {
99
- style,
100
- speed,
101
- pixelRatio,
102
- hoverVehicleId,
103
- selectedVehicleId,
104
- filter,
105
- sort,
106
- time,
107
- live,
108
- canvas,
109
- styleOptions
110
- } = options;
111
- let currSpeed = speed || 1;
112
- let currTime = time || new Date();
113
- super.defineProperties(options);
114
- Object.defineProperties(this, {
115
- isTrackerLayer: { value: true },
116
- canvas: {
117
- value: canvas || document.createElement("canvas")
118
- },
119
- style: {
120
- value: style || realtimeDefaultStyle
121
- },
122
- styleOptions: {
123
- value: { ...trackerConfig, ...styleOptions || {} }
124
- },
125
- speed: {
126
- get: () => currSpeed,
127
- set: (newSpeed) => {
128
- currSpeed = newSpeed;
129
- this.start();
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
130
23
  }
131
- },
132
- filter: {
133
- value: filter,
134
- writable: true
135
- },
136
- sort: {
137
- value: sort,
138
- writable: true
139
- },
140
- live: {
141
- value: live === false ? live : true,
142
- writable: true
143
- },
144
- time: {
145
- get: () => currTime,
146
- set: (newTime) => {
147
- currTime = newTime && newTime.getTime ? newTime : new Date(newTime);
148
- this.renderTrajectories();
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
149
33
  }
150
- },
151
- trajectories: {
152
- value: {},
153
- writable: true
154
- },
155
- hoverVehicleId: {
156
- value: hoverVehicleId,
157
- writable: true
158
- },
159
- selectedVehicleId: {
160
- value: selectedVehicleId,
161
- writable: true
162
- },
163
- pixelRatio: {
164
- value: pixelRatio || (typeof window !== "undefined" ? window.devicePixelRatio : 1),
165
- writable: true
166
- },
167
- useRequestAnimationFrame: {
168
- value: options.useRequestAnimationFrame || false,
169
- writable: true
170
- },
171
- useThrottle: {
172
- value: options.useThrottle !== false,
173
- writable: true
174
- },
175
- useDebounce: {
176
- value: options.useDebounce || false,
177
- writable: true
178
- }
179
- });
180
- }
181
- attachToMap(map) {
182
- super.attachToMap(map);
183
- if (this.visible) {
184
- this.start();
185
- }
186
- this.visibilityRef = this.on("change:visible", (evt) => {
187
- if (evt.target.visible) {
188
- this.start();
189
- } else {
190
- this.stop();
191
- }
192
- });
193
- document.addEventListener("visibilitychange", this.onDocumentVisibilityChange);
194
- }
195
- detachFromMap() {
196
- document.removeEventListener("visibilitychange", this.onDocumentVisibilityChange);
197
- this.stop();
198
- unByKey(this.visibilityRef);
199
- const context = this.canvas.getContext("2d");
200
- context.clearRect(0, 0, this.canvas.width, this.canvas.height);
201
- super.detachFromMap();
202
- }
203
- start() {
204
- this.stop();
205
- this.renderTrajectories();
206
- this.startUpdateTime();
207
- this.api.open();
208
- this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
209
- this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
210
- if (this.isUpdateBboxOnMoveEnd) {
211
- this.setBbox();
212
- }
213
- }
214
- startUpdateTime() {
215
- this.stopUpdateTime();
216
- this.updateTimeDelay = this.getRefreshTimeInMs();
217
- this.updateTimeInterval = setInterval(() => {
218
- this.time = this.live ? new Date() : this.time.getTime() + this.updateTimeDelay * this.speed;
219
- }, this.updateTimeDelay);
220
- }
221
- stop() {
222
- this.api.unsubscribeTrajectory(this.onTrajectoryMessage);
223
- this.api.unsubscribeDeletedVehicles(this.onDeleteTrajectoryMessage);
224
- this.api.close();
225
- }
226
- stopUpdateTime() {
227
- if (this.updateTimeInterval) {
228
- clearInterval(this.updateTimeInterval);
229
- }
230
- }
231
- renderTrajectoriesInternal(viewState, noInterpolate) {
232
- if (!this.map) {
233
- return false;
234
- }
235
- const time = this.live ? Date.now() : this.time;
236
- const trajectories = Object.values(this.trajectories);
237
- if (this.sort) {
238
- trajectories.sort(this.sort);
239
- }
240
- this.renderState = renderTrajectories(this.canvas, trajectories, this.style, { ...viewState, pixelRatio: this.pixelRatio, time }, {
241
- noInterpolate: viewState.zoom < this.minZoomInterpolation ? true : noInterpolate,
242
- hoverVehicleId: this.hoverVehicleId,
243
- selectedVehicleId: this.selectedVehicleId,
244
- ...this.styleOptions
245
- });
246
- return true;
247
- }
248
- renderTrajectories(viewState, noInterpolate) {
249
- if (this.requestId) {
250
- cancelAnimationFrame(this.requestId);
251
- this.requestId = null;
252
- }
253
- if (!noInterpolate && this.useRequestAnimationFrame) {
254
- this.requestId = requestAnimationFrame(() => {
255
- this.renderTrajectoriesInternal(viewState, noInterpolate);
256
- });
257
- } else if (!noInterpolate && this.useDebounce) {
258
- this.debounceRenderTrajectories(viewState, noInterpolate);
259
- } else if (!noInterpolate && this.useThrottle) {
260
- this.throttleRenderTrajectories(viewState, noInterpolate);
261
- } else {
262
- this.renderTrajectoriesInternal(viewState, noInterpolate);
263
- }
264
- }
265
- setBbox(extent, zoom) {
266
- const keys = Object.keys(this.trajectories);
267
- for (let i = keys.length - 1; i >= 0; i -= 1) {
268
- this.purgeTrajectory(this.trajectories[keys[i]], extent, zoom);
269
- }
270
- const bbox = [...extent];
271
- if (this.isUpdateBboxOnMoveEnd) {
272
- bbox.push(zoom);
273
- if (this.tenant) {
274
- bbox.push(`tenant=${this.tenant}`);
275
- }
276
- this.generalizationLevel = this.getGeneralizationLevelByZoom(zoom);
277
- if (this.generalizationLevel) {
278
- bbox.push(`gen=${this.generalizationLevel}`);
279
- }
280
- }
281
- this.api.bbox = bbox;
282
- }
283
- setMode(mode) {
284
- if (this.mode === mode) {
285
- return;
286
- }
287
- this.mode = mode;
288
- this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
289
- this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
290
- }
291
- getRefreshTimeInMs(zoom) {
292
- const roundedZoom = Math.round(zoom);
293
- const timeStep = this.getRenderTimeIntervalByZoom(roundedZoom) || 25;
294
- const nextTick = Math.max(25, timeStep / this.speed);
295
- const nextThrottleTick = Math.min(nextTick, 500);
296
- if (this.useThrottle) {
297
- this.throttleRenderTrajectories = throttle(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true });
298
- } else if (this.useDebounce) {
299
- this.debounceRenderTrajectories = debounce(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true, maxWait: 5e3 });
300
- }
301
- if (this.api?.buffer) {
302
- const [, size] = this.api.buffer;
303
- this.api.buffer = [nextThrottleTick, size];
304
- }
305
- return nextTick;
306
- }
307
- getVehicle(filterFc) {
308
- return Object.values(this.trajectories).filter(filterFc);
309
- }
310
- getFeatureInfoAtCoordinate(coordinate, options = {}) {
311
- const { resolution, nb } = options;
312
- const ext = buffer([...coordinate, ...coordinate], this.hitTolerance * resolution);
313
- let trajectories = Object.values(this.trajectories);
314
- if (this.sort) {
315
- trajectories = trajectories.sort(this.sort);
316
- }
317
- const vehicles = [];
318
- for (let i = 0; i < trajectories.length; i += 1) {
319
- if (trajectories[i].properties.coordinate && containsCoordinate(ext, trajectories[i].properties.coordinate)) {
320
- vehicles.push(trajectories[i]);
321
- }
322
- if (vehicles.length === nb) {
323
- break;
324
- }
325
34
  }
326
- return Promise.resolve({
327
- layer: this,
328
- features: vehicles.map((vehicle) => this.format.readFeature(vehicle)),
329
- coordinate
330
- });
331
- }
332
- getTrajectoryInfos(id) {
333
- const promises = [
334
- this.api.getStopSequence(id, this.mode),
335
- this.api.getFullTrajectory(id, this.mode, this.generalizationLevel)
336
- ];
337
- return Promise.all(promises).then(([stopSequence, fullTrajectory]) => {
338
- const response = {
339
- stopSequence,
340
- fullTrajectory
341
- };
342
- return response;
343
- });
344
- }
345
- purgeTrajectory(trajectory, extent, zoom) {
346
- const { type, bounds, train_id: id } = trajectory.properties;
347
- if (!intersects(extent, bounds) || type !== "rail" && zoom < (this.minZoomNonTrain || 9)) {
348
- this.removeTrajectory(id);
349
- return true;
350
- }
351
- return false;
352
- }
353
- addTrajectory(trajectory) {
354
- if (this.filter && !this.filter(trajectory)) {
355
- return;
356
- }
357
- this.trajectories[trajectory.properties.train_id] = trajectory;
358
- this.renderTrajectories();
359
- }
360
- removeTrajectory(id) {
361
- delete this.trajectories[id];
362
- }
363
- onZoomEnd(evt) {
364
- this.startUpdateTime();
365
- }
366
- onDocumentVisibilityChange() {
367
- if (!this.visible) {
368
- return;
369
- }
370
- if (document.hidden) {
371
- this.stop();
372
- } else {
373
- this.start();
374
- }
375
- }
376
- onTrajectoryMessage(data) {
377
- if (!data.content) {
378
- return;
379
- }
380
- const trajectory = data.content;
381
- const {
382
- geometry,
383
- properties: {
384
- train_id: id,
385
- time_since_update: timeSinceUpdate,
386
- raw_coordinates: rawCoordinates
387
- }
388
- } = trajectory;
389
- if (timeSinceUpdate < 0) {
390
- return;
391
- }
392
- if (this.purgeTrajectory(trajectory)) {
393
- return;
394
- }
395
- if (this.debug && this.mode === RealtimeModes.TOPOGRAPHIC && rawCoordinates) {
396
- trajectory.properties.olGeometry = {
397
- type: "Point",
398
- coordinates: fromLonLat(rawCoordinates, this.map.getView().getProjection())
399
- };
400
- } else {
401
- trajectory.properties.olGeometry = this.format.readGeometry(geometry);
402
- }
403
- trajectory.properties.timeOffset = Date.now() - data.timestamp;
404
- this.addTrajectory(trajectory);
405
- }
406
- onDeleteTrajectoryMessage(data) {
407
- if (!data.content) {
408
- return;
409
- }
410
- this.removeTrajectory(data.content);
411
- }
412
- onFeatureHover(features, layer, coordinate) {
413
- const [feature] = features;
414
- let id = null;
415
- if (feature) {
416
- id = feature.get("train_id");
417
- }
418
- if (this.hoverVehicleId !== id) {
419
- this.hoverVehicleId = id;
420
- this.renderTrajectories(true);
421
- }
422
- }
423
- onFeatureClick(features, layer, coordinate) {
424
- const [feature] = features;
425
- let id = null;
426
- if (feature) {
427
- id = feature.get("train_id");
428
- }
429
- if (this.selectedVehicleId !== id) {
430
- this.selectedVehicleId = id;
431
- this.selectedVehicle = feature;
432
- this.renderTrajectories(true);
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.RealtimeLayerInterface = void 0;
39
+ /* eslint-disable no-empty-function,@typescript-eslint/no-empty-function */
40
+ /* eslint-disable no-useless-constructor,@typescript-eslint/no-useless-constructor */
41
+ /* eslint-disable no-unused-vars,@typescript-eslint/no-unused-vars */
42
+ /* eslint-disable class-methods-use-this */
43
+ /* eslint-disable max-classes-per-file */
44
+ var extent_1 = require("ol/extent");
45
+ var Observable_1 = require("ol/Observable");
46
+ var GeoJSON_1 = require("ol/format/GeoJSON");
47
+ var lodash_debounce_1 = require("lodash.debounce");
48
+ var lodash_throttle_1 = require("lodash.throttle");
49
+ var proj_1 = require("ol/proj");
50
+ var realtimeDefaultStyle_1 = require("../styles/realtimeDefaultStyle");
51
+ var api_1 = require("../../api");
52
+ var renderTrajectories_1 = require("../utils/renderTrajectories");
53
+ var trackerConfig = require("../utils/trackerConfig");
54
+ /**
55
+ * RealtimeLayerInterface.
56
+ */
57
+ var RealtimeLayerInterface = /** @class */ (function () {
58
+ /*
59
+ * Constructor
60
+
61
+ * @param {Object} options Layer options.
62
+ * @param {string} options.url Realtime service url.
63
+ * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
64
+ * @param {boolean} [options.debug=false] Display additional debug informations.
65
+ * @param {RealtimeMode} [options.mode=RealtimeMode.TOPOGRAPHIC] Realtime's Mode.
66
+ * @param {number} [options.minZoomInterpolation=8] Minimal zoom when trains positions start to be interpolated.
67
+ * @param {number} [options.minZoomNonTrain=9] Minimal zoom when non trains vehicles are allowed to be displayed.
68
+ */
69
+ function RealtimeLayerInterface(options) {
70
+ if (options === void 0) { options = {}; }
433
71
  }
434
- }
72
+ /**
73
+ * Initialize the layer subscribing to the Realtime api.
74
+ *
75
+ * @param {ol/Map~Map} map
76
+ */
77
+ RealtimeLayerInterface.prototype.attachToMap = function (map) { };
78
+ /**
79
+ * Terminate the layer unsubscribing to the Realtime api.
80
+ */
81
+ RealtimeLayerInterface.prototype.detachFromMap = function () { };
82
+ /**
83
+ * Start the clock.
84
+ */
85
+ RealtimeLayerInterface.prototype.start = function () { };
86
+ /**
87
+ * Stop the clock.
88
+ */
89
+ RealtimeLayerInterface.prototype.stop = function () { };
90
+ /**
91
+ * Set the Realtime api's bbox.
92
+ *
93
+ * @param {Array<number>} extent Extent to request, [minX, minY, maxX, maxY, zoom].
94
+ * @param {number} zoom Zoom level to request. Must be an integer.
95
+ */
96
+ RealtimeLayerInterface.prototype.setBbox = function (extent, zoom) { };
97
+ /**
98
+ * Set the Realtime api's mode.
99
+ *
100
+ * @param {RealtimeMode} mode Realtime mode
101
+ */
102
+ RealtimeLayerInterface.prototype.setMode = function (mode) { };
103
+ /**
104
+ * Request the stopSequence and the fullTrajectory informations for a vehicle.
105
+ *
106
+ * @param {string} id The vehicle identifier (the train_id property).
107
+ * @param {RealtimeMode} mode The mode to request. If not defined, the layer´s mode propetrty will be used.
108
+ * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
109
+ */
110
+ RealtimeLayerInterface.prototype.getTrajectoryInfos = function (id, mode) { };
111
+ return RealtimeLayerInterface;
112
+ }());
113
+ exports.RealtimeLayerInterface = RealtimeLayerInterface;
114
+ /**
115
+ * Mixin for RealtimeLayerInterface.
116
+ *
117
+ * @param {Class} Base A class to extend with {RealtimeLayerInterface} functionnalities.
118
+ * @return {Class} A class that implements {RealtimeLayerInterface} class and extends Base;
119
+ * @private
120
+ */
121
+ var RealtimeLayerMixin = function (Base) {
122
+ return /** @class */ (function (_super) {
123
+ __extends(class_1, _super);
124
+ function class_1(options) {
125
+ if (options === void 0) { options = {}; }
126
+ var _this = _super.call(this, __assign({ hitTolerance: 10 }, options)) || this;
127
+ _this.debug = options.debug;
128
+ _this.mode = options.mode || api_1.RealtimeModes.TOPOGRAPHIC;
129
+ _this.api = options.api || new api_1.RealtimeAPI(options);
130
+ _this.tenant = options.tenant || ''; // sbb,sbh or sbm
131
+ _this.minZoomNonTrain = options.minZoomNonTrain || 9; // Min zoom level from which non trains are allowed to be displayed. Min value is 9 (as configured by the server
132
+ _this.minZoomInterpolation = options.minZoomInterpolation || 8; // Min zoom level from which trains positions are not interpolated.
133
+ _this.format = new GeoJSON_1.default();
134
+ _this.generalizationLevelByZoom = options.generalizationLevelByZoom || [
135
+ 5, 5, 5, 5, 5, 5, 5, 5, 10, 30, 30, 100, 100, 100,
136
+ ];
137
+ _this.getGeneralizationLevelByZoom = function (zoom) {
138
+ return ((options.getGeneralizationLevelByZoom &&
139
+ options.getGeneralizationLevelByZoom(zoom, _this.generalizationLevelByZoom)) ||
140
+ _this.generalizationLevelByZoom[zoom]);
141
+ };
142
+ _this.renderTimeIntervalByZoom = options.renderTimeIntervalByZoom || [
143
+ 100000, 50000, 40000, 30000, 20000, 15000, 10000, 5000, 2000, 1000, 400,
144
+ 300, 250, 180, 90, 60, 50, 50, 50, 50, 50,
145
+ ];
146
+ _this.getRenderTimeIntervalByZoom = function (zoom) {
147
+ return ((options.getRenderTimeIntervalByZoom &&
148
+ options.getRenderTimeIntervalByZoom(zoom, _this.renderTimeIntervalByZoom)) ||
149
+ _this.renderTimeIntervalByZoom[zoom]);
150
+ };
151
+ // This property will call api.setBbox on each movend event
152
+ _this.isUpdateBboxOnMoveEnd = options.isUpdateBboxOnMoveEnd !== false;
153
+ // Define throttling and debounce render function
154
+ _this.throttleRenderTrajectories = (0, lodash_throttle_1.default)(_this.renderTrajectoriesInternal, 50, { leading: false, trailing: true });
155
+ _this.debounceRenderTrajectories = (0, lodash_debounce_1.default)(_this.renderTrajectoriesInternal, 50, { leading: true, trailing: true, maxWait: 5000 });
156
+ // Bind callbacks
157
+ _this.onFeatureHover = _this.onFeatureHover.bind(_this);
158
+ _this.onFeatureClick = _this.onFeatureClick.bind(_this);
159
+ _this.renderTrajectoriesInternal =
160
+ _this.renderTrajectoriesInternal.bind(_this);
161
+ _this.onTrajectoryMessage = _this.onTrajectoryMessage.bind(_this);
162
+ _this.onDeleteTrajectoryMessage =
163
+ _this.onDeleteTrajectoryMessage.bind(_this);
164
+ _this.onDocumentVisibilityChange =
165
+ _this.onDocumentVisibilityChange.bind(_this);
166
+ return _this;
167
+ }
168
+ /**
169
+ * Define layer's properties.
170
+ *
171
+ * @ignore
172
+ */
173
+ class_1.prototype.defineProperties = function (options) {
174
+ var _this = this;
175
+ var style = options.style, speed = options.speed, pixelRatio = options.pixelRatio, hoverVehicleId = options.hoverVehicleId, selectedVehicleId = options.selectedVehicleId, filter = options.filter, sort = options.sort, time = options.time, live = options.live, canvas = options.canvas, styleOptions = options.styleOptions;
176
+ var currSpeed = speed || 1;
177
+ var currTime = time || new Date();
178
+ _super.prototype.defineProperties.call(this, options);
179
+ Object.defineProperties(this, {
180
+ isTrackerLayer: { value: true },
181
+ canvas: {
182
+ value: canvas || document.createElement('canvas'),
183
+ },
184
+ /**
185
+ * Style function used to render a vehicle.
186
+ */
187
+ style: {
188
+ value: style || realtimeDefaultStyle_1.default,
189
+ },
190
+ /**
191
+ * Custom options to pass as last parameter of the style function.
192
+ */
193
+ styleOptions: {
194
+ value: __assign(__assign({}, trackerConfig), (styleOptions || {})),
195
+ },
196
+ /**
197
+ * Speed of the wheel of time.
198
+ * If live property is true. The speed is ignored.
199
+ */
200
+ speed: {
201
+ get: function () { return currSpeed; },
202
+ set: function (newSpeed) {
203
+ currSpeed = newSpeed;
204
+ _this.start();
205
+ },
206
+ },
207
+ /**
208
+ * Function to filter which vehicles to display.
209
+ */
210
+ filter: {
211
+ value: filter,
212
+ writable: true,
213
+ },
214
+ /**
215
+ * Function to sort the vehicles to display.
216
+ */
217
+ sort: {
218
+ value: sort,
219
+ writable: true,
220
+ },
221
+ /**
222
+ * If true. The layer will always use Date.now() on the next tick to render the trajectories.
223
+ * When true, setting the time property has no effect.
224
+ */
225
+ live: {
226
+ value: live === false ? live : true,
227
+ writable: true,
228
+ },
229
+ /**
230
+ * Time used to display the trajectories. Can be a Date or a number in ms representing a Date.
231
+ * If live property is true. The setter does nothing.
232
+ */
233
+ time: {
234
+ get: function () { return currTime; },
235
+ set: function (newTime) {
236
+ currTime = newTime && newTime.getTime ? newTime : new Date(newTime);
237
+ _this.renderTrajectories();
238
+ },
239
+ },
240
+ /**
241
+ * Keep track of which trajectories are stored.
242
+ */
243
+ trajectories: {
244
+ value: {},
245
+ writable: true,
246
+ },
247
+ /**
248
+ * Id of the hovered vehicle.
249
+ */
250
+ hoverVehicleId: {
251
+ value: hoverVehicleId,
252
+ writable: true,
253
+ },
254
+ /**
255
+ * Id of the selected vehicle.
256
+ */
257
+ selectedVehicleId: {
258
+ value: selectedVehicleId,
259
+ writable: true,
260
+ },
261
+ /**
262
+ * Id of the selected vehicle.
263
+ */
264
+ pixelRatio: {
265
+ value: pixelRatio ||
266
+ (typeof window !== 'undefined' ? window.devicePixelRatio : 1),
267
+ writable: true,
268
+ },
269
+ /**
270
+ * If true, encapsulates the renderTrajectories calls in a requestAnimationFrame.
271
+ */
272
+ useRequestAnimationFrame: {
273
+ value: options.useRequestAnimationFrame || false,
274
+ writable: true,
275
+ },
276
+ /**
277
+ * If true, encapsulates the renderTrajectories calls in a throttle function. Default to true.
278
+ */
279
+ useThrottle: {
280
+ value: options.useThrottle !== false,
281
+ writable: true,
282
+ },
283
+ /**
284
+ * If true, encapsulates the renderTrajectories calls in a debounce function.
285
+ */
286
+ useDebounce: {
287
+ value: options.useDebounce || false,
288
+ writable: true,
289
+ },
290
+ /**
291
+ * Debug properties.
292
+ */
293
+ // Not used anymore, but could be useful for debugging.
294
+ // showVehicleTraj: {
295
+ // value:
296
+ // options.showVehicleTraj !== undefined
297
+ // ? options.showVehicleTraj
298
+ // : true,
299
+ // writable: true,
300
+ // },
301
+ });
302
+ };
303
+ class_1.prototype.attachToMap = function (map) {
304
+ var _this = this;
305
+ _super.prototype.attachToMap.call(this, map);
306
+ // If the layer is visible we start the rendering clock
307
+ if (this.visible) {
308
+ this.start();
309
+ }
310
+ // On change of visibility we start/stop the rendering clock
311
+ this.visibilityRef = this.on('change:visible', function (evt) {
312
+ if (evt.target.visible) {
313
+ _this.start();
314
+ }
315
+ else {
316
+ _this.stop();
317
+ }
318
+ });
319
+ // To avoid browser hanging when the tab is not visible for a certain amount of time,
320
+ // We stop the rendering and the websocket when hide and start again when show.
321
+ document.addEventListener('visibilitychange', this.onDocumentVisibilityChange);
322
+ };
323
+ class_1.prototype.detachFromMap = function () {
324
+ document.removeEventListener('visibilitychange', this.onDocumentVisibilityChange);
325
+ this.stop();
326
+ (0, Observable_1.unByKey)(this.visibilityRef);
327
+ var context = this.canvas.getContext('2d');
328
+ context.clearRect(0, 0, this.canvas.width, this.canvas.height);
329
+ _super.prototype.detachFromMap.call(this);
330
+ };
331
+ class_1.prototype.start = function () {
332
+ this.stop();
333
+ this.renderTrajectories();
334
+ this.startUpdateTime();
335
+ this.api.open();
336
+ this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
337
+ this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
338
+ if (this.isUpdateBboxOnMoveEnd) {
339
+ // Update the bbox on each move end
340
+ this.setBbox();
341
+ }
342
+ };
343
+ /**
344
+ * Start the clock.
345
+ * @private
346
+ */
347
+ class_1.prototype.startUpdateTime = function () {
348
+ var _this = this;
349
+ this.stopUpdateTime();
350
+ this.updateTimeDelay = this.getRefreshTimeInMs();
351
+ this.updateTimeInterval = setInterval(function () {
352
+ // When live=true, we update the time with new Date();
353
+ _this.time = _this.live
354
+ ? new Date()
355
+ : _this.time.getTime() + _this.updateTimeDelay * _this.speed;
356
+ }, this.updateTimeDelay);
357
+ };
358
+ class_1.prototype.stop = function () {
359
+ this.api.unsubscribeTrajectory(this.onTrajectoryMessage);
360
+ this.api.unsubscribeDeletedVehicles(this.onDeleteTrajectoryMessage);
361
+ this.api.close();
362
+ };
363
+ /**
364
+ * Stop the clock.
365
+ * @private
366
+ */
367
+ class_1.prototype.stopUpdateTime = function () {
368
+ if (this.updateTimeInterval) {
369
+ clearInterval(this.updateTimeInterval);
370
+ }
371
+ };
372
+ /**
373
+ * Launch renderTrajectories. it avoids duplicating code in renderTrajectories method.
374
+ *
375
+ * @param {object} viewState The view state of the map.
376
+ * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
377
+ * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
378
+ * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
379
+ * @param {number} [viewState.rotation = 0] Rotation of the map to render.
380
+ * @param {number} viewState.resolution Resolution of the map to render.
381
+ * @param {boolean} noInterpolate If true trajectories are not interpolated but
382
+ * drawn at the last known coordinate. Use this for performance optimization
383
+ * during map navigation.
384
+ * @private
385
+ */
386
+ class_1.prototype.renderTrajectoriesInternal = function (viewState, noInterpolate) {
387
+ if (!this.map) {
388
+ return false;
389
+ }
390
+ var time = this.live ? Date.now() : this.time;
391
+ var trajectories = Object.values(this.trajectories);
392
+ // console.time('sort');
393
+ if (this.sort) {
394
+ trajectories.sort(this.sort);
395
+ }
396
+ // console.timeEnd('sort');
397
+ // console.time('render');
398
+ this.renderState = (0, renderTrajectories_1.default)(this.canvas, trajectories, this.style, __assign(__assign({}, viewState), { pixelRatio: this.pixelRatio, time: time }), __assign({ noInterpolate: viewState.zoom < this.minZoomInterpolation ? true : noInterpolate, hoverVehicleId: this.hoverVehicleId, selectedVehicleId: this.selectedVehicleId }, this.styleOptions));
399
+ // console.timeEnd('render');
400
+ return true;
401
+ };
402
+ /**
403
+ * Render the trajectories requesting an animation frame and cancelling the previous one.
404
+ * This function must be overrided by children to provide the correct parameters.
405
+ *
406
+ * @param {object} viewState The view state of the map.
407
+ * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
408
+ * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
409
+ * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
410
+ * @param {number} [viewState.rotation = 0] Rotation of the map to render.
411
+ * @param {number} viewState.resolution Resolution of the map to render.
412
+ * @param {boolean} noInterpolate If true trajectories are not interpolated but
413
+ * drawn at the last known coordinate. Use this for performance optimization
414
+ * during map navigation.
415
+ * @private
416
+ */
417
+ class_1.prototype.renderTrajectories = function (viewState, noInterpolate) {
418
+ var _this = this;
419
+ if (this.requestId) {
420
+ cancelAnimationFrame(this.requestId);
421
+ this.requestId = null;
422
+ }
423
+ if (!noInterpolate && this.useRequestAnimationFrame) {
424
+ this.requestId = requestAnimationFrame(function () {
425
+ _this.renderTrajectoriesInternal(viewState, noInterpolate);
426
+ });
427
+ }
428
+ else if (!noInterpolate && this.useDebounce) {
429
+ this.debounceRenderTrajectories(viewState, noInterpolate);
430
+ }
431
+ else if (!noInterpolate && this.useThrottle) {
432
+ this.throttleRenderTrajectories(viewState, noInterpolate);
433
+ }
434
+ else {
435
+ this.renderTrajectoriesInternal(viewState, noInterpolate);
436
+ }
437
+ };
438
+ class_1.prototype.setBbox = function (extent, zoom) {
439
+ // Clean trajectories before sending the new bbox
440
+ // Purge trajectories:
441
+ // - which are outside the extent
442
+ // - when it's bus and zoom level is too low for them
443
+ var keys = Object.keys(this.trajectories);
444
+ for (var i = keys.length - 1; i >= 0; i -= 1) {
445
+ this.purgeTrajectory(this.trajectories[keys[i]], extent, zoom);
446
+ }
447
+ var bbox = __spreadArray([], extent, true);
448
+ if (this.isUpdateBboxOnMoveEnd) {
449
+ bbox.push(zoom);
450
+ if (this.tenant) {
451
+ bbox.push("tenant=".concat(this.tenant));
452
+ }
453
+ /* @ignore */
454
+ this.generalizationLevel = this.getGeneralizationLevelByZoom(zoom);
455
+ if (this.generalizationLevel) {
456
+ bbox.push("gen=".concat(this.generalizationLevel));
457
+ }
458
+ }
459
+ this.api.bbox = bbox;
460
+ };
461
+ class_1.prototype.setMode = function (mode) {
462
+ if (this.mode === mode) {
463
+ return;
464
+ }
465
+ this.mode = mode;
466
+ this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
467
+ this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
468
+ };
469
+ /**
470
+ * Get the duration before the next update depending on zoom level.
471
+ *
472
+ * @private
473
+ * @param {number} zoom
474
+ */
475
+ class_1.prototype.getRefreshTimeInMs = function (zoom) {
476
+ var _a;
477
+ var roundedZoom = Math.round(zoom);
478
+ var timeStep = this.getRenderTimeIntervalByZoom(roundedZoom) || 25;
479
+ var nextTick = Math.max(25, timeStep / this.speed);
480
+ var nextThrottleTick = Math.min(nextTick, 500);
481
+ // TODO: see if this should go elsewhere.
482
+ if (this.useThrottle) {
483
+ this.throttleRenderTrajectories = (0, lodash_throttle_1.default)(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true });
484
+ }
485
+ else if (this.useDebounce) {
486
+ this.debounceRenderTrajectories = (0, lodash_debounce_1.default)(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true, maxWait: 5000 });
487
+ }
488
+ if ((_a = this.api) === null || _a === void 0 ? void 0 : _a.buffer) {
489
+ var _b = this.api.buffer, size = _b[1];
490
+ this.api.buffer = [nextThrottleTick, size];
491
+ }
492
+ return nextTick;
493
+ };
494
+ /**
495
+ * Get vehicle.
496
+ * @param {function} filterFc A function use to filter results.
497
+ * @return {Array<Object>} Array of vehicle.
498
+ */
499
+ class_1.prototype.getVehicle = function (filterFc) {
500
+ return Object.values(this.trajectories).filter(filterFc);
501
+ };
502
+ /**
503
+ * Request feature information for a given coordinate.
504
+ *
505
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate.
506
+ * @param {Object} options Options See child classes to see which options are supported.
507
+ * @param {number} [options.resolution=1] The resolution of the map.
508
+ * @param {number} [options.nb=Infinity] The max number of vehicles to return.
509
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
510
+ */
511
+ class_1.prototype.getFeatureInfoAtCoordinate = function (coordinate, options) {
512
+ var _this = this;
513
+ if (options === void 0) { options = {}; }
514
+ var resolution = options.resolution, nb = options.nb;
515
+ var ext = (0, extent_1.buffer)(__spreadArray(__spreadArray([], coordinate, true), coordinate, true), this.hitTolerance * resolution);
516
+ var trajectories = Object.values(this.trajectories);
517
+ if (this.sort) {
518
+ trajectories = trajectories.sort(this.sort);
519
+ }
520
+ var vehicles = [];
521
+ for (var i = 0; i < trajectories.length; i += 1) {
522
+ if (trajectories[i].properties.coordinate &&
523
+ (0, extent_1.containsCoordinate)(ext, trajectories[i].properties.coordinate)) {
524
+ vehicles.push(trajectories[i]);
525
+ }
526
+ if (vehicles.length === nb) {
527
+ break;
528
+ }
529
+ }
530
+ return Promise.resolve({
531
+ layer: this,
532
+ features: vehicles.map(function (vehicle) { return _this.format.readFeature(vehicle); }),
533
+ coordinate: coordinate,
534
+ });
535
+ };
536
+ /**
537
+ * Request the stopSequence and the fullTrajectory informations for a vehicle.
538
+ *
539
+ * @param {string} id The vehicle identifier (the train_id property).
540
+ * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
541
+ */
542
+ class_1.prototype.getTrajectoryInfos = function (id) {
543
+ // When a vehicle is selected, we request the complete stop sequence and the complete full trajectory.
544
+ // Then we combine them in one response and send them to inherited layers.
545
+ var promises = [
546
+ this.api.getStopSequence(id, this.mode),
547
+ this.api.getFullTrajectory(id, this.mode, this.generalizationLevel),
548
+ ];
549
+ return Promise.all(promises).then(function (_a) {
550
+ var stopSequence = _a[0], fullTrajectory = _a[1];
551
+ var response = {
552
+ stopSequence: stopSequence,
553
+ fullTrajectory: fullTrajectory,
554
+ };
555
+ return response;
556
+ });
557
+ };
558
+ /**
559
+ * Determine if the trajectory is useless and should be removed from the list or not.
560
+ * By default, this function exclude vehicles:
561
+ * - that have their trajectory outside the current extent and
562
+ * - that are not a train and zoom level is lower than layer's minZoomNonTrain property.
563
+ *
564
+ * @param {RealtimeTrajectory} trajectory
565
+ * @param {Array<number>} extent
566
+ * @param {number} zoom
567
+ * @return {boolean} if the trajectory must be displayed or not.
568
+ * @ignore
569
+ */
570
+ class_1.prototype.purgeTrajectory = function (trajectory, extent, zoom) {
571
+ var _a = trajectory.properties, type = _a.type, bounds = _a.bounds, id = _a.train_id;
572
+ if (!(0, extent_1.intersects)(extent, bounds) ||
573
+ (type !== 'rail' && zoom < (this.minZoomNonTrain || 9))) {
574
+ this.removeTrajectory(id);
575
+ return true;
576
+ }
577
+ return false;
578
+ };
579
+ /**
580
+ * Add a trajectory.
581
+ * @param {RealtimeTrajectory} trajectory The trajectory to add.
582
+ * @private
583
+ */
584
+ class_1.prototype.addTrajectory = function (trajectory) {
585
+ if (this.filter && !this.filter(trajectory)) {
586
+ return;
587
+ }
588
+ this.trajectories[trajectory.properties.train_id] = trajectory;
589
+ this.renderTrajectories();
590
+ };
591
+ class_1.prototype.removeTrajectory = function (id) {
592
+ delete this.trajectories[id];
593
+ };
594
+ /**
595
+ * On zoomend we adjust the time interval of the update of vehicles positions.
596
+ *
597
+ * @param evt Event that triggered the function.
598
+ * @private
599
+ */
600
+ // eslint-disable-next-line no-unused-vars
601
+ class_1.prototype.onZoomEnd = function (evt) {
602
+ this.startUpdateTime();
603
+ };
604
+ class_1.prototype.onDocumentVisibilityChange = function () {
605
+ if (!this.visible) {
606
+ return;
607
+ }
608
+ if (document.hidden) {
609
+ this.stop();
610
+ }
611
+ else {
612
+ this.start();
613
+ }
614
+ };
615
+ /**
616
+ * Callback on websocket's trajectory channel events.
617
+ * It adds a trajectory to the list.
618
+ *
619
+ * @private
620
+ */
621
+ class_1.prototype.onTrajectoryMessage = function (data) {
622
+ if (!data.content) {
623
+ return;
624
+ }
625
+ var trajectory = data.content;
626
+ var geometry = trajectory.geometry, _a = trajectory.properties, id = _a.train_id, timeSinceUpdate = _a.time_since_update, rawCoordinates = _a.raw_coordinates;
627
+ // ignore old events [SBAHNM-97]
628
+ if (timeSinceUpdate < 0) {
629
+ return;
630
+ }
631
+ // console.time(`onTrajectoryMessage${data.content.properties.train_id}`);
632
+ if (this.purgeTrajectory(trajectory)) {
633
+ return;
634
+ }
635
+ if (this.debug &&
636
+ this.mode === api_1.RealtimeModes.TOPOGRAPHIC &&
637
+ rawCoordinates) {
638
+ trajectory.properties.olGeometry = {
639
+ type: 'Point',
640
+ coordinates: (0, proj_1.fromLonLat)(rawCoordinates, this.map.getView().getProjection()),
641
+ };
642
+ }
643
+ else {
644
+ trajectory.properties.olGeometry = this.format.readGeometry(geometry);
645
+ }
646
+ // TODO Make sure the timeOffset is useful. May be we can remove it.
647
+ trajectory.properties.timeOffset = Date.now() - data.timestamp;
648
+ this.addTrajectory(trajectory);
649
+ };
650
+ /**
651
+ * Callback on websocket's deleted_vehicles channel events.
652
+ * It removes the trajectory from the list.
653
+ *
654
+ * @private
655
+ * @override
656
+ */
657
+ class_1.prototype.onDeleteTrajectoryMessage = function (data) {
658
+ if (!data.content) {
659
+ return;
660
+ }
661
+ this.removeTrajectory(data.content);
662
+ };
663
+ /**
664
+ * Callback when user moves the mouse/pointer over the map.
665
+ * It sets the layer's hoverVehicleId property with the current hovered vehicle's id.
666
+ *
667
+ * @private
668
+ * @override
669
+ */
670
+ class_1.prototype.onFeatureHover = function (features, layer, coordinate) {
671
+ var feature = features[0];
672
+ var id = null;
673
+ if (feature) {
674
+ id = feature.get('train_id');
675
+ }
676
+ if (this.hoverVehicleId !== id) {
677
+ /** @ignore */
678
+ this.hoverVehicleId = id;
679
+ this.renderTrajectories(true);
680
+ }
681
+ };
682
+ /**
683
+ * Callback when user clicks on the map.
684
+ * It sets the layer's selectedVehicleId property with the current selected vehicle's id.
685
+ *
686
+ * @private
687
+ * @override
688
+ */
689
+ class_1.prototype.onFeatureClick = function (features, layer, coordinate) {
690
+ var feature = features[0];
691
+ var id = null;
692
+ if (feature) {
693
+ id = feature.get('train_id');
694
+ }
695
+ if (this.selectedVehicleId !== id) {
696
+ /** @ignore */
697
+ this.selectedVehicleId = id;
698
+ this.selectedVehicle = feature;
699
+ this.renderTrajectories(true);
700
+ }
701
+ };
702
+ return class_1;
703
+ }(Base));
435
704
  };
436
- export default RealtimeLayerMixin;
705
+ exports.default = RealtimeLayerMixin;