@maptrip/map-tiles 1.1.0 → 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 (39) hide show
  1. package/README.md +5 -3
  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 +23647 -10188
  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/core/rendering_config_default.ts +1131 -0
  26. package/lib/{main.js → main.ts} +2 -0
  27. package/lib/types/interfaces.ts +147 -0
  28. package/package.json +19 -14
  29. package/lib/api-extensions/google/MapTripGoogleMapType.js +0 -25
  30. package/lib/api-extensions/leaflet/MapTripLeafletGridLayer.js +0 -24
  31. package/lib/api-extensions/openlayers/MapTripOpenLayersSource.js +0 -43
  32. package/lib/core/CollisionDetector.js +0 -65
  33. package/lib/core/VectorTileFactory.js +0 -245
  34. package/lib/core/VectorTileLoader.js +0 -132
  35. package/lib/core/VectorTileRenderer.js +0 -826
  36. package/lib/core/constants.js +0 -18
  37. package/lib/core/rendering_config_default.js +0 -1107
  38. /package/lib/core/{rendering_config_grayscale.js → rendering_config_grayscale.ts} +0 -0
  39. /package/lib/core/{rendering_config_traffic.js → rendering_config_traffic.ts} +0 -0
@@ -0,0 +1,135 @@
1
+ export interface MapTilesProperties {
2
+ provider?: string | "OpenStreetMap";
3
+ baseMap?: boolean;
4
+ traffic?: boolean;
5
+ timeout?: number;
6
+ parallelDownloads?: number;
7
+ detourProfile?: string;
8
+ renderingConfig?: RENDERING_CONFIG;
9
+ token?: string;
10
+ attribution?: string;
11
+ minZoom?: number;
12
+ maxZoom?: number;
13
+ name?: string;
14
+ NAME?: string;
15
+ }
16
+ export interface Renderer {
17
+ from: number;
18
+ to: number;
19
+ text?: {
20
+ size: number;
21
+ color: string;
22
+ font: string;
23
+ fontWeight?: string;
24
+ outlineColor?: string;
25
+ outlineWidth?: number;
26
+ fontStyle?: string;
27
+ };
28
+ polygon?: PolygonStyle;
29
+ line?: PolylineStyle;
30
+ image?: {
31
+ url: string;
32
+ width: number;
33
+ height: number;
34
+ dx: number;
35
+ dy: number;
36
+ };
37
+ filters?: Filter[];
38
+ [key: string]: any;
39
+ }
40
+ export interface Filter {
41
+ interface: string;
42
+ key: string;
43
+ value?: any;
44
+ type: string;
45
+ }
46
+ export interface RENDERING_CONFIG {
47
+ background: {
48
+ from: number;
49
+ to: number;
50
+ color: string;
51
+ }[];
52
+ layers: {
53
+ [key: string]: string;
54
+ };
55
+ renderers: {
56
+ [key: string]: Renderer[];
57
+ };
58
+ }
59
+ export interface PolylineTextStyle {
60
+ size: number;
61
+ color: string;
62
+ font?: string | undefined;
63
+ fontWeight?: string | undefined;
64
+ outlineColor?: string | undefined;
65
+ outlineWidth?: number | undefined;
66
+ fontStyle?: string | undefined;
67
+ }
68
+ export interface PolylineStyle {
69
+ color?: string | undefined;
70
+ width?: number | undefined;
71
+ url?: string | undefined;
72
+ height?: number | undefined;
73
+ dx?: number | undefined;
74
+ dy?: number | undefined;
75
+ direction?: string | undefined;
76
+ }
77
+ export interface PolygonStyle {
78
+ color: string;
79
+ opacity?: number | undefined;
80
+ outlineColor?: string | undefined;
81
+ outlineWidth?: number | undefined;
82
+ url: string;
83
+ font: string;
84
+ fontStyle?: string | undefined;
85
+ fontWeight?: string | undefined;
86
+ size?: number | undefined;
87
+ }
88
+ export interface ImageStyle {
89
+ url: string;
90
+ width: number;
91
+ height: number;
92
+ dx: number;
93
+ dy: number;
94
+ }
95
+ export interface Tile {
96
+ x: number;
97
+ y: number;
98
+ z: number;
99
+ urls: string[];
100
+ token: string;
101
+ json?: JSON;
102
+ }
103
+ export interface TileQueueItem {
104
+ tile: Tile;
105
+ successCallback: SuccessCallback;
106
+ errorCallback: ErrorCallback;
107
+ }
108
+ export interface PointXY {
109
+ x: number;
110
+ y: number;
111
+ z?: number;
112
+ }
113
+ export interface Vertex {
114
+ x: number;
115
+ y: number;
116
+ }
117
+ export interface PositionXY {
118
+ x: number;
119
+ y: number;
120
+ angle: number;
121
+ }
122
+ export interface Coordinates {
123
+ lon: number;
124
+ lat: number;
125
+ }
126
+ export interface Boundaries {
127
+ min: Coordinates;
128
+ max: Coordinates;
129
+ }
130
+ export interface SuccessCallback {
131
+ (json: any): void;
132
+ }
133
+ export interface ErrorCallback {
134
+ (error: any): void;
135
+ }
@@ -0,0 +1,40 @@
1
+ import { VectorTileFactory } from "../../core/VectorTileFactory.js";
2
+ import { MIN_ZOOM, MAX_ZOOM } from "../../core/constants.js";
3
+ import { PointXY, RENDERING_CONFIG } from "../../types/interfaces.js";
4
+
5
+ class MapTripGoogleMapType {
6
+ tileSize = new google.maps.Size(256, 256);
7
+ alt = null;
8
+ maxZoom = MAX_ZOOM;
9
+ minZoom = MIN_ZOOM;
10
+ projection = null;
11
+ radius = 6378137;
12
+
13
+ name: string;
14
+ token: string;
15
+ factory: VectorTileFactory;
16
+
17
+ constructor(properties: {
18
+ provider?: string;
19
+ baseMap?: boolean;
20
+ traffic?: boolean;
21
+ timeout?: number;
22
+ parallelDownloads?: number;
23
+ detourProfile?: string;
24
+ renderingConfig?: RENDERING_CONFIG;
25
+ token?: string;
26
+ name?: string;
27
+ }) {
28
+ this.name = properties!.name ? properties!.name : "infoware";
29
+ this.token = properties!.token!;
30
+ this.factory = VectorTileFactory.createFactory(properties)!;
31
+ }
32
+
33
+ getTile(coord: PointXY, zoom: number) {
34
+ return this.factory.getJsonTile(zoom, coord.x, coord.y, this.token);
35
+ }
36
+
37
+ releaseTile() {}
38
+ }
39
+
40
+ export { MapTripGoogleMapType };
@@ -0,0 +1,44 @@
1
+ import L from "leaflet";
2
+
3
+ import { VectorTileFactory } from "../../core/VectorTileFactory.js";
4
+ import { MIN_ZOOM, MAX_ZOOM, ATTRIBUTION } from "../../core/constants.js";
5
+ import type { PointXY, RENDERING_CONFIG } from "../../types/interfaces.js";
6
+
7
+ const MapTripLeafletGridLayer = L.GridLayer.extend({
8
+ MIN_ZOOM: MIN_ZOOM,
9
+ MAX_ZOOM: MAX_ZOOM,
10
+
11
+ initialize: function (options: {
12
+ provider?: string;
13
+ baseMap?: boolean;
14
+ traffic?: boolean;
15
+ timeout?: number;
16
+ parallelDownloads?: number;
17
+ detourProfile?: string;
18
+ renderingConfig?: RENDERING_CONFIG;
19
+ token?: string;
20
+ attribution?: string;
21
+ minZoom?: number;
22
+ maxZoom?: number;
23
+ }) {
24
+ if (options) {
25
+ this.token = options.token;
26
+ this.options.attribution = options.attribution
27
+ ? options.attribution
28
+ : ATTRIBUTION;
29
+ this.options.minZoom = options.minZoom
30
+ ? Math.max(this.MIN_ZOOM, options.minZoom)
31
+ : this.MIN_ZOOM;
32
+ this.options.maxZoom = options.maxZoom
33
+ ? Math.min(this.MAX_ZOOM, options.maxZoom)
34
+ : this.MAX_ZOOM;
35
+ this.factory = VectorTileFactory.createFactory(options);
36
+ }
37
+ },
38
+
39
+ createTile: function (coords: PointXY) {
40
+ return this.factory.getJsonTile(coords.z, coords.x, coords.y, this.token);
41
+ },
42
+ });
43
+
44
+ export { MapTripLeafletGridLayer };
@@ -0,0 +1,61 @@
1
+ import XYZ from "ol/source/XYZ.js";
2
+ import { XYZOptions, createXYZ, extentFromProjection } from "ol/tilegrid.js";
3
+
4
+ import { VectorTileFactory } from "../../core/VectorTileFactory.js";
5
+ import { MIN_ZOOM, MAX_ZOOM, ATTRIBUTION } from "../../core/constants.js";
6
+ import { RENDERING_CONFIG } from "../../types/interfaces.js";
7
+
8
+ class MapTripOpenLayersSource extends XYZ {
9
+ token: string;
10
+ factory: VectorTileFactory;
11
+ constructor(properties: {
12
+ provider?: string;
13
+ baseMap?: boolean;
14
+ traffic?: boolean;
15
+ timeout?: number;
16
+ parallelDownloads?: number;
17
+ detourProfile?: string;
18
+ renderingConfig?: RENDERING_CONFIG;
19
+ token?: string;
20
+ }) {
21
+ const options: XYZOptions = {};
22
+ const projection = "EPSG:3857";
23
+
24
+ const tileGrid = createXYZ({
25
+ extent: extentFromProjection(projection),
26
+ maxResolution: options.maxResolution,
27
+ minZoom: MIN_ZOOM,
28
+ maxZoom: MAX_ZOOM,
29
+ tileSize: [256, 256],
30
+ });
31
+
32
+ super({
33
+ opaque: true,
34
+ minZoom: MIN_ZOOM,
35
+ maxZoom: MAX_ZOOM,
36
+ projection: projection,
37
+ tileGrid: tileGrid,
38
+ wrapX: true,
39
+ url: "z:{z} x:{x} y:{y}",
40
+ tileLoadFunction: (tile) => {
41
+ let coord = tile.getTileCoord();
42
+ let canvas = this.factory.getJsonTile(
43
+ coord[0],
44
+ coord[1],
45
+ coord[2],
46
+ this.token!,
47
+ () => {
48
+ //@ts-expect-error: 'tile' is an OL ImageTile & .setImage() is available but not exported from types
49
+ tile.setImage(canvas);
50
+ }
51
+ );
52
+ },
53
+ attributions: ATTRIBUTION,
54
+ });
55
+
56
+ this.token = properties.token!;
57
+ this.factory = VectorTileFactory.createFactory(properties)!;
58
+ }
59
+ }
60
+
61
+ export { MapTripOpenLayersSource };
@@ -0,0 +1,95 @@
1
+ import RTree from "rtree";
2
+ import { PositionXY } from "../types/interfaces.js";
3
+
4
+ class CollisionDetector {
5
+ private _tree: any; //use 'any' type because rtree package does not have .d.ts file
6
+ constructor() {
7
+ this._tree = RTree(10);
8
+ }
9
+
10
+ insert(x: number, y: number, w: number, h: number) {
11
+ this._tree.insert({ x: x, y: y, w: w, h: h }, 0);
12
+ }
13
+
14
+ insertPolylineText(
15
+ context: CanvasRenderingContext2D,
16
+ text: string,
17
+ position: PositionXY
18
+ ) {
19
+ let widthUsed = 0;
20
+
21
+ for (let i = 0; i <= text.length; i++) {
22
+ let box = this.getCollisionParams(
23
+ context,
24
+ text.charAt(i),
25
+ position,
26
+ widthUsed
27
+ );
28
+ this.insert(box.x, box.y, box.w, box.h);
29
+
30
+ widthUsed += context.measureText(text.charAt(i)).width;
31
+ }
32
+ }
33
+
34
+ isCollision(x: number, y: number, w: number, h: number) {
35
+ return this._tree.search({ x: x, y: y, w: w, h: h }).length > 0;
36
+ }
37
+
38
+ isPolylineTextCollision(
39
+ context: CanvasRenderingContext2D,
40
+ text: string,
41
+ position: PositionXY
42
+ ): boolean {
43
+ let border = 5;
44
+ let widthUsed = 0;
45
+
46
+ for (let i = 0; i <= text.length; i++) {
47
+ var box = this.getCollisionParams(
48
+ context,
49
+ text.charAt(i),
50
+ position,
51
+ widthUsed
52
+ );
53
+
54
+ if (
55
+ box.x < border ||
56
+ box.y < border ||
57
+ box.x + box.w > 256 - border ||
58
+ box.y + box.h > 256 - border
59
+ ) {
60
+ return true;
61
+ }
62
+
63
+ if (this.isCollision(box.x, box.y, box.w, box.h)) {
64
+ return true;
65
+ }
66
+
67
+ widthUsed += context.measureText(text.charAt(i)).width;
68
+ }
69
+
70
+ return false;
71
+ }
72
+
73
+ getCollisionParams(
74
+ context: CanvasRenderingContext2D,
75
+ text: string,
76
+ position: PositionXY,
77
+ widthUsed: number
78
+ ): { [key: string]: number } {
79
+ let textWidth = context.measureText(text).width;
80
+ let textHeight = context.measureText(text.charAt(0)).width * 1.5;
81
+
82
+ return {
83
+ x: position.x + 0.5 * Math.cos(position.angle) * textWidth,
84
+ y: position.y + 0.5 * Math.sin(position.angle) * textWidth,
85
+ w:
86
+ Math.abs(Math.cos(position.angle) * textWidth) +
87
+ Math.abs(Math.sin(position.angle) * textHeight),
88
+ h:
89
+ Math.abs(Math.sin(position.angle) * textWidth) +
90
+ Math.abs(Math.cos(position.angle) * textHeight),
91
+ };
92
+ }
93
+ }
94
+
95
+ export { CollisionDetector };
@@ -0,0 +1,327 @@
1
+ import { VectorTileLoader } from "./VectorTileLoader.js";
2
+
3
+ import { VectorTileRenderer } from "./VectorTileRenderer.js";
4
+
5
+ import {
6
+ URL_TEMPLATE,
7
+ PROVIDER_PROJECTS,
8
+ PROVIDER_TRAFFIC,
9
+ } from "./constants.js";
10
+
11
+ import { RENDERING_CONFIG_DEFAULT } from "./rendering_config_default.js";
12
+ import { RENDERING_CONFIG_TRAFFIC } from "./rendering_config_traffic.js";
13
+
14
+ import WebFont from "webfontloader";
15
+ import {
16
+ Boundaries,
17
+ Coordinates,
18
+ RENDERING_CONFIG,
19
+ SuccessCallback,
20
+ MapTilesProperties,
21
+ } from "../types/interfaces.js";
22
+
23
+ /**
24
+ * Creates a factory to create tiles from the provided URL templates.
25
+ *
26
+ * The templates must have variables ${x}, ${y} and ${z} for the tile index.
27
+ * When multiple URLs are specified, they are used round robin.
28
+ */
29
+ class VectorTileFactory {
30
+ private _layers: string[];
31
+ private _tileLoader: VectorTileLoader;
32
+ private _renderer: VectorTileRenderer;
33
+ private _urls: string[];
34
+ private _additionalUrls: string[];
35
+
36
+ static _fonts: string | undefined = undefined;
37
+ static _properties: typeof VectorTileFactory.DEFAULT_PROPERTIES;
38
+ constructor(
39
+ urls: string[],
40
+ additionalUrls: string[],
41
+ renderingConfig: RENDERING_CONFIG,
42
+ properties: MapTilesProperties
43
+ ) {
44
+ this._urls = urls;
45
+ this._additionalUrls = additionalUrls;
46
+ this._layers = ["all"]; // could be used to load just a selection of layers from the data server
47
+
48
+ this._tileLoader = new VectorTileLoader(properties);
49
+ this._renderer = new VectorTileRenderer(renderingConfig);
50
+
51
+ if (!VectorTileFactory._fonts) {
52
+ WebFont.load({
53
+ custom: {
54
+ families: ["Open Sans:n4,n7,i4,i7"],
55
+ urls: ["https://maps.infoware.de/MapAPI-1.4/css/opensans.css"],
56
+ },
57
+ loading: function () {
58
+ VectorTileFactory._fonts = "loading";
59
+ },
60
+ active: () => {
61
+ VectorTileFactory._fonts = "active";
62
+ this._tileLoader.loadNext();
63
+ },
64
+ inactive: () => {
65
+ // font loading failed
66
+ VectorTileFactory._fonts = "active";
67
+ this._tileLoader.loadNext();
68
+ },
69
+ });
70
+ }
71
+ }
72
+
73
+ public static createFactory(properties: {
74
+ provider?: string;
75
+ baseMap?: boolean;
76
+ traffic?: boolean;
77
+ timeout?: number;
78
+ parallelDownloads?: number;
79
+ detourProfile?: string;
80
+ renderingConfig?: RENDERING_CONFIG;
81
+ }): VectorTileFactory {
82
+ // apply default properties
83
+ this._properties = properties
84
+ ? { ...VectorTileFactory.DEFAULT_PROPERTIES, ...properties }
85
+ : VectorTileFactory.DEFAULT_PROPERTIES;
86
+ this._properties.provider = this._properties.provider.toLowerCase();
87
+
88
+ // assert the provider is valid
89
+ if (!PROVIDER_PROJECTS[this._properties.provider]) {
90
+ throw new Error("Unknown provider");
91
+ }
92
+
93
+ // assert there is no traffic requested for a provider which does not provide traffic information
94
+ if (
95
+ this._properties.traffic &&
96
+ !PROVIDER_TRAFFIC[this._properties.provider]
97
+ ) {
98
+ throw new Error("There is now traffic information for this provider");
99
+ }
100
+
101
+ // assert there is either a base map or traffic (or both)
102
+ if (!this._properties.baseMap && !this._properties.traffic) {
103
+ throw new Error(
104
+ "You cannot create a factory with neither a base map nor traffic"
105
+ );
106
+ }
107
+ return new VectorTileFactory(
108
+ VectorTileFactory.getUrlTemplatesForBaseLayer(this._properties),
109
+ VectorTileFactory.getUrlTemplatesForTrafficLayer(this._properties),
110
+ VectorTileFactory.getRenderingConfig(this._properties),
111
+ this._properties
112
+ );
113
+ }
114
+
115
+ private static getUrlTemplatesForBaseLayer(
116
+ properties: GeoJSON.GeoJsonProperties
117
+ ): string[] | [] {
118
+ if (properties) {
119
+ if (properties.baseMap) {
120
+ let template = URL_TEMPLATE.replace(
121
+ "${project}",
122
+ PROVIDER_PROJECTS[properties.provider!]
123
+ );
124
+ return [0, 1, 2, 3].map((index) =>
125
+ template.replace("${index}", index.toString())
126
+ );
127
+ } else {
128
+ return [];
129
+ }
130
+ } else {
131
+ return [];
132
+ }
133
+ }
134
+
135
+ private static getUrlTemplatesForTrafficLayer(
136
+ properties: GeoJSON.GeoJsonProperties
137
+ ): string[] | [] {
138
+ if (properties) {
139
+ const trafficUrl = properties.traffic
140
+ ? PROVIDER_TRAFFIC[properties.provider!]
141
+ : null;
142
+ return trafficUrl ? [trafficUrl] : [];
143
+ } else {
144
+ return [];
145
+ }
146
+ }
147
+
148
+ private static getRenderingConfig(properties: MapTilesProperties) {
149
+ if (properties) {
150
+ if (properties.renderingConfig) {
151
+ return properties.renderingConfig;
152
+ } else if (properties.baseMap) {
153
+ return RENDERING_CONFIG_DEFAULT;
154
+ } else {
155
+ return RENDERING_CONFIG_TRAFFIC;
156
+ }
157
+ } else {
158
+ return RENDERING_CONFIG_DEFAULT;
159
+ }
160
+ }
161
+
162
+ public getJsonTile(
163
+ z: number,
164
+ x: number,
165
+ y: number,
166
+ token: string,
167
+ callback?: SuccessCallback
168
+ ): HTMLCanvasElement {
169
+ let maximumIndex = Math.pow(2, z);
170
+ x = (x + 16 * maximumIndex) % maximumIndex;
171
+ y = (y + 16 * maximumIndex) % maximumIndex;
172
+
173
+ let canvas = document.createElement("canvas");
174
+ canvas.width = 256;
175
+ canvas.height = 256;
176
+
177
+ let baseMapUrl = this.resolveTileUrl(z, x, y);
178
+ let urls = baseMapUrl ? [baseMapUrl] : [];
179
+
180
+ // add URLs for additional data like traffic flows
181
+ this._additionalUrls.forEach((it: string) => {
182
+ urls.push(
183
+ it
184
+ .replace("${x}", String(x))
185
+ .replace("${y}", String(y))
186
+ .replace(/\$\{z\}/g, String(z))
187
+ );
188
+ });
189
+
190
+ let tileLoadedSuccessfullyCallback = (json: GeoJSON.Feature) => {
191
+ this.drawTile(json, z, x, y, canvas);
192
+
193
+ if (callback) {
194
+ callback(canvas);
195
+ }
196
+ };
197
+
198
+ let tile_FailedCallback = () => {
199
+ this.drawErroredTile(canvas);
200
+
201
+ if (callback) {
202
+ callback(null);
203
+ }
204
+ };
205
+
206
+ this._tileLoader.queue(
207
+ { x, y, z, urls, token },
208
+ tileLoadedSuccessfullyCallback,
209
+ tile_FailedCallback
210
+ );
211
+
212
+ if (VectorTileFactory._fonts === "active") {
213
+ this._tileLoader.loadNext();
214
+ }
215
+
216
+ return canvas;
217
+ }
218
+
219
+ /**
220
+ * @private
221
+ */
222
+ private resolveTileUrl(z: number, x: number, y: number): string | null {
223
+ if (this._urls.length > 0) {
224
+ var index = (x + y + z) % this._urls.length;
225
+
226
+ return this._urls[index]
227
+ .replace("${x}", String(x))
228
+ .replace("${y}", String(y))
229
+ .replace(/\$\{z\}/g, String(z))
230
+ .replace("${layers}", this._layers.join(","));
231
+ } else {
232
+ // there is no base map layer
233
+ return null;
234
+ }
235
+ }
236
+
237
+ /**
238
+ * @private
239
+ */
240
+ private tileIndexToLatLong(z: number, x: number, y: number): Coordinates {
241
+ var n = Math.PI - (2 * Math.PI * y) / Math.pow(2, z);
242
+
243
+ return {
244
+ lat: (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))),
245
+ lon: (x / Math.pow(2, z)) * 360 - 180,
246
+ };
247
+ }
248
+
249
+ /**
250
+ * @private
251
+ */
252
+ private getTileBoundaries(z: number, x: number, y: number): Boundaries {
253
+ return {
254
+ min: this.tileIndexToLatLong(z, x, y),
255
+ max: this.tileIndexToLatLong(z, x + 1, y + 1),
256
+ };
257
+ }
258
+
259
+ /**
260
+ * @private
261
+ */
262
+ private drawTile(
263
+ json: GeoJSON.GeoJsonObject,
264
+ z: number,
265
+ x: number,
266
+ y: number,
267
+ canvas: HTMLCanvasElement
268
+ ): void {
269
+ var features = this.getFeatures(json)!.sort(function (a: any, b: any) {
270
+ if (a.properties && a.properties.sort_rank) {
271
+ if (b.properties && b.properties.sort_rank) {
272
+ return a.properties.sort_rank - b.properties.sort_rank;
273
+ } else {
274
+ return -1;
275
+ }
276
+ } else {
277
+ if (b.properties && b.properties.sort_rank) {
278
+ return 1;
279
+ } else {
280
+ return 0;
281
+ }
282
+ }
283
+ });
284
+
285
+ const context = canvas.getContext("2d");
286
+ if (context) {
287
+ this._renderer.drawTile(
288
+ features,
289
+ z,
290
+ this.getTileBoundaries(z, x, y),
291
+ context
292
+ );
293
+ } else {
294
+ console.error("Failed to create canvas context");
295
+ }
296
+ }
297
+
298
+ /**
299
+ * Draws an error message on the tile.
300
+ * @private
301
+ *
302
+ * @param canvas
303
+ */
304
+ private drawErroredTile(canvas: HTMLCanvasElement): void {
305
+ this._renderer.drawErroredTile(canvas);
306
+ }
307
+
308
+ /**
309
+ * @private
310
+ */
311
+ private getFeatures(json: GeoJSON.GeoJsonObject): GeoJSON.Feature[] | void {
312
+ if (json.type && json.type == "FeatureCollection") {
313
+ const featureCollection = json as GeoJSON.FeatureCollection;
314
+ return featureCollection.features;
315
+ } else {
316
+ return;
317
+ }
318
+ }
319
+
320
+ static DEFAULT_PROPERTIES = {
321
+ provider: "OpenStreetmap",
322
+ baseMap: true,
323
+ traffic: false,
324
+ };
325
+ }
326
+
327
+ export { VectorTileFactory };