@maplibre/geojson-vt 6.3.2 → 7.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 (44) hide show
  1. package/dist/clip.d.ts.map +1 -1
  2. package/dist/cluster-tile-index.d.ts +8 -0
  3. package/dist/cluster-tile-index.d.ts.map +1 -1
  4. package/dist/convert.d.ts.map +1 -1
  5. package/dist/deconvert.d.ts +0 -1
  6. package/dist/deconvert.d.ts.map +1 -1
  7. package/dist/definitions.d.ts +66 -25
  8. package/dist/definitions.d.ts.map +1 -1
  9. package/dist/feature.d.ts +16 -12
  10. package/dist/feature.d.ts.map +1 -1
  11. package/dist/geojson-vt-dev.js +602 -453
  12. package/dist/geojson-vt.js +1 -1
  13. package/dist/geojson-vt.mjs +599 -450
  14. package/dist/geojson-vt.mjs.map +1 -1
  15. package/dist/index.d.ts +2 -3
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/simplify.d.ts +1 -1
  18. package/dist/simplify.d.ts.map +1 -1
  19. package/dist/tile-index.d.ts.map +1 -1
  20. package/dist/tile.d.ts +5 -2
  21. package/dist/tile.d.ts.map +1 -1
  22. package/dist/transform.d.ts +5 -6
  23. package/dist/transform.d.ts.map +1 -1
  24. package/dist/wrap.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/src/clip.test.ts +52 -32
  27. package/src/clip.ts +225 -143
  28. package/src/cluster-tile-index.ts +29 -21
  29. package/src/convert.ts +78 -65
  30. package/src/deconvert.test.ts +29 -28
  31. package/src/deconvert.ts +51 -23
  32. package/src/definitions.ts +69 -17
  33. package/src/difference.test.ts +38 -75
  34. package/src/feature.ts +53 -51
  35. package/src/geojson-to-tile.ts +1 -1
  36. package/src/geojsonvt.test.ts +113 -0
  37. package/src/index.ts +1 -17
  38. package/src/simplify.ts +2 -2
  39. package/src/tile-index.test.ts +3 -9
  40. package/src/tile-index.ts +11 -10
  41. package/src/tile.ts +212 -92
  42. package/src/transform.test.ts +2 -3
  43. package/src/transform.ts +24 -99
  44. package/src/wrap.ts +12 -73
@@ -73,52 +73,67 @@ function getSqSegDist(px, py, x, y, bx, by) {
73
73
  }
74
74
  // src/feature.ts
75
75
  /**
76
- *
76
+ *
77
77
  * @param id - the feature's ID
78
78
  * @param type - the feature's type
79
- * @param geom - the feature's geometry
79
+ * @param geom - the feature's coordinates, see {@link GeoJSONVTSourceCoords}
80
80
  * @param tags - the feature's properties
81
81
  * @returns the created feature
82
82
  */
83
83
  function createFeature(id, type, geom, tags) {
84
- const data = {
85
- type,
86
- geom
87
- };
88
84
  const feature = {
89
85
  id: id == null ? null : id,
90
- type: data.type,
91
- geometry: data.geom,
86
+ type,
87
+ geometry: geom,
92
88
  tags,
93
89
  minX: 1e400,
94
90
  minY: 1e400,
95
91
  maxX: -1e400,
96
- maxY: -1e400
92
+ maxY: -1e400,
93
+ worldOffset: 0
97
94
  };
98
- switch (data.type) {
99
- case "Point":
100
- case "MultiPoint":
101
- calcLineBBox(feature, data.geom);
102
- break;
103
- case "LineString":
104
- calcLineBBox(feature, data.geom.points);
105
- break;
106
- case "Polygon":
107
- calcLineBBox(feature, data.geom[0].points);
108
- break;
109
- case "MultiLineString":
110
- for (const line of data.geom) calcLineBBox(feature, line.points);
111
- break;
112
- case "MultiPolygon": for (const polygon of data.geom) calcLineBBox(feature, polygon[0].points);
95
+ if (type === "MultiPoint") {
96
+ calcBBox(feature, geom, 0, geom.length);
97
+ return feature;
98
+ }
99
+ const outerOnly = type === "Polygon" || type === "MultiPolygon";
100
+ for (let i = 0; i < geom.length;) {
101
+ const end = i + 2 + geom[i] * 3;
102
+ if (!outerOnly || !isHole(geom[i + 1])) calcBBox(feature, geom, i + 2, end);
103
+ i = end;
113
104
  }
114
105
  return feature;
115
106
  }
116
- function optimizeLineMemory(line) {
117
- const lineImmutable = line;
118
- if (line.points.length > 64) lineImmutable.points = new Float64Array(line.points);
107
+ /**
108
+ * Whether a polygon ring is a hole, going by the size stored before it. A hole has a negative size, which is -0
109
+ * when it has no area. An outer ring whose area is not a number has NaN, so the test is not `size >= 0`.
110
+ */
111
+ function isHole(size) {
112
+ return size < 0 || Object.is(size, -0);
119
113
  }
120
- function calcLineBBox(feature, geom) {
121
- for (let i = 0; i < geom.length; i += 3) {
114
+ function createPointFeature(id, x, y, tags) {
115
+ return {
116
+ id: id == null ? null : id,
117
+ type: "Point",
118
+ x,
119
+ y,
120
+ tags
121
+ };
122
+ }
123
+ function getMinX(feature) {
124
+ return feature.type === "Point" ? feature.x : feature.minX;
125
+ }
126
+ function getMinY(feature) {
127
+ return feature.type === "Point" ? feature.y : feature.minY;
128
+ }
129
+ function getMaxX(feature) {
130
+ return feature.type === "Point" ? feature.x : feature.maxX;
131
+ }
132
+ function getMaxY(feature) {
133
+ return feature.type === "Point" ? feature.y : feature.maxY;
134
+ }
135
+ function calcBBox(feature, geom, start, end) {
136
+ for (let i = start; i < end; i += 3) {
122
137
  feature.minX = Math.min(feature.minX, geom[i]);
123
138
  feature.minY = Math.min(feature.minY, geom[i + 1]);
124
139
  feature.maxX = Math.max(feature.maxX, geom[i]);
@@ -219,22 +234,26 @@ function featureToInternal(features, geojson, options, index, depth = 0) {
219
234
  const worldCoordinateHelper = getWorldCoordinateHelper(options.coordinates);
220
235
  switch (geojson.geometry.type) {
221
236
  case "Point":
222
- convertPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
237
+ features.push(createPointFeature(id, worldCoordinateHelper.projectX(geojson.geometry.coordinates[0]), worldCoordinateHelper.projectY(geojson.geometry.coordinates[1]), geojson.properties));
223
238
  return;
224
239
  case "MultiPoint":
225
240
  convertMultiPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
226
241
  return;
227
242
  case "LineString":
228
- convertLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
243
+ convertLineStringFeature(features, id, geojson.geometry.coordinates, tolerance, worldCoordinateHelper, options, geojson.properties);
229
244
  return;
230
245
  case "MultiLineString":
231
- convertMultiLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, options, geojson.properties);
246
+ if (options.lineMetrics) {
247
+ for (const line of geojson.geometry.coordinates) convertLineStringFeature(features, id, line, tolerance, worldCoordinateHelper, options, geojson.properties);
248
+ return;
249
+ }
250
+ convertRingsFeature(features, id, "MultiLineString", [geojson.geometry.coordinates], tolerance, worldCoordinateHelper, geojson.properties);
232
251
  return;
233
252
  case "Polygon":
234
- convertPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
253
+ convertRingsFeature(features, id, "Polygon", [geojson.geometry.coordinates], tolerance, worldCoordinateHelper, geojson.properties);
235
254
  return;
236
255
  case "MultiPolygon":
237
- convertMultiPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
256
+ convertRingsFeature(features, id, "MultiPolygon", geojson.geometry.coordinates, tolerance, worldCoordinateHelper, geojson.properties);
238
257
  return;
239
258
  default: throw new Error("Input data is not a valid GeoJSON object.");
240
259
  }
@@ -252,56 +271,70 @@ function convertGeometryCollection(features, geojson, geometry, options, index,
252
271
  properties: geojson.properties
253
272
  }, options, index, depth);
254
273
  }
255
- function convertPointFeature(features, id, geom, worldCoordinateHelper, properties) {
256
- const out = [];
257
- out.push(worldCoordinateHelper.projectX(geom.coordinates[0]), worldCoordinateHelper.projectY(geom.coordinates[1]), 0);
258
- features.push(createFeature(id, "Point", out, properties));
259
- }
260
274
  function convertMultiPointFeature(features, id, geom, worldCoordinateHelper, properties) {
261
- const out = [];
262
- for (const coords of geom.coordinates) out.push(worldCoordinateHelper.projectX(coords[0]), worldCoordinateHelper.projectY(coords[1]), 0);
275
+ const out = new Float64Array(geom.coordinates.length * 3);
276
+ for (let i = 0; i < geom.coordinates.length; i++) {
277
+ out[i * 3] = worldCoordinateHelper.projectX(geom.coordinates[i][0]);
278
+ out[i * 3 + 1] = worldCoordinateHelper.projectY(geom.coordinates[i][1]);
279
+ }
263
280
  features.push(createFeature(id, "MultiPoint", out, properties));
264
281
  }
265
- function convertLineStringFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
266
- const out = { points: [] };
267
- convertLine(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
268
- features.push(createFeature(id, "LineString", out, properties));
269
- }
270
- function convertMultiLineStringFeature(features, id, geom, tolerance, worldCoordinateHelper, options, properties) {
271
- if (options.lineMetrics) for (const line of geom.coordinates) {
272
- const out = { points: [] };
273
- convertLine(line, out, tolerance, false, worldCoordinateHelper);
274
- features.push(createFeature(id, "LineString", out, properties));
275
- }
276
- else {
277
- const out = [];
278
- convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
279
- features.push(createFeature(id, "MultiLineString", out, properties));
282
+ function convertLineStringFeature(features, id, line, tolerance, worldCoordinateHelper, options, properties) {
283
+ if (!line.length) return;
284
+ const out = new Float64Array(2 + line.length * 3);
285
+ convertRing(line, out, 0, tolerance, false, false, worldCoordinateHelper);
286
+ const feature = createFeature(id, "LineString", out, properties);
287
+ if (options.lineMetrics) {
288
+ feature.start = 0;
289
+ feature.end = feature.size = out[1];
280
290
  }
291
+ features.push(feature);
281
292
  }
282
- function convertPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
283
- const out = [];
284
- convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
285
- features.push(createFeature(id, "Polygon", out, properties));
286
- }
287
- function convertMultiPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
288
- const out = [];
289
- for (const polygon of geom.coordinates) {
290
- const polygonOut = [];
291
- convertLines(polygon, polygonOut, tolerance, true, worldCoordinateHelper);
292
- if (!polygonOut.length) continue;
293
- out.push(polygonOut);
293
+ /**
294
+ * Converts a MultiLineString, a Polygon or a MultiPolygon, whose rings come in groups: all lines in one,
295
+ * and the rings of one polygon in each. The first ring of a polygon is its outer ring, and the rest are its holes.
296
+ */
297
+ function convertRingsFeature(features, id, type, groups, tolerance, worldCoordinateHelper, properties) {
298
+ let length = 0;
299
+ for (const rings of groups) for (const ring of rings) if (ring.length) length += 2 + ring.length * 3;
300
+ if (!length) return;
301
+ const isPolygon = type !== "MultiLineString";
302
+ const out = new Float64Array(length);
303
+ let index = 0;
304
+ for (const rings of groups) {
305
+ let isOuter = true;
306
+ for (const ring of rings) {
307
+ if (!ring.length) continue;
308
+ index = convertRing(ring, out, index, tolerance, isPolygon, isOuter, worldCoordinateHelper);
309
+ isOuter = false;
310
+ }
294
311
  }
295
- if (!out.length) return;
296
- features.push(createFeature(id, "MultiPolygon", out, properties));
312
+ features.push(createFeature(id, type, out, properties));
297
313
  }
298
- function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
314
+ /**
315
+ * The size a hole is stored with, which has to be negative for {@link isHole} to tell it from an outer ring.
316
+ * A hole without area gets -0, and one whose area is not a number gets -Infinity. Both compare with a tolerance
317
+ * the way 0 and NaN do.
318
+ */
319
+ function toHoleSize(size) {
320
+ const magnitude = Math.abs(size);
321
+ return Number.isNaN(magnitude) ? -1e400 : -magnitude;
322
+ }
323
+ /**
324
+ * Writes a ring and the two values before it, see {@link GeoJSONVTSourceCoords}.
325
+ * @returns where the next ring starts
326
+ */
327
+ function convertRing(ring, out, index, tolerance, isPolygon, isOuter, worldCoordinateHelper) {
328
+ const first = index + 2;
299
329
  let x0, y0;
300
330
  let size = 0;
331
+ let next = first;
301
332
  for (let j = 0; j < ring.length; j++) {
302
333
  const x = worldCoordinateHelper.projectX(ring[j][0]);
303
334
  const y = worldCoordinateHelper.projectY(ring[j][1]);
304
- out.points.push(x, y, 0);
335
+ out[next] = x;
336
+ out[next + 1] = y;
337
+ next += 3;
305
338
  if (j > 0) {
306
339
  if (isPolygon) size += (x0 * y - x * y0) / 2;
307
340
  else size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2));
@@ -309,21 +342,13 @@ function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
309
342
  x0 = x;
310
343
  y0 = y;
311
344
  }
312
- const last = out.points.length - 3;
313
- out.points[2] = 1;
314
- if (tolerance > 0) simplify(out.points, 0, last, tolerance);
315
- out.points[last + 2] = 1;
316
- optimizeLineMemory(out);
317
- out.size = Math.abs(size);
318
- out.start = 0;
319
- out.end = out.size;
320
- }
321
- function convertLines(rings, out, tolerance, isPolygon, worldCoordinateHelper) {
322
- for (let i = 0; i < rings.length; i++) {
323
- const geom = { points: [] };
324
- convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
325
- out.push(geom);
326
- }
345
+ const last = next - 3;
346
+ out[first + 2] = 1;
347
+ if (tolerance > 0) simplify(out, first, last, tolerance);
348
+ out[last + 2] = 1;
349
+ out[index] = ring.length;
350
+ out[index + 1] = isPolygon && !isOuter ? toHoleSize(size) : Math.abs(size);
351
+ return next;
327
352
  }
328
353
  // src/deconvert.ts
329
354
  /**
@@ -351,41 +376,63 @@ function featureToGeoJSON(feature, worldCoordinateHelper = mercatorWorldCoordina
351
376
  * Converts a single internal feature geometry to GeoJSON format.
352
377
  */
353
378
  function geometryToGeoJSON(feature, worldCoordinateHelper) {
354
- const { type, geometry } = feature;
355
- switch (type) {
379
+ switch (feature.type) {
356
380
  case "Point": return {
357
- type,
358
- coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinateHelper)
381
+ type: feature.type,
382
+ coordinates: [worldCoordinateHelper.unprojectX(feature.x), worldCoordinateHelper.unprojectY(feature.y)]
359
383
  };
360
384
  case "MultiPoint": return {
361
- type,
362
- coordinates: unprojectPoints(geometry, worldCoordinateHelper)
385
+ type: feature.type,
386
+ coordinates: unprojectPoints(feature.geometry, feature.worldOffset, 0, feature.geometry.length, worldCoordinateHelper)
363
387
  };
364
388
  case "LineString": return {
365
- type,
366
- coordinates: unprojectPoints(geometry.points, worldCoordinateHelper)
389
+ type: feature.type,
390
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)[0]
367
391
  };
368
392
  case "MultiLineString":
369
393
  case "Polygon": return {
370
- type,
371
- coordinates: geometry.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper))
394
+ type: feature.type,
395
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)
372
396
  };
373
397
  case "MultiPolygon": return {
374
- type,
375
- coordinates: geometry.map((polygon) => polygon.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper)))
398
+ type: feature.type,
399
+ coordinates: unprojectPolygons(feature.geometry, feature.worldOffset, worldCoordinateHelper)
376
400
  };
377
401
  }
378
402
  }
379
- function unprojectPoints(coords, worldCoordinateHelper = mercatorWorldCoordinateHelper) {
403
+ function unprojectRings(coords, offset, worldCoordinateHelper) {
404
+ const rings = [];
405
+ for (let i = 0; i < coords.length;) {
406
+ const ringEnd = i + 2 + coords[i] * 3;
407
+ rings.push(unprojectPoints(coords, offset, i + 2, ringEnd, worldCoordinateHelper));
408
+ i = ringEnd;
409
+ }
410
+ return rings;
411
+ }
412
+ /**
413
+ * The rings in groups of one polygon each, where a ring with a negative size is a hole of the polygon before it.
414
+ */
415
+ function unprojectPolygons(coords, offset, worldCoordinateHelper) {
416
+ const polygons = [];
417
+ for (let i = 0; i < coords.length;) {
418
+ const ringEnd = i + 2 + coords[i] * 3;
419
+ const ring = unprojectPoints(coords, offset, i + 2, ringEnd, worldCoordinateHelper);
420
+ if (!isHole(coords[i + 1]) || !polygons.length) polygons.push([ring]);
421
+ else polygons[polygons.length - 1].push(ring);
422
+ i = ringEnd;
423
+ }
424
+ return polygons;
425
+ }
426
+ /**
427
+ * @param offset - what to add to every x of `coords`
428
+ */
429
+ function unprojectPoints(coords, offset, start, end, worldCoordinateHelper) {
380
430
  const result = [];
381
- for (let i = 0; i < coords.length; i += 3) result.push(unprojectPoint(coords[i], coords[i + 1], worldCoordinateHelper));
431
+ for (let i = start; i < end; i += 3) result.push([worldCoordinateHelper.unprojectX(coords[i] + offset), worldCoordinateHelper.unprojectY(coords[i + 1])]);
382
432
  return result;
383
433
  }
384
- function unprojectPoint(x, y, worldCoordinateHelper) {
385
- return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
386
- }
387
434
  // src/clip.ts
388
- /**
435
+ /**
389
436
  * clip features between two vertical or horizontal axis-parallel lines:
390
437
  * | |
391
438
  * ___|___ | /
@@ -407,6 +454,11 @@ function clip(features, scale, start, end, axis, minAll, maxAll, options) {
407
454
  if (maxAll < start || minAll >= end) return null;
408
455
  const clipped = [];
409
456
  for (const feature of features) {
457
+ if (feature.type === "Point") {
458
+ const a = axis === 0 ? feature.x : feature.y;
459
+ if (a >= start && a < end) clipped.push(feature);
460
+ continue;
461
+ }
410
462
  const min = axis === 0 ? feature.minX : feature.minY;
411
463
  const max = axis === 0 ? feature.maxX : feature.maxY;
412
464
  if (min >= start && max < end) {
@@ -414,157 +466,238 @@ function clip(features, scale, start, end, axis, minAll, maxAll, options) {
414
466
  continue;
415
467
  }
416
468
  if (max < start || min >= end) continue;
417
- switch (feature.type) {
418
- case "MultiPoint":
419
- clipPointFeature(feature, clipped, start, end, axis);
420
- continue;
421
- case "LineString":
422
- clipLineStringFeature(feature, clipped, start, end, axis, options);
423
- continue;
424
- case "MultiLineString":
425
- clipMultiLineStringFeature(feature, clipped, start, end, axis);
426
- continue;
427
- case "Polygon":
428
- clipPolygonFeature(feature, clipped, start, end, axis);
429
- continue;
430
- case "MultiPolygon":
431
- clipMultiPolygonFeature(feature, clipped, start, end, axis);
432
- continue;
469
+ if (feature.type === "MultiPoint") {
470
+ clipPointsFeature(feature, clipped, start, end, axis);
471
+ continue;
433
472
  }
473
+ clipRingsFeature(feature, clipped, start, end, axis, options);
434
474
  }
435
475
  if (!clipped.length) return null;
436
476
  return clipped;
437
477
  }
438
- function clipPointFeature(feature, clipped, start, end, axis) {
439
- const geom = [];
440
- clipPoints(feature.geometry, geom, start, end, axis);
441
- if (!geom.length) return;
442
- const type = geom.length === 3 ? "Point" : "MultiPoint";
443
- clipped.push(createFeature(feature.id, type, geom, feature.tags));
444
- }
445
- function clipLineStringFeature(feature, clipped, start, end, axis, options) {
446
- const geom = [];
447
- clipLine(feature.geometry, geom, start, end, axis, false, options.lineMetrics);
448
- if (!geom.length) return;
449
- if (options.lineMetrics) {
450
- for (const line of geom) clipped.push(createFeature(feature.id, "LineString", line, feature.tags));
478
+ function clipPointsFeature(feature, clipped, start, end, axis) {
479
+ const geom = feature.geometry;
480
+ const offset = feature.worldOffset;
481
+ const axisOffset = axis === 0 ? offset : 0;
482
+ let count = 0;
483
+ let lastInside = 0;
484
+ for (let i = 0; i < geom.length; i += 3) {
485
+ const a = geom[i + axis] + axisOffset;
486
+ if (a >= start && a <= end) {
487
+ count++;
488
+ lastInside = i;
489
+ }
490
+ }
491
+ if (!count) return;
492
+ if (count === 1) {
493
+ clipped.push(createPointFeature(feature.id, geom[lastInside] + offset, geom[lastInside + 1], feature.tags));
451
494
  return;
452
495
  }
453
- if (geom.length > 1) {
454
- clipped.push(createFeature(feature.id, "MultiLineString", geom, feature.tags));
496
+ const out = new Float64Array(count * 3);
497
+ let next = 0;
498
+ for (let i = 0; i < geom.length; i += 3) {
499
+ const a = geom[i + axis] + axisOffset;
500
+ if (a >= start && a <= end) {
501
+ out[next] = geom[i] + offset;
502
+ out[next + 1] = geom[i + 1];
503
+ out[next + 2] = geom[i + 2];
504
+ next += 3;
505
+ }
506
+ }
507
+ clipped.push(createFeature(feature.id, "MultiPoint", out, feature.tags));
508
+ }
509
+ function clipRingsFeature(feature, clipped, start, end, axis, options) {
510
+ const geom = feature.geometry;
511
+ const offset = feature.worldOffset;
512
+ const isPolygon = feature.type === "Polygon" || feature.type === "MultiPolygon";
513
+ let length = 0;
514
+ for (let i = 0; i < geom.length;) {
515
+ const ringEnd = i + 2 + geom[i] * 3;
516
+ length += countClippedRing(geom, offset, i + 2, ringEnd, start, end, axis, isPolygon);
517
+ i = ringEnd;
518
+ }
519
+ if (!length) return;
520
+ const metricsOf = options.lineMetrics && feature.type === "LineString" ? feature : null;
521
+ const metrics = [];
522
+ let out = new Float64Array(length);
523
+ let next = 0;
524
+ let hasOuter = false;
525
+ for (let i = 0; i < geom.length;) {
526
+ const ringEnd = i + 2 + geom[i] * 3;
527
+ let size = geom[i + 1];
528
+ if (isPolygon) {
529
+ if (!isHole(size)) hasOuter = false;
530
+ if (!hasOuter) size = Math.abs(size);
531
+ }
532
+ const ringStart = next;
533
+ next = clipRing(geom, offset, i + 2, ringEnd, size, out, next, start, end, axis, isPolygon, metricsOf, metrics);
534
+ if (next > ringStart) hasOuter = true;
535
+ i = ringEnd;
536
+ }
537
+ if (next < length) out = out.subarray(0, next);
538
+ if (metricsOf) {
539
+ for (let i = 0, part = 0; i < out.length; part += 2) {
540
+ const ringEnd = i + 2 + out[i] * 3;
541
+ const line = createFeature(feature.id, "LineString", i === 0 && ringEnd === out.length ? out : out.slice(i, ringEnd), feature.tags);
542
+ line.start = metrics[part];
543
+ line.end = metrics[part + 1];
544
+ line.size = metricsOf.size;
545
+ clipped.push(line);
546
+ i = ringEnd;
547
+ }
455
548
  return;
456
549
  }
457
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
458
- }
459
- function clipMultiLineStringFeature(feature, clipped, start, end, axis) {
460
- const geom = [];
461
- clipLines(feature.geometry, geom, start, end, axis, false);
462
- if (!geom.length) return;
463
- if (geom.length === 1) {
464
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
550
+ if (isPolygon) {
551
+ clipped.push(createFeature(feature.id, feature.type, out, feature.tags));
465
552
  return;
466
553
  }
467
- clipped.push(createFeature(feature.id, "MultiLineString", geom, feature.tags));
554
+ const isSingleLine = 2 + out[0] * 3 === out.length;
555
+ clipped.push(createFeature(feature.id, isSingleLine ? "LineString" : "MultiLineString", out, feature.tags));
468
556
  }
469
- function clipPolygonFeature(feature, clipped, start, end, axis) {
470
- const geom = [];
471
- clipLines(feature.geometry, geom, start, end, axis, true);
472
- if (!geom.length) return;
473
- clipped.push(createFeature(feature.id, "Polygon", geom, feature.tags));
474
- }
475
- function clipMultiPolygonFeature(feature, clipped, start, end, axis) {
476
- const geom = [];
477
- for (const polygon of feature.geometry) {
478
- const newPolygon = [];
479
- clipLines(polygon, newPolygon, start, end, axis, true);
480
- if (!newPolygon.length) continue;
481
- geom.push(newPolygon);
557
+ /**
558
+ * How many values `clipRing` writes for a ring, without working out the intersections.
559
+ * @param offset - what to add to every x of `geom`
560
+ */
561
+ function countClippedRing(geom, offset, first, ringEnd, start, end, axis, isPolygon) {
562
+ const axisOffset = axis === 0 ? offset : 0;
563
+ let parts = 0;
564
+ let total = 0;
565
+ let count = 0;
566
+ for (let i = first; i < ringEnd - 3; i += 3) {
567
+ const a = geom[i + axis] + axisOffset;
568
+ const b = geom[i + 3 + axis] + axisOffset;
569
+ let exited = false;
570
+ if (a < start) {
571
+ if (b >= start) count++;
572
+ } else if (a > end) {
573
+ if (b <= end) count++;
574
+ } else count++;
575
+ if (b < start && a >= start) {
576
+ count++;
577
+ exited = true;
578
+ }
579
+ if (b > end && a <= end) {
580
+ count++;
581
+ exited = true;
582
+ }
583
+ if (!isPolygon && exited) {
584
+ total += count;
585
+ parts++;
586
+ count = 0;
587
+ }
482
588
  }
483
- if (!geom.length) return;
484
- clipped.push(createFeature(feature.id, "MultiPolygon", geom, feature.tags));
485
- }
486
- function clipPoints(geom, newGeom, start, end, axis) {
487
- for (let i = 0; i < geom.length; i += 3) {
488
- const a = geom[i + axis];
489
- if (a >= start && a <= end) addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
589
+ const a = geom[ringEnd - 3 + axis] + axisOffset;
590
+ if (a >= start && a <= end) count++;
591
+ if (isPolygon && count >= 2) count++;
592
+ if (count > 0) {
593
+ total += count;
594
+ parts++;
490
595
  }
596
+ return total * 3 + parts * 2;
491
597
  }
492
- function clipLine(geom, newGeom, start, end, axis, isPolygon, trackMetrics) {
493
- let slice = newSlice(geom);
494
- const intersect = axis === 0 ? intersectX : intersectY;
495
- let len = geom.start;
598
+ /**
599
+ * Clips a ring and writes what is left of it to `out`, each part with the two values before it,
600
+ * see {@link GeoJSONVTSourceCoords}. A polygon ring stays one ring, and a line can come out in several parts.
601
+ * @param offset - what to add to every x of `geom`, which `out` has added already
602
+ * @param metricsOf - the line whose metrics to track, which adds where each part starts and ends to `metrics`
603
+ * @returns where the next ring starts in `out`
604
+ */
605
+ function clipRing(geom, offset, first, ringEnd, size, out, next, start, end, axis, isPolygon, metricsOf, metrics) {
606
+ let header = next;
607
+ out[next + 1] = size;
608
+ next += 2;
609
+ let partStart = next;
610
+ let len = metricsOf ? metricsOf.start : 0;
611
+ let metricStart = len;
496
612
  let segLen, t;
497
- for (let i = 0; i < geom.points.length - 3; i += 3) {
498
- const ax = geom.points[i];
499
- const ay = geom.points[i + 1];
500
- const az = geom.points[i + 2];
501
- const bx = geom.points[i + 3];
502
- const by = geom.points[i + 4];
613
+ for (let i = first; i < ringEnd - 3; i += 3) {
614
+ const ax = geom[i] + offset;
615
+ const ay = geom[i + 1];
616
+ const az = geom[i + 2];
617
+ const bx = geom[i + 3] + offset;
618
+ const by = geom[i + 4];
503
619
  const a = axis === 0 ? ax : ay;
504
620
  const b = axis === 0 ? bx : by;
505
621
  let exited = false;
506
- if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
622
+ if (metricsOf) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
507
623
  if (a < start) {
508
624
  if (b >= start) {
509
- t = intersect(slice, ax, ay, bx, by, start);
510
- if (trackMetrics) slice.start = len + segLen * t;
625
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
626
+ next += 3;
627
+ if (metricsOf) metricStart = len + segLen * t;
511
628
  }
512
629
  } else if (a > end) {
513
630
  if (b <= end) {
514
- t = intersect(slice, ax, ay, bx, by, end);
515
- if (trackMetrics) slice.start = len + segLen * t;
631
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
632
+ next += 3;
633
+ if (metricsOf) metricStart = len + segLen * t;
516
634
  }
517
- } else addPoint(slice.points, ax, ay, az);
635
+ } else {
636
+ out[next] = ax;
637
+ out[next + 1] = ay;
638
+ out[next + 2] = az;
639
+ next += 3;
640
+ }
518
641
  if (b < start && a >= start) {
519
- t = intersect(slice, ax, ay, bx, by, start);
642
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
643
+ next += 3;
520
644
  exited = true;
521
645
  }
522
646
  if (b > end && a <= end) {
523
- t = intersect(slice, ax, ay, bx, by, end);
647
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
648
+ next += 3;
524
649
  exited = true;
525
650
  }
526
651
  if (!isPolygon && exited) {
527
- if (trackMetrics) slice.end = len + segLen * t;
528
- newGeom.push(slice);
529
- slice = newSlice(geom);
652
+ out[header] = (next - partStart) / 3;
653
+ if (metricsOf) {
654
+ metrics.push(metricStart, len + segLen * t);
655
+ metricStart = metricsOf.start;
656
+ }
657
+ header = next;
658
+ out[next + 1] = size;
659
+ next += 2;
660
+ partStart = next;
530
661
  }
531
- if (trackMetrics) len += segLen;
662
+ if (metricsOf) len += segLen;
532
663
  }
533
- let last = geom.points.length - 3;
534
- const ax = geom.points[last];
535
- const ay = geom.points[last + 1];
536
- const az = geom.points[last + 2];
664
+ const last = ringEnd - 3;
665
+ const ax = geom[last] + offset;
666
+ const ay = geom[last + 1];
537
667
  const a = axis === 0 ? ax : ay;
538
- if (a >= start && a <= end) addPoint(slice.points, ax, ay, az);
539
- last = slice.points.length - 3;
540
- if (isPolygon && last >= 3 && (slice.points[last] !== slice.points[0] || slice.points[last + 1] !== slice.points[1])) addPoint(slice.points, slice.points[0], slice.points[1], slice.points[2]);
541
- if (slice.points.length) {
542
- optimizeLineMemory(slice);
543
- newGeom.push(slice);
544
- }
668
+ if (a >= start && a <= end) {
669
+ out[next] = ax;
670
+ out[next + 1] = ay;
671
+ out[next + 2] = geom[last + 2];
672
+ next += 3;
673
+ }
674
+ if (isPolygon && next - partStart >= 6 && (out[next - 3] !== out[partStart] || out[next - 2] !== out[partStart + 1])) {
675
+ out[next] = out[partStart];
676
+ out[next + 1] = out[partStart + 1];
677
+ out[next + 2] = out[partStart + 2];
678
+ next += 3;
679
+ }
680
+ if (next === partStart) return header;
681
+ out[header] = (next - partStart) / 3;
682
+ if (metricsOf) metrics.push(metricStart, metricsOf.end);
683
+ return next;
545
684
  }
546
- function newSlice(line) {
547
- return {
548
- points: [],
549
- size: line.size,
550
- start: line.start,
551
- end: line.end
552
- };
553
- }
554
- function clipLines(geom, newGeom, start, end, axis, isPolygon) {
555
- for (const line of geom) clipLine(line, newGeom, start, end, axis, isPolygon, false);
556
- }
557
- function addPoint(out, x, y, z) {
558
- out.push(x, y, z);
559
- }
560
- function intersectX(out, ax, ay, bx, by, x) {
561
- const t = (x - ax) / (bx - ax);
562
- addPoint(out.points, x, ay + (by - ay) * t, 1);
563
- return t;
564
- }
565
- function intersectY(out, ax, ay, bx, by, y) {
566
- const t = (y - ay) / (by - ay);
567
- addPoint(out.points, ax + (bx - ax) * t, y, 1);
685
+ /**
686
+ * Writes the position where a segment crosses the clip line at `k`.
687
+ * @returns how far along the segment that is, from 0 to 1
688
+ */
689
+ function intersect(out, next, ax, ay, bx, by, k, axis) {
690
+ let t;
691
+ if (axis === 0) {
692
+ t = (k - ax) / (bx - ax);
693
+ out[next] = k;
694
+ out[next + 1] = ay + (by - ay) * t;
695
+ } else {
696
+ t = (k - ay) / (by - ay);
697
+ out[next] = ax + (bx - ax) * t;
698
+ out[next + 1] = k;
699
+ }
700
+ out[next + 2] = 1;
568
701
  return t;
569
702
  }
570
703
  // src/wrap.ts
@@ -580,58 +713,26 @@ function wrap(features, options) {
580
713
  if (right) merged = merged.concat(shiftFeatureCoords(right, -1));
581
714
  return merged;
582
715
  }
716
+ /**
717
+ * The features on the other side of the antimeridian. A feature with a coordinates array shares it with its copy,
718
+ * which only gets the offset.
719
+ */
583
720
  function shiftFeatureCoords(features, offset) {
584
721
  const newFeatures = [];
585
- for (const feature of features) switch (feature.type) {
586
- case "Point":
587
- case "MultiPoint": {
588
- const newGeometry = shiftPointCoords(feature.geometry, offset);
589
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
590
- continue;
591
- }
592
- case "LineString": {
593
- const newGeometry = shiftLineCoords(feature.geometry, offset);
594
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
595
- continue;
596
- }
597
- case "MultiLineString":
598
- case "Polygon": {
599
- const newGeometry = [];
600
- for (const line of feature.geometry) newGeometry.push(shiftLineCoords(line, offset));
601
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
602
- continue;
603
- }
604
- case "MultiPolygon": {
605
- const newGeometry = [];
606
- for (const polygon of feature.geometry) {
607
- const newPolygon = [];
608
- for (const line of polygon) newPolygon.push(shiftLineCoords(line, offset));
609
- newGeometry.push(newPolygon);
610
- }
611
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
722
+ for (const feature of features) {
723
+ if (feature.type === "Point") {
724
+ newFeatures.push(createPointFeature(feature.id, feature.x + offset, feature.y, feature.tags));
612
725
  continue;
613
726
  }
727
+ newFeatures.push({
728
+ ...feature,
729
+ minX: feature.minX + offset,
730
+ maxX: feature.maxX + offset,
731
+ worldOffset: feature.worldOffset + offset
732
+ });
614
733
  }
615
734
  return newFeatures;
616
735
  }
617
- function shiftPointCoords(coords, offset) {
618
- const newCoords = [];
619
- for (let i = 0; i < coords.length; i += 3) newCoords.push(coords[i] + offset, coords[i + 1], coords[i + 2]);
620
- return newCoords;
621
- }
622
- function shiftLineCoords(line, offset) {
623
- const newLine = {
624
- points: [],
625
- size: line.size
626
- };
627
- if (line.start !== void 0) {
628
- newLine.start = line.start;
629
- newLine.end = line.end;
630
- }
631
- for (let i = 0; i < line.points.length; i += 3) newLine.points.push(line.points[i] + offset, line.points[i + 1], line.points[i + 2]);
632
- optimizeLineMemory(newLine);
633
- return newLine;
634
- }
635
736
  // src/difference.ts
636
737
  /**
637
738
  * Applies a GeoJSON Source Diff to an existing set of simplified features
@@ -1143,11 +1244,12 @@ var ClusterTileIndex = class {
1143
1244
  const positions = point.geometry.type === "MultiPoint" ? point.geometry.coordinates : [point.geometry.coordinates];
1144
1245
  for (const [lng, lat] of positions) {
1145
1246
  const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
1146
- if (!this.options.wrap && !isInWorld([x, y])) continue;
1247
+ if (!this.options.wrap && !isInWorld(x, y)) continue;
1147
1248
  const feature = {
1148
1249
  id: point.id,
1149
1250
  type: "Point",
1150
- geometry: [x, y],
1251
+ x,
1252
+ y,
1151
1253
  tags: point.properties
1152
1254
  };
1153
1255
  features.push(feature);
@@ -1165,18 +1267,20 @@ var ClusterTileIndex = class {
1165
1267
  const points = [];
1166
1268
  for (const feature of features) {
1167
1269
  if (feature.type === "Point") {
1168
- if (!this.options.wrap && !isInWorld(feature.geometry)) continue;
1270
+ if (!this.isClustered(feature.x, feature.y)) continue;
1169
1271
  points.push(feature);
1170
1272
  continue;
1171
1273
  }
1172
1274
  if (feature.type !== "MultiPoint") continue;
1173
1275
  for (let i = 0; i < feature.geometry.length; i += 3) {
1174
- const geometry = [feature.geometry[i], feature.geometry[i + 1]];
1175
- if (!this.options.wrap && !isInWorld(geometry)) continue;
1276
+ const x = feature.geometry[i] + feature.worldOffset;
1277
+ const y = feature.geometry[i + 1];
1278
+ if (!this.isClustered(x, y)) continue;
1176
1279
  points.push({
1177
1280
  id: feature.id,
1178
1281
  type: "Point",
1179
- geometry,
1282
+ x,
1283
+ y,
1180
1284
  tags: feature.tags
1181
1285
  });
1182
1286
  }
@@ -1184,6 +1288,14 @@ var ClusterTileIndex = class {
1184
1288
  this.createIndex(points);
1185
1289
  }
1186
1290
  /**
1291
+ * Whether a position of the data source goes into the clusters. In a world that wraps, the source also has a copy
1292
+ * of each position near the antimeridian on the other side of it. Only the one inside the world is clustered, since
1293
+ * `getTile` looks across the antimeridian by itself. In a world that doesn't wrap, the positions outside it are left out.
1294
+ */
1295
+ isClustered(x, y) {
1296
+ return this.options.wrap ? x >= 0 && x < 1 : isInWorld(x, y);
1297
+ }
1298
+ /**
1187
1299
  * @internal
1188
1300
  * Updates the cluster data by rebuilding.
1189
1301
  * @param features
@@ -1216,9 +1328,9 @@ var ClusterTileIndex = class {
1216
1328
  let numItems = 0;
1217
1329
  for (let i = 0; i < points.length; i++) {
1218
1330
  const p = points[i];
1219
- if (!p?.geometry || !fitsInteger(p.geometry[0]) || !fitsInteger(p.geometry[1])) continue;
1220
- coords[numItems * 2] = Math.round(encode(p.geometry[0]));
1221
- coords[numItems * 2 + 1] = Math.round(encode(p.geometry[1]));
1331
+ if (!fitsInteger(p.x) || !fitsInteger(p.y)) continue;
1332
+ coords[numItems * 2] = Math.round(encode(p.x));
1333
+ coords[numItems * 2 + 1] = Math.round(encode(p.y));
1222
1334
  const k = numItems * stride;
1223
1335
  data[k + OFFSET_ZOOM] = notProcessed;
1224
1336
  data[k + OFFSET_ID] = i;
@@ -1395,14 +1507,7 @@ var ClusterTileIndex = class {
1395
1507
  const p = radius / extent;
1396
1508
  const top = encode((y - p) / z2);
1397
1509
  const bottom = encode((y + 1 + p) / z2);
1398
- const tile = {
1399
- transformed: true,
1400
- features: [],
1401
- source: null,
1402
- x,
1403
- y,
1404
- z
1405
- };
1510
+ const tile = { features: [] };
1406
1511
  this.addTileFeatures(tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom), tree, x, y, z2, tile);
1407
1512
  if (this.options.wrap && x === 0) this.addTileFeatures(tree.range(encode(1 - p / z2), top, encode(1), bottom), tree, z2, y, z2, tile);
1408
1513
  if (this.options.wrap && x === z2 - 1) this.addTileFeatures(tree.range(encode(0), top, encode(p / z2), bottom), tree, -1, y, z2, tile);
@@ -1462,7 +1567,8 @@ var ClusterTileIndex = class {
1462
1567
  } else {
1463
1568
  const p = this.points[data[k + OFFSET_ID]];
1464
1569
  tags = p.tags;
1465
- [px, py] = p.geometry;
1570
+ px = p.x;
1571
+ py = p.y;
1466
1572
  }
1467
1573
  const f = {
1468
1574
  type: 1,
@@ -1585,7 +1691,8 @@ function getClusterFeature(coords, index, data, i, clusterProps) {
1585
1691
  id: data[i + OFFSET_ID],
1586
1692
  type: "Point",
1587
1693
  tags: getClusterProperties(data, i, clusterProps),
1588
- geometry: [decode(coords[index * 2]), decode(coords[index * 2 + 1])]
1694
+ x: decode(coords[index * 2]),
1695
+ y: decode(coords[index * 2 + 1])
1589
1696
  };
1590
1697
  }
1591
1698
  function getClusterGeoJSON(coords, index, data, i, clusterProps, worldCoordinateHelper) {
@@ -1611,25 +1718,31 @@ function getClusterProperties(data, i, clusterProps) {
1611
1718
  point_count_abbreviated: abbrev
1612
1719
  });
1613
1720
  }
1614
- function isInWorld(geometry) {
1615
- return geometry[0] >= 0 && geometry[0] <= 1 && geometry[1] >= 0 && geometry[1] <= 1;
1721
+ function isInWorld(x, y) {
1722
+ return x >= 0 && x <= 1 && y >= 0 && y <= 1;
1616
1723
  }
1617
1724
  // src/tile.ts
1618
1725
  const GEOJSONVT_CLIP_START = "geojsonvt_clip_start";
1619
1726
  const GEOJSONVT_CLIP_END = "geojsonvt_clip_end";
1620
1727
  /**
1621
- * Creates a tile object from the given features
1728
+ * The smallest array type that holds the coordinates of a tile whose features are cut at its buffer.
1729
+ */
1730
+ function getTileCoordsConstructor(options) {
1731
+ return options.extent + options.buffer <= 32767 ? Int16Array : Int32Array;
1732
+ }
1733
+ /**
1734
+ * Creates a tile object from the given features, with their coordinates in the tile's extent
1622
1735
  * @param features - the features to include in the tile
1623
1736
  * @param z
1624
1737
  * @param tx
1625
1738
  * @param ty
1626
1739
  * @param options - the options object
1740
+ * @param CoordArray - what holds the coordinates, which has to be a `Float64Array` when the features are not cut at the tile's buffer
1627
1741
  * @returns the created tile
1628
1742
  */
1629
- function createTile(features, z, tx, ty, options) {
1743
+ function createTile(features, z, tx, ty, options, CoordArray = getTileCoordsConstructor(options)) {
1630
1744
  const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
1631
1745
  const tile = {
1632
- transformed: false,
1633
1746
  features: [],
1634
1747
  source: null,
1635
1748
  x: tx,
@@ -1643,39 +1756,53 @@ function createTile(features, z, tx, ty, options) {
1643
1756
  numSimplified: 0,
1644
1757
  numFeatures: features.length
1645
1758
  };
1646
- for (const feature of features) addFeature(tile, feature, tolerance, options);
1759
+ const frame = {
1760
+ z2: 1 << z,
1761
+ tx,
1762
+ ty,
1763
+ extent: options.extent,
1764
+ CoordArray
1765
+ };
1766
+ for (const feature of features) addFeature(tile, feature, tolerance, options, frame);
1647
1767
  return tile;
1648
1768
  }
1649
- function addFeature(tile, feature, tolerance, options) {
1769
+ function addFeature(tile, feature, tolerance, options, frame) {
1770
+ if (feature.type === "Point") {
1771
+ addPointTileFeature(tile, feature, frame);
1772
+ return;
1773
+ }
1650
1774
  tile.minX = Math.min(tile.minX, feature.minX);
1651
1775
  tile.minY = Math.min(tile.minY, feature.minY);
1652
1776
  tile.maxX = Math.max(tile.maxX, feature.maxX);
1653
1777
  tile.maxY = Math.max(tile.maxY, feature.maxY);
1654
- switch (feature.type) {
1655
- case "Point":
1656
- case "MultiPoint":
1657
- addPointsTileFeature(tile, feature);
1658
- return;
1659
- case "LineString":
1660
- addLineTileFeautre(tile, feature, tolerance, options);
1661
- return;
1662
- case "MultiLineString":
1663
- case "Polygon":
1664
- addLinesTileFeature(tile, feature, tolerance);
1665
- return;
1666
- case "MultiPolygon":
1667
- addMultiPolygonTileFeature(tile, feature, tolerance);
1668
- return;
1778
+ if (feature.type === "MultiPoint") {
1779
+ addPointsTileFeature(tile, feature, frame);
1780
+ return;
1669
1781
  }
1782
+ addRingsTileFeature(tile, feature, tolerance, options, frame);
1670
1783
  }
1671
- function addPointsTileFeature(tile, feature) {
1672
- const geometry = [];
1673
- for (let i = 0; i < feature.geometry.length; i += 3) {
1674
- geometry.push(feature.geometry[i], feature.geometry[i + 1]);
1675
- tile.numPoints++;
1676
- tile.numSimplified++;
1677
- }
1678
- if (!geometry.length) return;
1784
+ function addPointTileFeature(tile, feature, frame) {
1785
+ tile.minX = Math.min(tile.minX, feature.x);
1786
+ tile.minY = Math.min(tile.minY, feature.y);
1787
+ tile.maxX = Math.max(tile.maxX, feature.x);
1788
+ tile.maxY = Math.max(tile.maxY, feature.y);
1789
+ tile.numPoints++;
1790
+ tile.numSimplified++;
1791
+ const tileFeature = {
1792
+ type: 4,
1793
+ tags: feature.tags || null,
1794
+ x: project(feature.x, frame.tx, frame),
1795
+ y: project(feature.y, frame.ty, frame)
1796
+ };
1797
+ if (feature.id !== null) tileFeature.id = feature.id;
1798
+ tile.features.push(tileFeature);
1799
+ }
1800
+ function addPointsTileFeature(tile, feature, frame) {
1801
+ const numPoints = feature.geometry.length / 3;
1802
+ if (!numPoints) return;
1803
+ const geometry = projectCoords(feature.geometry, feature.worldOffset, frame);
1804
+ tile.numPoints += numPoints;
1805
+ tile.numSimplified += numPoints;
1679
1806
  const tileFeature = {
1680
1807
  type: 1,
1681
1808
  tags: feature.tags || null,
@@ -1684,165 +1811,187 @@ function addPointsTileFeature(tile, feature) {
1684
1811
  if (feature.id !== null) tileFeature.id = feature.id;
1685
1812
  tile.features.push(tileFeature);
1686
1813
  }
1687
- function addLineTileFeautre(tile, feature, tolerance, options) {
1814
+ function addRingsTileFeature(tile, feature, tolerance, options, frame) {
1815
+ const isPolygon = feature.type === "Polygon" || feature.type === "MultiPolygon";
1816
+ const coords = feature.geometry;
1688
1817
  const geometry = [];
1689
- addLine(geometry, feature.geometry, tile, tolerance, false, false);
1818
+ for (let i = 0; i < coords.length;) {
1819
+ const ringEnd = i + 2 + coords[i] * 3;
1820
+ addLine(geometry, coords, feature.worldOffset, i + 2, ringEnd, coords[i + 1], tile, tolerance, isPolygon, frame);
1821
+ i = ringEnd;
1822
+ }
1690
1823
  if (!geometry.length) return;
1691
1824
  let tags = feature.tags || null;
1692
- if (options.lineMetrics) {
1825
+ if (feature.type === "LineString" && options.lineMetrics) {
1693
1826
  tags = {};
1694
1827
  for (const key in feature.tags) tags[key] = feature.tags[key];
1695
- tags[GEOJSONVT_CLIP_START] = feature.geometry.start / feature.geometry.size;
1696
- tags[GEOJSONVT_CLIP_END] = feature.geometry.end / feature.geometry.size;
1828
+ tags[GEOJSONVT_CLIP_START] = feature.start / feature.size;
1829
+ tags[GEOJSONVT_CLIP_END] = feature.end / feature.size;
1697
1830
  }
1698
1831
  const tileFeature = {
1699
- type: 2,
1832
+ type: isPolygon ? 3 : 2,
1700
1833
  tags,
1701
1834
  geometry
1702
1835
  };
1703
1836
  if (feature.id !== null) tileFeature.id = feature.id;
1704
1837
  tile.features.push(tileFeature);
1705
1838
  }
1706
- function addLinesTileFeature(tile, feature, tolerance) {
1707
- const geometry = [];
1708
- for (let i = 0; i < feature.geometry.length; i++) addLine(geometry, feature.geometry[i], tile, tolerance, feature.type === "Polygon", i === 0);
1709
- if (!geometry.length) return;
1710
- const tileFeature = {
1711
- type: feature.type === "Polygon" ? 3 : 2,
1712
- tags: feature.tags || null,
1713
- geometry
1714
- };
1715
- if (feature.id !== null) tileFeature.id = feature.id;
1716
- tile.features.push(tileFeature);
1839
+ /**
1840
+ * Adds a ring to a tile feature, with the positions that matter on the zoom level of the tile.
1841
+ * @param offset - what to add to every x of `coords`
1842
+ * @param size - the size of the ring, which is negative for the hole of a polygon
1843
+ */
1844
+ function addLine(result, coords, offset, first, ringEnd, size, tile, tolerance, isPolygon, frame) {
1845
+ const sqTolerance = tolerance * tolerance;
1846
+ const numPoints = (ringEnd - first) / 3;
1847
+ tile.numPoints += numPoints;
1848
+ if (tolerance > 0 && Math.abs(size) < (isPolygon ? sqTolerance : tolerance)) return;
1849
+ let last = ringEnd - 3;
1850
+ while (last > first && tolerance > 0 && coords[last + 2] <= sqTolerance) last -= 3;
1851
+ let x0 = coords[last] + offset;
1852
+ let y0 = coords[last + 1];
1853
+ let area = 0;
1854
+ let numKept = 0;
1855
+ for (let i = first; i < ringEnd; i += 3) if (tolerance === 0 || coords[i + 2] > sqTolerance) {
1856
+ area += (coords[i] + offset - x0) * (coords[i + 1] + y0);
1857
+ x0 = coords[i] + offset;
1858
+ y0 = coords[i + 1];
1859
+ numKept++;
1860
+ }
1861
+ tile.numSimplified += numKept;
1862
+ const isReversed = isPolygon && area > 0 === !isHole(size);
1863
+ const ring = projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame);
1864
+ if (isPolygon && isWindingFlipped(coords, offset, first, ringEnd, tolerance, isReversed, ring)) reverse(ring);
1865
+ result.push(ring);
1717
1866
  }
1718
- function addMultiPolygonTileFeature(tile, feature, tolerance) {
1719
- const geometry = [];
1720
- for (let k = 0; k < feature.geometry.length; k++) {
1721
- const polygon = feature.geometry[k];
1722
- for (let i = 0; i < polygon.length; i++) addLine(geometry, polygon[i], tile, tolerance, true, i === 0);
1867
+ /**
1868
+ * The positions of a ring that matter on the zoom level of the tile, as coordinates in the tile, last to first when
1869
+ * the ring is reversed. They go into a `Float64Array` when one of them doesn't fit the tile's array, which takes a
1870
+ * position far outside the tile.
1871
+ */
1872
+ function projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame, CoordArray = frame.CoordArray) {
1873
+ const sqTolerance = tolerance * tolerance;
1874
+ const limit = CoordArray === Int16Array ? 32767 : 2147483647;
1875
+ const ring = new CoordArray(numKept * 2);
1876
+ let next = isReversed ? ring.length - 2 : 0;
1877
+ for (let i = first; i < ringEnd; i += 3) if (tolerance === 0 || coords[i + 2] > sqTolerance) {
1878
+ const x = project(coords[i] + offset, frame.tx, frame);
1879
+ const y = project(coords[i + 1], frame.ty, frame);
1880
+ if (CoordArray !== Float64Array && !(Math.abs(x) <= limit && Math.abs(y) <= limit)) return projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame, Float64Array);
1881
+ ring[next] = x;
1882
+ ring[next + 1] = y;
1883
+ next += isReversed ? -2 : 2;
1723
1884
  }
1724
- if (!geometry.length) return;
1725
- const tileFeature = {
1726
- type: 3,
1727
- tags: feature.tags || null,
1728
- geometry
1729
- };
1730
- if (feature.id !== null) tileFeature.id = feature.id;
1731
- tile.features.push(tileFeature);
1885
+ return ring;
1732
1886
  }
1733
- function addLine(result, geom, tile, tolerance, isPolygon, isOuter) {
1887
+ /**
1888
+ * Whether rounding to tile coordinates turned a polygon ring around, so that a hole would read as a shell, or the
1889
+ * other way round. Both signed areas are summed in the order of the tile's ring and relative to its first position,
1890
+ * to reduce cancellation for narrow rings far from the origin. A ring that rounds to no area is left as it is, and
1891
+ * other changes rounding makes to a ring are not repaired.
1892
+ */
1893
+ function isWindingFlipped(coords, offset, first, ringEnd, tolerance, isReversed, ring) {
1734
1894
  const sqTolerance = tolerance * tolerance;
1735
- if (tolerance > 0 && geom.size < (isPolygon ? sqTolerance : tolerance)) {
1736
- tile.numPoints += geom.points.length / 3;
1737
- return;
1738
- }
1739
- const ring = [];
1740
- for (let i = 0; i < geom.points.length; i += 3) {
1741
- if (tolerance === 0 || geom.points[i + 2] > sqTolerance) {
1742
- tile.numSimplified++;
1743
- ring.push(geom.points[i], geom.points[i + 1]);
1895
+ const step = isReversed ? -3 : 3;
1896
+ let originX = 0;
1897
+ let originY = 0;
1898
+ let previousX = 0;
1899
+ let previousY = 0;
1900
+ let previousRoundedX = 0;
1901
+ let previousRoundedY = 0;
1902
+ let sourceArea = 0;
1903
+ let roundedArea = 0;
1904
+ let next = 0;
1905
+ for (let i = isReversed ? ringEnd - 3 : first; i >= first && i < ringEnd; i += step) {
1906
+ if (tolerance > 0 && !(coords[i + 2] > sqTolerance)) continue;
1907
+ if (next === 0) {
1908
+ originX = coords[i] + offset;
1909
+ originY = coords[i + 1];
1910
+ } else {
1911
+ const x = coords[i] + offset - originX;
1912
+ const y = coords[i + 1] - originY;
1913
+ const roundedX = ring[next] - ring[0];
1914
+ const roundedY = ring[next + 1] - ring[1];
1915
+ sourceArea += previousX * y - x * previousY;
1916
+ roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1917
+ previousX = x;
1918
+ previousY = y;
1919
+ previousRoundedX = roundedX;
1920
+ previousRoundedY = roundedY;
1744
1921
  }
1745
- tile.numPoints++;
1922
+ next += 2;
1746
1923
  }
1747
- if (isPolygon) rewind(ring, isOuter);
1748
- result.push(ring);
1924
+ return sourceArea * roundedArea < 0;
1749
1925
  }
1750
- function rewind(ring, clockwise) {
1751
- let area = 0;
1752
- for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
1753
- if (area > 0 !== clockwise) return;
1754
- for (let i = 0, len = ring.length; i < len / 2; i += 2) {
1755
- const x = ring[i];
1756
- const y = ring[i + 1];
1757
- ring[i] = ring[len - 2 - i];
1758
- ring[i + 1] = ring[len - 1 - i];
1759
- ring[len - 2 - i] = x;
1760
- ring[len - 1 - i] = y;
1926
+ function reverse(coords) {
1927
+ for (let i = 0, j = coords.length - 2; i < j; i += 2, j -= 2) {
1928
+ const x = coords[i];
1929
+ const y = coords[i + 1];
1930
+ coords[i] = coords[j];
1931
+ coords[i + 1] = coords[j + 1];
1932
+ coords[j] = x;
1933
+ coords[j + 1] = y;
1761
1934
  }
1762
1935
  }
1763
- // src/transform.ts
1764
1936
  /**
1765
- * Transforms the coordinates of each feature in the given tile from
1766
- * mercator-projected space into (extent x extent) tile space.
1767
- * @param tile - the tile to transform, this gets modified in place
1768
- * @param extent - the tile extent (usually 4096)
1769
- * @returns the transformed tile
1937
+ * A position in the world, on one axis, as a coordinate in the tile that starts at `t` on that axis.
1770
1938
  */
1771
- function transformTile(tile, extent) {
1772
- if (tile.transformed) return tile;
1773
- const z2 = 1 << tile.z;
1774
- const tx = tile.x;
1775
- const ty = tile.y;
1776
- for (const feature of tile.features) if (feature.type === 1) transformPointFeature(feature, extent, z2, tx, ty);
1777
- else transformNonPointFeature(feature, extent, z2, tx, ty);
1778
- tile.transformed = true;
1779
- return tile;
1939
+ function project(position, t, frame) {
1940
+ return Math.round(frame.extent * (position * frame.z2 - t));
1780
1941
  }
1781
1942
  /**
1782
- * Transforms a single point feature from mercator-projected space into (extent x extent) tile space.
1943
+ * The positions of a MultiPoint as coordinates in the tile. They go into a `Float64Array` when one of them doesn't
1944
+ * fit the tile's array, which takes a position far outside the tile.
1945
+ * @param offset - what to add to every x of `positions`
1783
1946
  */
1784
- function transformPointFeature(feature, extent, z2, tx, ty) {
1785
- const transformed = feature;
1786
- const geometry = feature.geometry;
1787
- const point = [];
1788
- for (let i = 0; i < geometry.length; i += 2) point.push(transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty));
1789
- transformed.geometry = point;
1790
- return transformed;
1947
+ function projectCoords(positions, offset, frame, CoordArray = frame.CoordArray) {
1948
+ const limit = CoordArray === Int16Array ? 32767 : 2147483647;
1949
+ const coords = new CoordArray(positions.length / 3 * 2);
1950
+ for (let i = 0, j = 0; i < positions.length; i += 3, j += 2) {
1951
+ const x = project(positions[i] + offset, frame.tx, frame);
1952
+ const y = project(positions[i + 1], frame.ty, frame);
1953
+ if (CoordArray !== Float64Array && !(Math.abs(x) <= limit && Math.abs(y) <= limit)) return projectCoords(positions, offset, frame, Float64Array);
1954
+ coords[j] = x;
1955
+ coords[j + 1] = y;
1956
+ }
1957
+ return coords;
1791
1958
  }
1959
+ // src/transform.ts
1792
1960
  /**
1793
- * Transforms a single non-point feature from mercator-projected space into (extent x extent) tile space.
1961
+ * Turns a stored tile, which keeps its coordinates in flat arrays, into the tile `getTile` returns,
1962
+ * with every position as an `[x, y]` pair. The stored tile stays as it is.
1963
+ * @param tile - the stored tile
1964
+ * @returns a new tile with the coordinates as pairs
1794
1965
  */
1795
- function transformNonPointFeature(feature, extent, z2, tx, ty) {
1796
- const transformed = feature;
1797
- const geometry = feature.geometry;
1798
- const nonPoint = [];
1799
- for (const geom of geometry) if (feature.type === 3) nonPoint.push(transformPolygonRing(geom, extent, z2, tx, ty));
1800
- else {
1801
- const line = [];
1802
- for (let i = 0; i < geom.length; i += 2) line.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
1803
- nonPoint.push(line);
1804
- }
1805
- transformed.geometry = nonPoint;
1806
- return transformed;
1966
+ function transformTile(tile) {
1967
+ const features = [];
1968
+ for (const feature of tile.features) features.push(transformFeature(feature));
1969
+ return { features };
1807
1970
  }
1808
- /**
1809
- * Quantizes a polygon ring while preserving its winding so a hole does not become
1810
- * a shell, or vice versa. Signed areas use coordinates relative to the first
1811
- * vertex to reduce cancellation for narrow rings far from the origin.
1812
- * Zero-area results are left unchanged; other quantization topology changes
1813
- * are not repaired.
1814
- */
1815
- function transformPolygonRing(geometry, extent, z2, tx, ty) {
1816
- if (!geometry.length) return [];
1817
- const originX = geometry[0];
1818
- const originY = geometry[1];
1819
- const origin = transformPoint(originX, originY, extent, z2, tx, ty);
1820
- const ring = [origin];
1821
- let previousX = 0;
1822
- let previousY = 0;
1823
- let previousRoundedX = 0;
1824
- let previousRoundedY = 0;
1825
- let sourceArea = 0;
1826
- let roundedArea = 0;
1827
- for (let i = 2; i < geometry.length; i += 2) {
1828
- const point = transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty);
1829
- const x = geometry[i] - originX;
1830
- const y = geometry[i + 1] - originY;
1831
- const roundedX = point[0] - origin[0];
1832
- const roundedY = point[1] - origin[1];
1833
- sourceArea += previousX * y - x * previousY;
1834
- roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1835
- previousX = x;
1836
- previousY = y;
1837
- previousRoundedX = roundedX;
1838
- previousRoundedY = roundedY;
1839
- ring.push(point);
1840
- }
1841
- if (sourceArea * roundedArea < 0) ring.reverse();
1842
- return ring;
1971
+ function transformFeature(feature) {
1972
+ let transformed;
1973
+ if (feature.type === 4) transformed = {
1974
+ type: 1,
1975
+ tags: feature.tags,
1976
+ geometry: [[feature.x, feature.y]]
1977
+ };
1978
+ else if (feature.type === 1) transformed = {
1979
+ type: 1,
1980
+ tags: feature.tags,
1981
+ geometry: toPairs(feature.geometry)
1982
+ };
1983
+ else transformed = {
1984
+ type: feature.type,
1985
+ tags: feature.tags,
1986
+ geometry: feature.geometry.map(toPairs)
1987
+ };
1988
+ if (feature.id !== void 0) transformed.id = feature.id;
1989
+ return transformed;
1843
1990
  }
1844
- function transformPoint(x, y, extent, z2, tx, ty) {
1845
- return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))];
1991
+ function toPairs(coords) {
1992
+ const pairs = [];
1993
+ for (let i = 0; i < coords.length; i += 2) pairs.push([coords[i], coords[i + 1]]);
1994
+ return pairs;
1846
1995
  }
1847
1996
  // src/tile-index.ts
1848
1997
  var TileIndex = class {
@@ -1907,12 +2056,12 @@ var TileIndex = class {
1907
2056
  }
1908
2057
  /** {@inheritdoc} */
1909
2058
  getTile(z, x, y) {
1910
- const { extent, debug } = this.options;
2059
+ const { debug } = this.options;
1911
2060
  const z2 = 1 << z;
1912
2061
  if (!this.options.wrap && (x < 0 || x >= z2)) return null;
1913
2062
  x = x + z2 & z2 - 1;
1914
2063
  const id = toID(z, x, y);
1915
- if (this.tiles[id]) return transformTile(this.tiles[id], extent);
2064
+ if (this.tiles[id]) return transformTile(this.tiles[id]);
1916
2065
  if (debug > 1) console.log("drilling down to z%d-%d-%d", z, x, y);
1917
2066
  let z0 = z;
1918
2067
  let x0 = x;
@@ -1932,7 +2081,7 @@ var TileIndex = class {
1932
2081
  this.splitTile(parent.source, z0, x0, y0, z, x, y);
1933
2082
  if (debug > 1) console.timeEnd("drilling down");
1934
2083
  if (!this.tiles[id]) return null;
1935
- return transformTile(this.tiles[id], extent);
2084
+ return transformTile(this.tiles[id]);
1936
2085
  }
1937
2086
  /**
1938
2087
  * The features the top tile starts from. A world that wraps keeps them all left and right, where wrapping already
@@ -2048,10 +2197,10 @@ var TileIndex = class {
2048
2197
  let minY = 1e400;
2049
2198
  let maxY = -1e400;
2050
2199
  for (const feature of features) {
2051
- minX = Math.min(minX, feature.minX);
2052
- maxX = Math.max(maxX, feature.maxX);
2053
- minY = Math.min(minY, feature.minY);
2054
- maxY = Math.max(maxY, feature.maxY);
2200
+ minX = Math.min(minX, getMinX(feature));
2201
+ maxX = Math.max(maxX, getMaxX(feature));
2202
+ minY = Math.min(minY, getMinY(feature));
2203
+ maxY = Math.max(maxY, getMaxY(feature));
2055
2204
  }
2056
2205
  const k1 = options.buffer / options.extent;
2057
2206
  const removedLookup = /* @__PURE__ */ new Set();
@@ -2064,7 +2213,7 @@ var TileIndex = class {
2064
2213
  const tileMaxY = (tile.y + 1 + k1) / z2;
2065
2214
  if (maxX < tileMinX || minX >= tileMaxX || maxY < tileMinY || minY >= tileMaxY) continue;
2066
2215
  let intersects = false;
2067
- for (const feature of features) if (feature.maxX >= tileMinX && feature.minX < tileMaxX && feature.maxY >= tileMinY && feature.minY < tileMaxY) {
2216
+ for (const feature of features) if (getMaxX(feature) >= tileMinX && getMinX(feature) < tileMaxX && getMaxY(feature) >= tileMinY && getMinY(feature) < tileMaxY) {
2068
2217
  intersects = true;
2069
2218
  break;
2070
2219
  }
@@ -2284,7 +2433,7 @@ function geoJSONToTile(data, z, x, y, options = {}) {
2284
2433
  const buffer = options.buffer / options.extent;
2285
2434
  features = clip(clip(features, pow2, x - buffer, x + 1 + buffer, 0, -1, 2, options) || [], pow2, y - buffer, y + 1 + buffer, 1, -1, 2, options);
2286
2435
  }
2287
- return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
2436
+ return transformTile(createTile(features ?? [], z, x, y, options, Float64Array));
2288
2437
  }
2289
2438
  export { GEOJSONVT_CLIP_END, GEOJSONVT_CLIP_START, GeoJSONVT, ClusterTileIndex as Supercluster, geoJSONToTile };
2290
2439