@loaders.gl/mvt 4.3.0-alpha.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 (77) hide show
  1. package/dist/dist.dev.js +568 -175
  2. package/dist/dist.min.js +1 -1
  3. package/dist/index.cjs +226 -163
  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 +1 -1
  9. package/dist/lib/parse-mvt.js +2 -30
  10. package/dist/lib/parse-tilejson.d.ts +4 -4
  11. package/dist/lib/parse-tilejson.d.ts.map +1 -1
  12. package/dist/lib/utils/geometry-utils.d.ts +38 -1
  13. package/dist/lib/utils/geometry-utils.d.ts.map +1 -1
  14. package/dist/lib/utils/geometry-utils.js +65 -6
  15. package/dist/lib/vector-tile/vector-tile-feature.d.ts +28 -9
  16. package/dist/lib/vector-tile/vector-tile-feature.d.ts.map +1 -1
  17. package/dist/lib/vector-tile/vector-tile-feature.js +47 -50
  18. package/dist/lib/{geojsonvt → vector-tiler}/clip.d.ts +3 -3
  19. package/dist/lib/vector-tiler/clip.d.ts.map +1 -0
  20. package/dist/lib/vector-tiler/convert.d.ts +18 -0
  21. package/dist/lib/vector-tiler/convert.d.ts.map +1 -0
  22. package/dist/lib/{geojsonvt → vector-tiler}/convert.js +8 -1
  23. package/dist/lib/vector-tiler/feature.d.ts +3 -0
  24. package/dist/lib/vector-tiler/feature.d.ts.map +1 -0
  25. package/dist/lib/vector-tiler/simplify.d.ts.map +1 -0
  26. package/dist/lib/{geojsonvt → vector-tiler}/tile.d.ts +4 -4
  27. package/dist/lib/vector-tiler/tile.d.ts.map +1 -0
  28. package/dist/lib/vector-tiler/transform.d.ts +7 -0
  29. package/dist/lib/vector-tiler/transform.d.ts.map +1 -0
  30. package/dist/lib/{geojsonvt → vector-tiler}/wrap.d.ts +2 -2
  31. package/dist/lib/vector-tiler/wrap.d.ts.map +1 -0
  32. package/dist/mvt-loader.d.ts +1 -1
  33. package/dist/mvt-loader.js +1 -1
  34. package/dist/mvt-source.d.ts +4 -4
  35. package/dist/mvt-source.d.ts.map +1 -1
  36. package/dist/mvt-source.js +4 -4
  37. package/dist/mvt-worker.js +101 -56
  38. package/dist/table-tile-source.d.ts +118 -0
  39. package/dist/table-tile-source.d.ts.map +1 -0
  40. package/dist/{geojson-tile-source.js → table-tile-source.js} +154 -103
  41. package/dist/tilejson-loader.js +1 -1
  42. package/package.json +7 -6
  43. package/src/index.ts +3 -3
  44. package/src/lib/parse-mvt.ts +4 -33
  45. package/src/lib/parse-tilejson.ts +5 -5
  46. package/src/lib/utils/geometry-utils.ts +66 -1
  47. package/src/lib/vector-tile/vector-tile-feature.ts +65 -56
  48. package/src/lib/{geojsonvt → vector-tiler}/clip.ts +4 -4
  49. package/src/lib/{geojsonvt → vector-tiler}/convert.ts +18 -8
  50. package/src/lib/{geojsonvt → vector-tiler}/feature.ts +3 -3
  51. package/src/lib/{geojsonvt → vector-tiler}/tile.ts +8 -8
  52. package/src/lib/{geojsonvt → vector-tiler}/transform.ts +2 -2
  53. package/src/lib/{geojsonvt → vector-tiler}/wrap.ts +5 -5
  54. package/src/mvt-source.ts +6 -7
  55. package/src/table-tile-source.ts +508 -0
  56. package/dist/geojson-tile-source.d.ts +0 -79
  57. package/dist/geojson-tile-source.d.ts.map +0 -1
  58. package/dist/lib/geojsonvt/clip.d.ts.map +0 -1
  59. package/dist/lib/geojsonvt/convert.d.ts +0 -10
  60. package/dist/lib/geojsonvt/convert.d.ts.map +0 -1
  61. package/dist/lib/geojsonvt/feature.d.ts +0 -3
  62. package/dist/lib/geojsonvt/feature.d.ts.map +0 -1
  63. package/dist/lib/geojsonvt/simplify.d.ts.map +0 -1
  64. package/dist/lib/geojsonvt/tile.d.ts.map +0 -1
  65. package/dist/lib/geojsonvt/transform.d.ts +0 -7
  66. package/dist/lib/geojsonvt/transform.d.ts.map +0 -1
  67. package/dist/lib/geojsonvt/wrap.d.ts.map +0 -1
  68. package/src/geojson-tile-source.ts +0 -422
  69. /package/dist/lib/{geojsonvt → vector-tiler}/clip.js +0 -0
  70. /package/dist/lib/{geojsonvt → vector-tiler}/feature.js +0 -0
  71. /package/dist/lib/{geojsonvt → vector-tiler}/simplify.d.ts +0 -0
  72. /package/dist/lib/{geojsonvt → vector-tiler}/simplify.js +0 -0
  73. /package/dist/lib/{geojsonvt → vector-tiler}/tile.js +0 -0
  74. /package/dist/lib/{geojsonvt → vector-tiler}/transform.js +0 -0
  75. /package/dist/lib/{geojsonvt → vector-tiler}/wrap.js +0 -0
  76. /package/src/lib/{geojsonvt → vector-tiler}/LICENSE +0 -0
  77. /package/src/lib/{geojsonvt → vector-tiler}/simplify.ts +0 -0
package/dist/index.cjs CHANGED
@@ -35,10 +35,10 @@ var __publicField = (obj, key, value) => {
35
35
  // dist/index.js
36
36
  var dist_exports = {};
37
37
  __export(dist_exports, {
38
- GeoJSONTileSource: () => GeoJSONTileSource,
39
38
  MVTLoader: () => MVTLoader,
40
39
  MVTSource: () => MVTSource,
41
40
  MVTWorkerLoader: () => MVTWorkerLoader,
41
+ TableTileSource: () => TableTileSource,
42
42
  TileJSONLoader: () => TileJSONLoader
43
43
  });
44
44
  module.exports = __toCommonJS(dist_exports);
@@ -59,6 +59,39 @@ function signedArea(ring) {
59
59
  }
60
60
  return sum;
61
61
  }
62
+ function convertToLocalCoordinates(coordinates, extent) {
63
+ if (Array.isArray(coordinates[0])) {
64
+ for (const subcoords of coordinates) {
65
+ convertToLocalCoordinates(subcoords, extent);
66
+ }
67
+ return;
68
+ }
69
+ const p = coordinates;
70
+ p[0] /= extent;
71
+ p[1] /= extent;
72
+ }
73
+ function convertToLocalCoordinatesFlat(data, extent) {
74
+ for (let i = 0, il = data.length; i < il; ++i) {
75
+ data[i] /= extent;
76
+ }
77
+ }
78
+ function projectToLngLat(line, tileIndex, extent) {
79
+ if (typeof line[0][0] !== "number") {
80
+ for (const point of line) {
81
+ projectToLngLat(point, tileIndex, extent);
82
+ }
83
+ return;
84
+ }
85
+ const size = extent * Math.pow(2, tileIndex.z);
86
+ const x0 = extent * tileIndex.x;
87
+ const y0 = extent * tileIndex.y;
88
+ for (let j = 0; j < line.length; j++) {
89
+ const p = line[j];
90
+ p[0] = (p[0] + x0) * 360 / size - 180;
91
+ const y2 = 180 - (p[1] + y0) * 360 / size;
92
+ p[1] = 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
93
+ }
94
+ }
62
95
  function projectToLngLatFlat(data, tileIndex, extent) {
63
96
  const { x, y, z } = tileIndex;
64
97
  const size = extent * Math.pow(2, z);
@@ -168,37 +201,30 @@ var VectorTileFeature = class {
168
201
  this._geometryInfo = geometryInfo;
169
202
  pbf.readFields(readFeature, this, end);
170
203
  }
171
- toGeoJSON(options) {
204
+ toGeoJSONFeature(coordinates, tileIndex) {
172
205
  const coords = this.loadGeometry();
173
- if (typeof options === "function") {
174
- return _toGeoJSON(this, coords, options);
175
- }
176
- const { x, y, z } = options;
177
- const size = this.extent * Math.pow(2, z);
178
- const x0 = this.extent * x;
179
- const y0 = this.extent * y;
180
- function project(line) {
181
- for (let j = 0; j < line.length; j++) {
182
- const p = line[j];
183
- p[0] = (p[0] + x0) * 360 / size - 180;
184
- const y2 = 180 - (p[1] + y0) * 360 / size;
185
- p[1] = 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
186
- }
206
+ switch (coordinates) {
207
+ case "wgs84":
208
+ return _toGeoJSONFeature(this, coords, (line) => projectToLngLat(line, tileIndex, this.extent));
209
+ default:
210
+ return _toGeoJSONFeature(this, coords, convertToLocalCoordinates);
187
211
  }
188
- return _toGeoJSON(this, coords, project);
189
212
  }
190
213
  /**
191
214
  *
192
215
  * @param options
193
216
  * @returns
194
217
  */
195
- toBinaryCoordinates(options) {
196
- if (typeof options === "function") {
197
- return this._toBinaryCoordinates(options);
218
+ toBinaryFeature(coordinates, tileIndex) {
219
+ const geom = this.loadFlatGeometry();
220
+ switch (coordinates) {
221
+ case "wgs84":
222
+ return this._toBinaryCoordinates(geom, (coords) => projectToLngLatFlat(coords, tileIndex, this.extent));
223
+ default:
224
+ return this._toBinaryCoordinates(geom, convertToLocalCoordinatesFlat);
198
225
  }
199
- const tileIndex = options;
200
- return this._toBinaryCoordinates((data) => projectToLngLatFlat(data, tileIndex, this.extent));
201
226
  }
227
+ /** Read a bounding box from the feature */
202
228
  // eslint-disable-next-line max-statements
203
229
  bbox() {
204
230
  const pbf = this._pbf;
@@ -242,10 +268,9 @@ var VectorTileFeature = class {
242
268
  * @param transform
243
269
  * @returns result
244
270
  */
245
- _toBinaryCoordinates(transform) {
246
- const geom = this.loadFlatGeometry();
271
+ _toBinaryCoordinates(geom, transform) {
247
272
  let geometry;
248
- transform(geom.data, this);
273
+ transform(geom.data, this.extent);
249
274
  const coordLength = 2;
250
275
  switch (this.type) {
251
276
  case 1:
@@ -322,6 +347,28 @@ var VectorTileFeature = class {
322
347
  lines.push(line);
323
348
  return lines;
324
349
  }
350
+ /**
351
+ * Expands the protobuf data to an intermediate Flat GeoJSON
352
+ * data format, which maps closely to the binary data buffers.
353
+ * It is similar to GeoJSON, but rather than storing the coordinates
354
+ * in multidimensional arrays, we have a 1D `data` with all the
355
+ * coordinates, and then index into this using the `indices`
356
+ * parameter, e.g.
357
+ *
358
+ * geometry: {
359
+ * type: 'Point', data: [1,2], indices: [0]
360
+ * }
361
+ * geometry: {
362
+ * type: 'LineString', data: [1,2,3,4,...], indices: [0]
363
+ * }
364
+ * geometry: {
365
+ * type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
366
+ * }
367
+ * Thus the indices member lets us look up the relevant range
368
+ * from the data array.
369
+ * The Multi* versions of the above types share the same data
370
+ * structure, just with multiple elements in the indices array
371
+ */
325
372
  // eslint-disable-next-line complexity, max-statements
326
373
  loadFlatGeometry() {
327
374
  const pbf = this._pbf;
@@ -364,7 +411,7 @@ var VectorTileFeature = class {
364
411
  }
365
412
  };
366
413
  __publicField(VectorTileFeature, "types", ["Unknown", "Point", "LineString", "Polygon"]);
367
- function _toGeoJSON(vtFeature, coords, transform) {
414
+ function _toGeoJSONFeature(vtFeature, coords, transform) {
368
415
  let type = VectorTileFeature.types[vtFeature.type];
369
416
  let i;
370
417
  let j;
@@ -376,19 +423,19 @@ function _toGeoJSON(vtFeature, coords, transform) {
376
423
  points[i] = coords[i][0];
377
424
  }
378
425
  coordinates = points;
379
- transform(coordinates, vtFeature);
426
+ transform(coordinates, vtFeature.extent);
380
427
  break;
381
428
  case 2:
382
429
  coordinates = coords;
383
430
  for (i = 0; i < coordinates.length; i++) {
384
- transform(coordinates[i], vtFeature);
431
+ transform(coordinates[i], vtFeature.extent);
385
432
  }
386
433
  break;
387
434
  case 3:
388
435
  coordinates = classifyRings(coords);
389
436
  for (i = 0; i < coordinates.length; i++) {
390
437
  for (j = 0; j < coordinates[i].length; j++) {
391
- transform(coordinates[i][j], vtFeature);
438
+ transform(coordinates[i][j], vtFeature.extent);
392
439
  }
393
440
  }
394
441
  break;
@@ -409,7 +456,8 @@ function _toGeoJSON(vtFeature, coords, transform) {
409
456
  properties: vtFeature.properties
410
457
  };
411
458
  if (vtFeature.id !== null) {
412
- result.id = vtFeature.id;
459
+ result.properties ||= {};
460
+ result.properties.id = vtFeature.id;
413
461
  }
414
462
  return result;
415
463
  }
@@ -627,10 +675,7 @@ function checkOptions(options) {
627
675
  return options.mvt;
628
676
  }
629
677
  function getDecodedFeature(feature, options, layerName) {
630
- const decodedFeature = feature.toGeoJSON(
631
- // @ts-expect-error What is going on here?
632
- options.coordinates === "wgs84" ? options.tileIndex : transformToLocalCoordinates
633
- );
678
+ const decodedFeature = feature.toGeoJSONFeature(options.coordinates || "local", options.tileIndex);
634
679
  if (options.layerProperty) {
635
680
  decodedFeature.properties ||= {};
636
681
  decodedFeature.properties[options.layerProperty] = layerName;
@@ -638,32 +683,15 @@ function getDecodedFeature(feature, options, layerName) {
638
683
  return decodedFeature;
639
684
  }
640
685
  function getDecodedFeatureBinary(feature, options, layerName) {
641
- const decodedFeature = feature.toBinaryCoordinates(
642
- // @ts-expect-error
643
- options.coordinates === "wgs84" ? options.tileIndex : transformToLocalCoordinatesBinary
644
- );
686
+ const decodedFeature = feature.toBinaryFeature(options.coordinates || "local", options.tileIndex);
645
687
  if (options.layerProperty && decodedFeature.properties) {
646
688
  decodedFeature.properties[options.layerProperty] = layerName;
647
689
  }
648
690
  return decodedFeature;
649
691
  }
650
- function transformToLocalCoordinates(line, feature) {
651
- const { extent } = feature;
652
- for (let i = 0; i < line.length; i++) {
653
- const p = line[i];
654
- p[0] /= extent;
655
- p[1] /= extent;
656
- }
657
- }
658
- function transformToLocalCoordinatesBinary(data, feature) {
659
- const { extent } = feature;
660
- for (let i = 0, il = data.length; i < il; ++i) {
661
- data[i] /= extent;
662
- }
663
- }
664
692
 
665
693
  // dist/mvt-loader.js
666
- var VERSION = true ? "4.2.0" : "latest";
694
+ var VERSION = true ? "4.3.0-alpha.1" : "latest";
667
695
  var MVTWorkerLoader = {
668
696
  dataType: null,
669
697
  batchType: null,
@@ -905,7 +933,7 @@ function attributeTypeToFieldType(aType) {
905
933
  }
906
934
 
907
935
  // dist/tilejson-loader.js
908
- var VERSION2 = true ? "4.2.0" : "latest";
936
+ var VERSION2 = true ? "4.3.0-alpha.1" : "latest";
909
937
  var TileJSONLoader = {
910
938
  dataType: null,
911
939
  batchType: null,
@@ -985,8 +1013,8 @@ var MVTSource = class extends import_loader_utils2.DataSource {
985
1013
  getTileMIMEType() {
986
1014
  return this.mimeType;
987
1015
  }
988
- async getTile(tileParams) {
989
- const { x, y, z } = tileParams;
1016
+ async getTile(parameters) {
1017
+ const { x, y, z } = parameters;
990
1018
  const tileUrl = this.getTileURL(x, y, z);
991
1019
  const response = await this.fetch(tileUrl);
992
1020
  if (!response.ok) {
@@ -997,8 +1025,8 @@ var MVTSource = class extends import_loader_utils2.DataSource {
997
1025
  }
998
1026
  // Tile Source interface implementation: deck.gl compatible API
999
1027
  // TODO - currently only handles image tiles, not vector tiles
1000
- async getTileData(tileParams) {
1001
- const { x, y, z } = tileParams.index;
1028
+ async getTileData(parameters) {
1029
+ const { x, y, z } = parameters.index;
1002
1030
  const arrayBuffer = await this.getTile({ x, y, z, layers: [] });
1003
1031
  if (arrayBuffer === null) {
1004
1032
  return null;
@@ -1078,7 +1106,12 @@ function stringHash(s) {
1078
1106
  return Math.abs(s.split("").reduce((a, b) => (a << 5) - a + b.charCodeAt(0) | 0, 0));
1079
1107
  }
1080
1108
 
1081
- // dist/lib/geojsonvt/simplify.js
1109
+ // dist/table-tile-source.js
1110
+ var import_loader_utils3 = require("@loaders.gl/loader-utils");
1111
+ var import_schema = require("@loaders.gl/schema");
1112
+ var import_stats = require("@probe.gl/stats");
1113
+
1114
+ // dist/lib/vector-tiler/simplify.js
1082
1115
  function simplify(coords, first, last, sqTolerance) {
1083
1116
  let maxSqDist = sqTolerance;
1084
1117
  const mid = last - first >> 1;
@@ -1127,7 +1160,7 @@ function getSqSegDist(px, py, x, y, bx, by) {
1127
1160
  return dx * dx + dy * dy;
1128
1161
  }
1129
1162
 
1130
- // dist/lib/geojsonvt/feature.js
1163
+ // dist/lib/vector-tiler/feature.js
1131
1164
  function createFeature(id, type, geom, tags) {
1132
1165
  const feature = {
1133
1166
  // eslint-disable-next-line
@@ -1164,7 +1197,7 @@ function calcLineBBox(feature, geom) {
1164
1197
  }
1165
1198
  }
1166
1199
 
1167
- // dist/lib/geojsonvt/convert.js
1200
+ // dist/lib/vector-tiler/convert.js
1168
1201
  function convert(data, options) {
1169
1202
  const features = [];
1170
1203
  if (data.type === "FeatureCollection") {
@@ -1277,7 +1310,7 @@ function projectY(y) {
1277
1310
  return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
1278
1311
  }
1279
1312
 
1280
- // dist/lib/geojsonvt/clip.js
1313
+ // dist/lib/vector-tiler/clip.js
1281
1314
  function clip(features, scale, k1, k2, axis, minAll, maxAll, options) {
1282
1315
  k1 /= scale;
1283
1316
  k2 /= scale;
@@ -1442,7 +1475,7 @@ function intersectY(out, ax, ay, bx, by, y) {
1442
1475
  return t;
1443
1476
  }
1444
1477
 
1445
- // dist/lib/geojsonvt/wrap.js
1478
+ // dist/lib/vector-tiler/wrap.js
1446
1479
  function wrap(features, options) {
1447
1480
  const buffer = options.buffer / options.extent;
1448
1481
  let merged = features;
@@ -1499,7 +1532,7 @@ function shiftCoords(points, offset) {
1499
1532
  return newPoints;
1500
1533
  }
1501
1534
 
1502
- // dist/lib/geojsonvt/transform.js
1535
+ // dist/lib/vector-tiler/transform.js
1503
1536
  function transformTile(tile, extent) {
1504
1537
  if (tile.transformed) {
1505
1538
  return tile;
@@ -1532,7 +1565,7 @@ function transformPoint(x, y, extent, z2, tx, ty) {
1532
1565
  return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))];
1533
1566
  }
1534
1567
 
1535
- // dist/lib/geojsonvt/tile.js
1568
+ // dist/lib/vector-tiler/tile.js
1536
1569
  function createTile(features, z, tx, ty, options) {
1537
1570
  const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
1538
1571
  const tile = {
@@ -1638,57 +1671,46 @@ function rewind(ring, clockwise) {
1638
1671
  }
1639
1672
  }
1640
1673
 
1641
- // dist/geojson-tile-source.js
1642
- var _GeoJSONTileSource = class {
1674
+ // dist/table-tile-source.js
1675
+ var _TableTileSource = class {
1676
+ /** Stats for this TableTileSource */
1677
+ stats = new import_stats.Stats({
1678
+ id: "table-tile-source",
1679
+ stats: [new import_stats.Stat("tiles", "count"), new import_stats.Stat("features", "count")]
1680
+ });
1681
+ /** MIME type of the tiles emitted by this tile source */
1643
1682
  mimeType = "application/vnd.mapbox-vector-tile";
1644
- options;
1645
- // tiles and tileCoords are part of the public API
1683
+ localCoordinates = true;
1684
+ /** The props that this tile source was created with */
1685
+ props;
1686
+ /* Schema of the data */
1687
+ schema = null;
1688
+ /** Map of generated tiles, indexed by stringified tile coordinates */
1646
1689
  tiles = {};
1690
+ /** Array of tile coordinates */
1647
1691
  tileCoords = [];
1648
- stats = {};
1649
- total = 0;
1650
- /** Sync methods can be called: the input data promise has been resolved and initial top-level tiling is done */
1692
+ /** Input data has loaded, initial top-level tiling is done, sync methods can now be called */
1651
1693
  ready;
1652
- constructor(data, options) {
1653
- this.options = { ..._GeoJSONTileSource.defaultOptions, ...options };
1694
+ /** Metadata for the tile source (generated TileJSON/tilestats */
1695
+ metadata;
1696
+ constructor(table, props) {
1697
+ this.props = { ..._TableTileSource.defaultProps, ...props };
1654
1698
  this.getTileData = this.getTileData.bind(this);
1655
- this.ready = this.initializeTilesAsync(data);
1656
- }
1657
- async initializeTilesAsync(dataPromise) {
1658
- const data = await dataPromise;
1659
- this.initializeTilesSync(data);
1660
- }
1661
- initializeTilesSync(data) {
1662
- const options = this.options;
1663
- const debug = options.debug;
1664
- if (debug)
1665
- console.time("preprocess data");
1666
- if (this.options.maxZoom < 0 || this.options.maxZoom > 24) {
1667
- throw new Error("maxZoom should be in the 0-24 range");
1668
- }
1669
- if (options.promoteId && this.options.generateId) {
1670
- throw new Error("promoteId and generateId cannot be used together.");
1671
- }
1672
- let features = convert(data, options);
1673
- if (debug) {
1674
- console.timeEnd("preprocess data");
1675
- console.log("index: maxZoom: %d, maxPoints: %d", options.indexMaxZoom, options.indexMaxPoints);
1676
- console.time("generate tiles");
1677
- }
1678
- features = wrap(features, this.options);
1679
- if (features.length) {
1680
- this.splitTile(features, 0, 0, 0);
1681
- }
1682
- if (debug) {
1683
- if (features.length) {
1684
- console.log("features: %d, points: %d", this.tiles[0].numFeatures, this.tiles[0].numPoints);
1685
- }
1686
- console.timeEnd("generate tiles");
1687
- console.log("tiles generated:", this.total, JSON.stringify(this.stats));
1688
- }
1699
+ this.ready = this.initializeTilesAsync(table);
1700
+ this.metadata = this.getMetadata();
1701
+ }
1702
+ async initializeTilesAsync(tablePromise) {
1703
+ const table = await tablePromise;
1704
+ this.schema = (0, import_schema.deduceTableSchema)(table);
1705
+ this.createRootTiles(table);
1689
1706
  }
1690
1707
  async getMetadata() {
1691
- return {};
1708
+ await this.ready;
1709
+ return { schema: this.schema, minZoom: 0, maxZoom: this.props.maxZoom };
1710
+ }
1711
+ async getSchema() {
1712
+ await this.ready;
1713
+ return this.schema;
1692
1714
  }
1693
1715
  /**
1694
1716
  * Get a tile at the specified index
@@ -1698,6 +1720,7 @@ var _GeoJSONTileSource = class {
1698
1720
  async getVectorTile(tileIndex) {
1699
1721
  await this.ready;
1700
1722
  const table = this.getTileSync(tileIndex);
1723
+ import_loader_utils3.log.info(2, "getVectorTile", tileIndex, table)();
1701
1724
  return table;
1702
1725
  }
1703
1726
  async getTile(tileIndex) {
@@ -1706,7 +1729,8 @@ var _GeoJSONTileSource = class {
1706
1729
  }
1707
1730
  async getTileData(tileParams) {
1708
1731
  const { x, y, z } = tileParams.index;
1709
- return await this.getVectorTile({ x, y, z });
1732
+ const tile = await this.getVectorTile({ x, y, z });
1733
+ return (tile == null ? void 0 : tile.features) || [];
1710
1734
  }
1711
1735
  // Implementation
1712
1736
  /**
@@ -1718,7 +1742,38 @@ var _GeoJSONTileSource = class {
1718
1742
  if (!rawTile) {
1719
1743
  return null;
1720
1744
  }
1721
- return convertToGeoJSONTable(rawTile, this.options.extent);
1745
+ return convertToGeoJSONTable(rawTile, {
1746
+ coordinates: this.props.coordinates,
1747
+ tileIndex,
1748
+ extent: this.props.extent
1749
+ });
1750
+ }
1751
+ /**
1752
+ * Create the initial tiles
1753
+ * @note the tiles stores all the features together with additional data
1754
+ */
1755
+ createRootTiles(table) {
1756
+ if (this.props.maxZoom < 0 || this.props.maxZoom > 24) {
1757
+ throw new Error("maxZoom should be in the 0-24 range");
1758
+ }
1759
+ if (this.props.promoteId && this.props.generateId) {
1760
+ throw new Error("promoteId and generateId cannot be used together.");
1761
+ }
1762
+ import_loader_utils3.log.log(1, "TableTileSource creating root tiles", this.props)();
1763
+ import_loader_utils3.log.time(1, "preprocess table")();
1764
+ let features = convert(table, this.props);
1765
+ import_loader_utils3.log.timeEnd(1, "preprocess table")();
1766
+ import_loader_utils3.log.time(1, "generate tiles")();
1767
+ features = wrap(features, this.props);
1768
+ if (features.length === 0) {
1769
+ import_loader_utils3.log.log(1, "TableTileSource: no features generated")();
1770
+ return;
1771
+ }
1772
+ this.splitTile(features, 0, 0, 0);
1773
+ const rootTile = this.tiles[0];
1774
+ import_loader_utils3.log.log(1, `root tile features: ${rootTile.numFeatures}, points: ${rootTile.numPoints}`)();
1775
+ import_loader_utils3.log.timeEnd(1, "generate tiles")();
1776
+ import_loader_utils3.log.log(1, `TableTileSource: tiles generated: ${this.stats.get("total").count}`, this.stats)();
1722
1777
  }
1723
1778
  /**
1724
1779
  * Return geojsonvt-style "half formed" vector tile
@@ -1728,7 +1783,7 @@ var _GeoJSONTileSource = class {
1728
1783
  getRawTile(tileIndex) {
1729
1784
  const { z, y } = tileIndex;
1730
1785
  let { x } = tileIndex;
1731
- const { extent, debug } = this.options;
1786
+ const { extent } = this.props;
1732
1787
  if (z < 0 || z > 24) {
1733
1788
  return null;
1734
1789
  }
@@ -1738,8 +1793,7 @@ var _GeoJSONTileSource = class {
1738
1793
  if (this.tiles[id]) {
1739
1794
  return transformTile(this.tiles[id], extent);
1740
1795
  }
1741
- if (debug > 1)
1742
- console.log("drilling down to z%d-%d-%d", z, x, y);
1796
+ import_loader_utils3.log.log(import_loader_utils3.log, "drilling down to z%d-%d-%d", z, x, y)();
1743
1797
  let z0 = z;
1744
1798
  let x0 = x;
1745
1799
  let y0 = y;
@@ -1753,14 +1807,10 @@ var _GeoJSONTileSource = class {
1753
1807
  if (!parent || !parent.source) {
1754
1808
  return null;
1755
1809
  }
1756
- if (debug > 1) {
1757
- console.log("found parent tile z%d-%d-%d", z0, x0, y0);
1758
- console.time("drilling down");
1759
- }
1810
+ import_loader_utils3.log.log(1, "found parent tile z%d-%d-%d", z0, x0, y0)();
1811
+ import_loader_utils3.log.time(1, "drilling down")();
1760
1812
  this.splitTile(parent.source, z0, x0, y0, z, x, y);
1761
- if (debug > 1) {
1762
- console.timeEnd("drilling down");
1763
- }
1813
+ import_loader_utils3.log.timeEnd(1, "drilling down")();
1764
1814
  return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
1765
1815
  }
1766
1816
  /**
@@ -1769,13 +1819,11 @@ var _GeoJSONTileSource = class {
1769
1819
  * @param cz, cx, and cy are the coordinates of the target tile
1770
1820
  *
1771
1821
  * If no target tile is specified, splitting stops when we reach the maximum
1772
- * zoom or the number of points is low as specified in the options.
1822
+ * zoom or the number of points is low as specified in the props.
1773
1823
  */
1774
1824
  // eslint-disable-next-line max-params, max-statements, complexity
1775
1825
  splitTile(features, z, x, y, cz, cx, cy) {
1776
1826
  const stack = [features, z, x, y];
1777
- const options = this.options;
1778
- const debug = options.debug;
1779
1827
  while (stack.length) {
1780
1828
  y = stack.pop();
1781
1829
  x = stack.pop();
@@ -1785,38 +1833,39 @@ var _GeoJSONTileSource = class {
1785
1833
  const id = toID(z, x, y);
1786
1834
  let tile = this.tiles[id];
1787
1835
  if (!tile) {
1788
- if (debug > 1) {
1789
- console.time("creation");
1790
- }
1791
- tile = this.tiles[id] = createTile(features, z, x, y, options);
1836
+ import_loader_utils3.log.time(2, "tile creation")();
1837
+ tile = this.tiles[id] = createTile(features, z, x, y, this.props);
1792
1838
  this.tileCoords.push({ z, x, y });
1793
- if (debug) {
1794
- if (debug > 1) {
1795
- console.log("tile z%d-%d-%d (features: %d, points: %d, simplified: %d)", z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
1796
- console.timeEnd("creation");
1797
- }
1798
- const key = `z${z}`;
1799
- this.stats[key] = (this.stats[key] || 0) + 1;
1800
- this.total++;
1801
- }
1839
+ const key = `z${z}`;
1840
+ let stat = this.stats.get(key, "count");
1841
+ stat.incrementCount();
1842
+ stat = this.stats.get("total");
1843
+ stat.incrementCount();
1844
+ stat = _TableTileSource.stats.get(key, "count");
1845
+ stat.incrementCount();
1846
+ stat = _TableTileSource.stats.get("total");
1847
+ stat.incrementCount();
1848
+ import_loader_utils3.log.log(2, "tile z%d-%d-%d (features: %d, points: %d, simplified: %d)", z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified)();
1849
+ import_loader_utils3.log.timeEnd(2, "tile creation")();
1802
1850
  }
1803
1851
  tile.source = features;
1804
1852
  if (cz === void 0) {
1805
- if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints)
1853
+ if (z === this.props.indexMaxZoom || tile.numPoints <= this.props.maxPointsPerTile) {
1806
1854
  continue;
1807
- } else if (z === options.maxZoom || z === cz) {
1855
+ }
1856
+ } else if (z === this.props.maxZoom || z === cz) {
1808
1857
  continue;
1809
1858
  } else if (cz !== void 0) {
1810
1859
  const zoomSteps = cz - z;
1811
- if (x !== cx >> zoomSteps || y !== cy >> zoomSteps)
1860
+ if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) {
1812
1861
  continue;
1862
+ }
1813
1863
  }
1814
1864
  tile.source = null;
1815
1865
  if (features.length === 0)
1816
1866
  continue;
1817
- if (debug > 1)
1818
- console.time("clipping");
1819
- const k1 = 0.5 * options.buffer / options.extent;
1867
+ import_loader_utils3.log.time(2, "clipping tile")();
1868
+ const k1 = 0.5 * this.props.buffer / this.props.extent;
1820
1869
  const k2 = 0.5 - k1;
1821
1870
  const k3 = 0.5 + k1;
1822
1871
  const k4 = 1 + k1;
@@ -1824,21 +1873,20 @@ var _GeoJSONTileSource = class {
1824
1873
  let bl = null;
1825
1874
  let tr = null;
1826
1875
  let br = null;
1827
- let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
1828
- let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
1876
+ let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, this.props);
1877
+ let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, this.props);
1829
1878
  features = null;
1830
1879
  if (left) {
1831
- tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
1832
- bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
1880
+ tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, this.props);
1881
+ bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, this.props);
1833
1882
  left = null;
1834
1883
  }
1835
1884
  if (right) {
1836
- tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
1837
- br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
1885
+ tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, this.props);
1886
+ br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, this.props);
1838
1887
  right = null;
1839
1888
  }
1840
- if (debug > 1)
1841
- console.timeEnd("clipping");
1889
+ import_loader_utils3.log.timeEnd(2, "clipping tile")();
1842
1890
  stack.push(tl || [], z + 1, x * 2, y * 2);
1843
1891
  stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
1844
1892
  stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
@@ -1846,13 +1894,15 @@ var _GeoJSONTileSource = class {
1846
1894
  }
1847
1895
  }
1848
1896
  };
1849
- var GeoJSONTileSource = _GeoJSONTileSource;
1850
- __publicField(GeoJSONTileSource, "defaultOptions", {
1897
+ var TableTileSource = _TableTileSource;
1898
+ __publicField(TableTileSource, "defaultProps", {
1899
+ coordinates: "wgs84",
1900
+ // coordinates in tile coordinates or lng/lat
1851
1901
  maxZoom: 14,
1852
1902
  // max zoom to preserve detail on
1853
1903
  indexMaxZoom: 5,
1854
1904
  // max zoom in the tile index
1855
- indexMaxPoints: 1e5,
1905
+ maxPointsPerTile: 1e5,
1856
1906
  // max number of points per tile in the tile index
1857
1907
  tolerance: 3,
1858
1908
  // simplification tolerance (higher means simpler)
@@ -1870,10 +1920,15 @@ __publicField(GeoJSONTileSource, "defaultOptions", {
1870
1920
  debug: 0
1871
1921
  // logging level (0, 1 or 2)
1872
1922
  });
1923
+ /** Global stats for all TableTileSources */
1924
+ __publicField(TableTileSource, "stats", new import_stats.Stats({
1925
+ id: "table-tile-source-all",
1926
+ stats: [new import_stats.Stat("count", "tiles"), new import_stats.Stat("count", "features")]
1927
+ }));
1873
1928
  function toID(z, x, y) {
1874
1929
  return ((1 << z) * y + x) * 32 + z;
1875
1930
  }
1876
- function convertToGeoJSONTable(vtTile, extent) {
1931
+ function convertToGeoJSONTable(vtTile, props) {
1877
1932
  const features = [];
1878
1933
  for (const rawFeature of vtTile.features) {
1879
1934
  if (!rawFeature || !rawFeature.geometry) {
@@ -1912,17 +1967,31 @@ function convertToGeoJSONTable(vtTile, extent) {
1912
1967
  default:
1913
1968
  continue;
1914
1969
  }
1915
- coordinates = toLngLat(coordinates, extent);
1970
+ switch (props.coordinates) {
1971
+ case "EPSG:4326":
1972
+ case "wgs84":
1973
+ projectToLngLat(coordinates, props.tileIndex, props.extent);
1974
+ break;
1975
+ case "local":
1976
+ convertToLocalCoordinates(coordinates, props.extent);
1977
+ break;
1978
+ default:
1979
+ throw new Error(`Unsupported CRS ${props.coordinates}`);
1980
+ }
1916
1981
  const feature = {
1917
1982
  type: "Feature",
1918
1983
  geometry: {
1919
1984
  type,
1920
1985
  coordinates
1921
1986
  },
1922
- properties: rawFeature.tags || {}
1987
+ properties: rawFeature.tags || {},
1988
+ id: rawFeature.id
1923
1989
  };
1924
1990
  features.push(feature);
1925
1991
  }
1992
+ if (features.length === 0) {
1993
+ return null;
1994
+ }
1926
1995
  const table = {
1927
1996
  shape: "geojson-table",
1928
1997
  type: "FeatureCollection",
@@ -1930,10 +1999,4 @@ function convertToGeoJSONTable(vtTile, extent) {
1930
1999
  };
1931
2000
  return table;
1932
2001
  }
1933
- function toLngLat(coords, extent) {
1934
- if (Array.isArray(coords[0])) {
1935
- return coords.map((c) => toLngLat(c, extent));
1936
- }
1937
- return [coords[0] / extent, coords[1] / extent];
1938
- }
1939
2002
  //# sourceMappingURL=index.cjs.map