@loaders.gl/mvt 4.2.0 → 4.3.0-alpha.1

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 (108) hide show
  1. package/dist/dist.dev.js +1311 -767
  2. package/dist/dist.min.js +1 -1
  3. package/dist/geojson-tile-source.d.ts +79 -0
  4. package/dist/geojson-tile-source.d.ts.map +1 -0
  5. package/dist/{lib/geojson-tiler/geojson-tiler.js → geojson-tile-source.js} +137 -27
  6. package/dist/index.cjs +570 -560
  7. package/dist/index.cjs.map +4 -4
  8. package/dist/index.d.ts +2 -2
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/lib/geojsonvt/clip.d.ts.map +1 -0
  12. package/dist/lib/geojsonvt/convert.d.ts +10 -0
  13. package/dist/lib/geojsonvt/convert.d.ts.map +1 -0
  14. package/dist/lib/geojsonvt/feature.d.ts.map +1 -0
  15. package/dist/lib/geojsonvt/simplify.d.ts.map +1 -0
  16. package/dist/lib/geojsonvt/tile.d.ts.map +1 -0
  17. package/dist/lib/geojsonvt/transform.d.ts.map +1 -0
  18. package/dist/lib/geojsonvt/wrap.d.ts.map +1 -0
  19. package/dist/lib/parse-mvt.d.ts +3 -2
  20. package/dist/lib/parse-mvt.d.ts.map +1 -1
  21. package/dist/lib/parse-mvt.js +13 -15
  22. package/dist/lib/types.d.ts +1 -39
  23. package/dist/lib/types.d.ts.map +1 -1
  24. package/dist/lib/utils/geometry-utils.d.ts +46 -0
  25. package/dist/lib/utils/geometry-utils.d.ts.map +1 -0
  26. package/dist/{helpers/binary-util-functions.js → lib/utils/geometry-utils.js} +79 -46
  27. package/dist/lib/{binary-vector-tile → vector-tile}/vector-tile-feature.d.ts +27 -15
  28. package/dist/lib/vector-tile/vector-tile-feature.d.ts.map +1 -0
  29. package/dist/lib/vector-tile/vector-tile-feature.js +345 -0
  30. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.d.ts +10 -1
  31. package/dist/lib/vector-tile/vector-tile-layer.d.ts.map +1 -0
  32. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.js +16 -1
  33. package/dist/lib/vector-tile/vector-tile.d.ts.map +1 -0
  34. package/dist/mvt-loader.d.ts +20 -9
  35. package/dist/mvt-loader.d.ts.map +1 -1
  36. package/dist/mvt-loader.js +3 -2
  37. package/dist/mvt-source.js +4 -4
  38. package/dist/mvt-worker.js +1172 -735
  39. package/dist/tilejson-loader.js +1 -1
  40. package/package.json +6 -6
  41. package/src/{lib/geojson-tiler/geojson-tiler.ts → geojson-tile-source.ts} +172 -33
  42. package/src/index.ts +3 -3
  43. package/src/lib/{geojson-tiler → geojsonvt}/convert.ts +2 -2
  44. package/src/lib/{geojson-tiler → geojsonvt}/tile.ts +2 -2
  45. package/src/lib/parse-mvt.ts +24 -33
  46. package/src/lib/types.ts +1 -39
  47. package/src/{helpers/binary-util-functions.ts → lib/utils/geometry-utils.ts} +92 -48
  48. package/src/lib/vector-tile/vector-tile-feature.ts +407 -0
  49. package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.ts +28 -2
  50. package/src/mvt-loader.ts +14 -6
  51. package/src/mvt-source.ts +4 -4
  52. package/dist/helpers/binary-util-functions.d.ts +0 -35
  53. package/dist/helpers/binary-util-functions.d.ts.map +0 -1
  54. package/dist/helpers/mapbox-util-functions.d.ts +0 -29
  55. package/dist/helpers/mapbox-util-functions.d.ts.map +0 -1
  56. package/dist/helpers/mapbox-util-functions.js +0 -78
  57. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +0 -1
  58. package/dist/lib/binary-vector-tile/vector-tile-feature.js +0 -163
  59. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -23
  60. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +0 -1
  61. package/dist/lib/binary-vector-tile/vector-tile-layer.js +0 -96
  62. package/dist/lib/binary-vector-tile/vector-tile.d.ts +0 -9
  63. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +0 -1
  64. package/dist/lib/binary-vector-tile/vector-tile.js +0 -27
  65. package/dist/lib/geojson-tiler/clip.d.ts.map +0 -1
  66. package/dist/lib/geojson-tiler/convert.d.ts +0 -10
  67. package/dist/lib/geojson-tiler/convert.d.ts.map +0 -1
  68. package/dist/lib/geojson-tiler/feature.d.ts.map +0 -1
  69. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +0 -44
  70. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +0 -1
  71. package/dist/lib/geojson-tiler/simplify.d.ts.map +0 -1
  72. package/dist/lib/geojson-tiler/tile.d.ts.map +0 -1
  73. package/dist/lib/geojson-tiler/transform.d.ts.map +0 -1
  74. package/dist/lib/geojson-tiler/wrap.d.ts.map +0 -1
  75. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -27
  76. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +0 -1
  77. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +0 -178
  78. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +0 -1
  79. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +0 -1
  80. package/src/helpers/mapbox-util-functions.ts +0 -82
  81. package/src/lib/binary-vector-tile/vector-tile-feature.ts +0 -203
  82. package/src/lib/binary-vector-tile/vector-tile-layer.ts +0 -113
  83. package/src/lib/binary-vector-tile/vector-tile.ts +0 -36
  84. package/src/lib/mapbox-vector-tile/LICENSE.txt +0 -31
  85. package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +0 -207
  86. /package/dist/lib/{geojson-tiler → geojsonvt}/clip.d.ts +0 -0
  87. /package/dist/lib/{geojson-tiler → geojsonvt}/clip.js +0 -0
  88. /package/dist/lib/{geojson-tiler → geojsonvt}/convert.js +0 -0
  89. /package/dist/lib/{geojson-tiler → geojsonvt}/feature.d.ts +0 -0
  90. /package/dist/lib/{geojson-tiler → geojsonvt}/feature.js +0 -0
  91. /package/dist/lib/{geojson-tiler → geojsonvt}/simplify.d.ts +0 -0
  92. /package/dist/lib/{geojson-tiler → geojsonvt}/simplify.js +0 -0
  93. /package/dist/lib/{geojson-tiler → geojsonvt}/tile.d.ts +0 -0
  94. /package/dist/lib/{geojson-tiler → geojsonvt}/tile.js +0 -0
  95. /package/dist/lib/{geojson-tiler → geojsonvt}/transform.d.ts +0 -0
  96. /package/dist/lib/{geojson-tiler → geojsonvt}/transform.js +0 -0
  97. /package/dist/lib/{geojson-tiler → geojsonvt}/wrap.d.ts +0 -0
  98. /package/dist/lib/{geojson-tiler → geojsonvt}/wrap.js +0 -0
  99. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.d.ts +0 -0
  100. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.js +0 -0
  101. /package/src/lib/{geojson-tiler → geojsonvt}/LICENSE +0 -0
  102. /package/src/lib/{geojson-tiler → geojsonvt}/clip.ts +0 -0
  103. /package/src/lib/{geojson-tiler → geojsonvt}/feature.ts +0 -0
  104. /package/src/lib/{geojson-tiler → geojsonvt}/simplify.ts +0 -0
  105. /package/src/lib/{geojson-tiler → geojsonvt}/transform.ts +0 -0
  106. /package/src/lib/{geojson-tiler → geojsonvt}/wrap.ts +0 -0
  107. /package/src/lib/{binary-vector-tile → vector-tile}/LICENSE.txt +0 -0
  108. /package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile.ts +0 -0
@@ -0,0 +1,407 @@
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
+ import {Feature} from '@loaders.gl/schema';
7
+ import {FlatFeature, FlatIndexedGeometry, GeojsonGeometryInfo} from '@loaders.gl/schema';
8
+ import Protobuf from 'pbf';
9
+ import {classifyRings, classifyRingsFlat, projectToLngLatFlat} from '../utils/geometry-utils';
10
+
11
+ export class VectorTileFeature {
12
+ properties: {[x: string]: string | number | boolean | null};
13
+ extent: any;
14
+ type: number;
15
+ id: number | null;
16
+ _pbf: Protobuf;
17
+ _geometry: number;
18
+ _keys: string[];
19
+ _values: (string | number | boolean | null)[];
20
+ _geometryInfo: GeojsonGeometryInfo;
21
+
22
+ static types: Readonly<string[]> = ['Unknown', 'Point', 'LineString', 'Polygon'];
23
+
24
+ // eslint-disable-next-line max-params
25
+ constructor(
26
+ pbf: Protobuf,
27
+ end: number,
28
+ extent: any,
29
+ keys: string[],
30
+ values: (string | number | boolean | null)[],
31
+ geometryInfo?: GeojsonGeometryInfo
32
+ ) {
33
+ // Public
34
+ this.properties = {};
35
+ this.extent = extent;
36
+ this.type = 0;
37
+ this.id = null;
38
+
39
+ // Private
40
+ this._pbf = pbf;
41
+ this._geometry = -1;
42
+ this._keys = keys;
43
+ this._values = values;
44
+
45
+ // Only used by binary tiles
46
+ this._geometryInfo = geometryInfo!;
47
+
48
+ pbf.readFields(readFeature, this, end);
49
+ }
50
+
51
+ toGeoJSON(
52
+ options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
53
+ ): Feature {
54
+ const coords = this.loadGeometry();
55
+
56
+ if (typeof options === 'function') {
57
+ return _toGeoJSON(this, coords, options);
58
+ }
59
+ const {x, y, z} = options;
60
+ const size = this.extent * Math.pow(2, z);
61
+ const x0 = this.extent * x;
62
+ const y0 = this.extent * y;
63
+
64
+ function project(line: number[]) {
65
+ for (let j = 0; j < line.length; j++) {
66
+ const p = line[j];
67
+ p[0] = ((p[0] + x0) * 360) / size - 180;
68
+ const y2 = 180 - ((p[1] + y0) * 360) / size;
69
+ p[1] = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90;
70
+ }
71
+ }
72
+ return _toGeoJSON(this, coords, project);
73
+ }
74
+
75
+ /**
76
+ *
77
+ * @param options
78
+ * @returns
79
+ */
80
+ toBinaryCoordinates(
81
+ options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
82
+ ): FlatFeature {
83
+ if (typeof options === 'function') {
84
+ return this._toBinaryCoordinates(options);
85
+ }
86
+ const tileIndex = options;
87
+ return this._toBinaryCoordinates((data: number[]) =>
88
+ projectToLngLatFlat(data, tileIndex, this.extent)
89
+ );
90
+ }
91
+
92
+ // eslint-disable-next-line max-statements
93
+ bbox() {
94
+ const pbf = this._pbf;
95
+ pbf.pos = this._geometry;
96
+
97
+ const end = pbf.readVarint() + pbf.pos;
98
+ let cmd = 1;
99
+ let length = 0;
100
+ let x = 0;
101
+ let y = 0;
102
+ let x1 = Infinity;
103
+ let x2 = -Infinity;
104
+ let y1 = Infinity;
105
+ let y2 = -Infinity;
106
+
107
+ while (pbf.pos < end) {
108
+ if (length <= 0) {
109
+ const cmdLen = pbf.readVarint();
110
+ cmd = cmdLen & 0x7;
111
+ length = cmdLen >> 3;
112
+ }
113
+
114
+ length--;
115
+
116
+ if (cmd === 1 || cmd === 2) {
117
+ x += pbf.readSVarint();
118
+ y += pbf.readSVarint();
119
+ if (x < x1) x1 = x;
120
+ if (x > x2) x2 = x;
121
+ if (y < y1) y1 = y;
122
+ if (y > y2) y2 = y;
123
+ } else if (cmd !== 7) {
124
+ throw new Error(`unknown command ${cmd}`);
125
+ }
126
+ }
127
+
128
+ return [x1, y1, x2, y2];
129
+ }
130
+
131
+ // BINARY HELPERS
132
+
133
+ /**
134
+ *
135
+ * @param transform
136
+ * @returns result
137
+ */
138
+ _toBinaryCoordinates(transform) {
139
+ // Expands the protobuf data to an intermediate Flat GeoJSON
140
+ // data format, which maps closely to the binary data buffers.
141
+ // It is similar to GeoJSON, but rather than storing the coordinates
142
+ // in multidimensional arrays, we have a 1D `data` with all the
143
+ // coordinates, and then index into this using the `indices`
144
+ // parameter, e.g.
145
+ //
146
+ // geometry: {
147
+ // type: 'Point', data: [1,2], indices: [0]
148
+ // }
149
+ // geometry: {
150
+ // type: 'LineString', data: [1,2,3,4,...], indices: [0]
151
+ // }
152
+ // geometry: {
153
+ // type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
154
+ // }
155
+ // Thus the indices member lets us look up the relevant range
156
+ // from the data array.
157
+ // The Multi* versions of the above types share the same data
158
+ // structure, just with multiple elements in the indices array
159
+ const geom = this.loadFlatGeometry();
160
+ let geometry;
161
+
162
+ // Apply the supplied transformation to data
163
+ transform(geom.data, this);
164
+
165
+ const coordLength = 2;
166
+
167
+ // eslint-disable-next-line default-case
168
+ switch (this.type) {
169
+ case 1: // Point
170
+ this._geometryInfo.pointFeaturesCount++;
171
+ this._geometryInfo.pointPositionsCount += geom.indices.length;
172
+ geometry = {type: 'Point', ...geom};
173
+ break;
174
+
175
+ case 2: // LineString
176
+ this._geometryInfo.lineFeaturesCount++;
177
+ this._geometryInfo.linePathsCount += geom.indices.length;
178
+ this._geometryInfo.linePositionsCount += geom.data.length / coordLength;
179
+ geometry = {type: 'LineString', ...geom};
180
+ break;
181
+
182
+ case 3: // Polygon
183
+ geometry = classifyRingsFlat(geom);
184
+
185
+ // Unlike Point & LineString geom.indices is a 2D array, thanks
186
+ // to the classifyRings method
187
+ this._geometryInfo.polygonFeaturesCount++;
188
+ this._geometryInfo.polygonObjectsCount += geometry.indices.length;
189
+
190
+ for (const indices of geometry.indices) {
191
+ this._geometryInfo.polygonRingsCount += indices.length;
192
+ }
193
+ this._geometryInfo.polygonPositionsCount += geometry.data.length / coordLength;
194
+
195
+ break;
196
+ default:
197
+ throw new Error(`Invalid geometry type: ${this.type}`);
198
+ }
199
+
200
+ const result: FlatFeature = {type: 'Feature', geometry, properties: this.properties};
201
+
202
+ if (this.id !== null) {
203
+ result.id = this.id;
204
+ }
205
+
206
+ return result;
207
+ }
208
+
209
+ // GEOJSON HELPER
210
+
211
+ // eslint-disable-next-line complexity, max-statements
212
+ loadGeometry(): number[][][] {
213
+ const pbf = this._pbf;
214
+ pbf.pos = this._geometry;
215
+
216
+ const end = pbf.readVarint() + pbf.pos;
217
+ let cmd = 1;
218
+ let length = 0;
219
+ let x = 0;
220
+ let y = 0;
221
+ const lines: number[][][] = [];
222
+ let line: number[][] | undefined;
223
+
224
+ while (pbf.pos < end) {
225
+ if (length <= 0) {
226
+ const cmdLen = pbf.readVarint();
227
+ cmd = cmdLen & 0x7;
228
+ length = cmdLen >> 3;
229
+ }
230
+
231
+ length--;
232
+
233
+ switch (cmd) {
234
+ case 1:
235
+ case 2:
236
+ x += pbf.readSVarint();
237
+ y += pbf.readSVarint();
238
+
239
+ if (cmd === 1) {
240
+ // moveTo
241
+ if (line) lines.push(line);
242
+ line = [];
243
+ }
244
+ if (line) line.push([x, y]);
245
+ break;
246
+ case 7:
247
+ // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
248
+ if (line) {
249
+ line.push(line[0].slice()); // closePolygon
250
+ }
251
+ break;
252
+ default:
253
+ throw new Error(`unknown command ${cmd}`);
254
+ }
255
+ }
256
+
257
+ if (line) lines.push(line);
258
+
259
+ return lines;
260
+ }
261
+
262
+ // eslint-disable-next-line complexity, max-statements
263
+ loadFlatGeometry(): FlatIndexedGeometry {
264
+ const pbf = this._pbf;
265
+ pbf.pos = this._geometry;
266
+
267
+ const endPos = pbf.readVarint() + pbf.pos;
268
+ let cmd = 1;
269
+ let cmdLen: number;
270
+ let length = 0;
271
+ let x = 0;
272
+ let y = 0;
273
+ let i = 0;
274
+
275
+ // Note: I attempted to replace the `data` array with a
276
+ // Float32Array, but performance was worse, both using
277
+ // `set()` and direct index access. Also, we cannot
278
+ // know how large the buffer should be, so it would
279
+ // increase memory usage
280
+ const indices: number[] = []; // Indices where geometries start
281
+ const data: number[] = []; // Flat array of coordinate data
282
+
283
+ while (pbf.pos < endPos) {
284
+ if (length <= 0) {
285
+ cmdLen = pbf.readVarint();
286
+ cmd = cmdLen & 0x7;
287
+ length = cmdLen >> 3;
288
+ }
289
+
290
+ length--;
291
+
292
+ if (cmd === 1 || cmd === 2) {
293
+ x += pbf.readSVarint();
294
+ y += pbf.readSVarint();
295
+
296
+ if (cmd === 1) {
297
+ // New line
298
+ indices.push(i);
299
+ }
300
+ data.push(x, y);
301
+ i += 2;
302
+ } else if (cmd === 7) {
303
+ // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
304
+ if (i > 0) {
305
+ const start = indices[indices.length - 1]; // start index of polygon
306
+ data.push(data[start], data[start + 1]); // closePolygon
307
+ i += 2;
308
+ }
309
+ } else {
310
+ throw new Error(`unknown command ${cmd}`);
311
+ }
312
+ }
313
+
314
+ return {data, indices};
315
+ }
316
+ }
317
+
318
+ function _toGeoJSON(vtFeature: VectorTileFeature, coords: number[][][], transform): Feature {
319
+ let type = VectorTileFeature.types[vtFeature.type];
320
+ let i: number;
321
+ let j: number;
322
+
323
+ let coordinates: number[][] | number[][][] | number[][][][];
324
+ switch (vtFeature.type) {
325
+ case 1:
326
+ const points: number[][] = [];
327
+ for (i = 0; i < coords.length; i++) {
328
+ points[i] = coords[i][0];
329
+ }
330
+ coordinates = points;
331
+ transform(coordinates, vtFeature);
332
+ break;
333
+
334
+ case 2:
335
+ coordinates = coords;
336
+ for (i = 0; i < coordinates.length; i++) {
337
+ transform(coordinates[i], vtFeature);
338
+ }
339
+ break;
340
+
341
+ case 3:
342
+ coordinates = classifyRings(coords);
343
+ for (i = 0; i < coordinates.length; i++) {
344
+ for (j = 0; j < coordinates[i].length; j++) {
345
+ transform(coordinates[i][j], vtFeature);
346
+ }
347
+ }
348
+ break;
349
+
350
+ default:
351
+ throw new Error('illegal vector tile type');
352
+ }
353
+
354
+ if (coordinates.length === 1) {
355
+ // @ts-expect-error
356
+ coordinates = coordinates[0];
357
+ } else {
358
+ type = `Multi${type}`;
359
+ }
360
+
361
+ const result: Feature = {
362
+ type: 'Feature',
363
+ geometry: {
364
+ type: type as any,
365
+ coordinates: coordinates as any
366
+ },
367
+ properties: vtFeature.properties
368
+ };
369
+
370
+ if (vtFeature.id !== null) {
371
+ result.id = vtFeature.id;
372
+ }
373
+
374
+ return result;
375
+ }
376
+
377
+ // PBF READER UTILS
378
+
379
+ /**
380
+ *
381
+ * @param tag
382
+ * @param feature
383
+ * @param pbf
384
+ */
385
+ function readFeature(tag: number, feature?: VectorTileFeature, pbf?: Protobuf): void {
386
+ if (feature && pbf) {
387
+ if (tag === 1) feature.id = pbf.readVarint();
388
+ else if (tag === 2) readTag(pbf, feature);
389
+ else if (tag === 3) feature.type = pbf.readVarint();
390
+ else if (tag === 4) feature._geometry = pbf.pos;
391
+ }
392
+ }
393
+
394
+ /**
395
+ *
396
+ * @param pbf
397
+ * @param feature
398
+ */
399
+ function readTag(pbf: Protobuf, feature: VectorTileFeature): void {
400
+ const end = pbf.readVarint() + pbf.pos;
401
+
402
+ while (pbf.pos < end) {
403
+ const key = feature._keys[pbf.readVarint()];
404
+ const value = feature._values[pbf.readVarint()];
405
+ feature.properties[key] = value;
406
+ }
407
+ }
@@ -7,6 +7,7 @@
7
7
 
8
8
  import Protobuf from 'pbf';
9
9
  import {VectorTileFeature} from './vector-tile-feature';
10
+ import {GeojsonGeometryInfo} from '@loaders.gl/schema';
10
11
 
11
12
  export class VectorTileLayer {
12
13
  version: number;
@@ -17,6 +18,7 @@ export class VectorTileLayer {
17
18
  _keys: string[];
18
19
  _values: (string | number | boolean | null)[];
19
20
  _features: number[];
21
+
20
22
  constructor(pbf: Protobuf, end: number) {
21
23
  // Public
22
24
  this.version = 1;
@@ -40,8 +42,7 @@ export class VectorTileLayer {
40
42
  * @param index
41
43
  * @returns feature
42
44
  */
43
-
44
- feature(i: number): VectorTileFeature {
45
+ getGeoJSONFeature(i: number): VectorTileFeature {
45
46
  if (i < 0 || i >= this._features.length) {
46
47
  throw new Error('feature index out of bounds');
47
48
  }
@@ -51,6 +52,31 @@ export class VectorTileLayer {
51
52
  const end = this._pbf.readVarint() + this._pbf.pos;
52
53
  return new VectorTileFeature(this._pbf, end, this.extent, this._keys, this._values);
53
54
  }
55
+
56
+ /**
57
+ * return binary feature `i` from this layer as a `VectorTileFeature`
58
+ *
59
+ * @param index
60
+ * @param geometryInfo
61
+ * @returns binary feature
62
+ */
63
+ getBinaryFeature(i: number, geometryInfo: GeojsonGeometryInfo): VectorTileFeature {
64
+ if (i < 0 || i >= this._features.length) {
65
+ throw new Error('feature index out of bounds');
66
+ }
67
+
68
+ this._pbf.pos = this._features[i];
69
+
70
+ const end = this._pbf.readVarint() + this._pbf.pos;
71
+ return new VectorTileFeature(
72
+ this._pbf,
73
+ end,
74
+ this.extent,
75
+ this._keys,
76
+ this._values,
77
+ geometryInfo
78
+ );
79
+ }
54
80
  }
55
81
 
56
82
  /**
package/src/mvt-loader.ts CHANGED
@@ -3,21 +3,29 @@
3
3
  // Copyright vis.gl contributors
4
4
 
5
5
  import type {Loader, LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';
6
+ // import type {MVTOptions} from './lib/types';
6
7
  import {parseMVT} from './lib/parse-mvt';
7
- import type {MVTOptions} from './lib/types';
8
8
 
9
9
  // __VERSION__ is injected by babel-plugin-version-inline
10
10
  // @ts-ignore TS2304: Cannot find name '__VERSION__'.
11
11
  const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
12
12
 
13
13
  export type MVTLoaderOptions = LoaderOptions & {
14
- mvt?: MVTOptions & {
14
+ mvt?: {
15
+ /** Shape of returned data */
16
+ shape?: 'geojson-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
17
+ /** `wgs84`: coordinates in long, lat (`tileIndex` must be provided. `local` coordinates are `0-1` from tile origin */
18
+ coordinates?: 'wgs84' | 'local';
19
+ /** An object containing tile index values (`x`, `y`, `z`) to reproject features' coordinates into WGS84. Mandatory with `wgs84` coordinates option. */
20
+ tileIndex?: {x: number; y: number; z: number};
21
+ /** If provided, stored the layer name of each feature is added to `feature.properties[layerProperty]`. */
22
+ layerProperty?: string | number;
23
+ /** layer filter. If provided, only features belonging to the named layers will be included, otherwise features from all layers are returned. */
24
+ layers?: string[];
15
25
  /** Override the URL to the worker bundle (by default loads from unpkg.com) */
16
26
  workerUrl?: string;
17
27
  };
18
28
  gis?: {
19
- /** `true`: parser will output the data in binary format. Equivalent to loading the data as GeoJSON and then applying geojsonToBinary */
20
- binary?: boolean;
21
29
  /** @deprecated. Use options.mvt.shape */
22
30
  format?: 'geojson-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
23
31
  };
@@ -49,8 +57,8 @@ export const MVTWorkerLoader = {
49
57
  shape: 'geojson',
50
58
  coordinates: 'local',
51
59
  layerProperty: 'layerName',
52
- layers: undefined,
53
- tileIndex: null
60
+ layers: undefined!,
61
+ tileIndex: undefined!
54
62
  }
55
63
  }
56
64
  } as const satisfies Loader<
package/src/mvt-source.ts CHANGED
@@ -102,7 +102,7 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
102
102
  }
103
103
 
104
104
  async getTile(tileParams: GetTileParameters): Promise<ArrayBuffer | null> {
105
- const {x, y, zoom: z} = tileParams;
105
+ const {x, y, z} = tileParams;
106
106
  const tileUrl = this.getTileURL(x, y, z);
107
107
  const response = await this.fetch(tileUrl);
108
108
  if (!response.ok) {
@@ -120,7 +120,7 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
120
120
  // const metadata = await this.metadata;
121
121
  // mimeType = metadata?.tileMIMEType || 'application/vnd.mapbox-vector-tile';
122
122
 
123
- const arrayBuffer = await this.getTile({x, y, zoom: z, layers: []});
123
+ const arrayBuffer = await this.getTile({x, y, z, layers: []});
124
124
  if (arrayBuffer === null) {
125
125
  return null;
126
126
  }
@@ -130,7 +130,7 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
130
130
  this.mimeType || imageMetadata?.mimeType || 'application/vnd.mapbox-vector-tile';
131
131
  switch (this.mimeType) {
132
132
  case 'application/vnd.mapbox-vector-tile':
133
- return await this._parseVectorTile(arrayBuffer, {x, y, zoom: z, layers: []});
133
+ return await this._parseVectorTile(arrayBuffer, {x, y, z, layers: []});
134
134
  default:
135
135
  return await this._parseImageTile(arrayBuffer);
136
136
  }
@@ -162,7 +162,7 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
162
162
  shape: 'geojson-table',
163
163
  mvt: {
164
164
  coordinates: 'wgs84',
165
- tileIndex: {x: tileParams.x, y: tileParams.y, z: tileParams.zoom},
165
+ tileIndex: {x: tileParams.x, y: tileParams.y, z: tileParams.z},
166
166
  ...(this.loadOptions as MVTLoaderOptions)?.mvt
167
167
  },
168
168
  ...this.loadOptions
@@ -1,35 +0,0 @@
1
- import Protobuf from 'pbf';
2
- import { FlatIndexedGeometry, FlatPolygon } from '@loaders.gl/schema';
3
- import { BinaryVectorTileFeature } from "../lib/binary-vector-tile/vector-tile-feature.js";
4
- /**
5
- * Classifies an array of rings into polygons with outer rings and holes
6
- * The function also detects holes which have zero area and
7
- * removes them. In doing so it modifies the input
8
- * `geom.data` array to remove the unneeded data
9
- *
10
- * @param geometry
11
- * @returns object
12
- */
13
- export declare function classifyRings(geom: FlatIndexedGeometry): FlatPolygon;
14
- /**
15
- *
16
- * @param data
17
- * @param x0
18
- * @param y0
19
- * @param size
20
- */
21
- export declare function project(data: number[], x0: number, y0: number, size: number): void;
22
- /**
23
- * All code below is unchanged from the original Mapbox implemenation
24
- *
25
- * @param tag
26
- * @param feature
27
- * @param pbf
28
- */
29
- export declare function readFeature(tag: number, feature?: BinaryVectorTileFeature, pbf?: Protobuf): void;
30
- /**
31
- * @param pbf
32
- * @param feature
33
- */
34
- export declare function readTag(pbf: Protobuf, feature: BinaryVectorTileFeature): void;
35
- //# sourceMappingURL=binary-util-functions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"binary-util-functions.d.ts","sourceRoot":"","sources":["../../src/helpers/binary-util-functions.ts"],"names":[],"mappings":"AAIA,OAAO,QAAQ,MAAM,KAAK,CAAC;AAE3B,OAAO,EAAC,mBAAmB,EAAE,WAAW,EAAC,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAC,uBAAuB,EAAC,yDAAsD;AAEtF;;;;;;;;GAQG;AAEH,wBAAgB,aAAa,CAAC,IAAI,EAAE,mBAAmB,GAAG,WAAW,CA4DpE;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAMlF;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,uBAAuB,EAAE,GAAG,CAAC,EAAE,QAAQ,GAAG,IAAI,CAOhG;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,uBAAuB,GAAG,IAAI,CAQ7E"}
@@ -1,29 +0,0 @@
1
- import Protobuf from 'pbf';
2
- import { MVTMapboxGeometry } from "../lib/types.js";
3
- import { VectorTileFeature } from "../lib/mapbox-vector-tile/vector-tile-feature.js";
4
- /**
5
- * Classifies an array of rings into polygons with outer rings and holes
6
- * @param rings
7
- * @returns polygons
8
- */
9
- export declare function classifyRings(rings: MVTMapboxGeometry): MVTMapboxGeometry[] | number[][][];
10
- /**
11
- *
12
- * @param ring
13
- * @returns sum
14
- */
15
- export declare function signedArea(ring: number[][]): number;
16
- /**
17
- *
18
- * @param tag
19
- * @param feature
20
- * @param pbf
21
- */
22
- export declare function readFeature(tag: number, feature?: VectorTileFeature, pbf?: Protobuf): void;
23
- /**
24
- *
25
- * @param pbf
26
- * @param feature
27
- */
28
- export declare function readTag(pbf: Protobuf, feature: VectorTileFeature): void;
29
- //# sourceMappingURL=mapbox-util-functions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapbox-util-functions.d.ts","sourceRoot":"","sources":["../../src/helpers/mapbox-util-functions.ts"],"names":[],"mappings":"AAIA,OAAO,QAAQ,MAAM,KAAK,CAAC;AAC3B,OAAO,EAAC,iBAAiB,EAAC,wBAAqB;AAC/C,OAAO,EAAC,iBAAiB,EAAC,yDAAsD;AAEhF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,iBAAiB,sCAuBrD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,UAQ1C;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,EAAE,GAAG,CAAC,EAAE,QAAQ,GAAG,IAAI,CAO1F;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,iBAAiB,GAAG,IAAI,CAQvE"}
@@ -1,78 +0,0 @@
1
- // loaders.gl
2
- // SPDX-License-Identifier: MIT
3
- // Copyright vis.gl contributors
4
- /**
5
- * Classifies an array of rings into polygons with outer rings and holes
6
- * @param rings
7
- * @returns polygons
8
- */
9
- export function classifyRings(rings) {
10
- const len = rings.length;
11
- if (len <= 1)
12
- return [rings];
13
- const polygons = [];
14
- let polygon;
15
- let ccw;
16
- for (let i = 0; i < len; i++) {
17
- const area = signedArea(rings[i]);
18
- if (area === 0)
19
- continue; // eslint-disable-line no-continue
20
- if (ccw === undefined)
21
- ccw = area < 0;
22
- if (ccw === area < 0) {
23
- if (polygon)
24
- polygons.push(polygon);
25
- polygon = [rings[i]];
26
- }
27
- else if (polygon)
28
- polygon.push(rings[i]);
29
- }
30
- if (polygon)
31
- polygons.push(polygon);
32
- return polygons;
33
- }
34
- /**
35
- *
36
- * @param ring
37
- * @returns sum
38
- */
39
- export function signedArea(ring) {
40
- let sum = 0;
41
- for (let i = 0, j = ring.length - 1, p1, p2; i < ring.length; j = i++) {
42
- p1 = ring[i];
43
- p2 = ring[j];
44
- sum += (p2[0] - p1[0]) * (p1[1] + p2[1]);
45
- }
46
- return sum;
47
- }
48
- /**
49
- *
50
- * @param tag
51
- * @param feature
52
- * @param pbf
53
- */
54
- export function readFeature(tag, feature, pbf) {
55
- if (feature && pbf) {
56
- if (tag === 1)
57
- feature.id = pbf.readVarint();
58
- else if (tag === 2)
59
- readTag(pbf, feature);
60
- else if (tag === 3)
61
- feature.type = pbf.readVarint();
62
- else if (tag === 4)
63
- feature._geometry = pbf.pos;
64
- }
65
- }
66
- /**
67
- *
68
- * @param pbf
69
- * @param feature
70
- */
71
- export function readTag(pbf, feature) {
72
- const end = pbf.readVarint() + pbf.pos;
73
- while (pbf.pos < end) {
74
- const key = feature._keys[pbf.readVarint()];
75
- const value = feature._values[pbf.readVarint()];
76
- feature.properties[key] = value;
77
- }
78
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"vector-tile-feature.d.ts","sourceRoot":"","sources":["../../../src/lib/binary-vector-tile/vector-tile-feature.ts"],"names":[],"mappings":"AAMA,OAAO,QAAQ,MAAM,KAAK,CAAC;AAC3B,OAAO,EAAC,WAAW,EAAE,mBAAmB,EAAE,mBAAmB,EAAC,MAAM,oBAAoB,CAAC;AACzF,OAAO,EAAC,aAAa,EAAuB,+CAA4C;AAWxF,eAAO,MAAM,YAAY;;CAExB,CAAC;AAEF,qBAAa,uBAAuB;IAClC,UAAU,EAAE;QAAC,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;KAAC,CAAC;IAC5D,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAClB,IAAI,EAAE,QAAQ,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC;IAC9C,aAAa,EAAE,mBAAmB,CAAC;gBAIjC,GAAG,EAAE,QAAQ,EACb,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,GAAG,EACX,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE,EAC5C,YAAY,EAAE,mBAAmB;IAmBnC,YAAY,IAAI,mBAAmB;IAqDnC;;;;OAIG;IACH,oBAAoB,CAAC,SAAS,KAAA;;;;;;;IAuE9B,mBAAmB,CACjB,OAAO,EAAE;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE;QAAC,MAAM,EAAE,GAAG,CAAA;KAAC,KAAK,IAAI,CAAC,GAC9F,WAAW;CAWf"}