@maplibre/geojson-vt 5.0.4 → 6.0.0

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 (53) hide show
  1. package/README.md +3 -13
  2. package/dist/clip.d.ts +20 -1
  3. package/dist/clip.d.ts.map +1 -1
  4. package/dist/cluster-tile-index.d.ts +76 -0
  5. package/dist/cluster-tile-index.d.ts.map +1 -0
  6. package/dist/cluster-tile-index.test.d.ts +2 -0
  7. package/dist/cluster-tile-index.test.d.ts.map +1 -0
  8. package/dist/convert.d.ts +10 -2
  9. package/dist/convert.d.ts.map +1 -1
  10. package/dist/deconvert.d.ts +19 -0
  11. package/dist/deconvert.d.ts.map +1 -0
  12. package/dist/deconvert.test.d.ts +2 -0
  13. package/dist/deconvert.test.d.ts.map +1 -0
  14. package/dist/definitions.d.ts +176 -20
  15. package/dist/definitions.d.ts.map +1 -1
  16. package/dist/feature.d.ts +11 -3
  17. package/dist/feature.d.ts.map +1 -1
  18. package/dist/geojson-to-tile.d.ts +35 -0
  19. package/dist/geojson-to-tile.d.ts.map +1 -0
  20. package/dist/geojson-vt-dev.js +1582 -478
  21. package/dist/geojson-vt.js +1 -1
  22. package/dist/geojson-vt.mjs +1250 -473
  23. package/dist/geojson-vt.mjs.map +1 -1
  24. package/dist/geojsonvt.d.ts +76 -0
  25. package/dist/geojsonvt.d.ts.map +1 -0
  26. package/dist/geojsonvt.test.d.ts +2 -0
  27. package/dist/geojsonvt.test.d.ts.map +1 -0
  28. package/dist/index.d.ts +8 -62
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/tile-index.d.ts +51 -0
  31. package/dist/tile-index.d.ts.map +1 -0
  32. package/dist/tile.d.ts +1 -29
  33. package/dist/tile.d.ts.map +1 -1
  34. package/dist/transform.d.ts +1 -18
  35. package/dist/transform.d.ts.map +1 -1
  36. package/package.json +18 -10
  37. package/src/clip.ts +119 -81
  38. package/src/cluster-tile-index.test.ts +205 -0
  39. package/src/cluster-tile-index.ts +513 -0
  40. package/src/convert.ts +97 -75
  41. package/src/deconvert.test.ts +153 -0
  42. package/src/deconvert.ts +92 -0
  43. package/src/definitions.ts +196 -18
  44. package/src/difference.ts +3 -3
  45. package/src/feature.ts +11 -4
  46. package/src/geojson-to-tile.ts +58 -0
  47. package/src/geojsonvt.test.ts +39 -0
  48. package/src/geojsonvt.ts +209 -0
  49. package/src/index.ts +27 -378
  50. package/src/tile-index.ts +310 -0
  51. package/src/tile.ts +92 -103
  52. package/src/transform.ts +41 -39
  53. package/src/wrap.ts +4 -4
package/src/index.ts CHANGED
@@ -1,387 +1,24 @@
1
1
 
2
- import {convert} from './convert';
3
- import {clip} from './clip';
4
- import {wrap} from './wrap';
5
- import {transformTile, type GeoJSONVTFeature, type GeoJSONVTFeatureNonPoint, type GeoJSONVTFeaturePoint, type GeoJSONVTTile} from './transform';
6
- import {createTile, type GeoJSONVTInternalTile, type GeoJSONVTInternalTileFeature, type GeoJSONVTInternalTileFeaturePoint, type GeoJSONVTInternalTileFeaturNonPoint} from './tile';
7
- import {applySourceDiff, type GeoJSONVTFeatureDiff, type GeoJSONVTSourceDiff} from './difference';
8
- import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, GeometryType, GeometryTypeMap, PartialGeoJSONVTFeature, StartEndSizeArray } from './definitions';
9
-
10
- const defaultOptions: GeoJSONVTOptions = {
11
- maxZoom: 14,
12
- indexMaxZoom: 5,
13
- indexMaxPoints: 100000,
14
- tolerance: 3,
15
- extent: 4096,
16
- buffer: 64,
17
- lineMetrics: false,
18
- promoteId: null,
19
- generateId: false,
20
- updateable: false,
21
- debug: 0
22
- };
23
-
24
- /**
25
- * Main class for creating and managing a vector tile index from GeoJSON data.
26
- */
27
- class GeoJSONVT {
28
- private options: GeoJSONVTOptions;
29
- /** @internal */
30
- public tiles: {[key: string]: GeoJSONVTInternalTile};
31
- private tileCoords: {z: number, x: number, y: number, id: number}[];
32
- /** @internal */
33
- public stats: {[key: string]: number} = {};
34
- /** @internal */
35
- public total: number = 0;
36
- private source?: GeoJSONVTInternalFeature[];
37
-
38
- constructor(data: GeoJSON.GeoJSON, options: GeoJSONVTOptions) {
39
- options = this.options = Object.assign({}, defaultOptions, options);
40
-
41
- const debug = options.debug;
42
-
43
- if (debug) console.time('preprocess data');
44
-
45
- if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error('maxZoom should be in the 0-24 range');
46
- if (options.promoteId && options.generateId) throw new Error('promoteId and generateId cannot be used together.');
47
-
48
- // projects and adds simplification info
49
- let features = convert(data, options);
50
-
51
- // tiles and tileCoords are part of the public API
52
- this.tiles = {};
53
- this.tileCoords = [];
54
-
55
- if (debug) {
56
- console.timeEnd('preprocess data');
57
- console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
58
- console.time('generate tiles');
59
- this.stats = {};
60
- this.total = 0;
61
- }
62
-
63
- // wraps features (ie extreme west and extreme east)
64
- features = wrap(features, options);
65
-
66
- // start slicing from the top tile down
67
- if (features.length) {
68
- this.splitTile(features, 0, 0, 0);
69
- }
70
-
71
- // for updateable indexes, store a copy of the original simplified features
72
- if (options.updateable) {
73
- this.source = features;
74
- }
75
-
76
- if (debug) {
77
- if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
78
- console.timeEnd('generate tiles');
79
- console.log('tiles generated:', this.total, JSON.stringify(this.stats));
80
- }
81
- }
82
-
83
- /**
84
- * splits features from a parent tile to sub-tiles.
85
- * z, x, and y are the coordinates of the parent tile
86
- * cz, cx, and cy are the coordinates of the target tile
87
- *
88
- * If no target tile is specified, splitting stops when we reach the maximum
89
- * zoom or the number of points is low as specified in the options.
90
- * @internal
91
- * @param features - features to split
92
- * @param z - tile zoom level
93
- * @param x - tile x coordinate
94
- * @param y - tile y coordinate
95
- * @param cz - target tile zoom level
96
- * @param cx - target tile x coordinate
97
- * @param cy - target tile y coordinate
98
- */
99
- splitTile(features: GeoJSONVTInternalFeature[], z: number, x: number, y: number, cz?: number, cx?: number, cy?: number) {
100
-
101
- const stack = [features, z, x, y];
102
- const options = this.options;
103
- const debug = options.debug;
104
-
105
- // avoid recursion by using a processing queue
106
- while (stack.length) {
107
- y = stack.pop() as number;
108
- x = stack.pop() as number;
109
- z = stack.pop() as number;
110
- features = stack.pop() as GeoJSONVTInternalFeature[];
111
-
112
- const z2 = 1 << z;
113
- const id = toID(z, x, y);
114
- let tile = this.tiles[id];
115
-
116
- if (!tile) {
117
- if (debug > 1) console.time('creation');
118
-
119
- tile = this.tiles[id] = createTile(features, z, x, y, options);
120
- this.tileCoords.push({z, x, y, id});
121
-
122
- if (debug) {
123
- if (debug > 1) {
124
- console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
125
- z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
126
- console.timeEnd('creation');
127
- }
128
- const key = `z${ z}`;
129
- this.stats[key] = (this.stats[key] || 0) + 1;
130
- this.total++;
131
- }
132
- }
133
-
134
- // save reference to original geometry in tile so that we can drill down later if we stop now
135
- tile.source = features;
136
-
137
- // if it's the first-pass tiling
138
- if (cz == null) {
139
- // stop tiling if we reached max zoom, or if the tile is too simple
140
- if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
141
- // if a drilldown to a specific tile
142
- } else if (z === options.maxZoom || z === cz) {
143
- // stop tiling if we reached base zoom or our target tile zoom
144
- continue;
145
- } else if (cz != null) {
146
- // stop tiling if it's not an ancestor of the target tile
147
- const zoomSteps = cz - z;
148
- if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
149
- }
150
-
151
- // if we slice further down, no need to keep source geometry
152
- tile.source = null;
153
-
154
- if (!features.length) continue;
155
-
156
- if (debug > 1) console.time('clipping');
157
-
158
- // values we'll use for clipping
159
- const k1 = 0.5 * options.buffer / options.extent;
160
- const k2 = 0.5 - k1;
161
- const k3 = 0.5 + k1;
162
- const k4 = 1 + k1;
163
-
164
- let tl = null;
165
- let bl = null;
166
- let tr = null;
167
- let br = null;
168
-
169
- const left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
170
- const right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
171
-
172
- if (left) {
173
- tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
174
- bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
175
- }
176
-
177
- if (right) {
178
- tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
179
- br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
180
- }
181
-
182
- if (debug > 1) console.timeEnd('clipping');
183
-
184
- stack.push(tl || [], z + 1, x * 2, y * 2);
185
- stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
186
- stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
187
- stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
188
- }
189
- }
190
-
191
- /**
192
- * Given z, x, and y tile coordinates, returns the corresponding tile with geometries in tile coordinates, much like MVT data is stored.
193
- * @param z - tile zoom level
194
- * @param x - tile x coordinate
195
- * @param y - tile y coordinate
196
- * @returns the transformed tile or null if not found
197
- */
198
- getTile(z: number | string, x: number | string, y: number | string): GeoJSONVTTile | null {
199
- z = +z;
200
- x = +x;
201
- y = +y;
202
-
203
- const options = this.options;
204
- const {extent, debug} = options;
205
-
206
- if (z < 0 || z > 24) return null;
207
-
208
- const z2 = 1 << z;
209
- x = (x + z2) & (z2 - 1); // wrap tile x coordinate
210
-
211
- const id = toID(z, x, y);
212
- if (this.tiles[id]) {
213
- return transformTile(this.tiles[id], extent);
214
- }
215
-
216
- if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
217
-
218
- let z0 = z;
219
- let x0 = x;
220
- let y0 = y;
221
- let parent;
222
-
223
- while (!parent && z0 > 0) {
224
- z0--;
225
- x0 = x0 >> 1;
226
- y0 = y0 >> 1;
227
- parent = this.tiles[toID(z0, x0, y0)];
228
- }
229
-
230
- if (!parent?.source) return null;
231
-
232
- // if we found a parent tile containing the original geometry, we can drill down from it
233
- if (debug > 1) {
234
- console.log('found parent tile z%d-%d-%d', z0, x0, y0);
235
- console.time('drilling down');
236
- }
237
- this.splitTile(parent.source, z0, x0, y0, z, x, y);
238
- if (debug > 1) console.timeEnd('drilling down');
239
-
240
- if (!this.tiles[id]) return null;
241
-
242
- return transformTile(this.tiles[id], extent);
243
- }
244
-
245
- /**
246
- * Invalidates (removes) tiles affected by the provided features
247
- * @internal
248
- * @param features
249
- */
250
- invalidateTiles(features: GeoJSONVTInternalFeature[]) {
251
- const options = this.options;
252
- const {debug} = options;
253
-
254
- // calculate bounding box of all features for trivial reject
255
- let minX = Infinity;
256
- let maxX = -Infinity;
257
- let minY = Infinity;
258
- let maxY = -Infinity;
259
-
260
- for (const feature of features) {
261
- minX = Math.min(minX, feature.minX);
262
- maxX = Math.max(maxX, feature.maxX);
263
- minY = Math.min(minY, feature.minY);
264
- maxY = Math.max(maxY, feature.maxY);
265
- }
266
-
267
- // tile buffer clipping value - not halved as in splitTile above because checking against tile's own extent
268
- const k1 = options.buffer / options.extent;
269
-
270
- // track removed tile ids for o(1) lookup
271
- const removedLookup = new Set();
272
-
273
- // iterate through existing tiles and remove ones that are affected by features
274
- for (const id in this.tiles) {
275
- const tile = this.tiles[id];
276
-
277
- // calculate tile bounds including buffer
278
- const z2 = 1 << tile.z;
279
- const tileMinX = (tile.x - k1) / z2;
280
- const tileMaxX = (tile.x + 1 + k1) / z2;
281
- const tileMinY = (tile.y - k1) / z2;
282
- const tileMaxY = (tile.y + 1 + k1) / z2;
283
-
284
- // trivial reject if feature bounds don't intersect tile
285
- if (maxX < tileMinX || minX >= tileMaxX ||
286
- maxY < tileMinY || minY >= tileMaxY) {
287
- continue;
288
- }
289
-
290
- // check if any feature intersects with the tile
291
- let intersects = false;
292
- for (const feature of features) {
293
- if (feature.maxX >= tileMinX && feature.minX < tileMaxX &&
294
- feature.maxY >= tileMinY && feature.minY < tileMaxY) {
295
- intersects = true;
296
- break;
297
- }
298
- }
299
- if (!intersects) continue;
300
-
301
- if (debug) {
302
- if (debug > 1) {
303
- console.log('invalidate tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
304
- tile.z, tile.x, tile.y, tile.numFeatures, tile.numPoints, tile.numSimplified);
305
- }
306
- const key = `z${ tile.z}`;
307
- this.stats[key] = (this.stats[key] || 0) - 1;
308
- this.total--;
309
- }
310
-
311
- delete this.tiles[id];
312
- removedLookup.add(id);
313
- }
314
-
315
- // remove tile coords that are no longer in the index
316
- if (removedLookup.size) {
317
- this.tileCoords = this.tileCoords.filter(c => !removedLookup.has(c.id));
318
- }
319
- }
320
-
321
- /**
322
- * Updates the tile index by adding and/or removing geojson features
323
- * invalidates tiles that are affected by the update for regeneration on next getTile call.
324
- * @param diff - the source diff object
325
- */
326
- updateData(diff: GeoJSONVTSourceDiff) {
327
- const options = this.options;
328
- const debug = options.debug;
329
-
330
- if (!options.updateable) throw new Error('to update tile geojson `updateable` option must be set to true');
331
-
332
- // apply diff and collect affected features and updated source that will be used to invalidate tiles
333
- const {affected, source} = applySourceDiff(this.source, diff, options);
334
-
335
- // nothing has changed
336
- if (!affected.length) return;
337
-
338
- // update source with new simplified feature set
339
- this.source = source;
340
-
341
- if (debug > 1) {
342
- console.log('invalidating tiles');
343
- console.time('invalidating');
344
- }
345
-
346
- this.invalidateTiles(affected);
347
-
348
- if (debug > 1) console.timeEnd('invalidating');
349
-
350
- // re-generate root tile with updated feature set
351
- const [z, x, y] = [0, 0, 0];
352
- const rootTile = createTile(this.source, z, x, y, this.options);
353
- rootTile.source = this.source;
354
-
355
- // update tile index with new root tile - ready for getTile calls
356
- const id = toID(z, x, y);
357
- this.tiles[id] = rootTile;
358
- this.tileCoords.push({z, x, y, id});
359
-
360
- if (debug) {
361
- const key = `z${ z}`;
362
- this.stats[key] = (this.stats[key] || 0) + 1;
363
- this.total++;
364
- }
365
- }
366
- }
367
-
368
- function toID(z: number, x: number, y: number): number {
369
- return (((1 << z) * y + x) * 32) + z;
370
- }
371
-
372
- export default function geojsonvt(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions) {
373
- return new GeoJSONVT(data, options);
2
+ import type {GeoJSONVTFeatureDiff, GeoJSONVTSourceDiff} from './difference';
3
+ import type {GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, GeoJSONToTileOptions, PartialGeoJSONVTFeature, StartEndSizeArray, ClusterProperties, ClusterFeature, ClusterOrPointFeature, GeoJSONVTTile, GeoJSONVTFeature, GeoJSONVTFeaturePoint, GeoJSONVTFeatureNonPoint, GeoJSONVTInternalTileFeaturePoint, GeoJSONVTInternalTileFeatureNonPoint, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTTileIndex, SuperclusterOptions} from './definitions';
4
+ import type {KDBushWithData} from './cluster-tile-index';
5
+ import {GeoJSONVT} from './geojsonvt';
6
+ import {ClusterTileIndex} from './cluster-tile-index';
7
+ import {geoJSONToTile} from './geojson-to-tile';
8
+
9
+ export {
10
+ GeoJSONVT,
11
+ ClusterTileIndex as Supercluster,
12
+ geoJSONToTile
374
13
  }
375
14
 
376
- export type {
15
+ export type {
377
16
  GeoJSONVTInternalFeature,
378
- GeoJSONVTOptions,
17
+ GeoJSONVTOptions,
18
+ GeoJSONToTileOptions,
379
19
  GeoJSONVTInternalTile,
380
20
  GeoJSONVTInternalTileFeature,
381
- GeometryType,
382
21
  PartialGeoJSONVTFeature,
383
- GeoJSONVT,
384
- GeometryTypeMap,
385
22
  StartEndSizeArray,
386
23
  GeoJSONVTTile,
387
24
  GeoJSONVTFeature,
@@ -390,5 +27,17 @@ export type {
390
27
  GeoJSONVTFeaturePoint,
391
28
  GeoJSONVTFeatureNonPoint,
392
29
  GeoJSONVTInternalTileFeaturePoint,
393
- GeoJSONVTInternalTileFeaturNonPoint
30
+ GeoJSONVTInternalTileFeatureNonPoint,
31
+ GeoJSONVTInternalPointFeature,
32
+ GeoJSONVTInternalMultiPointFeature,
33
+ GeoJSONVTInternalLineStringFeature,
34
+ GeoJSONVTInternalMultiLineStringFeature,
35
+ GeoJSONVTInternalPolygonFeature,
36
+ GeoJSONVTInternalMultiPolygonFeature,
37
+ SuperclusterOptions,
38
+ ClusterProperties,
39
+ ClusterFeature,
40
+ ClusterOrPointFeature,
41
+ KDBushWithData,
42
+ GeoJSONVTTileIndex
394
43
  };