@loaders.gl/mvt 3.1.0 → 3.1.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 (49) hide show
  1. package/dist/bundle.js +5 -2281
  2. package/dist/dist.min.js +2289 -0
  3. package/dist/es5/helpers/binary-util-functions.js +8 -5
  4. package/dist/es5/helpers/binary-util-functions.js.map +1 -1
  5. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +33 -31
  6. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
  7. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +2 -2
  8. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
  9. package/dist/es5/lib/parse-mvt.js +6 -5
  10. package/dist/es5/lib/parse-mvt.js.map +1 -1
  11. package/dist/es5/mvt-loader.js +1 -1
  12. package/dist/esm/helpers/binary-util-functions.js +8 -5
  13. package/dist/esm/helpers/binary-util-functions.js.map +1 -1
  14. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +31 -30
  15. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
  16. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +2 -2
  17. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
  18. package/dist/esm/lib/parse-mvt.js +6 -5
  19. package/dist/esm/lib/parse-mvt.js.map +1 -1
  20. package/dist/esm/mvt-loader.js +1 -1
  21. package/dist/helpers/binary-util-functions.d.ts +2 -6
  22. package/dist/helpers/binary-util-functions.d.ts.map +1 -1
  23. package/dist/helpers/binary-util-functions.js +7 -5
  24. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +12 -7
  25. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -1
  26. package/dist/lib/binary-vector-tile/vector-tile-feature.js +32 -39
  27. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +3 -3
  28. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -1
  29. package/dist/lib/binary-vector-tile/vector-tile-layer.js +3 -3
  30. package/dist/lib/parse-mvt.d.ts +8 -76
  31. package/dist/lib/parse-mvt.d.ts.map +1 -1
  32. package/dist/lib/parse-mvt.js +6 -5
  33. package/dist/lib/types.d.ts +0 -68
  34. package/dist/lib/types.d.ts.map +1 -1
  35. package/dist/mvt-worker.js +90 -89
  36. package/package.json +7 -6
  37. package/src/helpers/binary-util-functions.ts +9 -7
  38. package/src/lib/binary-vector-tile/vector-tile-feature.ts +36 -44
  39. package/src/lib/binary-vector-tile/vector-tile-layer.ts +4 -4
  40. package/src/lib/parse-mvt.ts +10 -8
  41. package/src/lib/types.ts +0 -75
  42. package/dist/es5/lib/binary-vector-tile/features-to-binary.js +0 -389
  43. package/dist/es5/lib/binary-vector-tile/features-to-binary.js.map +0 -1
  44. package/dist/esm/lib/binary-vector-tile/features-to-binary.js +0 -331
  45. package/dist/esm/lib/binary-vector-tile/features-to-binary.js.map +0 -1
  46. package/dist/lib/binary-vector-tile/features-to-binary.d.ts +0 -177
  47. package/dist/lib/binary-vector-tile/features-to-binary.d.ts.map +0 -1
  48. package/dist/lib/binary-vector-tile/features-to-binary.js +0 -358
  49. package/src/lib/binary-vector-tile/features-to-binary.ts +0 -527
@@ -1,527 +0,0 @@
1
- /* eslint-disable indent */
2
- import {earcut} from '@math.gl/polygon';
3
- import {
4
- MvtBinaryCoordinates,
5
- MvtBinaryGeometry,
6
- MvtBinaryOptions,
7
- MvtPropArrayConstructor,
8
- MvtFirstPassedData,
9
- MvtLines,
10
- MvtPoints,
11
- MvtPolygons
12
- } from '../types';
13
-
14
- /**
15
- * Convert binary features to flat binary arrays. Similar to
16
- * `geojsonToBinary` helper function, except that it expects
17
- * a binary representation of the feature data, which enables
18
- * 2X-3X speed increase in parse speed, compared to using
19
- * geoJSON. See `binary-vector-tile/VectorTileFeature` for
20
- * data format detais
21
- *
22
- * @param features
23
- * @param firstPassData
24
- * @param options
25
- * @returns filled arrays
26
- */
27
- export function featuresToBinary(
28
- features: MvtBinaryCoordinates[],
29
- firstPassData: MvtFirstPassedData,
30
- options?: MvtBinaryOptions
31
- ) {
32
- const propArrayTypes = extractNumericPropTypes(features);
33
- const numericPropKeys = Object.keys(propArrayTypes).filter((k) => propArrayTypes[k] !== Array);
34
- return fillArrays(features, firstPassData, {
35
- numericPropKeys: options ? options.numericPropKeys : numericPropKeys,
36
- propArrayTypes,
37
- PositionDataType: options ? options.PositionDataType : Float32Array
38
- });
39
- }
40
-
41
- export const TEST_EXPORTS = {
42
- fillArrays
43
- };
44
-
45
- /**
46
- * Extracts properties that are always numeric
47
- *
48
- * @param features
49
- * @returns object with numeric types
50
- */
51
- function extractNumericPropTypes(features: MvtBinaryCoordinates[]): {
52
- [key: string]: MvtPropArrayConstructor;
53
- } {
54
- const propArrayTypes = {};
55
- for (const feature of features) {
56
- if (feature.properties) {
57
- for (const key in feature.properties) {
58
- // If property has not been seen before, or if property has been numeric
59
- // in all previous features, check if numeric in this feature
60
- // If not numeric, Array is stored to prevent rechecking in the future
61
- // Additionally, detects if 64 bit precision is required
62
- const val = feature.properties[key];
63
- propArrayTypes[key] = deduceArrayType(val, propArrayTypes[key]);
64
- }
65
- }
66
- }
67
-
68
- return propArrayTypes;
69
- }
70
-
71
- /**
72
- * Fills coordinates into pre-allocated typed arrays
73
- *
74
- * @param features
75
- * @param firstPassData
76
- * @param options
77
- * @returns an accessor object with value and size keys
78
- */
79
- // eslint-disable-next-line complexity
80
- function fillArrays(
81
- features: MvtBinaryCoordinates[],
82
- firstPassData: MvtFirstPassedData,
83
- options: MvtBinaryOptions
84
- ) {
85
- const {
86
- pointPositionsCount,
87
- pointFeaturesCount,
88
- linePositionsCount,
89
- linePathsCount,
90
- lineFeaturesCount,
91
- polygonPositionsCount,
92
- polygonObjectsCount,
93
- polygonRingsCount,
94
- polygonFeaturesCount
95
- } = firstPassData;
96
- const {numericPropKeys, propArrayTypes, PositionDataType = Float32Array} = options;
97
- const hasGlobalId = features[0] && 'id' in features[0];
98
- const coordLength = 2;
99
- const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
100
- const points: MvtPoints = {
101
- positions: new PositionDataType(pointPositionsCount * coordLength),
102
- globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
103
- featureIds:
104
- pointFeaturesCount > 65535
105
- ? new Uint32Array(pointPositionsCount)
106
- : new Uint16Array(pointPositionsCount),
107
- numericProps: {},
108
- properties: [],
109
- fields: []
110
- };
111
- const lines: MvtLines = {
112
- pathIndices:
113
- linePositionsCount > 65535
114
- ? new Uint32Array(linePathsCount + 1)
115
- : new Uint16Array(linePathsCount + 1),
116
- positions: new PositionDataType(linePositionsCount * coordLength),
117
- globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
118
- featureIds:
119
- lineFeaturesCount > 65535
120
- ? new Uint32Array(linePositionsCount)
121
- : new Uint16Array(linePositionsCount),
122
- numericProps: {},
123
- properties: [],
124
- fields: []
125
- };
126
- const polygons: MvtPolygons = {
127
- polygonIndices:
128
- polygonPositionsCount > 65535
129
- ? new Uint32Array(polygonObjectsCount + 1)
130
- : new Uint16Array(polygonObjectsCount + 1),
131
- primitivePolygonIndices:
132
- polygonPositionsCount > 65535
133
- ? new Uint32Array(polygonRingsCount + 1)
134
- : new Uint16Array(polygonRingsCount + 1),
135
- positions: new PositionDataType(polygonPositionsCount * coordLength),
136
- triangles: [],
137
- globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
138
- featureIds:
139
- polygonFeaturesCount > 65535
140
- ? new Uint32Array(polygonPositionsCount)
141
- : new Uint16Array(polygonPositionsCount),
142
- numericProps: {},
143
- properties: [],
144
- fields: []
145
- };
146
-
147
- // Instantiate numeric properties arrays; one value per vertex
148
- for (const object of [points, lines, polygons]) {
149
- for (const propName of numericPropKeys) {
150
- // If property has been numeric in all previous features in which the property existed, check
151
- // if numeric in this feature
152
- const TypedArray = propArrayTypes[propName];
153
- object.numericProps[propName] = new TypedArray(object.positions.length / coordLength);
154
- }
155
- }
156
-
157
- // Set last element of path/polygon indices as positions length
158
- lines.pathIndices[linePathsCount] = linePositionsCount;
159
- polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
160
- polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
161
-
162
- const indexMap = {
163
- pointPosition: 0,
164
- pointFeature: 0,
165
- linePosition: 0,
166
- linePath: 0,
167
- lineFeature: 0,
168
- polygonPosition: 0,
169
- polygonObject: 0,
170
- polygonRing: 0,
171
- polygonFeature: 0,
172
- feature: 0
173
- };
174
-
175
- for (const feature of features) {
176
- const geometry = feature.geometry;
177
- const properties = feature.properties || {};
178
-
179
- switch (geometry.type) {
180
- case 'Point':
181
- case 'MultiPoint':
182
- handlePoint(geometry, points, indexMap, coordLength, properties);
183
- points.properties.push(keepStringProperties(properties, numericPropKeys));
184
- if (hasGlobalId) {
185
- points.fields.push({id: feature.id});
186
- }
187
- indexMap.pointFeature++;
188
- break;
189
- case 'LineString':
190
- case 'MultiLineString':
191
- handleLineString(geometry, lines, indexMap, coordLength, properties);
192
- lines.properties.push(keepStringProperties(properties, numericPropKeys));
193
- if (hasGlobalId) {
194
- lines.fields.push({id: feature.id});
195
- }
196
- indexMap.lineFeature++;
197
- break;
198
- case 'Polygon':
199
- case 'MultiPolygon':
200
- handlePolygon(geometry, polygons, indexMap, coordLength, properties);
201
- polygons.properties.push(keepStringProperties(properties, numericPropKeys));
202
- if (hasGlobalId) {
203
- polygons.fields.push({id: feature.id});
204
- }
205
- indexMap.polygonFeature++;
206
- break;
207
- default:
208
- throw new Error('Invalid geometry type');
209
- }
210
-
211
- indexMap.feature++;
212
- }
213
-
214
- // Wrap each array in an accessor object with value and size keys
215
- return makeAccessorObjects(points, lines, polygons, coordLength);
216
- }
217
-
218
- /**
219
- * Fills (Multi)Point coordinates into points object of arrays
220
- *
221
- * @param geometry
222
- * @param points
223
- * @param indexMap
224
- * @param coordLength
225
- * @param properties
226
- */
227
- function handlePoint(
228
- geometry: MvtBinaryGeometry,
229
- points: MvtPoints,
230
- indexMap: {
231
- pointPosition: number;
232
- pointFeature: number;
233
- linePosition?: number;
234
- linePath?: number;
235
- lineFeature?: number;
236
- polygonPosition?: number;
237
- polygonObject?: number;
238
- polygonRing?: number;
239
- polygonFeature?: number;
240
- feature: number;
241
- },
242
- coordLength: number,
243
- properties: {[x: string]: string | number | boolean | null}
244
- ): void {
245
- points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
246
-
247
- const nPositions = geometry.data.length / coordLength;
248
- fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
249
- points.globalFeatureIds.fill(
250
- indexMap.feature,
251
- indexMap.pointPosition,
252
- indexMap.pointPosition + nPositions
253
- );
254
- points.featureIds.fill(
255
- indexMap.pointFeature,
256
- indexMap.pointPosition,
257
- indexMap.pointPosition + nPositions
258
- );
259
-
260
- indexMap.pointPosition += nPositions;
261
- }
262
-
263
- /**
264
- * Fills (Multi)LineString coordinates into lines object of arrays
265
- *
266
- * @param geometry
267
- * @param lines
268
- * @param indexMap
269
- * @param coordLength
270
- * @param properties
271
- */
272
- function handleLineString(
273
- geometry: MvtBinaryGeometry,
274
- lines: MvtLines,
275
- indexMap: {
276
- pointPosition?: number;
277
- pointFeature?: number;
278
- linePosition: number;
279
- linePath: number;
280
- lineFeature: number;
281
- polygonPosition?: number;
282
- polygonObject?: number;
283
- polygonRing?: number;
284
- polygonFeature?: number;
285
- feature: number;
286
- },
287
- coordLength: number,
288
- properties: {[x: string]: string | number | boolean | null}
289
- ): void {
290
- lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
291
-
292
- const nPositions = geometry.data.length / coordLength;
293
- fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
294
-
295
- lines.globalFeatureIds.fill(
296
- indexMap.feature,
297
- indexMap.linePosition,
298
- indexMap.linePosition + nPositions
299
- );
300
- lines.featureIds.fill(
301
- indexMap.lineFeature,
302
- indexMap.linePosition,
303
- indexMap.linePosition + nPositions
304
- );
305
-
306
- for (let i = 0, il = geometry.lines.length; i < il; ++i) {
307
- // Extract range of data we are working with, defined by start
308
- // and end indices (these index into the geometry.data array)
309
- const start = geometry.lines[i];
310
- const end =
311
- i === il - 1
312
- ? geometry.data.length // last line, so read to end of data
313
- : geometry.lines[i + 1]; // start index for next line
314
-
315
- lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
316
- indexMap.linePosition += (end - start) / coordLength;
317
- }
318
- }
319
-
320
- /**
321
- * Fills (Multi)Polygon coordinates into polygons object of arrays
322
- *
323
- * @param geometry
324
- * @param polygons
325
- * @param indexMap
326
- * @param coordLength
327
- * @param properties
328
- */
329
- function handlePolygon(
330
- geometry: MvtBinaryGeometry,
331
- polygons: MvtPolygons,
332
- indexMap: {
333
- pointPosition?: number;
334
- pointFeature?: number;
335
- linePosition?: number;
336
- linePath?: number;
337
- lineFeature?: number;
338
- polygonPosition: number;
339
- polygonObject: number;
340
- polygonRing: number;
341
- polygonFeature: number;
342
- feature: number;
343
- },
344
- coordLength: number,
345
- properties: {[x: string]: string | number | boolean | null}
346
- ): void {
347
- polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
348
-
349
- const nPositions = geometry.data.length / coordLength;
350
- fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
351
- polygons.globalFeatureIds.fill(
352
- indexMap.feature,
353
- indexMap.polygonPosition,
354
- indexMap.polygonPosition + nPositions
355
- );
356
- polygons.featureIds.fill(
357
- indexMap.polygonFeature,
358
- indexMap.polygonPosition,
359
- indexMap.polygonPosition + nPositions
360
- );
361
-
362
- // Unlike Point & LineString geometry.lines is a 2D array
363
- for (let l = 0, ll = geometry.lines.length; l < ll; ++l) {
364
- const startPosition = indexMap.polygonPosition;
365
- polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
366
-
367
- const areas = geometry.areas![l];
368
- const lines = geometry.lines[l];
369
- const nextLines = geometry.lines[l + 1];
370
-
371
- for (let i = 0, il = lines.length; i < il; ++i) {
372
- const start = lines[i];
373
- const end =
374
- i === il - 1
375
- ? // last line, so either read to:
376
- nextLines === undefined
377
- ? geometry.data.length // end of data (no next lines)
378
- : nextLines[0] // start of first line in nextLines
379
- : lines[i + 1]; // start index for next line
380
-
381
- polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
382
- indexMap.polygonPosition += (end - start) / coordLength;
383
- }
384
-
385
- const endPosition = indexMap.polygonPosition;
386
- triangulatePolygon(polygons, areas, lines, {startPosition, endPosition, coordLength});
387
- }
388
- }
389
-
390
- /**
391
- * Triangulate polygon using earcut
392
- *
393
- * @param polygons
394
- * @param areas
395
- * @param lines
396
- * @param param3
397
- */
398
- function triangulatePolygon(
399
- polygons: MvtPolygons,
400
- areas: number,
401
- lines: number[],
402
- {
403
- startPosition,
404
- endPosition,
405
- coordLength
406
- }: {startPosition: number; endPosition: number; coordLength: number}
407
- ): void {
408
- const start = startPosition * coordLength;
409
- const end = endPosition * coordLength;
410
-
411
- // Extract positions and holes for just this polygon
412
- const polygonPositions = polygons.positions.subarray(start, end);
413
-
414
- // Holes are referenced relative to outer polygon
415
- const offset = lines[0];
416
- const holes = lines.slice(1).map((n: number) => (n - offset) / coordLength);
417
-
418
- // Compute triangulation
419
- const indices = earcut(polygonPositions, holes, coordLength, areas);
420
-
421
- // Indices returned by triangulation are relative to start
422
- // of polygon, so we need to offset
423
- for (let t = 0, tl = indices.length; t < tl; ++t) {
424
- polygons.triangles.push(startPosition + indices[t]);
425
- }
426
- }
427
-
428
- /**
429
- * Wrap each array in an accessor object with value and size keys
430
- *
431
- * @param points
432
- * @param lines
433
- * @param polygons
434
- * @param coordLength
435
- * @returns object
436
- */
437
- function makeAccessorObjects(
438
- points: MvtPoints,
439
- lines: MvtLines,
440
- polygons: MvtPolygons,
441
- coordLength: number
442
- ) {
443
- const returnObj = {
444
- points: {
445
- ...points,
446
- positions: {value: points.positions, size: coordLength},
447
- globalFeatureIds: {value: points.globalFeatureIds, size: 1},
448
- featureIds: {value: points.featureIds, size: 1}
449
- },
450
- lines: {
451
- ...lines,
452
- pathIndices: {value: lines.pathIndices, size: 1},
453
- positions: {value: lines.positions, size: coordLength},
454
- globalFeatureIds: {value: lines.globalFeatureIds, size: 1},
455
- featureIds: {value: lines.featureIds, size: 1}
456
- },
457
- polygons: {
458
- ...polygons,
459
- polygonIndices: {value: polygons.polygonIndices, size: 1},
460
- primitivePolygonIndices: {value: polygons.primitivePolygonIndices, size: 1},
461
- positions: {value: polygons.positions, size: coordLength},
462
- triangles: {value: new Uint32Array(polygons.triangles), size: 1},
463
- globalFeatureIds: {value: polygons.globalFeatureIds, size: 1},
464
- featureIds: {value: polygons.featureIds, size: 1}
465
- }
466
- };
467
-
468
- for (const geomType in returnObj) {
469
- for (const numericProp in returnObj[geomType].numericProps) {
470
- returnObj[geomType].numericProps[numericProp] = {
471
- value: returnObj[geomType].numericProps[numericProp],
472
- size: 1
473
- };
474
- }
475
- }
476
- return returnObj;
477
- }
478
-
479
- /**
480
- * Add numeric properties to object
481
- *
482
- * @param object
483
- * @param properties
484
- * @param index
485
- * @param length
486
- */
487
- function fillNumericProperties(
488
- object: MvtPoints,
489
- properties: {[x: string]: string | number | boolean | null},
490
- index: number,
491
- length: number
492
- ): void {
493
- for (const numericPropName in object.numericProps) {
494
- if (numericPropName in properties) {
495
- object.numericProps[numericPropName].fill(properties[numericPropName], index, index + length);
496
- }
497
- }
498
- }
499
-
500
- /**
501
- * Keep string properties in object
502
- *
503
- * @param properties
504
- * @param numericKeys
505
- * @returns object
506
- */
507
- function keepStringProperties(
508
- properties: {[x: string]: string | number | boolean | null},
509
- numericKeys: string[]
510
- ) {
511
- const props = {};
512
- for (const key in properties) {
513
- if (!numericKeys.includes(key)) {
514
- props[key] = properties[key];
515
- }
516
- }
517
- return props;
518
- }
519
-
520
- function deduceArrayType(x: any, constructor: MvtPropArrayConstructor): MvtPropArrayConstructor {
521
- if (constructor === Array || !Number.isFinite(x)) {
522
- return Array;
523
- }
524
-
525
- // If this or previous value required 64bits use Float64Array
526
- return constructor === Float64Array || Math.fround(x) !== x ? Float64Array : Float32Array;
527
- }