@maplibre/geojson-vt 6.3.3 → 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 +606 -461
  12. package/dist/geojson-vt.js +1 -1
  13. package/dist/geojson-vt.mjs +603 -458
  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 +77 -72
  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 +75 -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,59 +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
- if (!line.length) continue;
273
- const out = { points: [] };
274
- convertLine(line, out, tolerance, false, worldCoordinateHelper);
275
- features.push(createFeature(id, "LineString", out, properties));
276
- }
277
- else {
278
- const out = [];
279
- convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
280
- if (!out.length) return;
281
- features.push(createFeature(id, "MultiLineString", out, properties));
282
- }
283
- }
284
- function convertPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
285
- const out = [];
286
- convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
287
- if (!out.length) return;
288
- features.push(createFeature(id, "Polygon", out, properties));
289
- }
290
- function convertMultiPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
291
- const out = [];
292
- for (const polygon of geom.coordinates) {
293
- const polygonOut = [];
294
- convertLines(polygon, polygonOut, tolerance, true, worldCoordinateHelper);
295
- if (!polygonOut.length) continue;
296
- out.push(polygonOut);
297
- }
298
- if (!out.length) return;
299
- features.push(createFeature(id, "MultiPolygon", out, properties));
300
- }
301
- function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
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];
290
+ }
291
+ features.push(feature);
292
+ }
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
+ }
311
+ }
312
+ features.push(createFeature(id, type, out, properties));
313
+ }
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;
302
329
  let x0, y0;
303
330
  let size = 0;
331
+ let next = first;
304
332
  for (let j = 0; j < ring.length; j++) {
305
333
  const x = worldCoordinateHelper.projectX(ring[j][0]);
306
334
  const y = worldCoordinateHelper.projectY(ring[j][1]);
307
- out.points.push(x, y, 0);
335
+ out[next] = x;
336
+ out[next + 1] = y;
337
+ next += 3;
308
338
  if (j > 0) {
309
339
  if (isPolygon) size += (x0 * y - x * y0) / 2;
310
340
  else size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2));
@@ -312,22 +342,13 @@ function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
312
342
  x0 = x;
313
343
  y0 = y;
314
344
  }
315
- const last = out.points.length - 3;
316
- out.points[2] = 1;
317
- if (tolerance > 0) simplify(out.points, 0, last, tolerance);
318
- out.points[last + 2] = 1;
319
- optimizeLineMemory(out);
320
- out.size = Math.abs(size);
321
- out.start = 0;
322
- out.end = out.size;
323
- }
324
- function convertLines(rings, out, tolerance, isPolygon, worldCoordinateHelper) {
325
- for (let i = 0; i < rings.length; i++) {
326
- if (!rings[i].length) continue;
327
- const geom = { points: [] };
328
- convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
329
- out.push(geom);
330
- }
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;
331
352
  }
332
353
  // src/deconvert.ts
333
354
  /**
@@ -355,41 +376,63 @@ function featureToGeoJSON(feature, worldCoordinateHelper = mercatorWorldCoordina
355
376
  * Converts a single internal feature geometry to GeoJSON format.
356
377
  */
357
378
  function geometryToGeoJSON(feature, worldCoordinateHelper) {
358
- const { type, geometry } = feature;
359
- switch (type) {
379
+ switch (feature.type) {
360
380
  case "Point": return {
361
- type,
362
- coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinateHelper)
381
+ type: feature.type,
382
+ coordinates: [worldCoordinateHelper.unprojectX(feature.x), worldCoordinateHelper.unprojectY(feature.y)]
363
383
  };
364
384
  case "MultiPoint": return {
365
- type,
366
- coordinates: unprojectPoints(geometry, worldCoordinateHelper)
385
+ type: feature.type,
386
+ coordinates: unprojectPoints(feature.geometry, feature.worldOffset, 0, feature.geometry.length, worldCoordinateHelper)
367
387
  };
368
388
  case "LineString": return {
369
- type,
370
- coordinates: unprojectPoints(geometry.points, worldCoordinateHelper)
389
+ type: feature.type,
390
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)[0]
371
391
  };
372
392
  case "MultiLineString":
373
393
  case "Polygon": return {
374
- type,
375
- coordinates: geometry.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper))
394
+ type: feature.type,
395
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)
376
396
  };
377
397
  case "MultiPolygon": return {
378
- type,
379
- coordinates: geometry.map((polygon) => polygon.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper)))
398
+ type: feature.type,
399
+ coordinates: unprojectPolygons(feature.geometry, feature.worldOffset, worldCoordinateHelper)
380
400
  };
381
401
  }
382
402
  }
383
- 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) {
384
430
  const result = [];
385
- 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])]);
386
432
  return result;
387
433
  }
388
- function unprojectPoint(x, y, worldCoordinateHelper) {
389
- return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
390
- }
391
434
  // src/clip.ts
392
- /**
435
+ /**
393
436
  * clip features between two vertical or horizontal axis-parallel lines:
394
437
  * | |
395
438
  * ___|___ | /
@@ -411,6 +454,11 @@ function clip(features, scale, start, end, axis, minAll, maxAll, options) {
411
454
  if (maxAll < start || minAll >= end) return null;
412
455
  const clipped = [];
413
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
+ }
414
462
  const min = axis === 0 ? feature.minX : feature.minY;
415
463
  const max = axis === 0 ? feature.maxX : feature.maxY;
416
464
  if (min >= start && max < end) {
@@ -418,157 +466,238 @@ function clip(features, scale, start, end, axis, minAll, maxAll, options) {
418
466
  continue;
419
467
  }
420
468
  if (max < start || min >= end) continue;
421
- switch (feature.type) {
422
- case "MultiPoint":
423
- clipPointFeature(feature, clipped, start, end, axis);
424
- continue;
425
- case "LineString":
426
- clipLineStringFeature(feature, clipped, start, end, axis, options);
427
- continue;
428
- case "MultiLineString":
429
- clipMultiLineStringFeature(feature, clipped, start, end, axis);
430
- continue;
431
- case "Polygon":
432
- clipPolygonFeature(feature, clipped, start, end, axis);
433
- continue;
434
- case "MultiPolygon":
435
- clipMultiPolygonFeature(feature, clipped, start, end, axis);
436
- continue;
469
+ if (feature.type === "MultiPoint") {
470
+ clipPointsFeature(feature, clipped, start, end, axis);
471
+ continue;
437
472
  }
473
+ clipRingsFeature(feature, clipped, start, end, axis, options);
438
474
  }
439
475
  if (!clipped.length) return null;
440
476
  return clipped;
441
477
  }
442
- function clipPointFeature(feature, clipped, start, end, axis) {
443
- const geom = [];
444
- clipPoints(feature.geometry, geom, start, end, axis);
445
- if (!geom.length) return;
446
- const type = geom.length === 3 ? "Point" : "MultiPoint";
447
- clipped.push(createFeature(feature.id, type, geom, feature.tags));
448
- }
449
- function clipLineStringFeature(feature, clipped, start, end, axis, options) {
450
- const geom = [];
451
- clipLine(feature.geometry, geom, start, end, axis, false, options.lineMetrics);
452
- if (!geom.length) return;
453
- if (options.lineMetrics) {
454
- 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));
455
494
  return;
456
495
  }
457
- if (geom.length > 1) {
458
- 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
+ }
459
548
  return;
460
549
  }
461
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
462
- }
463
- function clipMultiLineStringFeature(feature, clipped, start, end, axis) {
464
- const geom = [];
465
- clipLines(feature.geometry, geom, start, end, axis, false);
466
- if (!geom.length) return;
467
- if (geom.length === 1) {
468
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
550
+ if (isPolygon) {
551
+ clipped.push(createFeature(feature.id, feature.type, out, feature.tags));
469
552
  return;
470
553
  }
471
- clipped.push(createFeature(feature.id, "MultiLineString", geom, feature.tags));
472
- }
473
- function clipPolygonFeature(feature, clipped, start, end, axis) {
474
- const geom = [];
475
- clipLines(feature.geometry, geom, start, end, axis, true);
476
- if (!geom.length) return;
477
- clipped.push(createFeature(feature.id, "Polygon", geom, feature.tags));
554
+ const isSingleLine = 2 + out[0] * 3 === out.length;
555
+ clipped.push(createFeature(feature.id, isSingleLine ? "LineString" : "MultiLineString", out, feature.tags));
478
556
  }
479
- function clipMultiPolygonFeature(feature, clipped, start, end, axis) {
480
- const geom = [];
481
- for (const polygon of feature.geometry) {
482
- const newPolygon = [];
483
- clipLines(polygon, newPolygon, start, end, axis, true);
484
- if (!newPolygon.length) continue;
485
- 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
+ }
486
588
  }
487
- if (!geom.length) return;
488
- clipped.push(createFeature(feature.id, "MultiPolygon", geom, feature.tags));
489
- }
490
- function clipPoints(geom, newGeom, start, end, axis) {
491
- for (let i = 0; i < geom.length; i += 3) {
492
- const a = geom[i + axis];
493
- 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++;
494
595
  }
596
+ return total * 3 + parts * 2;
495
597
  }
496
- function clipLine(geom, newGeom, start, end, axis, isPolygon, trackMetrics) {
497
- let slice = newSlice(geom);
498
- const intersect = axis === 0 ? intersectX : intersectY;
499
- 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;
500
612
  let segLen, t;
501
- for (let i = 0; i < geom.points.length - 3; i += 3) {
502
- const ax = geom.points[i];
503
- const ay = geom.points[i + 1];
504
- const az = geom.points[i + 2];
505
- const bx = geom.points[i + 3];
506
- 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];
507
619
  const a = axis === 0 ? ax : ay;
508
620
  const b = axis === 0 ? bx : by;
509
621
  let exited = false;
510
- 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));
511
623
  if (a < start) {
512
624
  if (b >= start) {
513
- t = intersect(slice, ax, ay, bx, by, start);
514
- 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;
515
628
  }
516
629
  } else if (a > end) {
517
630
  if (b <= end) {
518
- t = intersect(slice, ax, ay, bx, by, end);
519
- 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;
520
634
  }
521
- } 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
+ }
522
641
  if (b < start && a >= start) {
523
- t = intersect(slice, ax, ay, bx, by, start);
642
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
643
+ next += 3;
524
644
  exited = true;
525
645
  }
526
646
  if (b > end && a <= end) {
527
- t = intersect(slice, ax, ay, bx, by, end);
647
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
648
+ next += 3;
528
649
  exited = true;
529
650
  }
530
651
  if (!isPolygon && exited) {
531
- if (trackMetrics) slice.end = len + segLen * t;
532
- newGeom.push(slice);
533
- 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;
534
661
  }
535
- if (trackMetrics) len += segLen;
662
+ if (metricsOf) len += segLen;
536
663
  }
537
- let last = geom.points.length - 3;
538
- const ax = geom.points[last];
539
- const ay = geom.points[last + 1];
540
- const az = geom.points[last + 2];
664
+ const last = ringEnd - 3;
665
+ const ax = geom[last] + offset;
666
+ const ay = geom[last + 1];
541
667
  const a = axis === 0 ? ax : ay;
542
- if (a >= start && a <= end) addPoint(slice.points, ax, ay, az);
543
- last = slice.points.length - 3;
544
- 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]);
545
- if (slice.points.length) {
546
- optimizeLineMemory(slice);
547
- newGeom.push(slice);
548
- }
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;
549
684
  }
550
- function newSlice(line) {
551
- return {
552
- points: [],
553
- size: line.size,
554
- start: line.start,
555
- end: line.end
556
- };
557
- }
558
- function clipLines(geom, newGeom, start, end, axis, isPolygon) {
559
- for (const line of geom) clipLine(line, newGeom, start, end, axis, isPolygon, false);
560
- }
561
- function addPoint(out, x, y, z) {
562
- out.push(x, y, z);
563
- }
564
- function intersectX(out, ax, ay, bx, by, x) {
565
- const t = (x - ax) / (bx - ax);
566
- addPoint(out.points, x, ay + (by - ay) * t, 1);
567
- return t;
568
- }
569
- function intersectY(out, ax, ay, bx, by, y) {
570
- const t = (y - ay) / (by - ay);
571
- 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;
572
701
  return t;
573
702
  }
574
703
  // src/wrap.ts
@@ -584,58 +713,26 @@ function wrap(features, options) {
584
713
  if (right) merged = merged.concat(shiftFeatureCoords(right, -1));
585
714
  return merged;
586
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
+ */
587
720
  function shiftFeatureCoords(features, offset) {
588
721
  const newFeatures = [];
589
- for (const feature of features) switch (feature.type) {
590
- case "Point":
591
- case "MultiPoint": {
592
- const newGeometry = shiftPointCoords(feature.geometry, offset);
593
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
594
- continue;
595
- }
596
- case "LineString": {
597
- const newGeometry = shiftLineCoords(feature.geometry, offset);
598
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
599
- continue;
600
- }
601
- case "MultiLineString":
602
- case "Polygon": {
603
- const newGeometry = [];
604
- for (const line of feature.geometry) newGeometry.push(shiftLineCoords(line, offset));
605
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
606
- continue;
607
- }
608
- case "MultiPolygon": {
609
- const newGeometry = [];
610
- for (const polygon of feature.geometry) {
611
- const newPolygon = [];
612
- for (const line of polygon) newPolygon.push(shiftLineCoords(line, offset));
613
- newGeometry.push(newPolygon);
614
- }
615
- 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));
616
725
  continue;
617
726
  }
727
+ newFeatures.push({
728
+ ...feature,
729
+ minX: feature.minX + offset,
730
+ maxX: feature.maxX + offset,
731
+ worldOffset: feature.worldOffset + offset
732
+ });
618
733
  }
619
734
  return newFeatures;
620
735
  }
621
- function shiftPointCoords(coords, offset) {
622
- const newCoords = [];
623
- for (let i = 0; i < coords.length; i += 3) newCoords.push(coords[i] + offset, coords[i + 1], coords[i + 2]);
624
- return newCoords;
625
- }
626
- function shiftLineCoords(line, offset) {
627
- const newLine = {
628
- points: [],
629
- size: line.size
630
- };
631
- if (line.start !== void 0) {
632
- newLine.start = line.start;
633
- newLine.end = line.end;
634
- }
635
- 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]);
636
- optimizeLineMemory(newLine);
637
- return newLine;
638
- }
639
736
  // src/difference.ts
640
737
  /**
641
738
  * Applies a GeoJSON Source Diff to an existing set of simplified features
@@ -1147,11 +1244,12 @@ var ClusterTileIndex = class {
1147
1244
  const positions = point.geometry.type === "MultiPoint" ? point.geometry.coordinates : [point.geometry.coordinates];
1148
1245
  for (const [lng, lat] of positions) {
1149
1246
  const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
1150
- if (!this.options.wrap && !isInWorld([x, y])) continue;
1247
+ if (!this.options.wrap && !isInWorld(x, y)) continue;
1151
1248
  const feature = {
1152
1249
  id: point.id,
1153
1250
  type: "Point",
1154
- geometry: [x, y],
1251
+ x,
1252
+ y,
1155
1253
  tags: point.properties
1156
1254
  };
1157
1255
  features.push(feature);
@@ -1169,18 +1267,20 @@ var ClusterTileIndex = class {
1169
1267
  const points = [];
1170
1268
  for (const feature of features) {
1171
1269
  if (feature.type === "Point") {
1172
- if (!this.options.wrap && !isInWorld(feature.geometry)) continue;
1270
+ if (!this.isClustered(feature.x, feature.y)) continue;
1173
1271
  points.push(feature);
1174
1272
  continue;
1175
1273
  }
1176
1274
  if (feature.type !== "MultiPoint") continue;
1177
1275
  for (let i = 0; i < feature.geometry.length; i += 3) {
1178
- const geometry = [feature.geometry[i], feature.geometry[i + 1]];
1179
- 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;
1180
1279
  points.push({
1181
1280
  id: feature.id,
1182
1281
  type: "Point",
1183
- geometry,
1282
+ x,
1283
+ y,
1184
1284
  tags: feature.tags
1185
1285
  });
1186
1286
  }
@@ -1188,6 +1288,14 @@ var ClusterTileIndex = class {
1188
1288
  this.createIndex(points);
1189
1289
  }
1190
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
+ /**
1191
1299
  * @internal
1192
1300
  * Updates the cluster data by rebuilding.
1193
1301
  * @param features
@@ -1220,9 +1328,9 @@ var ClusterTileIndex = class {
1220
1328
  let numItems = 0;
1221
1329
  for (let i = 0; i < points.length; i++) {
1222
1330
  const p = points[i];
1223
- if (!p?.geometry || !fitsInteger(p.geometry[0]) || !fitsInteger(p.geometry[1])) continue;
1224
- coords[numItems * 2] = Math.round(encode(p.geometry[0]));
1225
- 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));
1226
1334
  const k = numItems * stride;
1227
1335
  data[k + OFFSET_ZOOM] = notProcessed;
1228
1336
  data[k + OFFSET_ID] = i;
@@ -1399,14 +1507,7 @@ var ClusterTileIndex = class {
1399
1507
  const p = radius / extent;
1400
1508
  const top = encode((y - p) / z2);
1401
1509
  const bottom = encode((y + 1 + p) / z2);
1402
- const tile = {
1403
- transformed: true,
1404
- features: [],
1405
- source: null,
1406
- x,
1407
- y,
1408
- z
1409
- };
1510
+ const tile = { features: [] };
1410
1511
  this.addTileFeatures(tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom), tree, x, y, z2, tile);
1411
1512
  if (this.options.wrap && x === 0) this.addTileFeatures(tree.range(encode(1 - p / z2), top, encode(1), bottom), tree, z2, y, z2, tile);
1412
1513
  if (this.options.wrap && x === z2 - 1) this.addTileFeatures(tree.range(encode(0), top, encode(p / z2), bottom), tree, -1, y, z2, tile);
@@ -1466,7 +1567,8 @@ var ClusterTileIndex = class {
1466
1567
  } else {
1467
1568
  const p = this.points[data[k + OFFSET_ID]];
1468
1569
  tags = p.tags;
1469
- [px, py] = p.geometry;
1570
+ px = p.x;
1571
+ py = p.y;
1470
1572
  }
1471
1573
  const f = {
1472
1574
  type: 1,
@@ -1589,7 +1691,8 @@ function getClusterFeature(coords, index, data, i, clusterProps) {
1589
1691
  id: data[i + OFFSET_ID],
1590
1692
  type: "Point",
1591
1693
  tags: getClusterProperties(data, i, clusterProps),
1592
- geometry: [decode(coords[index * 2]), decode(coords[index * 2 + 1])]
1694
+ x: decode(coords[index * 2]),
1695
+ y: decode(coords[index * 2 + 1])
1593
1696
  };
1594
1697
  }
1595
1698
  function getClusterGeoJSON(coords, index, data, i, clusterProps, worldCoordinateHelper) {
@@ -1615,25 +1718,31 @@ function getClusterProperties(data, i, clusterProps) {
1615
1718
  point_count_abbreviated: abbrev
1616
1719
  });
1617
1720
  }
1618
- function isInWorld(geometry) {
1619
- 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;
1620
1723
  }
1621
1724
  // src/tile.ts
1622
1725
  const GEOJSONVT_CLIP_START = "geojsonvt_clip_start";
1623
1726
  const GEOJSONVT_CLIP_END = "geojsonvt_clip_end";
1624
1727
  /**
1625
- * 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
1626
1735
  * @param features - the features to include in the tile
1627
1736
  * @param z
1628
1737
  * @param tx
1629
1738
  * @param ty
1630
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
1631
1741
  * @returns the created tile
1632
1742
  */
1633
- function createTile(features, z, tx, ty, options) {
1743
+ function createTile(features, z, tx, ty, options, CoordArray = getTileCoordsConstructor(options)) {
1634
1744
  const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
1635
1745
  const tile = {
1636
- transformed: false,
1637
1746
  features: [],
1638
1747
  source: null,
1639
1748
  x: tx,
@@ -1647,39 +1756,53 @@ function createTile(features, z, tx, ty, options) {
1647
1756
  numSimplified: 0,
1648
1757
  numFeatures: features.length
1649
1758
  };
1650
- 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);
1651
1767
  return tile;
1652
1768
  }
1653
- 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
+ }
1654
1774
  tile.minX = Math.min(tile.minX, feature.minX);
1655
1775
  tile.minY = Math.min(tile.minY, feature.minY);
1656
1776
  tile.maxX = Math.max(tile.maxX, feature.maxX);
1657
1777
  tile.maxY = Math.max(tile.maxY, feature.maxY);
1658
- switch (feature.type) {
1659
- case "Point":
1660
- case "MultiPoint":
1661
- addPointsTileFeature(tile, feature);
1662
- return;
1663
- case "LineString":
1664
- addLineTileFeautre(tile, feature, tolerance, options);
1665
- return;
1666
- case "MultiLineString":
1667
- case "Polygon":
1668
- addLinesTileFeature(tile, feature, tolerance);
1669
- return;
1670
- case "MultiPolygon":
1671
- addMultiPolygonTileFeature(tile, feature, tolerance);
1672
- return;
1778
+ if (feature.type === "MultiPoint") {
1779
+ addPointsTileFeature(tile, feature, frame);
1780
+ return;
1673
1781
  }
1782
+ addRingsTileFeature(tile, feature, tolerance, options, frame);
1674
1783
  }
1675
- function addPointsTileFeature(tile, feature) {
1676
- const geometry = [];
1677
- for (let i = 0; i < feature.geometry.length; i += 3) {
1678
- geometry.push(feature.geometry[i], feature.geometry[i + 1]);
1679
- tile.numPoints++;
1680
- tile.numSimplified++;
1681
- }
1682
- 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;
1683
1806
  const tileFeature = {
1684
1807
  type: 1,
1685
1808
  tags: feature.tags || null,
@@ -1688,165 +1811,187 @@ function addPointsTileFeature(tile, feature) {
1688
1811
  if (feature.id !== null) tileFeature.id = feature.id;
1689
1812
  tile.features.push(tileFeature);
1690
1813
  }
1691
- 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;
1692
1817
  const geometry = [];
1693
- 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
+ }
1694
1823
  if (!geometry.length) return;
1695
1824
  let tags = feature.tags || null;
1696
- if (options.lineMetrics) {
1825
+ if (feature.type === "LineString" && options.lineMetrics) {
1697
1826
  tags = {};
1698
1827
  for (const key in feature.tags) tags[key] = feature.tags[key];
1699
- tags[GEOJSONVT_CLIP_START] = feature.geometry.start / feature.geometry.size;
1700
- 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;
1701
1830
  }
1702
1831
  const tileFeature = {
1703
- type: 2,
1832
+ type: isPolygon ? 3 : 2,
1704
1833
  tags,
1705
1834
  geometry
1706
1835
  };
1707
1836
  if (feature.id !== null) tileFeature.id = feature.id;
1708
1837
  tile.features.push(tileFeature);
1709
1838
  }
1710
- function addLinesTileFeature(tile, feature, tolerance) {
1711
- const geometry = [];
1712
- for (let i = 0; i < feature.geometry.length; i++) addLine(geometry, feature.geometry[i], tile, tolerance, feature.type === "Polygon", i === 0);
1713
- if (!geometry.length) return;
1714
- const tileFeature = {
1715
- type: feature.type === "Polygon" ? 3 : 2,
1716
- tags: feature.tags || null,
1717
- geometry
1718
- };
1719
- if (feature.id !== null) tileFeature.id = feature.id;
1720
- 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);
1721
1866
  }
1722
- function addMultiPolygonTileFeature(tile, feature, tolerance) {
1723
- const geometry = [];
1724
- for (let k = 0; k < feature.geometry.length; k++) {
1725
- const polygon = feature.geometry[k];
1726
- 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;
1727
1884
  }
1728
- if (!geometry.length) return;
1729
- const tileFeature = {
1730
- type: 3,
1731
- tags: feature.tags || null,
1732
- geometry
1733
- };
1734
- if (feature.id !== null) tileFeature.id = feature.id;
1735
- tile.features.push(tileFeature);
1885
+ return ring;
1736
1886
  }
1737
- 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) {
1738
1894
  const sqTolerance = tolerance * tolerance;
1739
- if (tolerance > 0 && geom.size < (isPolygon ? sqTolerance : tolerance)) {
1740
- tile.numPoints += geom.points.length / 3;
1741
- return;
1742
- }
1743
- const ring = [];
1744
- for (let i = 0; i < geom.points.length; i += 3) {
1745
- if (tolerance === 0 || geom.points[i + 2] > sqTolerance) {
1746
- tile.numSimplified++;
1747
- 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;
1748
1921
  }
1749
- tile.numPoints++;
1922
+ next += 2;
1750
1923
  }
1751
- if (isPolygon) rewind(ring, isOuter);
1752
- result.push(ring);
1924
+ return sourceArea * roundedArea < 0;
1753
1925
  }
1754
- function rewind(ring, clockwise) {
1755
- let area = 0;
1756
- 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]);
1757
- if (area > 0 !== clockwise) return;
1758
- for (let i = 0, len = ring.length; i < len / 2; i += 2) {
1759
- const x = ring[i];
1760
- const y = ring[i + 1];
1761
- ring[i] = ring[len - 2 - i];
1762
- ring[i + 1] = ring[len - 1 - i];
1763
- ring[len - 2 - i] = x;
1764
- 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;
1765
1934
  }
1766
1935
  }
1767
- // src/transform.ts
1768
1936
  /**
1769
- * Transforms the coordinates of each feature in the given tile from
1770
- * mercator-projected space into (extent x extent) tile space.
1771
- * @param tile - the tile to transform, this gets modified in place
1772
- * @param extent - the tile extent (usually 4096)
1773
- * @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.
1774
1938
  */
1775
- function transformTile(tile, extent) {
1776
- if (tile.transformed) return tile;
1777
- const z2 = 1 << tile.z;
1778
- const tx = tile.x;
1779
- const ty = tile.y;
1780
- for (const feature of tile.features) if (feature.type === 1) transformPointFeature(feature, extent, z2, tx, ty);
1781
- else transformNonPointFeature(feature, extent, z2, tx, ty);
1782
- tile.transformed = true;
1783
- return tile;
1939
+ function project(position, t, frame) {
1940
+ return Math.round(frame.extent * (position * frame.z2 - t));
1784
1941
  }
1785
1942
  /**
1786
- * 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`
1787
1946
  */
1788
- function transformPointFeature(feature, extent, z2, tx, ty) {
1789
- const transformed = feature;
1790
- const geometry = feature.geometry;
1791
- const point = [];
1792
- for (let i = 0; i < geometry.length; i += 2) point.push(transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty));
1793
- transformed.geometry = point;
1794
- 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;
1795
1958
  }
1959
+ // src/transform.ts
1796
1960
  /**
1797
- * 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
1798
1965
  */
1799
- function transformNonPointFeature(feature, extent, z2, tx, ty) {
1800
- const transformed = feature;
1801
- const geometry = feature.geometry;
1802
- const nonPoint = [];
1803
- for (const geom of geometry) if (feature.type === 3) nonPoint.push(transformPolygonRing(geom, extent, z2, tx, ty));
1804
- else {
1805
- const line = [];
1806
- for (let i = 0; i < geom.length; i += 2) line.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
1807
- nonPoint.push(line);
1808
- }
1809
- transformed.geometry = nonPoint;
1810
- return transformed;
1966
+ function transformTile(tile) {
1967
+ const features = [];
1968
+ for (const feature of tile.features) features.push(transformFeature(feature));
1969
+ return { features };
1811
1970
  }
1812
- /**
1813
- * Quantizes a polygon ring while preserving its winding so a hole does not become
1814
- * a shell, or vice versa. Signed areas use coordinates relative to the first
1815
- * vertex to reduce cancellation for narrow rings far from the origin.
1816
- * Zero-area results are left unchanged; other quantization topology changes
1817
- * are not repaired.
1818
- */
1819
- function transformPolygonRing(geometry, extent, z2, tx, ty) {
1820
- if (!geometry.length) return [];
1821
- const originX = geometry[0];
1822
- const originY = geometry[1];
1823
- const origin = transformPoint(originX, originY, extent, z2, tx, ty);
1824
- const ring = [origin];
1825
- let previousX = 0;
1826
- let previousY = 0;
1827
- let previousRoundedX = 0;
1828
- let previousRoundedY = 0;
1829
- let sourceArea = 0;
1830
- let roundedArea = 0;
1831
- for (let i = 2; i < geometry.length; i += 2) {
1832
- const point = transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty);
1833
- const x = geometry[i] - originX;
1834
- const y = geometry[i + 1] - originY;
1835
- const roundedX = point[0] - origin[0];
1836
- const roundedY = point[1] - origin[1];
1837
- sourceArea += previousX * y - x * previousY;
1838
- roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1839
- previousX = x;
1840
- previousY = y;
1841
- previousRoundedX = roundedX;
1842
- previousRoundedY = roundedY;
1843
- ring.push(point);
1844
- }
1845
- if (sourceArea * roundedArea < 0) ring.reverse();
1846
- 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;
1847
1990
  }
1848
- function transformPoint(x, y, extent, z2, tx, ty) {
1849
- 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;
1850
1995
  }
1851
1996
  // src/tile-index.ts
1852
1997
  var TileIndex = class {
@@ -1911,12 +2056,12 @@ var TileIndex = class {
1911
2056
  }
1912
2057
  /** {@inheritdoc} */
1913
2058
  getTile(z, x, y) {
1914
- const { extent, debug } = this.options;
2059
+ const { debug } = this.options;
1915
2060
  const z2 = 1 << z;
1916
2061
  if (!this.options.wrap && (x < 0 || x >= z2)) return null;
1917
2062
  x = x + z2 & z2 - 1;
1918
2063
  const id = toID(z, x, y);
1919
- if (this.tiles[id]) return transformTile(this.tiles[id], extent);
2064
+ if (this.tiles[id]) return transformTile(this.tiles[id]);
1920
2065
  if (debug > 1) console.log("drilling down to z%d-%d-%d", z, x, y);
1921
2066
  let z0 = z;
1922
2067
  let x0 = x;
@@ -1936,7 +2081,7 @@ var TileIndex = class {
1936
2081
  this.splitTile(parent.source, z0, x0, y0, z, x, y);
1937
2082
  if (debug > 1) console.timeEnd("drilling down");
1938
2083
  if (!this.tiles[id]) return null;
1939
- return transformTile(this.tiles[id], extent);
2084
+ return transformTile(this.tiles[id]);
1940
2085
  }
1941
2086
  /**
1942
2087
  * The features the top tile starts from. A world that wraps keeps them all left and right, where wrapping already
@@ -2052,10 +2197,10 @@ var TileIndex = class {
2052
2197
  let minY = 1e400;
2053
2198
  let maxY = -1e400;
2054
2199
  for (const feature of features) {
2055
- minX = Math.min(minX, feature.minX);
2056
- maxX = Math.max(maxX, feature.maxX);
2057
- minY = Math.min(minY, feature.minY);
2058
- 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));
2059
2204
  }
2060
2205
  const k1 = options.buffer / options.extent;
2061
2206
  const removedLookup = /* @__PURE__ */ new Set();
@@ -2068,7 +2213,7 @@ var TileIndex = class {
2068
2213
  const tileMaxY = (tile.y + 1 + k1) / z2;
2069
2214
  if (maxX < tileMinX || minX >= tileMaxX || maxY < tileMinY || minY >= tileMaxY) continue;
2070
2215
  let intersects = false;
2071
- 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) {
2072
2217
  intersects = true;
2073
2218
  break;
2074
2219
  }
@@ -2288,7 +2433,7 @@ function geoJSONToTile(data, z, x, y, options = {}) {
2288
2433
  const buffer = options.buffer / options.extent;
2289
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);
2290
2435
  }
2291
- return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
2436
+ return transformTile(createTile(features ?? [], z, x, y, options, Float64Array));
2292
2437
  }
2293
2438
  export { GEOJSONVT_CLIP_END, GEOJSONVT_CLIP_START, GeoJSONVT, ClusterTileIndex as Supercluster, geoJSONToTile };
2294
2439