@loaders.gl/mvt 4.2.1 → 4.3.0-alpha.2

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 (120) hide show
  1. package/dist/dist.dev.js +1784 -847
  2. package/dist/dist.min.js +1 -1
  3. package/dist/index.cjs +713 -640
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/lib/parse-mvt.d.ts +2 -1
  9. package/dist/lib/parse-mvt.d.ts.map +1 -1
  10. package/dist/lib/parse-mvt.js +15 -45
  11. package/dist/lib/parse-tilejson.d.ts +4 -4
  12. package/dist/lib/parse-tilejson.d.ts.map +1 -1
  13. package/dist/lib/types.d.ts +1 -39
  14. package/dist/lib/types.d.ts.map +1 -1
  15. package/dist/lib/utils/geometry-utils.d.ts +83 -0
  16. package/dist/lib/utils/geometry-utils.d.ts.map +1 -0
  17. package/dist/lib/utils/geometry-utils.js +206 -0
  18. package/dist/lib/vector-tile/vector-tile-feature.d.ts +72 -0
  19. package/dist/lib/vector-tile/vector-tile-feature.d.ts.map +1 -0
  20. package/dist/lib/vector-tile/vector-tile-feature.js +342 -0
  21. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.d.ts +10 -1
  22. package/dist/lib/vector-tile/vector-tile-layer.d.ts.map +1 -0
  23. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.js +16 -1
  24. package/dist/lib/vector-tile/vector-tile.d.ts.map +1 -0
  25. package/dist/lib/{geojson-tiler → vector-tiler}/clip.d.ts +3 -3
  26. package/dist/lib/vector-tiler/clip.d.ts.map +1 -0
  27. package/dist/lib/vector-tiler/convert.d.ts +18 -0
  28. package/dist/lib/vector-tiler/convert.d.ts.map +1 -0
  29. package/dist/lib/{geojson-tiler → vector-tiler}/convert.js +8 -1
  30. package/dist/lib/vector-tiler/feature.d.ts +3 -0
  31. package/dist/lib/vector-tiler/feature.d.ts.map +1 -0
  32. package/dist/lib/vector-tiler/simplify.d.ts.map +1 -0
  33. package/dist/lib/{geojson-tiler → vector-tiler}/tile.d.ts +4 -4
  34. package/dist/lib/vector-tiler/tile.d.ts.map +1 -0
  35. package/dist/lib/vector-tiler/transform.d.ts +7 -0
  36. package/dist/lib/vector-tiler/transform.d.ts.map +1 -0
  37. package/dist/lib/{geojson-tiler → vector-tiler}/wrap.d.ts +2 -2
  38. package/dist/lib/vector-tiler/wrap.d.ts.map +1 -0
  39. package/dist/mvt-loader.d.ts +19 -8
  40. package/dist/mvt-loader.d.ts.map +1 -1
  41. package/dist/mvt-loader.js +3 -2
  42. package/dist/mvt-source.d.ts +4 -4
  43. package/dist/mvt-source.d.ts.map +1 -1
  44. package/dist/mvt-source.js +7 -7
  45. package/dist/mvt-worker.js +1244 -762
  46. package/dist/table-tile-source.d.ts +118 -0
  47. package/dist/table-tile-source.d.ts.map +1 -0
  48. package/dist/table-tile-source.js +370 -0
  49. package/dist/tilejson-loader.js +1 -1
  50. package/package.json +7 -6
  51. package/src/index.ts +3 -3
  52. package/src/lib/parse-mvt.ts +28 -66
  53. package/src/lib/parse-tilejson.ts +5 -5
  54. package/src/lib/types.ts +1 -39
  55. package/src/lib/utils/geometry-utils.ts +234 -0
  56. package/src/lib/vector-tile/vector-tile-feature.ts +416 -0
  57. package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.ts +28 -2
  58. package/src/lib/{geojson-tiler → vector-tiler}/clip.ts +4 -4
  59. package/src/lib/{geojson-tiler → vector-tiler}/convert.ts +19 -9
  60. package/src/lib/{geojson-tiler → vector-tiler}/feature.ts +3 -3
  61. package/src/lib/{geojson-tiler → vector-tiler}/tile.ts +10 -10
  62. package/src/lib/{geojson-tiler → vector-tiler}/transform.ts +2 -2
  63. package/src/lib/{geojson-tiler → vector-tiler}/wrap.ts +5 -5
  64. package/src/mvt-loader.ts +14 -6
  65. package/src/mvt-source.ts +9 -10
  66. package/src/table-tile-source.ts +508 -0
  67. package/dist/helpers/binary-util-functions.d.ts +0 -35
  68. package/dist/helpers/binary-util-functions.d.ts.map +0 -1
  69. package/dist/helpers/binary-util-functions.js +0 -114
  70. package/dist/helpers/mapbox-util-functions.d.ts +0 -29
  71. package/dist/helpers/mapbox-util-functions.d.ts.map +0 -1
  72. package/dist/helpers/mapbox-util-functions.js +0 -78
  73. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -41
  74. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +0 -1
  75. package/dist/lib/binary-vector-tile/vector-tile-feature.js +0 -163
  76. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -23
  77. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +0 -1
  78. package/dist/lib/binary-vector-tile/vector-tile-layer.js +0 -96
  79. package/dist/lib/binary-vector-tile/vector-tile.d.ts +0 -9
  80. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +0 -1
  81. package/dist/lib/binary-vector-tile/vector-tile.js +0 -27
  82. package/dist/lib/geojson-tiler/clip.d.ts.map +0 -1
  83. package/dist/lib/geojson-tiler/convert.d.ts +0 -10
  84. package/dist/lib/geojson-tiler/convert.d.ts.map +0 -1
  85. package/dist/lib/geojson-tiler/feature.d.ts +0 -3
  86. package/dist/lib/geojson-tiler/feature.d.ts.map +0 -1
  87. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +0 -44
  88. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +0 -1
  89. package/dist/lib/geojson-tiler/geojson-tiler.js +0 -209
  90. package/dist/lib/geojson-tiler/simplify.d.ts.map +0 -1
  91. package/dist/lib/geojson-tiler/tile.d.ts.map +0 -1
  92. package/dist/lib/geojson-tiler/transform.d.ts +0 -7
  93. package/dist/lib/geojson-tiler/transform.d.ts.map +0 -1
  94. package/dist/lib/geojson-tiler/wrap.d.ts.map +0 -1
  95. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -27
  96. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +0 -1
  97. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +0 -178
  98. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +0 -1
  99. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +0 -1
  100. package/src/helpers/binary-util-functions.ts +0 -125
  101. package/src/helpers/mapbox-util-functions.ts +0 -82
  102. package/src/lib/binary-vector-tile/vector-tile-feature.ts +0 -203
  103. package/src/lib/binary-vector-tile/vector-tile-layer.ts +0 -113
  104. package/src/lib/binary-vector-tile/vector-tile.ts +0 -36
  105. package/src/lib/geojson-tiler/geojson-tiler.ts +0 -283
  106. package/src/lib/mapbox-vector-tile/LICENSE.txt +0 -31
  107. package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +0 -207
  108. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.d.ts +0 -0
  109. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.js +0 -0
  110. /package/dist/lib/{geojson-tiler → vector-tiler}/clip.js +0 -0
  111. /package/dist/lib/{geojson-tiler → vector-tiler}/feature.js +0 -0
  112. /package/dist/lib/{geojson-tiler → vector-tiler}/simplify.d.ts +0 -0
  113. /package/dist/lib/{geojson-tiler → vector-tiler}/simplify.js +0 -0
  114. /package/dist/lib/{geojson-tiler → vector-tiler}/tile.js +0 -0
  115. /package/dist/lib/{geojson-tiler → vector-tiler}/transform.js +0 -0
  116. /package/dist/lib/{geojson-tiler → vector-tiler}/wrap.js +0 -0
  117. /package/src/lib/{binary-vector-tile → vector-tile}/LICENSE.txt +0 -0
  118. /package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile.ts +0 -0
  119. /package/src/lib/{geojson-tiler → vector-tiler}/LICENSE +0 -0
  120. /package/src/lib/{geojson-tiler → vector-tiler}/simplify.ts +0 -0
@@ -1,203 +0,0 @@
1
- // loaders.gl
2
- // SPDX-License-Identifier: MIT
3
- // Copyright vis.gl contributors
4
-
5
- // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
6
-
7
- import Protobuf from 'pbf';
8
- import {FlatFeature, FlatIndexedGeometry, GeojsonGeometryInfo} from '@loaders.gl/schema';
9
- import {classifyRings, project, readFeature} from '../../helpers/binary-util-functions';
10
-
11
- // Reduce GC by reusing variables
12
- let endPos: number;
13
- let cmd: number;
14
- let cmdLen: number;
15
- let length: number;
16
- let x: number;
17
- let y: number;
18
- let i: number;
19
-
20
- export const TEST_EXPORTS = {
21
- classifyRings
22
- };
23
-
24
- export class BinaryVectorTileFeature {
25
- properties: {[x: string]: string | number | boolean | null};
26
- extent: any;
27
- type: number;
28
- id: number | null;
29
- _pbf: Protobuf;
30
- _geometry: number;
31
- _keys: string[];
32
- _values: (string | number | boolean | null)[];
33
- _geometryInfo: GeojsonGeometryInfo;
34
-
35
- // eslint-disable-next-line max-params
36
- constructor(
37
- pbf: Protobuf,
38
- end: number,
39
- extent: any,
40
- keys: string[],
41
- values: (string | number | boolean | null)[],
42
- geometryInfo: GeojsonGeometryInfo
43
- ) {
44
- // Public
45
- this.properties = {};
46
- this.extent = extent;
47
- this.type = 0;
48
- this.id = null;
49
-
50
- // Private
51
- this._pbf = pbf;
52
- this._geometry = -1;
53
- this._keys = keys;
54
- this._values = values;
55
- this._geometryInfo = geometryInfo;
56
-
57
- pbf.readFields(readFeature, this, end);
58
- }
59
-
60
- // eslint-disable-next-line complexity, max-statements
61
- loadGeometry(): FlatIndexedGeometry {
62
- const pbf = this._pbf;
63
- pbf.pos = this._geometry;
64
-
65
- endPos = pbf.readVarint() + pbf.pos;
66
- cmd = 1;
67
- length = 0;
68
- x = 0;
69
- y = 0;
70
- i = 0;
71
-
72
- // Note: I attempted to replace the `data` array with a
73
- // Float32Array, but performance was worse, both using
74
- // `set()` and direct index access. Also, we cannot
75
- // know how large the buffer should be, so it would
76
- // increase memory usage
77
- const indices: number[] = []; // Indices where geometries start
78
- const data: number[] = []; // Flat array of coordinate data
79
-
80
- while (pbf.pos < endPos) {
81
- if (length <= 0) {
82
- cmdLen = pbf.readVarint();
83
- cmd = cmdLen & 0x7;
84
- length = cmdLen >> 3;
85
- }
86
-
87
- length--;
88
-
89
- if (cmd === 1 || cmd === 2) {
90
- x += pbf.readSVarint();
91
- y += pbf.readSVarint();
92
-
93
- if (cmd === 1) {
94
- // New line
95
- indices.push(i);
96
- }
97
- data.push(x, y);
98
- i += 2;
99
- } else if (cmd === 7) {
100
- // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
101
- if (i > 0) {
102
- const start = indices[indices.length - 1]; // start index of polygon
103
- data.push(data[start], data[start + 1]); // closePolygon
104
- i += 2;
105
- }
106
- } else {
107
- throw new Error(`unknown command ${cmd}`);
108
- }
109
- }
110
-
111
- return {data, indices};
112
- }
113
-
114
- /**
115
- *
116
- * @param transform
117
- * @returns result
118
- */
119
- _toBinaryCoordinates(transform) {
120
- // Expands the protobuf data to an intermediate Flat GeoJSON
121
- // data format, which maps closely to the binary data buffers.
122
- // It is similar to GeoJSON, but rather than storing the coordinates
123
- // in multidimensional arrays, we have a 1D `data` with all the
124
- // coordinates, and then index into this using the `indices`
125
- // parameter, e.g.
126
- //
127
- // geometry: {
128
- // type: 'Point', data: [1,2], indices: [0]
129
- // }
130
- // geometry: {
131
- // type: 'LineString', data: [1,2,3,4,...], indices: [0]
132
- // }
133
- // geometry: {
134
- // type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
135
- // }
136
- // Thus the indices member lets us look up the relevant range
137
- // from the data array.
138
- // The Multi* versions of the above types share the same data
139
- // structure, just with multiple elements in the indices array
140
- const geom = this.loadGeometry();
141
- let geometry;
142
-
143
- // Apply the supplied transformation to data
144
- transform(geom.data, this);
145
-
146
- const coordLength = 2;
147
-
148
- // eslint-disable-next-line default-case
149
- switch (this.type) {
150
- case 1: // Point
151
- this._geometryInfo.pointFeaturesCount++;
152
- this._geometryInfo.pointPositionsCount += geom.indices.length;
153
- geometry = {type: 'Point', ...geom};
154
- break;
155
-
156
- case 2: // LineString
157
- this._geometryInfo.lineFeaturesCount++;
158
- this._geometryInfo.linePathsCount += geom.indices.length;
159
- this._geometryInfo.linePositionsCount += geom.data.length / coordLength;
160
- geometry = {type: 'LineString', ...geom};
161
- break;
162
-
163
- case 3: // Polygon
164
- geometry = classifyRings(geom);
165
-
166
- // Unlike Point & LineString geom.indices is a 2D array, thanks
167
- // to the classifyRings method
168
- this._geometryInfo.polygonFeaturesCount++;
169
- this._geometryInfo.polygonObjectsCount += geometry.indices.length;
170
-
171
- for (const indices of geometry.indices) {
172
- this._geometryInfo.polygonRingsCount += indices.length;
173
- }
174
- this._geometryInfo.polygonPositionsCount += geometry.data.length / coordLength;
175
-
176
- break;
177
- default:
178
- throw new Error(`Invalid geometry type: ${this.type}`);
179
- }
180
-
181
- const result: FlatFeature = {type: 'Feature', geometry, properties: this.properties};
182
-
183
- if (this.id !== null) {
184
- result.id = this.id;
185
- }
186
-
187
- return result;
188
- }
189
-
190
- toBinaryCoordinates(
191
- options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
192
- ): FlatFeature {
193
- if (typeof options === 'function') {
194
- return this._toBinaryCoordinates(options);
195
- }
196
- const {x, y, z} = options;
197
- const size = this.extent * Math.pow(2, z);
198
- const x0 = this.extent * x;
199
- const y0 = this.extent * y;
200
-
201
- return this._toBinaryCoordinates((data: number[]) => project(data, x0, y0, size));
202
- }
203
- }
@@ -1,113 +0,0 @@
1
- // loaders.gl
2
- // SPDX-License-Identifier: MIT
3
- // Copyright vis.gl contributors
4
-
5
- /* eslint-disable indent */
6
- // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
7
-
8
- import {BinaryVectorTileFeature} from './vector-tile-feature';
9
- import Protobuf from 'pbf';
10
- import {GeojsonGeometryInfo} from '@loaders.gl/schema';
11
-
12
- export class BinaryVectorTileLayer {
13
- version: number;
14
- name: string;
15
- extent: number;
16
- length: number;
17
- _pbf: Protobuf;
18
- _keys: string[];
19
- _values: (string | number | boolean | null)[];
20
- _features: number[];
21
- constructor(pbf: Protobuf, end: number) {
22
- // Public
23
- this.version = 1;
24
- this.name = '';
25
- this.extent = 4096;
26
- this.length = 0;
27
-
28
- // Private
29
- this._pbf = pbf;
30
- this._keys = [];
31
- this._values = [];
32
- this._features = [];
33
-
34
- pbf.readFields(readLayer, this, end);
35
-
36
- this.length = this._features.length;
37
- }
38
-
39
- /**
40
- * return feature `i` from this layer as a `BinaryVectorTileFeature`
41
- *
42
- * @param index
43
- * @param geometryInfo
44
- * @returns {BinaryVectorTileFeature}
45
- */
46
- feature(i: number, geometryInfo: GeojsonGeometryInfo): BinaryVectorTileFeature {
47
- if (i < 0 || i >= this._features.length) {
48
- throw new Error('feature index out of bounds');
49
- }
50
-
51
- this._pbf.pos = this._features[i];
52
-
53
- const end = this._pbf.readVarint() + this._pbf.pos;
54
- return new BinaryVectorTileFeature(
55
- this._pbf,
56
- end,
57
- this.extent,
58
- this._keys,
59
- this._values,
60
- geometryInfo
61
- );
62
- }
63
- }
64
-
65
- /**
66
- *
67
- * @param tag
68
- * @param layer
69
- * @param pbf
70
- */
71
- function readLayer(tag: number, layer?: BinaryVectorTileLayer, pbf?: Protobuf): void {
72
- if (layer && pbf) {
73
- if (tag === 15) layer.version = pbf.readVarint();
74
- else if (tag === 1) layer.name = pbf.readString();
75
- else if (tag === 5) layer.extent = pbf.readVarint();
76
- else if (tag === 2) layer._features.push(pbf.pos);
77
- else if (tag === 3) layer._keys.push(pbf.readString());
78
- else if (tag === 4) layer._values.push(readValueMessage(pbf));
79
- }
80
- }
81
-
82
- /**
83
- *
84
- * @param pbf
85
- * @returns value
86
- */
87
- function readValueMessage(pbf: Protobuf) {
88
- let value: string | number | boolean | null = null;
89
- const end = pbf.readVarint() + pbf.pos;
90
-
91
- while (pbf.pos < end) {
92
- const tag = pbf.readVarint() >> 3;
93
-
94
- value =
95
- tag === 1
96
- ? pbf.readString()
97
- : tag === 2
98
- ? pbf.readFloat()
99
- : tag === 3
100
- ? pbf.readDouble()
101
- : tag === 4
102
- ? pbf.readVarint64()
103
- : tag === 5
104
- ? pbf.readVarint()
105
- : tag === 6
106
- ? pbf.readSVarint()
107
- : tag === 7
108
- ? pbf.readBoolean()
109
- : null;
110
- }
111
-
112
- return value;
113
- }
@@ -1,36 +0,0 @@
1
- // loaders.gl
2
- // SPDX-License-Identifier: MIT
3
- // Copyright vis.gl contributors
4
-
5
- // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
6
-
7
- import {BinaryVectorTileLayer} from './vector-tile-layer';
8
- import Protobuf from 'pbf';
9
-
10
- export class BinaryVectorTile {
11
- layers: {[x: string]: BinaryVectorTileLayer};
12
- constructor(pbf: Protobuf, end?: number) {
13
- this.layers = pbf.readFields(readTile, {}, end);
14
- }
15
- }
16
-
17
- /**
18
- *
19
- * @param tag
20
- * @param layers
21
- * @param pbf
22
- */
23
- function readTile(
24
- tag: number,
25
- layers?: {[x: string]: BinaryVectorTileLayer},
26
- pbf?: Protobuf
27
- ): void {
28
- if (tag === 3) {
29
- if (pbf) {
30
- const layer = new BinaryVectorTileLayer(pbf, pbf.readVarint() + pbf.pos);
31
- if (layer.length && layers) {
32
- layers[layer.name] = layer;
33
- }
34
- }
35
- }
36
- }
@@ -1,283 +0,0 @@
1
- // loaders.gl
2
- // SPDX-License-Identifier: MIT
3
- // Copyright (c) vis.gl contributors
4
- // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
5
-
6
- /* eslint-disable no-console, no-continue */
7
-
8
- import type {GeoJSONTile, GeoJSONTileFeature} from './tile';
9
-
10
- import {convert} from './convert'; // GeoJSON conversion and preprocessing
11
- import {clip} from './clip'; // stripe clipping algorithm
12
- import {wrap} from './wrap'; // date line processing
13
- import {transformTile} from './transform'; // coordinate transformation
14
- import {createTile} from './tile'; // final simplified tile generation
15
-
16
- /** Options to configure tiling */
17
- export type GeoJSONTilerOptions = {
18
- maxZoom?: number /** max zoom to preserve detail on */;
19
- indexMaxZoom?: number /** max zoom in the tile index */;
20
- indexMaxPoints?: number /** max number of points per tile in the tile index */;
21
- tolerance?: number /** simplification tolerance (higher means simpler) */;
22
- extent?: number /** tile extent */;
23
- buffer?: number /** tile buffer on each side */;
24
- lineMetrics?: boolean /** whether to calculate line metrics */;
25
- promoteId?: string /** name of a feature property to be promoted to feature.id */;
26
- generateId?: boolean /** whether to generate feature ids. Cannot be used with promoteId */;
27
- debug?: number /** logging level (0, 1 or 2) */;
28
- };
29
-
30
- const DEFAULT_OPTIONS: Required<GeoJSONTilerOptions> = {
31
- maxZoom: 14, // max zoom to preserve detail on
32
- indexMaxZoom: 5, // max zoom in the tile index
33
- indexMaxPoints: 100000, // max number of points per tile in the tile index
34
- tolerance: 3, // simplification tolerance (higher means simpler)
35
- extent: 4096, // tile extent
36
- buffer: 64, // tile buffer on each side
37
- lineMetrics: false, // whether to calculate line metrics
38
- // @ts-expect-error Ensures all these required params have defaults
39
- promoteId: undefined, // name of a feature property to be promoted to feature.id
40
- generateId: false, // whether to generate feature ids. Cannot be used with promoteId
41
- debug: 0 // logging level (0, 1 or 2)
42
- };
43
-
44
- export class GeoJSONTiler {
45
- options: Required<GeoJSONTilerOptions>;
46
-
47
- // tiles and tileCoords are part of the public API
48
- tiles: Record<string, GeoJSONTile> = {};
49
- tileCoords: {x: number; y: number; z: number}[] = [];
50
-
51
- stats: Record<string, number> = {};
52
- total: number = 0;
53
-
54
- constructor(data, options?: GeoJSONTilerOptions) {
55
- this.options = {...DEFAULT_OPTIONS, ...options};
56
- options = this.options;
57
-
58
- const debug = options.debug;
59
-
60
- if (debug) console.time('preprocess data');
61
-
62
- if (this.options.maxZoom < 0 || this.options.maxZoom > 24) {
63
- throw new Error('maxZoom should be in the 0-24 range');
64
- }
65
- if (options.promoteId && this.options.generateId) {
66
- throw new Error('promoteId and generateId cannot be used together.');
67
- }
68
-
69
- // projects and adds simplification info
70
- let features = convert(data, options);
71
-
72
- if (debug) {
73
- console.timeEnd('preprocess data');
74
- console.log(
75
- 'index: maxZoom: %d, maxPoints: %d',
76
- options.indexMaxZoom,
77
- options.indexMaxPoints
78
- );
79
- console.time('generate tiles');
80
- }
81
-
82
- // wraps features (ie extreme west and extreme east)
83
- features = wrap(features, this.options);
84
-
85
- // start slicing from the top tile down
86
- if (features.length) {
87
- this.splitTile(features, 0, 0, 0);
88
- }
89
-
90
- if (debug) {
91
- if (features.length) {
92
- console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
93
- }
94
- console.timeEnd('generate tiles');
95
- console.log('tiles generated:', this.total, JSON.stringify(this.stats));
96
- }
97
- }
98
-
99
- /**
100
- * Get a tile at the specified index
101
- * @param z
102
- * @param x
103
- * @param y
104
- * @returns
105
- */
106
- // eslint-disable-next-line complexity, max-statements
107
- getTile(z: number, x: number, y: number): GeoJSONTile | null {
108
- // z = +z;
109
- // x = +x;
110
- // y = +y;
111
-
112
- const {extent, debug} = this.options;
113
-
114
- if (z < 0 || z > 24) {
115
- return null;
116
- }
117
-
118
- const z2 = 1 << z;
119
- x = (x + z2) & (z2 - 1); // wrap tile x coordinate
120
-
121
- const id = toID(z, x, y);
122
- if (this.tiles[id]) {
123
- return transformTile(this.tiles[id], extent);
124
- }
125
-
126
- if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
127
-
128
- let z0 = z;
129
- let x0 = x;
130
- let y0 = y;
131
- let parent;
132
-
133
- while (!parent && z0 > 0) {
134
- z0--;
135
- x0 = x0 >> 1;
136
- y0 = y0 >> 1;
137
- parent = this.tiles[toID(z0, x0, y0)];
138
- }
139
-
140
- if (!parent || !parent.source) {
141
- return null;
142
- }
143
-
144
- // if we found a parent tile containing the original geometry, we can drill down from it
145
- if (debug > 1) {
146
- console.log('found parent tile z%d-%d-%d', z0, x0, y0);
147
- console.time('drilling down');
148
- }
149
- this.splitTile(parent.source, z0, x0, y0, z, x, y);
150
- if (debug > 1) {
151
- console.timeEnd('drilling down');
152
- }
153
-
154
- return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
155
- }
156
-
157
- /**
158
- * splits features from a parent tile to sub-tiles.
159
- * @param z, x, and y are the coordinates of the parent tile
160
- * @param cz, cx, and cy are the coordinates of the target tile
161
- *
162
- * If no target tile is specified, splitting stops when we reach the maximum
163
- * zoom or the number of points is low as specified in the options.
164
- */
165
- // eslint-disable-next-line max-params, max-statements, complexity
166
- splitTile(
167
- features: GeoJSONTileFeature[],
168
- z: number,
169
- x: number,
170
- y: number,
171
- cz?: number,
172
- cx?: number,
173
- cy?: number
174
- ): void {
175
- const stack: any[] = [features, z, x, y];
176
- const options = this.options;
177
- const debug = options.debug;
178
-
179
- // avoid recursion by using a processing queue
180
- while (stack.length) {
181
- y = stack.pop();
182
- x = stack.pop();
183
- z = stack.pop();
184
- features = stack.pop();
185
-
186
- const z2 = 1 << z;
187
- const id = toID(z, x, y);
188
- let tile = this.tiles[id];
189
-
190
- if (!tile) {
191
- if (debug > 1) {
192
- console.time('creation');
193
- }
194
-
195
- tile = this.tiles[id] = createTile(features, z, x, y, options);
196
- this.tileCoords.push({z, x, y});
197
-
198
- if (debug) {
199
- if (debug > 1) {
200
- console.log(
201
- 'tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
202
- z,
203
- x,
204
- y,
205
- tile.numFeatures,
206
- tile.numPoints,
207
- tile.numSimplified
208
- );
209
- console.timeEnd('creation');
210
- }
211
- const key = `z${z}`;
212
- this.stats[key] = (this.stats[key] || 0) + 1;
213
- this.total++;
214
- }
215
- }
216
-
217
- // save reference to original geometry in tile so that we can drill down later if we stop now
218
- tile.source = features;
219
-
220
- // if it's the first-pass tiling
221
- if (cz === undefined) {
222
- // stop tiling if we reached max zoom, or if the tile is too simple
223
- if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
224
- // if a drilldown to a specific tile
225
- } else if (z === options.maxZoom || z === cz) {
226
- // stop tiling if we reached base zoom or our target tile zoom
227
- continue;
228
- } else if (cz !== undefined) {
229
- // stop tiling if it's not an ancestor of the target tile
230
- const zoomSteps = cz - z;
231
- // @ts-expect-error TODO fix the types of cx cy
232
- if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
233
- }
234
-
235
- // if we slice further down, no need to keep source geometry
236
- tile.source = null;
237
-
238
- if (features.length === 0) continue;
239
-
240
- if (debug > 1) console.time('clipping');
241
-
242
- // values we'll use for clipping
243
- const k1 = (0.5 * options.buffer) / options.extent;
244
- const k2 = 0.5 - k1;
245
- const k3 = 0.5 + k1;
246
- const k4 = 1 + k1;
247
-
248
- let tl: GeoJSONTileFeature[] | null = null;
249
- let bl: GeoJSONTileFeature[] | null = null;
250
- let tr: GeoJSONTileFeature[] | null = null;
251
- let br: GeoJSONTileFeature[] | null = null;
252
-
253
- let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
254
- let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
255
-
256
- // @ts-expect-error - unclear why this is needed?
257
- features = null;
258
-
259
- if (left) {
260
- tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
261
- bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
262
- left = null;
263
- }
264
-
265
- if (right) {
266
- tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
267
- br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
268
- right = null;
269
- }
270
-
271
- if (debug > 1) console.timeEnd('clipping');
272
-
273
- stack.push(tl || [], z + 1, x * 2, y * 2);
274
- stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
275
- stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
276
- stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
277
- }
278
- }
279
- }
280
-
281
- function toID(z, x, y) {
282
- return ((1 << z) * y + x) * 32 + z;
283
- }
@@ -1,31 +0,0 @@
1
- This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
2
-
3
- ---
4
-
5
- Copyright (c) 2014, Mapbox
6
-
7
- All rights reserved.
8
-
9
- Redistribution and use in source and binary forms, with or without modification,
10
- are permitted provided that the following conditions are met:
11
-
12
- * Redistributions of source code must retain the above copyright notice,
13
- this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above copyright notice,
15
- this list of conditions and the following disclaimer in the documentation
16
- and/or other materials provided with the distribution.
17
- * Neither the name of Mapbox nor the names of its contributors
18
- may be used to endorse or promote products derived from this software
19
- without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.