@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
@@ -1,3 +1,4 @@
1
+ import type * as types from "./types/interfaces.js";
1
2
  import { VectorTileFactory } from "./core/VectorTileFactory.js";
2
3
  import { RENDERING_CONFIG_DEFAULT } from "./core/rendering_config_default.js";
3
4
  import { RENDERING_CONFIG_GRAYSCALE } from "./core/rendering_config_grayscale.js";
@@ -15,4 +16,5 @@ export {
15
16
  MapTripLeafletGridLayer,
16
17
  MapTripGoogleMapType,
17
18
  MapTripOpenLayersSource,
19
+ types,
18
20
  };
@@ -0,0 +1,147 @@
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
+
17
+ export interface Renderer {
18
+ from: number;
19
+ to: number;
20
+ text?: {
21
+ size: number;
22
+ color: string;
23
+ font: string;
24
+ fontWeight?: string;
25
+ outlineColor?: string;
26
+ outlineWidth?: number;
27
+ fontStyle?: string;
28
+ };
29
+ polygon?: PolygonStyle;
30
+ line?: PolylineStyle;
31
+ image?: {
32
+ url: string;
33
+ width: number;
34
+ height: number;
35
+ dx: number;
36
+ dy: number;
37
+ };
38
+ filters?: Filter[];
39
+ [key: string]: any;
40
+ }
41
+
42
+ export interface Filter {
43
+ interface: string;
44
+ key: string;
45
+ value?: any;
46
+ type: string;
47
+ }
48
+
49
+ export interface RENDERING_CONFIG {
50
+ background: {
51
+ from: number;
52
+ to: number;
53
+ color: string;
54
+ }[];
55
+ layers: { [key: string]: string };
56
+ renderers: { [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
+
69
+ export interface PolylineStyle {
70
+ color?: string | undefined;
71
+ width?: number | undefined;
72
+ url?: string | undefined;
73
+ height?: number | undefined;
74
+ dx?: number | undefined;
75
+ dy?: number | undefined;
76
+ direction?: string | undefined;
77
+ }
78
+
79
+ export interface PolygonStyle {
80
+ color: string;
81
+ opacity?: number | undefined;
82
+ outlineColor?: string | undefined;
83
+ outlineWidth?: number | undefined;
84
+ url: string;
85
+ font: string;
86
+ fontStyle?: string | undefined;
87
+ fontWeight?: string | undefined;
88
+ size?: number | undefined;
89
+ }
90
+
91
+ export interface ImageStyle {
92
+ url: string;
93
+ width: number;
94
+ height: number;
95
+ dx: number;
96
+ dy: number;
97
+ }
98
+
99
+ export interface Tile {
100
+ x: number;
101
+ y: number;
102
+ z: number;
103
+ urls: string[];
104
+ token: string;
105
+ json?: JSON;
106
+ }
107
+
108
+ export interface TileQueueItem {
109
+ tile: Tile;
110
+ successCallback: SuccessCallback;
111
+ errorCallback: ErrorCallback;
112
+ }
113
+
114
+ export interface PointXY {
115
+ x: number;
116
+ y: number;
117
+ z?: number;
118
+ }
119
+
120
+ export interface Vertex {
121
+ x: number;
122
+ y: number;
123
+ }
124
+
125
+ export interface PositionXY {
126
+ x: number;
127
+ y: number;
128
+ angle: number;
129
+ }
130
+
131
+ export interface Coordinates {
132
+ lon: number;
133
+ lat: number;
134
+ }
135
+
136
+ export interface Boundaries {
137
+ min: Coordinates;
138
+ max: Coordinates;
139
+ }
140
+
141
+ export interface SuccessCallback {
142
+ (json: any): void;
143
+ }
144
+
145
+ export interface ErrorCallback {
146
+ (error: any): void;
147
+ }
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@maptrip/map-tiles",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "API to use MapTrip map tiles in other map APIs",
5
5
  "keywords": [
6
6
  "map",
7
- "mapping"
7
+ "mapping",
8
+ "typescript",
9
+ "vector tiles"
8
10
  ],
9
11
  "author": "infoware GmbH",
10
12
  "license": "ISC",
@@ -13,33 +15,36 @@
13
15
  "dist",
14
16
  "lib"
15
17
  ],
16
- "main": "./dist/map-tiles.js",
17
- "exports": {
18
- ".": {
19
- "import": "./dist/map-tiles.js"
20
- }
21
- },
18
+ "main": "dist/map-tiles.js",
19
+ "types": "dist/map-tiles.d.ts",
22
20
  "scripts": {
23
21
  "test": "echo \"Error: no test specified\" && exit 1",
24
22
  "dev": "vite src",
25
- "build": "vite build",
26
- "prepublishOnly": "npm run build"
23
+ "prepublish": "tsc",
24
+ "build": "tsc && vite build"
27
25
  },
28
26
  "devDependencies": {
29
- "vite": "^4.5.0",
27
+ "@googlemaps/js-api-loader": "^1.16.2",
28
+ "@types/google.maps": "^3.55.4",
29
+ "@types/leaflet": "^1.9.8",
30
+ "@types/rtree": "^1.4.30",
30
31
  "eslint": "^8.52.0",
31
32
  "leaflet": "~1.9.x",
32
33
  "ol": "~8.2.x",
33
- "@googlemaps/js-api-loader": "^1.16.2"
34
+ "typescript": "^5.4.5",
35
+ "vite": "^4.5.0"
34
36
  },
35
37
  "dependencies": {
38
+ "@types/geojson": "^7946.0.14",
39
+ "@types/webfontloader": "^1.6.38",
36
40
  "rtree": "^1.4.2",
41
+ "vite-plugin-dts": "^3.9.0",
37
42
  "webfontloader": "^1.6.28"
38
43
  },
39
44
  "peerDependencies": {
45
+ "@googlemaps/js-api-loader": "^1.16.2",
40
46
  "leaflet": "~1.9.x",
41
- "ol": "~8.2.x",
42
- "@googlemaps/js-api-loader": "^1.16.2"
47
+ "ol": "~8.2.x"
43
48
  },
44
49
  "directories": {
45
50
  "lib": "lib"
@@ -1,25 +0,0 @@
1
- import { VectorTileFactory } from "../../core/VectorTileFactory";
2
- import { MIN_ZOOM, MAX_ZOOM } from "../../core/constants";
3
-
4
- class MapTripGoogleMapType {
5
- tileSize = new google.maps.Size(256, 256);
6
- alt = null;
7
- maxZoom = MAX_ZOOM;
8
- minZoom = MIN_ZOOM;
9
- projection = null;
10
- radius = 6378137;
11
-
12
- constructor(properties) {
13
- this.name = properties.name ? properties.name : 'infoware';
14
- this.token = properties.token;
15
- this.factory = VectorTileFactory.createFactory(properties);
16
- }
17
-
18
- getTile(coord, zoom) {
19
- return this.factory.getJsonTile(zoom, coord.x, coord.y, this.token);
20
- }
21
-
22
- releaseTile() {}
23
- }
24
-
25
- export { MapTripGoogleMapType };
@@ -1,24 +0,0 @@
1
- import L from "leaflet";
2
-
3
- import { VectorTileFactory } from "../../core/VectorTileFactory";
4
- import { MIN_ZOOM, MAX_ZOOM, ATTRIBUTION } from "../../core/constants";
5
-
6
- const MapTripLeafletGridLayer = L.GridLayer.extend({
7
-
8
- MIN_ZOOM: MIN_ZOOM,
9
- MAX_ZOOM: MAX_ZOOM,
10
-
11
- initialize: function(options) {
12
- this.token = options.token;
13
- this.options.attribution = options.attribution ? options.attribution : ATTRIBUTION;
14
- this.options.minZoom = options.minZoom ? Math.max(this.MIN_ZOOM, options.minZoom) : this.MIN_ZOOM;
15
- this.options.maxZoom = options.maxZoom ? Math.min(this.MAX_ZOOM, options.maxZoom) : this.MAX_ZOOM;
16
- this.factory = VectorTileFactory.createFactory(options);
17
- },
18
-
19
- createTile: function (coords) {
20
- return this.factory.getJsonTile(coords.z, coords.x, coords.y, this.token);
21
- }
22
- });
23
-
24
- export { MapTripLeafletGridLayer };
@@ -1,43 +0,0 @@
1
- import XYZ from 'ol/source/XYZ.js';
2
- import { createXYZ, extentFromProjection } from 'ol/tilegrid.js';
3
-
4
- import { VectorTileFactory } from "../../core/VectorTileFactory";
5
- import { MIN_ZOOM, MAX_ZOOM, ATTRIBUTION } from "../../core/constants";
6
-
7
- class MapTripOpenLayersSource extends XYZ {
8
- constructor(properties) {
9
-
10
- const options = {};
11
- const projection = 'EPSG:3857';
12
-
13
- const tileGrid = createXYZ({
14
- extent: extentFromProjection(projection),
15
- maxResolution: options.maxResolution,
16
- minZoom: MIN_ZOOM,
17
- maxZoom: MAX_ZOOM,
18
- tileSize: [256, 256]
19
- });
20
-
21
- super({
22
- opaque: true,
23
- minZoom: MIN_ZOOM,
24
- maxZoom: MAX_ZOOM,
25
- projection: projection,
26
- tileGrid: tileGrid,
27
- wrapX: true,
28
- url: 'z:{z} x:{x} y:{y}',
29
- tileLoadFunction: (tile) => {
30
- let coord = tile.getTileCoord();
31
- let canvas = this.factory.getJsonTile(coord[0], coord[1], coord[2], this.token, () => {
32
- tile.setImage(canvas);
33
- })
34
- },
35
- attributions: ATTRIBUTION
36
- });
37
-
38
- this.token = properties.token;
39
- this.factory = VectorTileFactory.createFactory(properties);
40
- }
41
- }
42
-
43
- export { MapTripOpenLayersSource };
@@ -1,65 +0,0 @@
1
- import RTree from 'rtree';
2
-
3
- class CollisionDetector {
4
- constructor() {
5
- this._tree = RTree(10);
6
- }
7
-
8
- insert(x, y, w, h) {
9
- this._tree.insert({ x: x, y: y, w: w, h: h}, 0);
10
- }
11
-
12
- insertPolylineText(context, text, position) {
13
- let widthUsed = 0;
14
-
15
- for (let i = 0; i <= text.length; i++)
16
- {
17
- let box = this.getCollisionParams(context, text.charAt(i), position, widthUsed);
18
- this.insert(box.x, box.y, box.w, box.h);
19
-
20
- widthUsed += context.measureText(text.charAt(i)).width;
21
- }
22
- }
23
-
24
- isCollision(x, y, w, h) {
25
- return this._tree.search({ x: x, y: y, w: w, h: h }).length > 0;
26
- }
27
-
28
- isPolylineTextCollision(context, text, position) {
29
- let border = 5;
30
- let widthUsed = 0;
31
-
32
- for (let i = 0; i <= text.length; i++)
33
- {
34
- var box = this.getCollisionParams(context, text.charAt(i), position, widthUsed);
35
-
36
- if (box.x < border || box.y < border || box.x + box.w > 256 - border || box.y + box.h > 256 - border)
37
- {
38
- return true;
39
- }
40
-
41
- if (this.isCollision(box.x, box.y, box.w, box.h))
42
- {
43
- return true;
44
- }
45
-
46
- widthUsed += context.measureText(text.charAt(i)).width;
47
- }
48
-
49
- return false;
50
- }
51
-
52
- getCollisionParams(context, text, position) {
53
- let textWidth = context.measureText(text).width;
54
- let textHeight = context.measureText(text.charAt(0)).width * 1.5;
55
-
56
- return {
57
- x: position.x + 0.5 * Math.cos(position.angle) * textWidth,
58
- y: position.y + 0.5 * Math.sin(position.angle) * textWidth,
59
- w: Math.abs(Math.cos(position.angle) * textWidth) + Math.abs(Math.sin(position.angle) * textHeight),
60
- h: Math.abs(Math.sin(position.angle) * textWidth) + Math.abs(Math.cos(position.angle) * textHeight)
61
- }
62
- }
63
- }
64
-
65
- export { CollisionDetector };
@@ -1,245 +0,0 @@
1
- import { VectorTileLoader } from './VectorTileLoader.js';
2
- import { VectorTileRenderer } from './VectorTileRenderer.js';
3
-
4
- import { URL_TEMPLATE, PROVIDER_PROJECTS, PROVIDER_TRAFFIC } from './constants.js';
5
-
6
- import { RENDERING_CONFIG_DEFAULT } from './rendering_config_default.js';
7
- import { RENDERING_CONFIG_TRAFFIC } from './rendering_config_traffic.js';
8
-
9
- import WebFont from 'webfontloader';
10
-
11
- /**
12
- * Creates a factory to create tiles from the provided URL templates.
13
- *
14
- * The templates must have variables ${x}, ${y} and ${z} for the tile index.
15
- * When multiple URLs are specified, they are used round robin.
16
- */
17
- class VectorTileFactory {
18
-
19
- static _fonts = undefined;
20
-
21
- constructor(urls, additionalUrls, renderingConfig, properties)
22
- {
23
- this._urls = urls;
24
- this._additionalUrls = additionalUrls;
25
- this._layers = ['all']; // could be used to load just a selection of layers from the data server
26
-
27
- this._tileLoader = new VectorTileLoader(properties);
28
- this._renderer = new VectorTileRenderer(renderingConfig);
29
-
30
- if (!VectorTileFactory._fonts) {
31
- //console.log(this._renderer.findConfiguredFonts()); // TODO create families string like Open Sans:n4,n7,i4,i7
32
-
33
- WebFont.load({
34
- custom: {
35
- families: ['Open Sans:n4,n7,i4,i7'],
36
- urls: ['https://maps.infoware.de/MapAPI-1.4/css/opensans.css']
37
- },
38
- loading: function() {
39
- VectorTileFactory._fonts = 'loading';
40
- },
41
- active: function() {
42
- VectorTileFactory._fonts = 'active';
43
- this._tileLoader.loadNext();
44
- }.bind(this),
45
- inactive: function() {
46
- // font loading failed
47
- VectorTileFactory._fonts = 'active';
48
- this._tileLoader.loadNext();
49
- }.bind(this)
50
- });
51
- }
52
- }
53
-
54
- static createFactory(properties) {
55
- // apply default properties
56
- this._properties = properties ? { ...VectorTileFactory.DEFAULT_PROPERTIES, ...properties } : VectorTileFactory.DEFAULT_PROPERTIES;
57
- this._properties.provider = this._properties.provider.toLowerCase();
58
-
59
- // assert the provider is valid
60
- if (!PROVIDER_PROJECTS[this._properties.provider]) {
61
- throw new Error('Unknown provider');
62
- }
63
-
64
- // assert there is no traffic requested for a provider which does not provide traffic information
65
- if (this._properties.traffic && !PROVIDER_TRAFFIC[this._properties.provider]) {
66
- throw new Error('There is now traffic information for this provider');
67
- }
68
-
69
- // assert there is either a base map or traffic (or both)
70
- if (!this._properties.baseMap && !this._properties.traffic) {
71
- throw new Error('You cannot create a factory with neither a base map nor traffic');
72
- }
73
-
74
- return new VectorTileFactory(
75
- VectorTileFactory.getUrlTemplatesForBaseLayer(this._properties),
76
- VectorTileFactory.getUrlTemplatesForTrafficLayer(this._properties),
77
- VectorTileFactory.getRenderingConfig(this._properties),
78
- this._properties);
79
- }
80
-
81
- static getUrlTemplatesForBaseLayer(properties) {
82
- if (properties.baseMap) {
83
- let template = URL_TEMPLATE.replace('${project}', PROVIDER_PROJECTS[properties.provider]);
84
- return [0, 1, 2, 3].map(index => template.replace('${index}', index));
85
- }
86
- else {
87
- return [];
88
- }
89
- }
90
-
91
- static getUrlTemplatesForTrafficLayer(properties) {
92
- return properties.traffic ? [PROVIDER_TRAFFIC[properties.provider]] : [];
93
- }
94
-
95
- static getRenderingConfig(properties) {
96
- if (properties.renderingConfig) {
97
- return properties.renderingConfig;
98
- }
99
- else if (properties.baseMap) {
100
- return RENDERING_CONFIG_DEFAULT;
101
- }
102
- else {
103
- return RENDERING_CONFIG_TRAFFIC;
104
- }
105
- }
106
-
107
- getJsonTile(z, x, y, token, callback) {
108
- let maximumIndex = Math.pow(2, z);
109
- x = (x + 16 * maximumIndex) % maximumIndex;
110
- y = (y + 16 * maximumIndex) % maximumIndex;
111
-
112
- let canvas = document.createElement('canvas');
113
- canvas.width = 256;
114
- canvas.height = 256;
115
-
116
- let baseMapUrl = this.resolveTileUrl(z, x, y);
117
- let urls = baseMapUrl ? [ baseMapUrl ] : [];
118
-
119
- // add URLs for additional data like traffic flows
120
- this._additionalUrls.forEach(it => {
121
- urls.push(it.replace('${x}', x).replace('${y}', y).replace(/\$\{z\}/g, z));
122
- });
123
-
124
- let tileLoadedSuccessfullyCallback = function(json) {
125
- this.drawTile(json, z, x, y, canvas);
126
-
127
- if (callback) {
128
- callback(canvas);
129
- }
130
- }.bind(this);
131
-
132
- let tile_FailedCallback = function() {
133
- this.drawErroredTile(canvas);
134
-
135
- if (callback) {
136
- callback(null);
137
- }
138
- }.bind(this);
139
-
140
- this._tileLoader.queue(
141
- { x: x, y: y, z: z, urls: urls, token: token },
142
- tileLoadedSuccessfullyCallback,
143
- tile_FailedCallback
144
- );
145
-
146
- if (VectorTileFactory._fonts === 'active') {
147
- this._tileLoader.loadNext();
148
- }
149
-
150
- return canvas;
151
- }
152
-
153
- /**
154
- * @private
155
- */
156
- resolveTileUrl(z, x, y) {
157
- if (this._urls.length > 0) {
158
- var index = (x + y + z) % this._urls.length;
159
-
160
- return this._urls[index]
161
- .replace('${x}', x)
162
- .replace('${y}', y)
163
- .replace(/\$\{z\}/g, z)
164
- .replace('${layers}', this._layers.join(','));
165
- }
166
- else {
167
- // there is no base map layer
168
- return null;
169
- }
170
- }
171
-
172
- /**
173
- * @private
174
- */
175
- tileIndexToLatLong(z, x, y) {
176
- var n = Math.PI - 2 * Math.PI * y / Math.pow(2, z);
177
-
178
- return {
179
- lat: (180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))),
180
- lon: (x / Math.pow(2, z) * 360 - 180)
181
- };
182
- }
183
-
184
- /**
185
- * @private
186
- */
187
- getTileBoundaries(z, x, y) {
188
- return {
189
- min: this.tileIndexToLatLong(z, x, y),
190
- max: this.tileIndexToLatLong(z, x + 1, y + 1)
191
- }
192
- }
193
-
194
- /**
195
- * @private
196
- */
197
- drawTile(json, z, x, y, canvas) {
198
- var features = this.getFeatures(json).sort(function(a, b) {
199
- if (a.properties && a.properties.sort_rank) {
200
- if (b.properties && b.properties.sort_rank) {
201
- return a.properties.sort_rank - b.properties.sort_rank;
202
- }
203
- else {
204
- return -1;
205
- }
206
- }
207
- else {
208
- if (b.properties && b.properties.sort_rank) {
209
- return 1;
210
- }
211
- else {
212
- return 0;
213
- }
214
- }
215
- });
216
-
217
- this._renderer.drawTile(features, z, this.getTileBoundaries(z, x, y), canvas.getContext('2d'));
218
- }
219
-
220
- /**
221
- * Draws an error message on the tile.
222
- *
223
- * @param canvas
224
- */
225
- drawErroredTile(canvas) {
226
- this._renderer.drawErroredTile(canvas);
227
- }
228
-
229
- /**
230
- * @private
231
- */
232
- getFeatures(json) {
233
- if (json.type && json.type == 'FeatureCollection') {
234
- return json.features;
235
- }
236
- }
237
-
238
- static DEFAULT_PROPERTIES = {
239
- provider: 'OpenStreetmap',
240
- baseMap: true,
241
- traffic: false
242
- };
243
- }
244
-
245
- export { VectorTileFactory };