@loaders.gl/mvt 3.0.12 → 3.1.0-alpha.4

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 (98) hide show
  1. package/dist/dist.min.js +2 -2
  2. package/dist/dist.min.js.map +1 -1
  3. package/dist/es5/bundle.js +2 -4
  4. package/dist/es5/bundle.js.map +1 -1
  5. package/dist/es5/helpers/binary-util-functions.js +95 -0
  6. package/dist/es5/helpers/binary-util-functions.js.map +1 -0
  7. package/dist/es5/helpers/mapbox-util-functions.js +62 -0
  8. package/dist/es5/helpers/mapbox-util-functions.js.map +1 -0
  9. package/dist/es5/lib/binary-vector-tile/features-to-binary.js +4 -7
  10. package/dist/es5/lib/binary-vector-tile/features-to-binary.js.map +1 -1
  11. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +23 -94
  12. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
  13. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +15 -3
  14. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
  15. package/dist/es5/lib/binary-vector-tile/vector-tile.js +8 -3
  16. package/dist/es5/lib/binary-vector-tile/vector-tile.js.map +1 -1
  17. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js +15 -55
  18. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
  19. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js +15 -3
  20. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -1
  21. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js +8 -3
  22. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js.map +1 -1
  23. package/dist/es5/lib/parse-mvt.js +56 -53
  24. package/dist/es5/lib/parse-mvt.js.map +1 -1
  25. package/dist/es5/lib/types.js +2 -0
  26. package/dist/es5/lib/types.js.map +1 -0
  27. package/dist/es5/mvt-loader.js +1 -1
  28. package/dist/es5/mvt-loader.js.map +1 -1
  29. package/dist/es5/workers/mvt-worker.js +7 -3
  30. package/dist/es5/workers/mvt-worker.js.map +1 -0
  31. package/dist/esm/bundle.js +2 -4
  32. package/dist/esm/bundle.js.map +1 -1
  33. package/dist/esm/helpers/binary-util-functions.js +81 -0
  34. package/dist/esm/helpers/binary-util-functions.js.map +1 -0
  35. package/dist/esm/helpers/mapbox-util-functions.js +49 -0
  36. package/dist/esm/helpers/mapbox-util-functions.js.map +1 -0
  37. package/dist/esm/lib/binary-vector-tile/features-to-binary.js +4 -4
  38. package/dist/esm/lib/binary-vector-tile/features-to-binary.js.map +1 -1
  39. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +27 -92
  40. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
  41. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +22 -3
  42. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
  43. package/dist/esm/lib/binary-vector-tile/vector-tile.js +8 -3
  44. package/dist/esm/lib/binary-vector-tile/vector-tile.js.map +1 -1
  45. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js +20 -54
  46. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
  47. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js +22 -3
  48. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -1
  49. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js +8 -3
  50. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js.map +1 -1
  51. package/dist/esm/lib/parse-mvt.js +57 -52
  52. package/dist/esm/lib/parse-mvt.js.map +1 -1
  53. package/dist/esm/lib/types.js +2 -0
  54. package/dist/esm/lib/types.js.map +1 -0
  55. package/dist/esm/mvt-loader.js +1 -1
  56. package/dist/esm/mvt-loader.js.map +1 -1
  57. package/dist/esm/workers/mvt-worker.js +3 -3
  58. package/dist/esm/workers/mvt-worker.js.map +1 -0
  59. package/dist/mvt-worker.js +2 -2
  60. package/dist/mvt-worker.js.map +1 -1
  61. package/package.json +5 -5
  62. package/src/bundle.ts +2 -3
  63. package/src/helpers/binary-util-functions.ts +119 -0
  64. package/src/helpers/mapbox-util-functions.ts +78 -0
  65. package/src/lib/binary-vector-tile/{features-to-binary.js → features-to-binary.ts} +184 -29
  66. package/src/lib/binary-vector-tile/{vector-tile-feature.js → vector-tile-feature.ts} +41 -109
  67. package/src/lib/binary-vector-tile/vector-tile-layer.ts +109 -0
  68. package/src/lib/binary-vector-tile/vector-tile.ts +28 -0
  69. package/src/lib/mapbox-vector-tile/{vector-tile-feature.js → vector-tile-feature.ts} +31 -72
  70. package/src/lib/mapbox-vector-tile/{vector-tile-layer.js → vector-tile-layer.ts} +42 -14
  71. package/src/lib/mapbox-vector-tile/vector-tile.ts +28 -0
  72. package/src/lib/parse-mvt.ts +174 -0
  73. package/src/lib/types.ts +92 -0
  74. package/src/workers/{mvt-worker.js → mvt-worker.ts} +0 -0
  75. package/dist/dist.es5.min.js +0 -4
  76. package/dist/dist.es5.min.js.map +0 -1
  77. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -39
  78. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -25
  79. package/dist/es5/lib/binary-vector-tile/vector-tile.d.ts +0 -16
  80. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -19
  81. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.d.ts +0 -14
  82. package/dist/es5/lib/mapbox-vector-tile/vector-tile.d.ts +0 -7
  83. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -39
  84. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -25
  85. package/dist/esm/lib/binary-vector-tile/vector-tile.d.ts +0 -16
  86. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -19
  87. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.d.ts +0 -14
  88. package/dist/esm/lib/mapbox-vector-tile/vector-tile.d.ts +0 -7
  89. package/src/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -39
  90. package/src/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -25
  91. package/src/lib/binary-vector-tile/vector-tile-layer.js +0 -79
  92. package/src/lib/binary-vector-tile/vector-tile.d.ts +0 -16
  93. package/src/lib/binary-vector-tile/vector-tile.js +0 -18
  94. package/src/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -19
  95. package/src/lib/mapbox-vector-tile/vector-tile-layer.d.ts +0 -14
  96. package/src/lib/mapbox-vector-tile/vector-tile.d.ts +0 -7
  97. package/src/lib/mapbox-vector-tile/vector-tile.js +0 -18
  98. package/src/lib/parse-mvt.js +0 -142
@@ -1,5 +1,14 @@
1
- // @ts-nocheck
1
+ /* eslint-disable indent */
2
2
  import {earcut} from '@math.gl/polygon';
3
+ import {
4
+ MvtBinaryCoordinates,
5
+ MvtBinaryGeometry,
6
+ MvtBinaryOptions,
7
+ MvtFirstPassedData,
8
+ MvtLines,
9
+ MvtPoints,
10
+ MvtPolygons
11
+ } from '../types';
3
12
 
4
13
  /**
5
14
  * Convert binary features to flat binary arrays. Similar to
@@ -8,11 +17,20 @@ import {earcut} from '@math.gl/polygon';
8
17
  * 2X-3X speed increase in parse speed, compared to using
9
18
  * geoJSON. See `binary-vector-tile/VectorTileFeature` for
10
19
  * data format detais
20
+ *
21
+ * @param features
22
+ * @param firstPassData
23
+ * @param options
24
+ * @returns filled arrays
11
25
  */
12
- export function featuresToBinary(features, firstPassData, options = {}) {
26
+ export function featuresToBinary(
27
+ features: MvtBinaryCoordinates[],
28
+ firstPassData: MvtFirstPassedData,
29
+ options?: MvtBinaryOptions
30
+ ) {
13
31
  return fillArrays(features, firstPassData, {
14
- numericPropKeys: options.numericPropKeys || extractNumericPropKeys(features),
15
- PositionDataType: options.PositionDataType || Float32Array
32
+ numericPropKeys: options ? options.numericPropKeys : extractNumericPropKeys(features),
33
+ PositionDataType: options ? options.PositionDataType : Float32Array
16
34
  });
17
35
  }
18
36
 
@@ -21,9 +39,13 @@ export const TEST_EXPORTS = {
21
39
  fillArrays
22
40
  };
23
41
 
24
- // Extracts properties that are always numeric
25
- // eslint-disable-next-line complexity, max-statements
26
- function extractNumericPropKeys(features) {
42
+ /**
43
+ * Extracts properties that are always numeric
44
+ *
45
+ * @param features
46
+ * @returns object with numeric keys
47
+ */
48
+ function extractNumericPropKeys(features: MvtBinaryCoordinates[]): string[] {
27
49
  const numericPropKeys = {};
28
50
  for (const feature of features) {
29
51
  if (feature.properties) {
@@ -32,7 +54,6 @@ function extractNumericPropKeys(features) {
32
54
  // in all previous features, check if numeric in this feature
33
55
  // If not numeric, false is stored to prevent rechecking in the future
34
56
  const numericSoFar = numericPropKeys[key];
35
- // eslint-disable-next-line max-depth
36
57
  if (numericSoFar || numericSoFar === undefined) {
37
58
  const val = feature.properties[key];
38
59
  numericPropKeys[key] = isNumeric(val);
@@ -44,9 +65,20 @@ function extractNumericPropKeys(features) {
44
65
  return Object.keys(numericPropKeys).filter((k) => numericPropKeys[k]);
45
66
  }
46
67
 
47
- // Fills coordinates into pre-allocated typed arrays
68
+ /**
69
+ * Fills coordinates into pre-allocated typed arrays
70
+ *
71
+ * @param features
72
+ * @param firstPassData
73
+ * @param options
74
+ * @returns an accessor object with value and size keys
75
+ */
48
76
  // eslint-disable-next-line complexity
49
- function fillArrays(features, firstPassData = {}, options = {}) {
77
+ function fillArrays(
78
+ features: MvtBinaryCoordinates[],
79
+ firstPassData: MvtFirstPassedData,
80
+ options: MvtBinaryOptions
81
+ ) {
50
82
  const {
51
83
  pointPositionsCount,
52
84
  pointFeaturesCount,
@@ -62,7 +94,7 @@ function fillArrays(features, firstPassData = {}, options = {}) {
62
94
  const hasGlobalId = features[0] && 'id' in features[0];
63
95
  const coordLength = 2;
64
96
  const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
65
- const points = {
97
+ const points: MvtPoints = {
66
98
  positions: new PositionDataType(pointPositionsCount * coordLength),
67
99
  globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
68
100
  featureIds:
@@ -73,7 +105,7 @@ function fillArrays(features, firstPassData = {}, options = {}) {
73
105
  properties: [],
74
106
  fields: []
75
107
  };
76
- const lines = {
108
+ const lines: MvtLines = {
77
109
  pathIndices:
78
110
  linePositionsCount > 65535
79
111
  ? new Uint32Array(linePathsCount + 1)
@@ -88,7 +120,7 @@ function fillArrays(features, firstPassData = {}, options = {}) {
88
120
  properties: [],
89
121
  fields: []
90
122
  };
91
- const polygons = {
123
+ const polygons: MvtPolygons = {
92
124
  polygonIndices:
93
125
  polygonPositionsCount > 65535
94
126
  ? new Uint32Array(polygonObjectsCount + 1)
@@ -179,8 +211,33 @@ function fillArrays(features, firstPassData = {}, options = {}) {
179
211
  return makeAccessorObjects(points, lines, polygons, coordLength);
180
212
  }
181
213
 
182
- // Fills (Multi)Point coordinates into points object of arrays
183
- function handlePoint(geometry, points, indexMap, coordLength, properties) {
214
+ /**
215
+ * Fills (Multi)Point coordinates into points object of arrays
216
+ *
217
+ * @param geometry
218
+ * @param points
219
+ * @param indexMap
220
+ * @param coordLength
221
+ * @param properties
222
+ */
223
+ function handlePoint(
224
+ geometry: MvtBinaryGeometry,
225
+ points: MvtPoints,
226
+ indexMap: {
227
+ pointPosition: number;
228
+ pointFeature: number;
229
+ linePosition?: number;
230
+ linePath?: number;
231
+ lineFeature?: number;
232
+ polygonPosition?: number;
233
+ polygonObject?: number;
234
+ polygonRing?: number;
235
+ polygonFeature?: number;
236
+ feature: number;
237
+ },
238
+ coordLength: number,
239
+ properties: {[x: string]: string | number | boolean | null}
240
+ ): void {
184
241
  points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
185
242
 
186
243
  const nPositions = geometry.data.length / coordLength;
@@ -199,8 +256,33 @@ function handlePoint(geometry, points, indexMap, coordLength, properties) {
199
256
  indexMap.pointPosition += nPositions;
200
257
  }
201
258
 
202
- // Fills (Multi)LineString coordinates into lines object of arrays
203
- function handleLineString(geometry, lines, indexMap, coordLength, properties) {
259
+ /**
260
+ * Fills (Multi)LineString coordinates into lines object of arrays
261
+ *
262
+ * @param geometry
263
+ * @param lines
264
+ * @param indexMap
265
+ * @param coordLength
266
+ * @param properties
267
+ */
268
+ function handleLineString(
269
+ geometry: MvtBinaryGeometry,
270
+ lines: MvtLines,
271
+ indexMap: {
272
+ pointPosition?: number;
273
+ pointFeature?: number;
274
+ linePosition: number;
275
+ linePath: number;
276
+ lineFeature: number;
277
+ polygonPosition?: number;
278
+ polygonObject?: number;
279
+ polygonRing?: number;
280
+ polygonFeature?: number;
281
+ feature: number;
282
+ },
283
+ coordLength: number,
284
+ properties: {[x: string]: string | number | boolean | null}
285
+ ): void {
204
286
  lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
205
287
 
206
288
  const nPositions = geometry.data.length / coordLength;
@@ -231,8 +313,33 @@ function handleLineString(geometry, lines, indexMap, coordLength, properties) {
231
313
  }
232
314
  }
233
315
 
234
- // Fills (Multi)Polygon coordinates into polygons object of arrays
235
- function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
316
+ /**
317
+ * Fills (Multi)Polygon coordinates into polygons object of arrays
318
+ *
319
+ * @param geometry
320
+ * @param polygons
321
+ * @param indexMap
322
+ * @param coordLength
323
+ * @param properties
324
+ */
325
+ function handlePolygon(
326
+ geometry: MvtBinaryGeometry,
327
+ polygons: MvtPolygons,
328
+ indexMap: {
329
+ pointPosition?: number;
330
+ pointFeature?: number;
331
+ linePosition?: number;
332
+ linePath?: number;
333
+ lineFeature?: number;
334
+ polygonPosition: number;
335
+ polygonObject: number;
336
+ polygonRing: number;
337
+ polygonFeature: number;
338
+ feature: number;
339
+ },
340
+ coordLength: number,
341
+ properties: {[x: string]: string | number | boolean | null}
342
+ ): void {
236
343
  polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
237
344
 
238
345
  const nPositions = geometry.data.length / coordLength;
@@ -253,7 +360,7 @@ function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
253
360
  const startPosition = indexMap.polygonPosition;
254
361
  polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
255
362
 
256
- const areas = geometry.areas[l];
363
+ const areas = geometry.areas![l];
257
364
  const lines = geometry.lines[l];
258
365
  const nextLines = geometry.lines[l + 1];
259
366
 
@@ -278,8 +385,22 @@ function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
278
385
 
279
386
  /**
280
387
  * Triangulate polygon using earcut
388
+ *
389
+ * @param polygons
390
+ * @param areas
391
+ * @param lines
392
+ * @param param3
281
393
  */
282
- function triangulatePolygon(polygons, areas, lines, {startPosition, endPosition, coordLength}) {
394
+ function triangulatePolygon(
395
+ polygons: MvtPolygons,
396
+ areas: number,
397
+ lines: number[],
398
+ {
399
+ startPosition,
400
+ endPosition,
401
+ coordLength
402
+ }: {startPosition: number; endPosition: number; coordLength: number}
403
+ ): void {
283
404
  const start = startPosition * coordLength;
284
405
  const end = endPosition * coordLength;
285
406
 
@@ -288,7 +409,7 @@ function triangulatePolygon(polygons, areas, lines, {startPosition, endPosition,
288
409
 
289
410
  // Holes are referenced relative to outer polygon
290
411
  const offset = lines[0];
291
- const holes = lines.slice(1).map((n) => (n - offset) / coordLength);
412
+ const holes = lines.slice(1).map((n: number) => (n - offset) / coordLength);
292
413
 
293
414
  // Compute triangulation
294
415
  const indices = earcut(polygonPositions, holes, coordLength, areas);
@@ -300,8 +421,21 @@ function triangulatePolygon(polygons, areas, lines, {startPosition, endPosition,
300
421
  }
301
422
  }
302
423
 
303
- // Wrap each array in an accessor object with value and size keys
304
- function makeAccessorObjects(points, lines, polygons, coordLength) {
424
+ /**
425
+ * Wrap each array in an accessor object with value and size keys
426
+ *
427
+ * @param points
428
+ * @param lines
429
+ * @param polygons
430
+ * @param coordLength
431
+ * @returns object
432
+ */
433
+ function makeAccessorObjects(
434
+ points: MvtPoints,
435
+ lines: MvtLines,
436
+ polygons: MvtPolygons,
437
+ coordLength: number
438
+ ) {
305
439
  const returnObj = {
306
440
  points: {
307
441
  ...points,
@@ -338,8 +472,20 @@ function makeAccessorObjects(points, lines, polygons, coordLength) {
338
472
  return returnObj;
339
473
  }
340
474
 
341
- // Add numeric properties to object
342
- function fillNumericProperties(object, properties, index, length) {
475
+ /**
476
+ * Add numeric properties to object
477
+ *
478
+ * @param object
479
+ * @param properties
480
+ * @param index
481
+ * @param length
482
+ */
483
+ function fillNumericProperties(
484
+ object: MvtPoints,
485
+ properties: {[x: string]: string | number | boolean | null},
486
+ index: number,
487
+ length: number
488
+ ): void {
343
489
  for (const numericPropName in object.numericProps) {
344
490
  if (numericPropName in properties) {
345
491
  object.numericProps[numericPropName].fill(properties[numericPropName], index, index + length);
@@ -347,8 +493,17 @@ function fillNumericProperties(object, properties, index, length) {
347
493
  }
348
494
  }
349
495
 
350
- // Keep string properties in object
351
- function keepStringProperties(properties, numericKeys) {
496
+ /**
497
+ * Keep string properties in object
498
+ *
499
+ * @param properties
500
+ * @param numericKeys
501
+ * @returns object
502
+ */
503
+ function keepStringProperties(
504
+ properties: {[x: string]: string | number | boolean | null},
505
+ numericKeys: string[]
506
+ ) {
352
507
  const props = {};
353
508
  for (const key in properties) {
354
509
  if (!numericKeys.includes(key)) {
@@ -358,6 +513,6 @@ function keepStringProperties(properties, numericKeys) {
358
513
  return props;
359
514
  }
360
515
 
361
- function isNumeric(x) {
516
+ function isNumeric(x: unknown) {
362
517
  return Number.isFinite(x);
363
518
  }
@@ -1,21 +1,45 @@
1
1
  // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
2
- /* eslint-disable */
3
2
 
4
- import {getPolygonSignedArea} from '@math.gl/polygon';
3
+ import Protobuf from 'pbf';
4
+ import {MvtBinaryCoordinates, MvtBinaryGeometry, MvtFirstPassedData} from '../types';
5
+ import {classifyRings, project, readFeature} from '../../helpers/binary-util-functions';
5
6
 
6
7
  // Reduce GC by reusing variables
7
- let endPos, cmd, cmdLen, length, x, y, i;
8
+ let endPos: number;
9
+ let cmd: number;
10
+ let cmdLen: number;
11
+ let length: number;
12
+ let x: number;
13
+ let y: number;
14
+ let i: number;
8
15
 
9
16
  export const TEST_EXPORTS = {
10
17
  classifyRings
11
18
  };
12
19
 
13
20
  export default class VectorTileFeature {
21
+ properties: {[x: string]: string | number | boolean | null};
22
+ extent: any;
23
+ type: number;
24
+ id: number | null;
25
+ _pbf: Protobuf;
26
+ _geometry: number;
27
+ _keys: string[];
28
+ _values: (string | number | boolean | null)[];
29
+ _firstPassData: MvtFirstPassedData;
14
30
  static get types() {
15
31
  return ['Unknown', 'Point', 'LineString', 'Polygon'];
16
32
  }
17
33
 
18
- constructor(pbf, end, extent, keys, values, firstPassData) {
34
+ // eslint-disable-next-line max-params
35
+ constructor(
36
+ pbf: Protobuf,
37
+ end: number,
38
+ extent: any,
39
+ keys: string[],
40
+ values: (string | number | boolean | null)[],
41
+ firstPassData: MvtFirstPassedData
42
+ ) {
19
43
  // Public
20
44
  this.properties = {};
21
45
  this.extent = extent;
@@ -33,7 +57,7 @@ export default class VectorTileFeature {
33
57
  }
34
58
 
35
59
  // eslint-disable-next-line complexity, max-statements
36
- loadGeometry() {
60
+ loadGeometry(): MvtBinaryGeometry {
37
61
  const pbf = this._pbf;
38
62
  pbf.pos = this._geometry;
39
63
 
@@ -49,8 +73,8 @@ export default class VectorTileFeature {
49
73
  // `set()` and direct index access. Also, we cannot
50
74
  // know how large the buffer should be, so it would
51
75
  // increase memory usage
52
- const lines = []; // Indices where lines start
53
- const data = []; // Flat array of coordinate data
76
+ const lines: number[] = []; // Indices where lines start
77
+ const data: number[] = []; // Flat array of coordinate data
54
78
 
55
79
  while (pbf.pos < endPos) {
56
80
  if (length <= 0) {
@@ -86,6 +110,11 @@ export default class VectorTileFeature {
86
110
  return {data, lines};
87
111
  }
88
112
 
113
+ /**
114
+ *
115
+ * @param transform
116
+ * @returns result
117
+ */
89
118
  _toBinaryCoordinates(transform) {
90
119
  // Expands the protobuf data to an intermediate `lines`
91
120
  // data format, which maps closely to the binary data buffers.
@@ -114,6 +143,7 @@ export default class VectorTileFeature {
114
143
 
115
144
  const coordLength = 2;
116
145
 
146
+ // eslint-disable-next-line default-case
117
147
  switch (this.type) {
118
148
  case 1: // Point
119
149
  this._firstPassData.pointFeaturesCount++;
@@ -139,7 +169,6 @@ export default class VectorTileFeature {
139
169
  }
140
170
  this._firstPassData.polygonPositionsCount += classified.data.length / coordLength;
141
171
 
142
- // @ts-ignore
143
172
  geom = classified;
144
173
  break;
145
174
  }
@@ -149,7 +178,7 @@ export default class VectorTileFeature {
149
178
  geom.type = `Multi${geom.type}`;
150
179
  }
151
180
 
152
- const result = {
181
+ const result: MvtBinaryCoordinates = {
153
182
  type: 'Feature',
154
183
  geometry: geom,
155
184
  properties: this.properties
@@ -162,109 +191,12 @@ export default class VectorTileFeature {
162
191
  return result;
163
192
  }
164
193
 
165
- toBinaryCoordinates(options) {
194
+ toBinaryCoordinates(
195
+ options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
196
+ ): MvtBinaryCoordinates {
166
197
  if (typeof options === 'function') {
167
198
  return this._toBinaryCoordinates(options);
168
199
  }
169
- const {x, y, z} = options;
170
- const size = this.extent * Math.pow(2, z);
171
- const x0 = this.extent * x;
172
- const y0 = this.extent * y;
173
-
174
- function project(data) {
175
- for (let j = 0, jl = data.length; j < jl; j += 2) {
176
- data[j] = ((data[j] + x0) * 360) / size - 180;
177
- const y2 = 180 - ((data[j + 1] + y0) * 360) / size;
178
- data[j + 1] = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90;
179
- }
180
- }
181
200
  return this._toBinaryCoordinates(project);
182
201
  }
183
202
  }
184
-
185
- /**
186
- * Classifies an array of rings into polygons with outer rings and holes
187
- * The function also detects holes which have zero area and
188
- * removes them. In doing so it modifies the input
189
- * `geom.data` array to remove the unneeded data
190
- */
191
- function classifyRings(geom) {
192
- const len = geom.lines.length;
193
-
194
- if (len <= 1) {
195
- return {
196
- data: geom.data,
197
- areas: [[getPolygonSignedArea(geom.data)]],
198
- lines: [geom.lines]
199
- };
200
- }
201
-
202
- const areas = [];
203
- const polygons = [];
204
- let ringAreas;
205
- let polygon;
206
- let ccw;
207
- let offset = 0;
208
-
209
- for (let i = 0, startIndex, endIndex; i < len; i++) {
210
- startIndex = geom.lines[i] - offset;
211
-
212
- endIndex = geom.lines[i + 1] - offset || geom.data.length;
213
- const shape = geom.data.slice(startIndex, endIndex);
214
- const area = getPolygonSignedArea(shape);
215
-
216
- if (area === 0) {
217
- // This polygon has no area, so remove it from the shape
218
- // Remove the section from the data array
219
- const before = geom.data.slice(0, startIndex);
220
- const after = geom.data.slice(endIndex);
221
- geom.data = before.concat(after);
222
-
223
- // Need to offset any remaining indices as we have
224
- // modified the data buffer
225
- offset += endIndex - startIndex;
226
-
227
- // Do not add this index to the output and process next shape
228
- continue;
229
- }
230
-
231
- if (ccw === undefined) ccw = area < 0;
232
-
233
- if (ccw === area < 0) {
234
- if (polygon) {
235
- areas.push(ringAreas);
236
- polygons.push(polygon);
237
- }
238
- polygon = [startIndex];
239
- ringAreas = [area];
240
- } else {
241
- // @ts-ignore
242
- ringAreas.push(area);
243
- // @ts-ignore
244
- polygon.push(startIndex);
245
- }
246
- }
247
- if (ringAreas) areas.push(ringAreas);
248
- if (polygon) polygons.push(polygon);
249
-
250
- return {areas, lines: polygons, data: geom.data};
251
- }
252
-
253
- // All code below is unchanged from the original Mapbox implemenation
254
-
255
- function readFeature(tag, feature, pbf) {
256
- if (tag === 1) feature.id = pbf.readVarint();
257
- else if (tag === 2) readTag(pbf, feature);
258
- else if (tag === 3) feature.type = pbf.readVarint();
259
- else if (tag === 4) feature._geometry = pbf.pos;
260
- }
261
-
262
- function readTag(pbf, feature) {
263
- const end = pbf.readVarint() + pbf.pos;
264
-
265
- while (pbf.pos < end) {
266
- const key = feature._keys[pbf.readVarint()];
267
- const value = feature._values[pbf.readVarint()];
268
- feature.properties[key] = value;
269
- }
270
- }