@maptrip/map-tiles 1.1.1 → 1.2.0

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 (38) hide show
  1. package/README.md +1 -0
  2. package/dist/api-extensions/google/MapTripGoogleMapType.d.ts +28 -0
  3. package/dist/api-extensions/leaflet/MapTripLeafletGridLayer.d.ts +4 -0
  4. package/dist/api-extensions/openlayers/MapTripOpenLayersSource.d.ts +19 -0
  5. package/dist/core/CollisionDetector.d.ts +14 -0
  6. package/dist/core/VectorTileFactory.d.ts +64 -0
  7. package/dist/core/VectorTileLoader.d.ts +40 -0
  8. package/dist/core/VectorTileRenderer.d.ts +123 -0
  9. package/dist/core/constants.d.ts +10 -0
  10. package/dist/core/rendering_config_default.d.ts +658 -0
  11. package/dist/core/rendering_config_grayscale.d.ts +638 -0
  12. package/dist/core/rendering_config_traffic.d.ts +39 -0
  13. package/dist/map-tiles.d.ts +9 -0
  14. package/dist/map-tiles.js +23617 -10182
  15. package/dist/map-tiles.js.map +1 -0
  16. package/dist/types/interfaces.d.ts +135 -0
  17. package/lib/api-extensions/google/MapTripGoogleMapType.ts +40 -0
  18. package/lib/api-extensions/leaflet/MapTripLeafletGridLayer.ts +44 -0
  19. package/lib/api-extensions/openlayers/MapTripOpenLayersSource.ts +61 -0
  20. package/lib/core/CollisionDetector.ts +95 -0
  21. package/lib/core/VectorTileFactory.ts +327 -0
  22. package/lib/core/VectorTileLoader.ts +165 -0
  23. package/lib/core/VectorTileRenderer.ts +1212 -0
  24. package/lib/core/constants.ts +21 -0
  25. package/lib/{main.js → main.ts} +2 -0
  26. package/lib/types/interfaces.ts +147 -0
  27. package/package.json +19 -14
  28. package/lib/api-extensions/google/MapTripGoogleMapType.js +0 -25
  29. package/lib/api-extensions/leaflet/MapTripLeafletGridLayer.js +0 -24
  30. package/lib/api-extensions/openlayers/MapTripOpenLayersSource.js +0 -43
  31. package/lib/core/CollisionDetector.js +0 -65
  32. package/lib/core/VectorTileFactory.js +0 -245
  33. package/lib/core/VectorTileLoader.js +0 -132
  34. package/lib/core/VectorTileRenderer.js +0 -826
  35. package/lib/core/constants.js +0 -18
  36. /package/lib/core/{rendering_config_default.js → rendering_config_default.ts} +0 -0
  37. /package/lib/core/{rendering_config_grayscale.js → rendering_config_grayscale.ts} +0 -0
  38. /package/lib/core/{rendering_config_traffic.js → rendering_config_traffic.ts} +0 -0
@@ -0,0 +1,165 @@
1
+ import type {
2
+ SuccessCallback,
3
+ TileQueueItem,
4
+ Tile,
5
+ MapTilesProperties,
6
+ } from "../types/interfaces.js";
7
+
8
+ class VectorTileLoader {
9
+ private _properties: typeof VectorTileLoader.DEFAULT_PROPERTIES;
10
+ private _tiles: TileQueueItem[];
11
+ private _numLoading: number;
12
+
13
+ constructor(properties: MapTilesProperties) {
14
+ this._properties = properties
15
+ ? { ...VectorTileLoader.DEFAULT_PROPERTIES, ...properties }
16
+ : VectorTileLoader.DEFAULT_PROPERTIES;
17
+
18
+ this._tiles = [];
19
+ this._numLoading = 0;
20
+ }
21
+
22
+ queue(
23
+ tile: Tile,
24
+ successCallback: SuccessCallback,
25
+ errorCallback: ErrorCallback
26
+ ): void {
27
+ this._tiles.push({
28
+ tile: tile,
29
+ successCallback: successCallback,
30
+ errorCallback: errorCallback,
31
+ });
32
+ }
33
+
34
+ loadNext(): void {
35
+ if (this._numLoading >= this._properties!.parallelDownloads! || 0) {
36
+ return;
37
+ }
38
+
39
+ if (this._tiles.length > 0) {
40
+ // filter all files with wrong zoom levels
41
+ let latestZoom = this._tiles[this._tiles.length - 1].tile.z;
42
+ this._tiles = this._tiles.filter((tile) => tile.tile.z === latestZoom);
43
+
44
+ // load the first tile
45
+ let next = this._tiles.shift();
46
+ if (!next) {
47
+ return;
48
+ }
49
+
50
+ // load all URLs for that tile
51
+ this.loadAllUrls(next, next.tile.urls);
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Checks if a HTTP Request is successful
57
+ *
58
+ * @param {XMLHttpRequest} httpRequest
59
+ *
60
+ * @return {boolean}
61
+ */
62
+ isHttpRequestSuccessful(httpRequest: XMLHttpRequest): boolean {
63
+ return (
64
+ httpRequest.readyState === XMLHttpRequest.DONE &&
65
+ httpRequest.status === 200 &&
66
+ httpRequest.responseText.length > 0
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Checks if a HTTP Request has failed (there was a timeout or the status code is an error)
72
+ *
73
+ * @param {XMLHttpRequest} httpRequest
74
+ *
75
+ * @return {boolean}
76
+ */
77
+ isHttpRequestFailed(httpRequest: XMLHttpRequest): boolean {
78
+ return (
79
+ this.isHttpRequestTimeout(httpRequest) ||
80
+ (httpRequest.readyState === XMLHttpRequest.DONE &&
81
+ httpRequest.status >= 400)
82
+ );
83
+ }
84
+
85
+ /**
86
+ * Checks if a HTTP Request has timed out
87
+ *
88
+ * @param {XMLHttpRequest} httpRequest
89
+ *
90
+ * @return {boolean}
91
+ */
92
+ isHttpRequestTimeout(httpRequest: XMLHttpRequest): boolean {
93
+ return (
94
+ httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 0
95
+ );
96
+ }
97
+
98
+ loadAllUrls(tile: TileQueueItem, urls: string[]): void {
99
+ var url = urls.pop();
100
+
101
+ if (!url) {
102
+ console.error("URl is null or undefined");
103
+ return;
104
+ }
105
+
106
+ var xhr = new XMLHttpRequest();
107
+ xhr.onreadystatechange = () => {
108
+ if (xhr.readyState !== XMLHttpRequest.DONE) {
109
+ return;
110
+ }
111
+
112
+ if (this.isHttpRequestSuccessful(xhr)) {
113
+ var json = JSON.parse(xhr.responseText); // XXX bei Fehlern des Data Servers kommt hier kein JSON sondern XML!
114
+
115
+ if (!tile.tile.json) {
116
+ tile.tile.json = json;
117
+ } else {
118
+ if (json && json.features && Array.isArray(json.features)) {
119
+ //@ts-ignore
120
+ tile.tile.json.features = tile.tile.json.features.concat(
121
+ json.features
122
+ );
123
+ } else {
124
+ console.error("Invalid GeoJSON data structure received");
125
+ }
126
+ }
127
+
128
+ if (urls.length === 0) {
129
+ // no more URLs left, so call the callback
130
+ tile.successCallback(tile.tile.json);
131
+ } else {
132
+ // load the remaining URLs
133
+ this.loadAllUrls(tile, urls);
134
+ }
135
+ }
136
+
137
+ if (this.isHttpRequestFailed(xhr) || this.isHttpRequestTimeout(xhr)) {
138
+ tile.errorCallback(xhr.responseText);
139
+ }
140
+
141
+ this._numLoading--;
142
+ this.loadNext();
143
+ };
144
+
145
+ xhr.ontimeout = () => {
146
+ tile.errorCallback(xhr.responseText);
147
+ this._numLoading--;
148
+ this.loadNext();
149
+ };
150
+
151
+ xhr.open("GET", url || "", true);
152
+ xhr.setRequestHeader("Authorization", `Bearer ${tile.tile.token}`);
153
+ xhr.timeout = this._properties!.timeout || 0;
154
+ xhr.send(null);
155
+
156
+ this._numLoading++;
157
+ }
158
+
159
+ static DEFAULT_PROPERTIES = {
160
+ timeout: 5000,
161
+ parallelDownloads: 12,
162
+ };
163
+ }
164
+
165
+ export { VectorTileLoader };