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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/dist/dist.dev.js +1784 -847
  2. package/dist/dist.min.js +1 -1
  3. package/dist/index.cjs +713 -640
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/lib/parse-mvt.d.ts +2 -1
  9. package/dist/lib/parse-mvt.d.ts.map +1 -1
  10. package/dist/lib/parse-mvt.js +15 -45
  11. package/dist/lib/parse-tilejson.d.ts +4 -4
  12. package/dist/lib/parse-tilejson.d.ts.map +1 -1
  13. package/dist/lib/types.d.ts +1 -39
  14. package/dist/lib/types.d.ts.map +1 -1
  15. package/dist/lib/utils/geometry-utils.d.ts +83 -0
  16. package/dist/lib/utils/geometry-utils.d.ts.map +1 -0
  17. package/dist/lib/utils/geometry-utils.js +206 -0
  18. package/dist/lib/vector-tile/vector-tile-feature.d.ts +72 -0
  19. package/dist/lib/vector-tile/vector-tile-feature.d.ts.map +1 -0
  20. package/dist/lib/vector-tile/vector-tile-feature.js +342 -0
  21. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.d.ts +10 -1
  22. package/dist/lib/vector-tile/vector-tile-layer.d.ts.map +1 -0
  23. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.js +16 -1
  24. package/dist/lib/vector-tile/vector-tile.d.ts.map +1 -0
  25. package/dist/lib/{geojson-tiler → vector-tiler}/clip.d.ts +3 -3
  26. package/dist/lib/vector-tiler/clip.d.ts.map +1 -0
  27. package/dist/lib/vector-tiler/convert.d.ts +18 -0
  28. package/dist/lib/vector-tiler/convert.d.ts.map +1 -0
  29. package/dist/lib/{geojson-tiler → vector-tiler}/convert.js +8 -1
  30. package/dist/lib/vector-tiler/feature.d.ts +3 -0
  31. package/dist/lib/vector-tiler/feature.d.ts.map +1 -0
  32. package/dist/lib/vector-tiler/simplify.d.ts.map +1 -0
  33. package/dist/lib/{geojson-tiler → vector-tiler}/tile.d.ts +4 -4
  34. package/dist/lib/vector-tiler/tile.d.ts.map +1 -0
  35. package/dist/lib/vector-tiler/transform.d.ts +7 -0
  36. package/dist/lib/vector-tiler/transform.d.ts.map +1 -0
  37. package/dist/lib/{geojson-tiler → vector-tiler}/wrap.d.ts +2 -2
  38. package/dist/lib/vector-tiler/wrap.d.ts.map +1 -0
  39. package/dist/mvt-loader.d.ts +19 -8
  40. package/dist/mvt-loader.d.ts.map +1 -1
  41. package/dist/mvt-loader.js +3 -2
  42. package/dist/mvt-source.d.ts +4 -4
  43. package/dist/mvt-source.d.ts.map +1 -1
  44. package/dist/mvt-source.js +7 -7
  45. package/dist/mvt-worker.js +1244 -762
  46. package/dist/table-tile-source.d.ts +118 -0
  47. package/dist/table-tile-source.d.ts.map +1 -0
  48. package/dist/table-tile-source.js +370 -0
  49. package/dist/tilejson-loader.js +1 -1
  50. package/package.json +7 -6
  51. package/src/index.ts +3 -3
  52. package/src/lib/parse-mvt.ts +28 -66
  53. package/src/lib/parse-tilejson.ts +5 -5
  54. package/src/lib/types.ts +1 -39
  55. package/src/lib/utils/geometry-utils.ts +234 -0
  56. package/src/lib/vector-tile/vector-tile-feature.ts +416 -0
  57. package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.ts +28 -2
  58. package/src/lib/{geojson-tiler → vector-tiler}/clip.ts +4 -4
  59. package/src/lib/{geojson-tiler → vector-tiler}/convert.ts +19 -9
  60. package/src/lib/{geojson-tiler → vector-tiler}/feature.ts +3 -3
  61. package/src/lib/{geojson-tiler → vector-tiler}/tile.ts +10 -10
  62. package/src/lib/{geojson-tiler → vector-tiler}/transform.ts +2 -2
  63. package/src/lib/{geojson-tiler → vector-tiler}/wrap.ts +5 -5
  64. package/src/mvt-loader.ts +14 -6
  65. package/src/mvt-source.ts +9 -10
  66. package/src/table-tile-source.ts +508 -0
  67. package/dist/helpers/binary-util-functions.d.ts +0 -35
  68. package/dist/helpers/binary-util-functions.d.ts.map +0 -1
  69. package/dist/helpers/binary-util-functions.js +0 -114
  70. package/dist/helpers/mapbox-util-functions.d.ts +0 -29
  71. package/dist/helpers/mapbox-util-functions.d.ts.map +0 -1
  72. package/dist/helpers/mapbox-util-functions.js +0 -78
  73. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -41
  74. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +0 -1
  75. package/dist/lib/binary-vector-tile/vector-tile-feature.js +0 -163
  76. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -23
  77. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +0 -1
  78. package/dist/lib/binary-vector-tile/vector-tile-layer.js +0 -96
  79. package/dist/lib/binary-vector-tile/vector-tile.d.ts +0 -9
  80. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +0 -1
  81. package/dist/lib/binary-vector-tile/vector-tile.js +0 -27
  82. package/dist/lib/geojson-tiler/clip.d.ts.map +0 -1
  83. package/dist/lib/geojson-tiler/convert.d.ts +0 -10
  84. package/dist/lib/geojson-tiler/convert.d.ts.map +0 -1
  85. package/dist/lib/geojson-tiler/feature.d.ts +0 -3
  86. package/dist/lib/geojson-tiler/feature.d.ts.map +0 -1
  87. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +0 -44
  88. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +0 -1
  89. package/dist/lib/geojson-tiler/geojson-tiler.js +0 -209
  90. package/dist/lib/geojson-tiler/simplify.d.ts.map +0 -1
  91. package/dist/lib/geojson-tiler/tile.d.ts.map +0 -1
  92. package/dist/lib/geojson-tiler/transform.d.ts +0 -7
  93. package/dist/lib/geojson-tiler/transform.d.ts.map +0 -1
  94. package/dist/lib/geojson-tiler/wrap.d.ts.map +0 -1
  95. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -27
  96. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +0 -1
  97. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +0 -178
  98. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +0 -1
  99. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +0 -1
  100. package/src/helpers/binary-util-functions.ts +0 -125
  101. package/src/helpers/mapbox-util-functions.ts +0 -82
  102. package/src/lib/binary-vector-tile/vector-tile-feature.ts +0 -203
  103. package/src/lib/binary-vector-tile/vector-tile-layer.ts +0 -113
  104. package/src/lib/binary-vector-tile/vector-tile.ts +0 -36
  105. package/src/lib/geojson-tiler/geojson-tiler.ts +0 -283
  106. package/src/lib/mapbox-vector-tile/LICENSE.txt +0 -31
  107. package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +0 -207
  108. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.d.ts +0 -0
  109. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.js +0 -0
  110. /package/dist/lib/{geojson-tiler → vector-tiler}/clip.js +0 -0
  111. /package/dist/lib/{geojson-tiler → vector-tiler}/feature.js +0 -0
  112. /package/dist/lib/{geojson-tiler → vector-tiler}/simplify.d.ts +0 -0
  113. /package/dist/lib/{geojson-tiler → vector-tiler}/simplify.js +0 -0
  114. /package/dist/lib/{geojson-tiler → vector-tiler}/tile.js +0 -0
  115. /package/dist/lib/{geojson-tiler → vector-tiler}/transform.js +0 -0
  116. /package/dist/lib/{geojson-tiler → vector-tiler}/wrap.js +0 -0
  117. /package/src/lib/{binary-vector-tile → vector-tile}/LICENSE.txt +0 -0
  118. /package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile.ts +0 -0
  119. /package/src/lib/{geojson-tiler → vector-tiler}/LICENSE +0 -0
  120. /package/src/lib/{geojson-tiler → vector-tiler}/simplify.ts +0 -0
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
@@ -2,8 +2,9 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {GetTileParameters, ImageType, DataSourceProps} from '@loaders.gl/loader-utils';
5
+ import type {ImageType, DataSourceProps} from '@loaders.gl/loader-utils';
6
6
  import type {ImageTileSource, VectorTileSource} from '@loaders.gl/loader-utils';
7
+ import type {GetTileParameters, GetTileDataParameters} from '@loaders.gl/loader-utils';
7
8
  import {DataSource, resolvePath} from '@loaders.gl/loader-utils';
8
9
  import {ImageLoader, ImageLoaderOptions, getBinaryImageMetadata} from '@loaders.gl/images';
9
10
  import {
@@ -14,8 +15,6 @@ import {
14
15
  TileJSONLoaderOptions
15
16
  } from '@loaders.gl/mvt';
16
17
 
17
- import {TileLoadParameters} from '@loaders.gl/loader-utils';
18
-
19
18
  /** Properties for a Mapbox Vector Tile Source */
20
19
  export type MVTSourceProps = DataSourceProps & {
21
20
  /** Root url of tileset */
@@ -101,8 +100,8 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
101
100
  return this.mimeType;
102
101
  }
103
102
 
104
- async getTile(tileParams: GetTileParameters): Promise<ArrayBuffer | null> {
105
- const {x, y, zoom: z} = tileParams;
103
+ async getTile(parameters: GetTileParameters): Promise<ArrayBuffer | null> {
104
+ const {x, y, z} = parameters;
106
105
  const tileUrl = this.getTileURL(x, y, z);
107
106
  const response = await this.fetch(tileUrl);
108
107
  if (!response.ok) {
@@ -115,12 +114,12 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
115
114
  // Tile Source interface implementation: deck.gl compatible API
116
115
  // TODO - currently only handles image tiles, not vector tiles
117
116
 
118
- async getTileData(tileParams: TileLoadParameters): Promise<unknown | null> {
119
- const {x, y, z} = tileParams.index;
117
+ async getTileData(parameters: GetTileDataParameters): Promise<any> {
118
+ const {x, y, z} = parameters.index;
120
119
  // const metadata = await this.metadata;
121
120
  // mimeType = metadata?.tileMIMEType || 'application/vnd.mapbox-vector-tile';
122
121
 
123
- const arrayBuffer = await this.getTile({x, y, zoom: z, layers: []});
122
+ const arrayBuffer = await this.getTile({x, y, z, layers: []});
124
123
  if (arrayBuffer === null) {
125
124
  return null;
126
125
  }
@@ -130,7 +129,7 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
130
129
  this.mimeType || imageMetadata?.mimeType || 'application/vnd.mapbox-vector-tile';
131
130
  switch (this.mimeType) {
132
131
  case 'application/vnd.mapbox-vector-tile':
133
- return await this._parseVectorTile(arrayBuffer, {x, y, zoom: z, layers: []});
132
+ return await this._parseVectorTile(arrayBuffer, {x, y, z, layers: []});
134
133
  default:
135
134
  return await this._parseImageTile(arrayBuffer);
136
135
  }
@@ -162,7 +161,7 @@ export class MVTSource extends DataSource implements ImageTileSource, VectorTile
162
161
  shape: 'geojson-table',
163
162
  mvt: {
164
163
  coordinates: 'wgs84',
165
- tileIndex: {x: tileParams.x, y: tileParams.y, z: tileParams.zoom},
164
+ tileIndex: {x: tileParams.x, y: tileParams.y, z: tileParams.z},
166
165
  ...(this.loadOptions as MVTLoaderOptions)?.mvt
167
166
  },
168
167
  ...this.loadOptions
@@ -0,0 +1,508 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT AND ISC
3
+ // Copyright (c) vis.gl contributors
4
+ // Based on https://github.com/mapbox/geojson-vt under compatible ISC license
5
+
6
+ import type {
7
+ VectorTileSourceProps,
8
+ GetTileDataParameters,
9
+ GetTileParameters
10
+ } from '@loaders.gl/loader-utils';
11
+ import {VectorTileSource, log} from '@loaders.gl/loader-utils';
12
+ import {Schema, GeoJSONTable, Feature, BinaryFeatureCollection} from '@loaders.gl/schema';
13
+ import {deduceTableSchema} from '@loaders.gl/schema';
14
+ import {Stats, Stat} from '@probe.gl/stats';
15
+
16
+ import type {TableTile, TableTileFeature} from './lib/vector-tiler/tile';
17
+ import {convert} from './lib/vector-tiler/convert'; // GeoJSON conversion and preprocessing
18
+ import {clip} from './lib/vector-tiler/clip'; // stripe clipping algorithm
19
+ import {wrap} from './lib/vector-tiler/wrap'; // date line processing
20
+ import {transformTile} from './lib/vector-tiler/transform'; // coordinate transformation
21
+ import {createTile} from './lib/vector-tiler/tile'; // final simplified tile generation
22
+
23
+ import {projectToLngLat} from './lib/utils/geometry-utils';
24
+ import {convertToLocalCoordinates} from './lib/utils/geometry-utils';
25
+
26
+ /** Options to configure tiling */
27
+ export type TableTileSourceProps = VectorTileSourceProps & {
28
+ coordinates: 'local' | 'wgs84' | 'EPSG:4326';
29
+ /** max zoom to preserve detail on */
30
+ maxZoom?: number;
31
+ /** max zoom in the tile index */
32
+ indexMaxZoom?: number;
33
+ /** max number of points per tile in the tile index */
34
+ maxPointsPerTile?: number;
35
+ /** simplification tolerance (higher means simpler) */
36
+ tolerance?: number;
37
+ /** tile extent */
38
+ extent?: number;
39
+ /** tile buffer on each side */
40
+ buffer?: number;
41
+ /** name of a feature property to be promoted to feature.id */
42
+ promoteId?: string;
43
+ /** whether to generate feature ids. Cannot be used with promoteId */
44
+ generateId?: boolean;
45
+ /** logging level (0, 1 or 2) */
46
+ debug?: number;
47
+ /** whether to calculate line metrics */
48
+ lineMetrics?: boolean;
49
+ };
50
+
51
+ /**
52
+ * Dynamically vector tiles a table (the table needs a geometry column)
53
+ * - Tiles are generated when requested.
54
+ * - Each tile contains a tables of clipped features.
55
+ *
56
+ * @note - Currently only accepts `GeoJSONTable` tables
57
+ * @note - Currently only outputs `GeoJSONTable`
58
+ * @note - (can be initialized with a promise that resolves to GeoJSONTable).
59
+ *
60
+ * @todo - metadata should scan all rows to determine schema
61
+ * @todo - metadata scan all rows to determine tilestats (field values[] etc).
62
+ * @todo - handle binary input tables
63
+ * @todo - generate binary output tables
64
+ * @todo - how does TileSourceLayer specify coordinates / decided which layer to render with
65
+ */
66
+ export class TableTileSource implements VectorTileSource<any> {
67
+ static defaultProps: Required<TableTileSourceProps> = {
68
+ coordinates: 'wgs84', // coordinates in tile coordinates or lng/lat
69
+ maxZoom: 14, // max zoom to preserve detail on
70
+ indexMaxZoom: 5, // max zoom in the tile index
71
+ maxPointsPerTile: 100000, // max number of points per tile in the tile index
72
+ tolerance: 3, // simplification tolerance (higher means simpler)
73
+ extent: 4096, // tile extent
74
+ buffer: 64, // tile buffer on each side
75
+ lineMetrics: false, // whether to calculate line metrics
76
+ // @ts-expect-error
77
+ promoteId: undefined, // name of a feature property to be promoted to feature.id
78
+ generateId: false, // whether to generate feature ids. Cannot be used with promoteId
79
+ debug: 0 // logging level (0, 1 or 2)
80
+ };
81
+
82
+ /** Global stats for all TableTileSources */
83
+ static stats = new Stats({
84
+ id: 'table-tile-source-all',
85
+ stats: [new Stat('count', 'tiles'), new Stat('count', 'features')]
86
+ });
87
+
88
+ /** Stats for this TableTileSource */
89
+ stats = new Stats({
90
+ id: 'table-tile-source',
91
+ stats: [new Stat('tiles', 'count'), new Stat('features', 'count')]
92
+ });
93
+
94
+ /** MIME type of the tiles emitted by this tile source */
95
+ readonly mimeType = 'application/vnd.mapbox-vector-tile';
96
+ readonly localCoordinates = true;
97
+
98
+ /** The props that this tile source was created with */
99
+ props: Required<TableTileSourceProps>;
100
+
101
+ /* Schema of the data */
102
+ schema: Schema | null = null;
103
+
104
+ /** Map of generated tiles, indexed by stringified tile coordinates */
105
+ tiles: Record<string, TableTile> = {};
106
+ /** Array of tile coordinates */
107
+ tileCoords: {x: number; y: number; z: number}[] = [];
108
+
109
+ /** Input data has loaded, initial top-level tiling is done, sync methods can now be called */
110
+ ready: Promise<void>;
111
+ /** Metadata for the tile source (generated TileJSON/tilestats */
112
+ metadata: Promise<unknown>;
113
+
114
+ constructor(table: GeoJSONTable | Promise<GeoJSONTable>, props?: TableTileSourceProps) {
115
+ this.props = {...TableTileSource.defaultProps, ...props};
116
+ this.getTileData = this.getTileData.bind(this);
117
+ this.ready = this.initializeTilesAsync(table);
118
+ this.metadata = this.getMetadata();
119
+ }
120
+
121
+ async initializeTilesAsync(tablePromise: GeoJSONTable | Promise<GeoJSONTable>): Promise<void> {
122
+ const table = await tablePromise;
123
+ this.schema = deduceTableSchema(table);
124
+ this.createRootTiles(table);
125
+ }
126
+
127
+ async getMetadata(): Promise<unknown> {
128
+ await this.ready;
129
+ return {schema: this.schema, minZoom: 0, maxZoom: this.props.maxZoom};
130
+ }
131
+
132
+ async getSchema(): Promise<Schema> {
133
+ await this.ready;
134
+ return this.schema!;
135
+ }
136
+
137
+ /**
138
+ * Get a tile at the specified index
139
+ * @param tileIndex z, x, y of tile
140
+ * @returns
141
+ */
142
+ async getVectorTile(tileIndex: GetTileParameters): Promise<GeoJSONTable | null> {
143
+ await this.ready;
144
+ const table = this.getTileSync(tileIndex);
145
+ log.info(2, 'getVectorTile', tileIndex, table)();
146
+ return table;
147
+ }
148
+
149
+ async getTile(tileIndex: {z: number; x: number; y: number}): Promise<GeoJSONTable | null> {
150
+ await this.ready;
151
+ return this.getTileSync(tileIndex);
152
+ }
153
+
154
+ async getTileData(
155
+ tileParams: GetTileDataParameters
156
+ ): Promise<Feature[] | BinaryFeatureCollection> {
157
+ const {x, y, z} = tileParams.index;
158
+ const tile = await this.getVectorTile({x, y, z});
159
+ return tile?.features || [];
160
+ }
161
+
162
+ // Implementation
163
+
164
+ /**
165
+ * Synchronously request a tile
166
+ * @note Application must await `source.ready` before calling sync methods.
167
+ */
168
+ getTileSync(tileIndex: {z: number; x: number; y: number}): GeoJSONTable | null {
169
+ const rawTile = this.getRawTile(tileIndex);
170
+ if (!rawTile) {
171
+ return null;
172
+ }
173
+
174
+ return convertToGeoJSONTable(rawTile, {
175
+ coordinates: this.props.coordinates,
176
+ tileIndex,
177
+ extent: this.props.extent
178
+ });
179
+ }
180
+
181
+ /**
182
+ * Create the initial tiles
183
+ * @note the tiles stores all the features together with additional data
184
+ */
185
+ createRootTiles(table: GeoJSONTable): void {
186
+ if (this.props.maxZoom < 0 || this.props.maxZoom > 24) {
187
+ throw new Error('maxZoom should be in the 0-24 range');
188
+ }
189
+ if (this.props.promoteId && this.props.generateId) {
190
+ throw new Error('promoteId and generateId cannot be used together.');
191
+ }
192
+
193
+ log.log(1, 'TableTileSource creating root tiles', this.props)();
194
+
195
+ // projects and adds simplification info
196
+ log.time(1, 'preprocess table')();
197
+ let features = convert(table, this.props);
198
+ log.timeEnd(1, 'preprocess table')();
199
+
200
+ // wraps features (ie extreme west and extreme east)
201
+ log.time(1, 'generate tiles')();
202
+
203
+ features = wrap(features, this.props);
204
+
205
+ // start slicing from the top tile down
206
+ if (features.length === 0) {
207
+ log.log(1, 'TableTileSource: no features generated')();
208
+ return;
209
+ }
210
+
211
+ this.splitTile(features, 0, 0, 0);
212
+
213
+ const rootTile = this.tiles[0];
214
+ log.log(1, `root tile features: ${rootTile.numFeatures}, points: ${rootTile.numPoints}`)();
215
+
216
+ log.timeEnd(1, 'generate tiles')();
217
+ log.log(1, `TableTileSource: tiles generated: ${this.stats.get('total').count}`, this.stats)();
218
+ }
219
+
220
+ /**
221
+ * Return geojsonvt-style "half formed" vector tile
222
+ * @note Application must await `source.ready` before calling sync methods.
223
+ */
224
+ // eslint-disable-next-line complexity, max-statements
225
+ getRawTile(tileIndex: {z: number; x: number; y: number}): TableTile | null {
226
+ const {z, y} = tileIndex;
227
+ let {x} = tileIndex;
228
+ // z = +z;
229
+ // x = +x;
230
+ // y = +y;
231
+
232
+ const {extent} = this.props;
233
+
234
+ if (z < 0 || z > 24) {
235
+ return null;
236
+ }
237
+
238
+ const z2 = 1 << z;
239
+ x = (x + z2) & (z2 - 1); // wrap tile x coordinate
240
+
241
+ const id = toID(z, x, y);
242
+ if (this.tiles[id]) {
243
+ return transformTile(this.tiles[id], extent);
244
+ }
245
+
246
+ log.log(log, 'drilling down to z%d-%d-%d', z, x, y)();
247
+
248
+ let z0 = z;
249
+ let x0 = x;
250
+ let y0 = y;
251
+ let parent;
252
+
253
+ while (!parent && z0 > 0) {
254
+ z0--;
255
+ x0 = x0 >> 1;
256
+ y0 = y0 >> 1;
257
+ parent = this.tiles[toID(z0, x0, y0)];
258
+ }
259
+
260
+ if (!parent || !parent.source) {
261
+ return null;
262
+ }
263
+
264
+ // if we found a parent tile containing the original geometry, we can drill down from it
265
+ log.log(1, 'found parent tile z%d-%d-%d', z0, x0, y0)();
266
+ log.time(1, 'drilling down')();
267
+
268
+ this.splitTile(parent.source, z0, x0, y0, z, x, y);
269
+
270
+ log.timeEnd(1, 'drilling down')();
271
+
272
+ return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
273
+ }
274
+
275
+ /**
276
+ * splits features from a parent tile to sub-tiles.
277
+ * @param z, x, and y are the coordinates of the parent tile
278
+ * @param cz, cx, and cy are the coordinates of the target tile
279
+ *
280
+ * If no target tile is specified, splitting stops when we reach the maximum
281
+ * zoom or the number of points is low as specified in the props.
282
+ */
283
+ // eslint-disable-next-line max-params, max-statements, complexity
284
+ splitTile(
285
+ features: TableTileFeature[],
286
+ z: number,
287
+ x: number,
288
+ y: number,
289
+ cz?: number,
290
+ cx?: number,
291
+ cy?: number
292
+ ): void {
293
+ const stack: any[] = [features, z, x, y];
294
+
295
+ // avoid recursion by using a processing queue
296
+ while (stack.length) {
297
+ y = stack.pop();
298
+ x = stack.pop();
299
+ z = stack.pop();
300
+ features = stack.pop();
301
+
302
+ const z2 = 1 << z;
303
+ const id = toID(z, x, y);
304
+ let tile = this.tiles[id];
305
+
306
+ if (!tile) {
307
+ log.time(2, 'tile creation')();
308
+
309
+ tile = this.tiles[id] = createTile(features, z, x, y, this.props);
310
+ this.tileCoords.push({z, x, y});
311
+
312
+ const key = `z${z}`;
313
+ let stat = this.stats.get(key, 'count');
314
+ stat.incrementCount();
315
+
316
+ stat = this.stats.get('total');
317
+ stat.incrementCount();
318
+
319
+ stat = TableTileSource.stats.get(key, 'count');
320
+ stat.incrementCount();
321
+
322
+ stat = TableTileSource.stats.get('total');
323
+ stat.incrementCount();
324
+
325
+ log.log(
326
+ 2,
327
+ 'tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
328
+ z,
329
+ x,
330
+ y,
331
+ tile.numFeatures,
332
+ tile.numPoints,
333
+ tile.numSimplified
334
+ )();
335
+ log.timeEnd(2, 'tile creation')();
336
+ }
337
+
338
+ // save reference to original geometry in tile so that we can drill down later if we stop now
339
+ tile.source = features;
340
+
341
+ /** eslint-disable no-continue */
342
+
343
+ // if it's the first-pass tiling
344
+ if (cz === undefined) {
345
+ // stop tiling if we reached max zoom, or if the tile is too simple
346
+ if (z === this.props.indexMaxZoom || tile.numPoints <= this.props.maxPointsPerTile) {
347
+ continue;
348
+ }
349
+ // if a drilldown to a specific tile
350
+ } else if (z === this.props.maxZoom || z === cz) {
351
+ // stop tiling if we reached base zoom or our target tile zoom
352
+ continue;
353
+ } else if (cz !== undefined) {
354
+ // stop tiling if it's not an ancestor of the target tile
355
+ const zoomSteps = cz - z;
356
+ // @ts-expect-error TODO fix the types of cx cy
357
+ if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) {
358
+ continue;
359
+ }
360
+ }
361
+
362
+ // if we slice further down, no need to keep source geometry
363
+ tile.source = null;
364
+
365
+ if (features.length === 0) continue;
366
+
367
+ log.time(2, 'clipping tile')();
368
+
369
+ // values we'll use for clipping
370
+ const k1 = (0.5 * this.props.buffer) / this.props.extent;
371
+ const k2 = 0.5 - k1;
372
+ const k3 = 0.5 + k1;
373
+ const k4 = 1 + k1;
374
+
375
+ let tl: TableTileFeature[] | null = null;
376
+ let bl: TableTileFeature[] | null = null;
377
+ let tr: TableTileFeature[] | null = null;
378
+ let br: TableTileFeature[] | null = null;
379
+
380
+ let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, this.props);
381
+ let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, this.props);
382
+
383
+ // @ts-expect-error - unclear why this is needed?
384
+ features = null;
385
+
386
+ if (left) {
387
+ tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, this.props);
388
+ bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, this.props);
389
+ left = null;
390
+ }
391
+
392
+ if (right) {
393
+ tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, this.props);
394
+ br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, this.props);
395
+ right = null;
396
+ }
397
+
398
+ log.timeEnd(2, 'clipping tile')();
399
+
400
+ stack.push(tl || [], z + 1, x * 2, y * 2);
401
+ stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
402
+ stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
403
+ stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
404
+ }
405
+ }
406
+ }
407
+
408
+ function toID(z, x, y): number {
409
+ return ((1 << z) * y + x) * 32 + z;
410
+ }
411
+
412
+ // eslint-disable-next-line max-statements, complexity
413
+ function convertToGeoJSONTable(
414
+ vtTile: TableTile,
415
+ props: {
416
+ coordinates: 'local' | 'wgs84' | 'EPSG:4326';
417
+ tileIndex: {x: number; y: number; z: number};
418
+ extent: number;
419
+ }
420
+ ): GeoJSONTable | null {
421
+ const features: Feature[] = [];
422
+ for (const rawFeature of vtTile.features) {
423
+ if (!rawFeature || !rawFeature.geometry) {
424
+ continue;
425
+ }
426
+
427
+ let type:
428
+ | 'Point'
429
+ | 'MultiPoint'
430
+ | 'LineString'
431
+ | 'MultiLineString'
432
+ | 'Polygon'
433
+ | 'MultiPolygon';
434
+
435
+ let coordinates: any;
436
+
437
+ // raw geometry
438
+ switch (rawFeature.type) {
439
+ case 1:
440
+ if (rawFeature.geometry.length === 1) {
441
+ type = 'Point';
442
+ coordinates = rawFeature.geometry[0];
443
+ } else {
444
+ type = 'MultiPoint';
445
+ coordinates = rawFeature.geometry;
446
+ }
447
+ break;
448
+ case 2:
449
+ if (rawFeature.geometry.length === 1) {
450
+ type = 'LineString';
451
+ coordinates = rawFeature.geometry[0];
452
+ } else {
453
+ type = 'MultiLineString';
454
+ coordinates = rawFeature.geometry;
455
+ }
456
+ break;
457
+ case 3:
458
+ if (rawFeature.geometry.length > 1) {
459
+ type = 'MultiPolygon';
460
+ coordinates = [rawFeature.geometry];
461
+ } else {
462
+ type = 'Polygon';
463
+ coordinates = rawFeature.geometry;
464
+ }
465
+ break;
466
+ default:
467
+ continue;
468
+ }
469
+
470
+ switch (props.coordinates) {
471
+ case 'EPSG:4326':
472
+ case 'wgs84':
473
+ projectToLngLat(coordinates, props.tileIndex, props.extent);
474
+ break;
475
+
476
+ case 'local':
477
+ convertToLocalCoordinates(coordinates, props.extent);
478
+ break;
479
+
480
+ default:
481
+ throw new Error(`Unsupported CRS ${props.coordinates}`);
482
+ }
483
+
484
+ const feature: Feature = {
485
+ type: 'Feature',
486
+ geometry: {
487
+ type,
488
+ coordinates
489
+ },
490
+ properties: rawFeature.tags || {},
491
+ id: rawFeature.id
492
+ };
493
+
494
+ features.push(feature);
495
+ }
496
+
497
+ if (features.length === 0) {
498
+ return null;
499
+ }
500
+
501
+ const table: GeoJSONTable = {
502
+ shape: 'geojson-table',
503
+ type: 'FeatureCollection',
504
+ features
505
+ };
506
+
507
+ return table;
508
+ }
@@ -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"}