@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
@@ -80,52 +80,67 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
80
80
 
81
81
  // src/feature.ts
82
82
  /**
83
- *
83
+ *
84
84
  * @param id - the feature's ID
85
85
  * @param type - the feature's type
86
- * @param geom - the feature's geometry
86
+ * @param geom - the feature's coordinates, see {@link GeoJSONVTSourceCoords}
87
87
  * @param tags - the feature's properties
88
88
  * @returns the created feature
89
89
  */
90
90
  function createFeature(id, type, geom, tags) {
91
- const data = {
92
- type,
93
- geom
94
- };
95
91
  const feature = {
96
92
  id: id == null ? null : id,
97
- type: data.type,
98
- geometry: data.geom,
93
+ type,
94
+ geometry: geom,
99
95
  tags,
100
96
  minX: 1e400,
101
97
  minY: 1e400,
102
98
  maxX: -1e400,
103
- maxY: -1e400
99
+ maxY: -1e400,
100
+ worldOffset: 0
104
101
  };
105
- switch (data.type) {
106
- case "Point":
107
- case "MultiPoint":
108
- calcLineBBox(feature, data.geom);
109
- break;
110
- case "LineString":
111
- calcLineBBox(feature, data.geom.points);
112
- break;
113
- case "Polygon":
114
- calcLineBBox(feature, data.geom[0].points);
115
- break;
116
- case "MultiLineString":
117
- for (const line of data.geom) calcLineBBox(feature, line.points);
118
- break;
119
- case "MultiPolygon": for (const polygon of data.geom) calcLineBBox(feature, polygon[0].points);
102
+ if (type === "MultiPoint") {
103
+ calcBBox(feature, geom, 0, geom.length);
104
+ return feature;
105
+ }
106
+ const outerOnly = type === "Polygon" || type === "MultiPolygon";
107
+ for (let i = 0; i < geom.length;) {
108
+ const end = i + 2 + geom[i] * 3;
109
+ if (!outerOnly || !isHole(geom[i + 1])) calcBBox(feature, geom, i + 2, end);
110
+ i = end;
120
111
  }
121
112
  return feature;
122
113
  }
123
- function optimizeLineMemory(line) {
124
- const lineImmutable = line;
125
- if (line.points.length > 64) lineImmutable.points = new Float64Array(line.points);
114
+ /**
115
+ * Whether a polygon ring is a hole, going by the size stored before it. A hole has a negative size, which is -0
116
+ * when it has no area. An outer ring whose area is not a number has NaN, so the test is not `size >= 0`.
117
+ */
118
+ function isHole(size) {
119
+ return size < 0 || Object.is(size, -0);
126
120
  }
127
- function calcLineBBox(feature, geom) {
128
- for (let i = 0; i < geom.length; i += 3) {
121
+ function createPointFeature(id, x, y, tags) {
122
+ return {
123
+ id: id == null ? null : id,
124
+ type: "Point",
125
+ x,
126
+ y,
127
+ tags
128
+ };
129
+ }
130
+ function getMinX(feature) {
131
+ return feature.type === "Point" ? feature.x : feature.minX;
132
+ }
133
+ function getMinY(feature) {
134
+ return feature.type === "Point" ? feature.y : feature.minY;
135
+ }
136
+ function getMaxX(feature) {
137
+ return feature.type === "Point" ? feature.x : feature.maxX;
138
+ }
139
+ function getMaxY(feature) {
140
+ return feature.type === "Point" ? feature.y : feature.maxY;
141
+ }
142
+ function calcBBox(feature, geom, start, end) {
143
+ for (let i = start; i < end; i += 3) {
129
144
  feature.minX = Math.min(feature.minX, geom[i]);
130
145
  feature.minY = Math.min(feature.minY, geom[i + 1]);
131
146
  feature.maxX = Math.max(feature.maxX, geom[i]);
@@ -228,22 +243,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
228
243
  const worldCoordinateHelper = getWorldCoordinateHelper(options.coordinates);
229
244
  switch (geojson.geometry.type) {
230
245
  case "Point":
231
- convertPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
246
+ features.push(createPointFeature(id, worldCoordinateHelper.projectX(geojson.geometry.coordinates[0]), worldCoordinateHelper.projectY(geojson.geometry.coordinates[1]), geojson.properties));
232
247
  return;
233
248
  case "MultiPoint":
234
249
  convertMultiPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
235
250
  return;
236
251
  case "LineString":
237
- convertLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
252
+ convertLineStringFeature(features, id, geojson.geometry.coordinates, tolerance, worldCoordinateHelper, options, geojson.properties);
238
253
  return;
239
254
  case "MultiLineString":
240
- convertMultiLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, options, geojson.properties);
255
+ if (options.lineMetrics) {
256
+ for (const line of geojson.geometry.coordinates) convertLineStringFeature(features, id, line, tolerance, worldCoordinateHelper, options, geojson.properties);
257
+ return;
258
+ }
259
+ convertRingsFeature(features, id, "MultiLineString", [geojson.geometry.coordinates], tolerance, worldCoordinateHelper, geojson.properties);
241
260
  return;
242
261
  case "Polygon":
243
- convertPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
262
+ convertRingsFeature(features, id, "Polygon", [geojson.geometry.coordinates], tolerance, worldCoordinateHelper, geojson.properties);
244
263
  return;
245
264
  case "MultiPolygon":
246
- convertMultiPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
265
+ convertRingsFeature(features, id, "MultiPolygon", geojson.geometry.coordinates, tolerance, worldCoordinateHelper, geojson.properties);
247
266
  return;
248
267
  default: throw new Error("Input data is not a valid GeoJSON object.");
249
268
  }
@@ -261,56 +280,70 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
261
280
  properties: geojson.properties
262
281
  }, options, index, depth);
263
282
  }
264
- function convertPointFeature(features, id, geom, worldCoordinateHelper, properties) {
265
- const out = [];
266
- out.push(worldCoordinateHelper.projectX(geom.coordinates[0]), worldCoordinateHelper.projectY(geom.coordinates[1]), 0);
267
- features.push(createFeature(id, "Point", out, properties));
268
- }
269
283
  function convertMultiPointFeature(features, id, geom, worldCoordinateHelper, properties) {
270
- const out = [];
271
- for (const coords of geom.coordinates) out.push(worldCoordinateHelper.projectX(coords[0]), worldCoordinateHelper.projectY(coords[1]), 0);
284
+ const out = new Float64Array(geom.coordinates.length * 3);
285
+ for (let i = 0; i < geom.coordinates.length; i++) {
286
+ out[i * 3] = worldCoordinateHelper.projectX(geom.coordinates[i][0]);
287
+ out[i * 3 + 1] = worldCoordinateHelper.projectY(geom.coordinates[i][1]);
288
+ }
272
289
  features.push(createFeature(id, "MultiPoint", out, properties));
273
290
  }
274
- function convertLineStringFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
275
- const out = { points: [] };
276
- convertLine(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
277
- features.push(createFeature(id, "LineString", out, properties));
278
- }
279
- function convertMultiLineStringFeature(features, id, geom, tolerance, worldCoordinateHelper, options, properties) {
280
- if (options.lineMetrics) for (const line of geom.coordinates) {
281
- const out = { points: [] };
282
- convertLine(line, out, tolerance, false, worldCoordinateHelper);
283
- features.push(createFeature(id, "LineString", out, properties));
284
- }
285
- else {
286
- const out = [];
287
- convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
288
- features.push(createFeature(id, "MultiLineString", out, properties));
291
+ function convertLineStringFeature(features, id, line, tolerance, worldCoordinateHelper, options, properties) {
292
+ if (!line.length) return;
293
+ const out = new Float64Array(2 + line.length * 3);
294
+ convertRing(line, out, 0, tolerance, false, false, worldCoordinateHelper);
295
+ const feature = createFeature(id, "LineString", out, properties);
296
+ if (options.lineMetrics) {
297
+ feature.start = 0;
298
+ feature.end = feature.size = out[1];
289
299
  }
300
+ features.push(feature);
290
301
  }
291
- function convertPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
292
- const out = [];
293
- convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
294
- features.push(createFeature(id, "Polygon", out, properties));
295
- }
296
- function convertMultiPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
297
- const out = [];
298
- for (const polygon of geom.coordinates) {
299
- const polygonOut = [];
300
- convertLines(polygon, polygonOut, tolerance, true, worldCoordinateHelper);
301
- if (!polygonOut.length) continue;
302
- out.push(polygonOut);
302
+ /**
303
+ * Converts a MultiLineString, a Polygon or a MultiPolygon, whose rings come in groups: all lines in one,
304
+ * and the rings of one polygon in each. The first ring of a polygon is its outer ring, and the rest are its holes.
305
+ */
306
+ function convertRingsFeature(features, id, type, groups, tolerance, worldCoordinateHelper, properties) {
307
+ let length = 0;
308
+ for (const rings of groups) for (const ring of rings) if (ring.length) length += 2 + ring.length * 3;
309
+ if (!length) return;
310
+ const isPolygon = type !== "MultiLineString";
311
+ const out = new Float64Array(length);
312
+ let index = 0;
313
+ for (const rings of groups) {
314
+ let isOuter = true;
315
+ for (const ring of rings) {
316
+ if (!ring.length) continue;
317
+ index = convertRing(ring, out, index, tolerance, isPolygon, isOuter, worldCoordinateHelper);
318
+ isOuter = false;
319
+ }
303
320
  }
304
- if (!out.length) return;
305
- features.push(createFeature(id, "MultiPolygon", out, properties));
321
+ features.push(createFeature(id, type, out, properties));
306
322
  }
307
- function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
323
+ /**
324
+ * The size a hole is stored with, which has to be negative for {@link isHole} to tell it from an outer ring.
325
+ * A hole without area gets -0, and one whose area is not a number gets -Infinity. Both compare with a tolerance
326
+ * the way 0 and NaN do.
327
+ */
328
+ function toHoleSize(size) {
329
+ const magnitude = Math.abs(size);
330
+ return Number.isNaN(magnitude) ? -1e400 : -magnitude;
331
+ }
332
+ /**
333
+ * Writes a ring and the two values before it, see {@link GeoJSONVTSourceCoords}.
334
+ * @returns where the next ring starts
335
+ */
336
+ function convertRing(ring, out, index, tolerance, isPolygon, isOuter, worldCoordinateHelper) {
337
+ const first = index + 2;
308
338
  let x0, y0;
309
339
  let size = 0;
340
+ let next = first;
310
341
  for (let j = 0; j < ring.length; j++) {
311
342
  const x = worldCoordinateHelper.projectX(ring[j][0]);
312
343
  const y = worldCoordinateHelper.projectY(ring[j][1]);
313
- out.points.push(x, y, 0);
344
+ out[next] = x;
345
+ out[next + 1] = y;
346
+ next += 3;
314
347
  if (j > 0) {
315
348
  if (isPolygon) size += (x0 * y - x * y0) / 2;
316
349
  else size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2));
@@ -318,21 +351,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
318
351
  x0 = x;
319
352
  y0 = y;
320
353
  }
321
- const last = out.points.length - 3;
322
- out.points[2] = 1;
323
- if (tolerance > 0) simplify(out.points, 0, last, tolerance);
324
- out.points[last + 2] = 1;
325
- optimizeLineMemory(out);
326
- out.size = Math.abs(size);
327
- out.start = 0;
328
- out.end = out.size;
329
- }
330
- function convertLines(rings, out, tolerance, isPolygon, worldCoordinateHelper) {
331
- for (let i = 0; i < rings.length; i++) {
332
- const geom = { points: [] };
333
- convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
334
- out.push(geom);
335
- }
354
+ const last = next - 3;
355
+ out[first + 2] = 1;
356
+ if (tolerance > 0) simplify(out, first, last, tolerance);
357
+ out[last + 2] = 1;
358
+ out[index] = ring.length;
359
+ out[index + 1] = isPolygon && !isOuter ? toHoleSize(size) : Math.abs(size);
360
+ return next;
336
361
  }
337
362
 
338
363
  // src/deconvert.ts
@@ -361,42 +386,64 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
361
386
  * Converts a single internal feature geometry to GeoJSON format.
362
387
  */
363
388
  function geometryToGeoJSON(feature, worldCoordinateHelper) {
364
- const { type, geometry } = feature;
365
- switch (type) {
389
+ switch (feature.type) {
366
390
  case "Point": return {
367
- type,
368
- coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinateHelper)
391
+ type: feature.type,
392
+ coordinates: [worldCoordinateHelper.unprojectX(feature.x), worldCoordinateHelper.unprojectY(feature.y)]
369
393
  };
370
394
  case "MultiPoint": return {
371
- type,
372
- coordinates: unprojectPoints(geometry, worldCoordinateHelper)
395
+ type: feature.type,
396
+ coordinates: unprojectPoints(feature.geometry, feature.worldOffset, 0, feature.geometry.length, worldCoordinateHelper)
373
397
  };
374
398
  case "LineString": return {
375
- type,
376
- coordinates: unprojectPoints(geometry.points, worldCoordinateHelper)
399
+ type: feature.type,
400
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)[0]
377
401
  };
378
402
  case "MultiLineString":
379
403
  case "Polygon": return {
380
- type,
381
- coordinates: geometry.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper))
404
+ type: feature.type,
405
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)
382
406
  };
383
407
  case "MultiPolygon": return {
384
- type,
385
- coordinates: geometry.map((polygon) => polygon.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper)))
408
+ type: feature.type,
409
+ coordinates: unprojectPolygons(feature.geometry, feature.worldOffset, worldCoordinateHelper)
386
410
  };
387
411
  }
388
412
  }
389
- function unprojectPoints(coords, worldCoordinateHelper = mercatorWorldCoordinateHelper) {
413
+ function unprojectRings(coords, offset, worldCoordinateHelper) {
414
+ const rings = [];
415
+ for (let i = 0; i < coords.length;) {
416
+ const ringEnd = i + 2 + coords[i] * 3;
417
+ rings.push(unprojectPoints(coords, offset, i + 2, ringEnd, worldCoordinateHelper));
418
+ i = ringEnd;
419
+ }
420
+ return rings;
421
+ }
422
+ /**
423
+ * The rings in groups of one polygon each, where a ring with a negative size is a hole of the polygon before it.
424
+ */
425
+ function unprojectPolygons(coords, offset, worldCoordinateHelper) {
426
+ const polygons = [];
427
+ for (let i = 0; i < coords.length;) {
428
+ const ringEnd = i + 2 + coords[i] * 3;
429
+ const ring = unprojectPoints(coords, offset, i + 2, ringEnd, worldCoordinateHelper);
430
+ if (!isHole(coords[i + 1]) || !polygons.length) polygons.push([ring]);
431
+ else polygons[polygons.length - 1].push(ring);
432
+ i = ringEnd;
433
+ }
434
+ return polygons;
435
+ }
436
+ /**
437
+ * @param offset - what to add to every x of `coords`
438
+ */
439
+ function unprojectPoints(coords, offset, start, end, worldCoordinateHelper) {
390
440
  const result = [];
391
- for (let i = 0; i < coords.length; i += 3) result.push(unprojectPoint(coords[i], coords[i + 1], worldCoordinateHelper));
441
+ for (let i = start; i < end; i += 3) result.push([worldCoordinateHelper.unprojectX(coords[i] + offset), worldCoordinateHelper.unprojectY(coords[i + 1])]);
392
442
  return result;
393
443
  }
394
- function unprojectPoint(x, y, worldCoordinateHelper) {
395
- return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
396
- }
397
444
 
398
445
  // src/clip.ts
399
- /**
446
+ /**
400
447
  * clip features between two vertical or horizontal axis-parallel lines:
401
448
  * | |
402
449
  * ___|___ | /
@@ -418,6 +465,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
418
465
  if (maxAll < start || minAll >= end) return null;
419
466
  const clipped = [];
420
467
  for (const feature of features) {
468
+ if (feature.type === "Point") {
469
+ const a = axis === 0 ? feature.x : feature.y;
470
+ if (a >= start && a < end) clipped.push(feature);
471
+ continue;
472
+ }
421
473
  const min = axis === 0 ? feature.minX : feature.minY;
422
474
  const max = axis === 0 ? feature.maxX : feature.maxY;
423
475
  if (min >= start && max < end) {
@@ -425,157 +477,238 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
425
477
  continue;
426
478
  }
427
479
  if (max < start || min >= end) continue;
428
- switch (feature.type) {
429
- case "MultiPoint":
430
- clipPointFeature(feature, clipped, start, end, axis);
431
- continue;
432
- case "LineString":
433
- clipLineStringFeature(feature, clipped, start, end, axis, options);
434
- continue;
435
- case "MultiLineString":
436
- clipMultiLineStringFeature(feature, clipped, start, end, axis);
437
- continue;
438
- case "Polygon":
439
- clipPolygonFeature(feature, clipped, start, end, axis);
440
- continue;
441
- case "MultiPolygon":
442
- clipMultiPolygonFeature(feature, clipped, start, end, axis);
443
- continue;
480
+ if (feature.type === "MultiPoint") {
481
+ clipPointsFeature(feature, clipped, start, end, axis);
482
+ continue;
444
483
  }
484
+ clipRingsFeature(feature, clipped, start, end, axis, options);
445
485
  }
446
486
  if (!clipped.length) return null;
447
487
  return clipped;
448
488
  }
449
- function clipPointFeature(feature, clipped, start, end, axis) {
450
- const geom = [];
451
- clipPoints(feature.geometry, geom, start, end, axis);
452
- if (!geom.length) return;
453
- const type = geom.length === 3 ? "Point" : "MultiPoint";
454
- clipped.push(createFeature(feature.id, type, geom, feature.tags));
455
- }
456
- function clipLineStringFeature(feature, clipped, start, end, axis, options) {
457
- const geom = [];
458
- clipLine(feature.geometry, geom, start, end, axis, false, options.lineMetrics);
459
- if (!geom.length) return;
460
- if (options.lineMetrics) {
461
- for (const line of geom) clipped.push(createFeature(feature.id, "LineString", line, feature.tags));
489
+ function clipPointsFeature(feature, clipped, start, end, axis) {
490
+ const geom = feature.geometry;
491
+ const offset = feature.worldOffset;
492
+ const axisOffset = axis === 0 ? offset : 0;
493
+ let count = 0;
494
+ let lastInside = 0;
495
+ for (let i = 0; i < geom.length; i += 3) {
496
+ const a = geom[i + axis] + axisOffset;
497
+ if (a >= start && a <= end) {
498
+ count++;
499
+ lastInside = i;
500
+ }
501
+ }
502
+ if (!count) return;
503
+ if (count === 1) {
504
+ clipped.push(createPointFeature(feature.id, geom[lastInside] + offset, geom[lastInside + 1], feature.tags));
462
505
  return;
463
506
  }
464
- if (geom.length > 1) {
465
- clipped.push(createFeature(feature.id, "MultiLineString", geom, feature.tags));
507
+ const out = new Float64Array(count * 3);
508
+ let next = 0;
509
+ for (let i = 0; i < geom.length; i += 3) {
510
+ const a = geom[i + axis] + axisOffset;
511
+ if (a >= start && a <= end) {
512
+ out[next] = geom[i] + offset;
513
+ out[next + 1] = geom[i + 1];
514
+ out[next + 2] = geom[i + 2];
515
+ next += 3;
516
+ }
517
+ }
518
+ clipped.push(createFeature(feature.id, "MultiPoint", out, feature.tags));
519
+ }
520
+ function clipRingsFeature(feature, clipped, start, end, axis, options) {
521
+ const geom = feature.geometry;
522
+ const offset = feature.worldOffset;
523
+ const isPolygon = feature.type === "Polygon" || feature.type === "MultiPolygon";
524
+ let length = 0;
525
+ for (let i = 0; i < geom.length;) {
526
+ const ringEnd = i + 2 + geom[i] * 3;
527
+ length += countClippedRing(geom, offset, i + 2, ringEnd, start, end, axis, isPolygon);
528
+ i = ringEnd;
529
+ }
530
+ if (!length) return;
531
+ const metricsOf = options.lineMetrics && feature.type === "LineString" ? feature : null;
532
+ const metrics = [];
533
+ let out = new Float64Array(length);
534
+ let next = 0;
535
+ let hasOuter = false;
536
+ for (let i = 0; i < geom.length;) {
537
+ const ringEnd = i + 2 + geom[i] * 3;
538
+ let size = geom[i + 1];
539
+ if (isPolygon) {
540
+ if (!isHole(size)) hasOuter = false;
541
+ if (!hasOuter) size = Math.abs(size);
542
+ }
543
+ const ringStart = next;
544
+ next = clipRing(geom, offset, i + 2, ringEnd, size, out, next, start, end, axis, isPolygon, metricsOf, metrics);
545
+ if (next > ringStart) hasOuter = true;
546
+ i = ringEnd;
547
+ }
548
+ if (next < length) out = out.subarray(0, next);
549
+ if (metricsOf) {
550
+ for (let i = 0, part = 0; i < out.length; part += 2) {
551
+ const ringEnd = i + 2 + out[i] * 3;
552
+ const line = createFeature(feature.id, "LineString", i === 0 && ringEnd === out.length ? out : out.slice(i, ringEnd), feature.tags);
553
+ line.start = metrics[part];
554
+ line.end = metrics[part + 1];
555
+ line.size = metricsOf.size;
556
+ clipped.push(line);
557
+ i = ringEnd;
558
+ }
466
559
  return;
467
560
  }
468
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
469
- }
470
- function clipMultiLineStringFeature(feature, clipped, start, end, axis) {
471
- const geom = [];
472
- clipLines(feature.geometry, geom, start, end, axis, false);
473
- if (!geom.length) return;
474
- if (geom.length === 1) {
475
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
561
+ if (isPolygon) {
562
+ clipped.push(createFeature(feature.id, feature.type, out, feature.tags));
476
563
  return;
477
564
  }
478
- clipped.push(createFeature(feature.id, "MultiLineString", geom, feature.tags));
565
+ const isSingleLine = 2 + out[0] * 3 === out.length;
566
+ clipped.push(createFeature(feature.id, isSingleLine ? "LineString" : "MultiLineString", out, feature.tags));
479
567
  }
480
- function clipPolygonFeature(feature, clipped, start, end, axis) {
481
- const geom = [];
482
- clipLines(feature.geometry, geom, start, end, axis, true);
483
- if (!geom.length) return;
484
- clipped.push(createFeature(feature.id, "Polygon", geom, feature.tags));
485
- }
486
- function clipMultiPolygonFeature(feature, clipped, start, end, axis) {
487
- const geom = [];
488
- for (const polygon of feature.geometry) {
489
- const newPolygon = [];
490
- clipLines(polygon, newPolygon, start, end, axis, true);
491
- if (!newPolygon.length) continue;
492
- geom.push(newPolygon);
568
+ /**
569
+ * How many values `clipRing` writes for a ring, without working out the intersections.
570
+ * @param offset - what to add to every x of `geom`
571
+ */
572
+ function countClippedRing(geom, offset, first, ringEnd, start, end, axis, isPolygon) {
573
+ const axisOffset = axis === 0 ? offset : 0;
574
+ let parts = 0;
575
+ let total = 0;
576
+ let count = 0;
577
+ for (let i = first; i < ringEnd - 3; i += 3) {
578
+ const a = geom[i + axis] + axisOffset;
579
+ const b = geom[i + 3 + axis] + axisOffset;
580
+ let exited = false;
581
+ if (a < start) {
582
+ if (b >= start) count++;
583
+ } else if (a > end) {
584
+ if (b <= end) count++;
585
+ } else count++;
586
+ if (b < start && a >= start) {
587
+ count++;
588
+ exited = true;
589
+ }
590
+ if (b > end && a <= end) {
591
+ count++;
592
+ exited = true;
593
+ }
594
+ if (!isPolygon && exited) {
595
+ total += count;
596
+ parts++;
597
+ count = 0;
598
+ }
493
599
  }
494
- if (!geom.length) return;
495
- clipped.push(createFeature(feature.id, "MultiPolygon", geom, feature.tags));
496
- }
497
- function clipPoints(geom, newGeom, start, end, axis) {
498
- for (let i = 0; i < geom.length; i += 3) {
499
- const a = geom[i + axis];
500
- if (a >= start && a <= end) addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
600
+ const a = geom[ringEnd - 3 + axis] + axisOffset;
601
+ if (a >= start && a <= end) count++;
602
+ if (isPolygon && count >= 2) count++;
603
+ if (count > 0) {
604
+ total += count;
605
+ parts++;
501
606
  }
607
+ return total * 3 + parts * 2;
502
608
  }
503
- function clipLine(geom, newGeom, start, end, axis, isPolygon, trackMetrics) {
504
- let slice = newSlice(geom);
505
- const intersect = axis === 0 ? intersectX : intersectY;
506
- let len = geom.start;
609
+ /**
610
+ * Clips a ring and writes what is left of it to `out`, each part with the two values before it,
611
+ * see {@link GeoJSONVTSourceCoords}. A polygon ring stays one ring, and a line can come out in several parts.
612
+ * @param offset - what to add to every x of `geom`, which `out` has added already
613
+ * @param metricsOf - the line whose metrics to track, which adds where each part starts and ends to `metrics`
614
+ * @returns where the next ring starts in `out`
615
+ */
616
+ function clipRing(geom, offset, first, ringEnd, size, out, next, start, end, axis, isPolygon, metricsOf, metrics) {
617
+ let header = next;
618
+ out[next + 1] = size;
619
+ next += 2;
620
+ let partStart = next;
621
+ let len = metricsOf ? metricsOf.start : 0;
622
+ let metricStart = len;
507
623
  let segLen, t;
508
- for (let i = 0; i < geom.points.length - 3; i += 3) {
509
- const ax = geom.points[i];
510
- const ay = geom.points[i + 1];
511
- const az = geom.points[i + 2];
512
- const bx = geom.points[i + 3];
513
- const by = geom.points[i + 4];
624
+ for (let i = first; i < ringEnd - 3; i += 3) {
625
+ const ax = geom[i] + offset;
626
+ const ay = geom[i + 1];
627
+ const az = geom[i + 2];
628
+ const bx = geom[i + 3] + offset;
629
+ const by = geom[i + 4];
514
630
  const a = axis === 0 ? ax : ay;
515
631
  const b = axis === 0 ? bx : by;
516
632
  let exited = false;
517
- if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
633
+ if (metricsOf) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
518
634
  if (a < start) {
519
635
  if (b >= start) {
520
- t = intersect(slice, ax, ay, bx, by, start);
521
- if (trackMetrics) slice.start = len + segLen * t;
636
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
637
+ next += 3;
638
+ if (metricsOf) metricStart = len + segLen * t;
522
639
  }
523
640
  } else if (a > end) {
524
641
  if (b <= end) {
525
- t = intersect(slice, ax, ay, bx, by, end);
526
- if (trackMetrics) slice.start = len + segLen * t;
642
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
643
+ next += 3;
644
+ if (metricsOf) metricStart = len + segLen * t;
527
645
  }
528
- } else addPoint(slice.points, ax, ay, az);
646
+ } else {
647
+ out[next] = ax;
648
+ out[next + 1] = ay;
649
+ out[next + 2] = az;
650
+ next += 3;
651
+ }
529
652
  if (b < start && a >= start) {
530
- t = intersect(slice, ax, ay, bx, by, start);
653
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
654
+ next += 3;
531
655
  exited = true;
532
656
  }
533
657
  if (b > end && a <= end) {
534
- t = intersect(slice, ax, ay, bx, by, end);
658
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
659
+ next += 3;
535
660
  exited = true;
536
661
  }
537
662
  if (!isPolygon && exited) {
538
- if (trackMetrics) slice.end = len + segLen * t;
539
- newGeom.push(slice);
540
- slice = newSlice(geom);
663
+ out[header] = (next - partStart) / 3;
664
+ if (metricsOf) {
665
+ metrics.push(metricStart, len + segLen * t);
666
+ metricStart = metricsOf.start;
667
+ }
668
+ header = next;
669
+ out[next + 1] = size;
670
+ next += 2;
671
+ partStart = next;
541
672
  }
542
- if (trackMetrics) len += segLen;
673
+ if (metricsOf) len += segLen;
543
674
  }
544
- let last = geom.points.length - 3;
545
- const ax = geom.points[last];
546
- const ay = geom.points[last + 1];
547
- const az = geom.points[last + 2];
675
+ const last = ringEnd - 3;
676
+ const ax = geom[last] + offset;
677
+ const ay = geom[last + 1];
548
678
  const a = axis === 0 ? ax : ay;
549
- if (a >= start && a <= end) addPoint(slice.points, ax, ay, az);
550
- last = slice.points.length - 3;
551
- 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]);
552
- if (slice.points.length) {
553
- optimizeLineMemory(slice);
554
- newGeom.push(slice);
555
- }
556
- }
557
- function newSlice(line) {
558
- return {
559
- points: [],
560
- size: line.size,
561
- start: line.start,
562
- end: line.end
563
- };
564
- }
565
- function clipLines(geom, newGeom, start, end, axis, isPolygon) {
566
- for (const line of geom) clipLine(line, newGeom, start, end, axis, isPolygon, false);
679
+ if (a >= start && a <= end) {
680
+ out[next] = ax;
681
+ out[next + 1] = ay;
682
+ out[next + 2] = geom[last + 2];
683
+ next += 3;
684
+ }
685
+ if (isPolygon && next - partStart >= 6 && (out[next - 3] !== out[partStart] || out[next - 2] !== out[partStart + 1])) {
686
+ out[next] = out[partStart];
687
+ out[next + 1] = out[partStart + 1];
688
+ out[next + 2] = out[partStart + 2];
689
+ next += 3;
690
+ }
691
+ if (next === partStart) return header;
692
+ out[header] = (next - partStart) / 3;
693
+ if (metricsOf) metrics.push(metricStart, metricsOf.end);
694
+ return next;
567
695
  }
568
- function addPoint(out, x, y, z) {
569
- out.push(x, y, z);
570
- }
571
- function intersectX(out, ax, ay, bx, by, x) {
572
- const t = (x - ax) / (bx - ax);
573
- addPoint(out.points, x, ay + (by - ay) * t, 1);
574
- return t;
575
- }
576
- function intersectY(out, ax, ay, bx, by, y) {
577
- const t = (y - ay) / (by - ay);
578
- addPoint(out.points, ax + (bx - ax) * t, y, 1);
696
+ /**
697
+ * Writes the position where a segment crosses the clip line at `k`.
698
+ * @returns how far along the segment that is, from 0 to 1
699
+ */
700
+ function intersect(out, next, ax, ay, bx, by, k, axis) {
701
+ let t;
702
+ if (axis === 0) {
703
+ t = (k - ax) / (bx - ax);
704
+ out[next] = k;
705
+ out[next + 1] = ay + (by - ay) * t;
706
+ } else {
707
+ t = (k - ay) / (by - ay);
708
+ out[next] = ax + (bx - ax) * t;
709
+ out[next + 1] = k;
710
+ }
711
+ out[next + 2] = 1;
579
712
  return t;
580
713
  }
581
714
 
@@ -592,58 +725,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
592
725
  if (right) merged = merged.concat(shiftFeatureCoords(right, -1));
593
726
  return merged;
594
727
  }
728
+ /**
729
+ * The features on the other side of the antimeridian. A feature with a coordinates array shares it with its copy,
730
+ * which only gets the offset.
731
+ */
595
732
  function shiftFeatureCoords(features, offset) {
596
733
  const newFeatures = [];
597
- for (const feature of features) switch (feature.type) {
598
- case "Point":
599
- case "MultiPoint": {
600
- const newGeometry = shiftPointCoords(feature.geometry, offset);
601
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
602
- continue;
603
- }
604
- case "LineString": {
605
- const newGeometry = shiftLineCoords(feature.geometry, offset);
606
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
607
- continue;
608
- }
609
- case "MultiLineString":
610
- case "Polygon": {
611
- const newGeometry = [];
612
- for (const line of feature.geometry) newGeometry.push(shiftLineCoords(line, offset));
613
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
614
- continue;
615
- }
616
- case "MultiPolygon": {
617
- const newGeometry = [];
618
- for (const polygon of feature.geometry) {
619
- const newPolygon = [];
620
- for (const line of polygon) newPolygon.push(shiftLineCoords(line, offset));
621
- newGeometry.push(newPolygon);
622
- }
623
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
734
+ for (const feature of features) {
735
+ if (feature.type === "Point") {
736
+ newFeatures.push(createPointFeature(feature.id, feature.x + offset, feature.y, feature.tags));
624
737
  continue;
625
738
  }
739
+ newFeatures.push({
740
+ ...feature,
741
+ minX: feature.minX + offset,
742
+ maxX: feature.maxX + offset,
743
+ worldOffset: feature.worldOffset + offset
744
+ });
626
745
  }
627
746
  return newFeatures;
628
747
  }
629
- function shiftPointCoords(coords, offset) {
630
- const newCoords = [];
631
- for (let i = 0; i < coords.length; i += 3) newCoords.push(coords[i] + offset, coords[i + 1], coords[i + 2]);
632
- return newCoords;
633
- }
634
- function shiftLineCoords(line, offset) {
635
- const newLine = {
636
- points: [],
637
- size: line.size
638
- };
639
- if (line.start !== void 0) {
640
- newLine.start = line.start;
641
- newLine.end = line.end;
642
- }
643
- 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]);
644
- optimizeLineMemory(newLine);
645
- return newLine;
646
- }
647
748
 
648
749
  // src/difference.ts
649
750
  /**
@@ -1159,11 +1260,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1159
1260
  const positions = point.geometry.type === "MultiPoint" ? point.geometry.coordinates : [point.geometry.coordinates];
1160
1261
  for (const [lng, lat] of positions) {
1161
1262
  const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
1162
- if (!this.options.wrap && !isInWorld([x, y])) continue;
1263
+ if (!this.options.wrap && !isInWorld(x, y)) continue;
1163
1264
  const feature = {
1164
1265
  id: point.id,
1165
1266
  type: "Point",
1166
- geometry: [x, y],
1267
+ x,
1268
+ y,
1167
1269
  tags: point.properties
1168
1270
  };
1169
1271
  features.push(feature);
@@ -1181,18 +1283,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1181
1283
  const points = [];
1182
1284
  for (const feature of features) {
1183
1285
  if (feature.type === "Point") {
1184
- if (!this.options.wrap && !isInWorld(feature.geometry)) continue;
1286
+ if (!this.isClustered(feature.x, feature.y)) continue;
1185
1287
  points.push(feature);
1186
1288
  continue;
1187
1289
  }
1188
1290
  if (feature.type !== "MultiPoint") continue;
1189
1291
  for (let i = 0; i < feature.geometry.length; i += 3) {
1190
- const geometry = [feature.geometry[i], feature.geometry[i + 1]];
1191
- if (!this.options.wrap && !isInWorld(geometry)) continue;
1292
+ const x = feature.geometry[i] + feature.worldOffset;
1293
+ const y = feature.geometry[i + 1];
1294
+ if (!this.isClustered(x, y)) continue;
1192
1295
  points.push({
1193
1296
  id: feature.id,
1194
1297
  type: "Point",
1195
- geometry,
1298
+ x,
1299
+ y,
1196
1300
  tags: feature.tags
1197
1301
  });
1198
1302
  }
@@ -1200,6 +1304,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1200
1304
  this.createIndex(points);
1201
1305
  }
1202
1306
  /**
1307
+ * Whether a position of the data source goes into the clusters. In a world that wraps, the source also has a copy
1308
+ * of each position near the antimeridian on the other side of it. Only the one inside the world is clustered, since
1309
+ * `getTile` looks across the antimeridian by itself. In a world that doesn't wrap, the positions outside it are left out.
1310
+ */
1311
+ isClustered(x, y) {
1312
+ return this.options.wrap ? x >= 0 && x < 1 : isInWorld(x, y);
1313
+ }
1314
+ /**
1203
1315
  * @internal
1204
1316
  * Updates the cluster data by rebuilding.
1205
1317
  * @param features
@@ -1232,9 +1344,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1232
1344
  let numItems = 0;
1233
1345
  for (let i = 0; i < points.length; i++) {
1234
1346
  const p = points[i];
1235
- if (!p?.geometry || !fitsInteger(p.geometry[0]) || !fitsInteger(p.geometry[1])) continue;
1236
- coords[numItems * 2] = Math.round(encode(p.geometry[0]));
1237
- coords[numItems * 2 + 1] = Math.round(encode(p.geometry[1]));
1347
+ if (!fitsInteger(p.x) || !fitsInteger(p.y)) continue;
1348
+ coords[numItems * 2] = Math.round(encode(p.x));
1349
+ coords[numItems * 2 + 1] = Math.round(encode(p.y));
1238
1350
  const k = numItems * stride;
1239
1351
  data[k + OFFSET_ZOOM] = notProcessed;
1240
1352
  data[k + OFFSET_ID] = i;
@@ -1411,14 +1523,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1411
1523
  const p = radius / extent;
1412
1524
  const top = encode((y - p) / z2);
1413
1525
  const bottom = encode((y + 1 + p) / z2);
1414
- const tile = {
1415
- transformed: true,
1416
- features: [],
1417
- source: null,
1418
- x,
1419
- y,
1420
- z
1421
- };
1526
+ const tile = { features: [] };
1422
1527
  this.addTileFeatures(tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom), tree, x, y, z2, tile);
1423
1528
  if (this.options.wrap && x === 0) this.addTileFeatures(tree.range(encode(1 - p / z2), top, encode(1), bottom), tree, z2, y, z2, tile);
1424
1529
  if (this.options.wrap && x === z2 - 1) this.addTileFeatures(tree.range(encode(0), top, encode(p / z2), bottom), tree, -1, y, z2, tile);
@@ -1478,7 +1583,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1478
1583
  } else {
1479
1584
  const p = this.points[data[k + OFFSET_ID]];
1480
1585
  tags = p.tags;
1481
- [px, py] = p.geometry;
1586
+ px = p.x;
1587
+ py = p.y;
1482
1588
  }
1483
1589
  const f = {
1484
1590
  type: 1,
@@ -1601,7 +1707,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1601
1707
  id: data[i + OFFSET_ID],
1602
1708
  type: "Point",
1603
1709
  tags: getClusterProperties(data, i, clusterProps),
1604
- geometry: [decode(coords[index * 2]), decode(coords[index * 2 + 1])]
1710
+ x: decode(coords[index * 2]),
1711
+ y: decode(coords[index * 2 + 1])
1605
1712
  };
1606
1713
  }
1607
1714
  function getClusterGeoJSON(coords, index, data, i, clusterProps, worldCoordinateHelper) {
@@ -1627,26 +1734,32 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1627
1734
  point_count_abbreviated: abbrev
1628
1735
  });
1629
1736
  }
1630
- function isInWorld(geometry) {
1631
- return geometry[0] >= 0 && geometry[0] <= 1 && geometry[1] >= 0 && geometry[1] <= 1;
1737
+ function isInWorld(x, y) {
1738
+ return x >= 0 && x <= 1 && y >= 0 && y <= 1;
1632
1739
  }
1633
1740
 
1634
1741
  // src/tile.ts
1635
1742
  const GEOJSONVT_CLIP_START = "geojsonvt_clip_start";
1636
1743
  const GEOJSONVT_CLIP_END = "geojsonvt_clip_end";
1637
1744
  /**
1638
- * Creates a tile object from the given features
1745
+ * The smallest array type that holds the coordinates of a tile whose features are cut at its buffer.
1746
+ */
1747
+ function getTileCoordsConstructor(options) {
1748
+ return options.extent + options.buffer <= 32767 ? Int16Array : Int32Array;
1749
+ }
1750
+ /**
1751
+ * Creates a tile object from the given features, with their coordinates in the tile's extent
1639
1752
  * @param features - the features to include in the tile
1640
1753
  * @param z
1641
1754
  * @param tx
1642
1755
  * @param ty
1643
1756
  * @param options - the options object
1757
+ * @param CoordArray - what holds the coordinates, which has to be a `Float64Array` when the features are not cut at the tile's buffer
1644
1758
  * @returns the created tile
1645
1759
  */
1646
- function createTile(features, z, tx, ty, options) {
1760
+ function createTile(features, z, tx, ty, options, CoordArray = getTileCoordsConstructor(options)) {
1647
1761
  const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
1648
1762
  const tile = {
1649
- transformed: false,
1650
1763
  features: [],
1651
1764
  source: null,
1652
1765
  x: tx,
@@ -1660,39 +1773,53 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1660
1773
  numSimplified: 0,
1661
1774
  numFeatures: features.length
1662
1775
  };
1663
- for (const feature of features) addFeature(tile, feature, tolerance, options);
1776
+ const frame = {
1777
+ z2: 1 << z,
1778
+ tx,
1779
+ ty,
1780
+ extent: options.extent,
1781
+ CoordArray
1782
+ };
1783
+ for (const feature of features) addFeature(tile, feature, tolerance, options, frame);
1664
1784
  return tile;
1665
1785
  }
1666
- function addFeature(tile, feature, tolerance, options) {
1786
+ function addFeature(tile, feature, tolerance, options, frame) {
1787
+ if (feature.type === "Point") {
1788
+ addPointTileFeature(tile, feature, frame);
1789
+ return;
1790
+ }
1667
1791
  tile.minX = Math.min(tile.minX, feature.minX);
1668
1792
  tile.minY = Math.min(tile.minY, feature.minY);
1669
1793
  tile.maxX = Math.max(tile.maxX, feature.maxX);
1670
1794
  tile.maxY = Math.max(tile.maxY, feature.maxY);
1671
- switch (feature.type) {
1672
- case "Point":
1673
- case "MultiPoint":
1674
- addPointsTileFeature(tile, feature);
1675
- return;
1676
- case "LineString":
1677
- addLineTileFeautre(tile, feature, tolerance, options);
1678
- return;
1679
- case "MultiLineString":
1680
- case "Polygon":
1681
- addLinesTileFeature(tile, feature, tolerance);
1682
- return;
1683
- case "MultiPolygon":
1684
- addMultiPolygonTileFeature(tile, feature, tolerance);
1685
- return;
1795
+ if (feature.type === "MultiPoint") {
1796
+ addPointsTileFeature(tile, feature, frame);
1797
+ return;
1686
1798
  }
1799
+ addRingsTileFeature(tile, feature, tolerance, options, frame);
1687
1800
  }
1688
- function addPointsTileFeature(tile, feature) {
1689
- const geometry = [];
1690
- for (let i = 0; i < feature.geometry.length; i += 3) {
1691
- geometry.push(feature.geometry[i], feature.geometry[i + 1]);
1692
- tile.numPoints++;
1693
- tile.numSimplified++;
1694
- }
1695
- if (!geometry.length) return;
1801
+ function addPointTileFeature(tile, feature, frame) {
1802
+ tile.minX = Math.min(tile.minX, feature.x);
1803
+ tile.minY = Math.min(tile.minY, feature.y);
1804
+ tile.maxX = Math.max(tile.maxX, feature.x);
1805
+ tile.maxY = Math.max(tile.maxY, feature.y);
1806
+ tile.numPoints++;
1807
+ tile.numSimplified++;
1808
+ const tileFeature = {
1809
+ type: 4,
1810
+ tags: feature.tags || null,
1811
+ x: project(feature.x, frame.tx, frame),
1812
+ y: project(feature.y, frame.ty, frame)
1813
+ };
1814
+ if (feature.id !== null) tileFeature.id = feature.id;
1815
+ tile.features.push(tileFeature);
1816
+ }
1817
+ function addPointsTileFeature(tile, feature, frame) {
1818
+ const numPoints = feature.geometry.length / 3;
1819
+ if (!numPoints) return;
1820
+ const geometry = projectCoords(feature.geometry, feature.worldOffset, frame);
1821
+ tile.numPoints += numPoints;
1822
+ tile.numSimplified += numPoints;
1696
1823
  const tileFeature = {
1697
1824
  type: 1,
1698
1825
  tags: feature.tags || null,
@@ -1701,166 +1828,188 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1701
1828
  if (feature.id !== null) tileFeature.id = feature.id;
1702
1829
  tile.features.push(tileFeature);
1703
1830
  }
1704
- function addLineTileFeautre(tile, feature, tolerance, options) {
1831
+ function addRingsTileFeature(tile, feature, tolerance, options, frame) {
1832
+ const isPolygon = feature.type === "Polygon" || feature.type === "MultiPolygon";
1833
+ const coords = feature.geometry;
1705
1834
  const geometry = [];
1706
- addLine(geometry, feature.geometry, tile, tolerance, false, false);
1835
+ for (let i = 0; i < coords.length;) {
1836
+ const ringEnd = i + 2 + coords[i] * 3;
1837
+ addLine(geometry, coords, feature.worldOffset, i + 2, ringEnd, coords[i + 1], tile, tolerance, isPolygon, frame);
1838
+ i = ringEnd;
1839
+ }
1707
1840
  if (!geometry.length) return;
1708
1841
  let tags = feature.tags || null;
1709
- if (options.lineMetrics) {
1842
+ if (feature.type === "LineString" && options.lineMetrics) {
1710
1843
  tags = {};
1711
1844
  for (const key in feature.tags) tags[key] = feature.tags[key];
1712
- tags[GEOJSONVT_CLIP_START] = feature.geometry.start / feature.geometry.size;
1713
- tags[GEOJSONVT_CLIP_END] = feature.geometry.end / feature.geometry.size;
1845
+ tags[GEOJSONVT_CLIP_START] = feature.start / feature.size;
1846
+ tags[GEOJSONVT_CLIP_END] = feature.end / feature.size;
1714
1847
  }
1715
1848
  const tileFeature = {
1716
- type: 2,
1849
+ type: isPolygon ? 3 : 2,
1717
1850
  tags,
1718
1851
  geometry
1719
1852
  };
1720
1853
  if (feature.id !== null) tileFeature.id = feature.id;
1721
1854
  tile.features.push(tileFeature);
1722
1855
  }
1723
- function addLinesTileFeature(tile, feature, tolerance) {
1724
- const geometry = [];
1725
- for (let i = 0; i < feature.geometry.length; i++) addLine(geometry, feature.geometry[i], tile, tolerance, feature.type === "Polygon", i === 0);
1726
- if (!geometry.length) return;
1727
- const tileFeature = {
1728
- type: feature.type === "Polygon" ? 3 : 2,
1729
- tags: feature.tags || null,
1730
- geometry
1731
- };
1732
- if (feature.id !== null) tileFeature.id = feature.id;
1733
- tile.features.push(tileFeature);
1856
+ /**
1857
+ * Adds a ring to a tile feature, with the positions that matter on the zoom level of the tile.
1858
+ * @param offset - what to add to every x of `coords`
1859
+ * @param size - the size of the ring, which is negative for the hole of a polygon
1860
+ */
1861
+ function addLine(result, coords, offset, first, ringEnd, size, tile, tolerance, isPolygon, frame) {
1862
+ const sqTolerance = tolerance * tolerance;
1863
+ const numPoints = (ringEnd - first) / 3;
1864
+ tile.numPoints += numPoints;
1865
+ if (tolerance > 0 && Math.abs(size) < (isPolygon ? sqTolerance : tolerance)) return;
1866
+ let last = ringEnd - 3;
1867
+ while (last > first && tolerance > 0 && coords[last + 2] <= sqTolerance) last -= 3;
1868
+ let x0 = coords[last] + offset;
1869
+ let y0 = coords[last + 1];
1870
+ let area = 0;
1871
+ let numKept = 0;
1872
+ for (let i = first; i < ringEnd; i += 3) if (tolerance === 0 || coords[i + 2] > sqTolerance) {
1873
+ area += (coords[i] + offset - x0) * (coords[i + 1] + y0);
1874
+ x0 = coords[i] + offset;
1875
+ y0 = coords[i + 1];
1876
+ numKept++;
1877
+ }
1878
+ tile.numSimplified += numKept;
1879
+ const isReversed = isPolygon && area > 0 === !isHole(size);
1880
+ const ring = projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame);
1881
+ if (isPolygon && isWindingFlipped(coords, offset, first, ringEnd, tolerance, isReversed, ring)) reverse(ring);
1882
+ result.push(ring);
1734
1883
  }
1735
- function addMultiPolygonTileFeature(tile, feature, tolerance) {
1736
- const geometry = [];
1737
- for (let k = 0; k < feature.geometry.length; k++) {
1738
- const polygon = feature.geometry[k];
1739
- for (let i = 0; i < polygon.length; i++) addLine(geometry, polygon[i], tile, tolerance, true, i === 0);
1884
+ /**
1885
+ * The positions of a ring that matter on the zoom level of the tile, as coordinates in the tile, last to first when
1886
+ * the ring is reversed. They go into a `Float64Array` when one of them doesn't fit the tile's array, which takes a
1887
+ * position far outside the tile.
1888
+ */
1889
+ function projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame, CoordArray = frame.CoordArray) {
1890
+ const sqTolerance = tolerance * tolerance;
1891
+ const limit = CoordArray === Int16Array ? 32767 : 2147483647;
1892
+ const ring = new CoordArray(numKept * 2);
1893
+ let next = isReversed ? ring.length - 2 : 0;
1894
+ for (let i = first; i < ringEnd; i += 3) if (tolerance === 0 || coords[i + 2] > sqTolerance) {
1895
+ const x = project(coords[i] + offset, frame.tx, frame);
1896
+ const y = project(coords[i + 1], frame.ty, frame);
1897
+ if (CoordArray !== Float64Array && !(Math.abs(x) <= limit && Math.abs(y) <= limit)) return projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame, Float64Array);
1898
+ ring[next] = x;
1899
+ ring[next + 1] = y;
1900
+ next += isReversed ? -2 : 2;
1740
1901
  }
1741
- if (!geometry.length) return;
1742
- const tileFeature = {
1743
- type: 3,
1744
- tags: feature.tags || null,
1745
- geometry
1746
- };
1747
- if (feature.id !== null) tileFeature.id = feature.id;
1748
- tile.features.push(tileFeature);
1902
+ return ring;
1749
1903
  }
1750
- function addLine(result, geom, tile, tolerance, isPolygon, isOuter) {
1904
+ /**
1905
+ * Whether rounding to tile coordinates turned a polygon ring around, so that a hole would read as a shell, or the
1906
+ * other way round. Both signed areas are summed in the order of the tile's ring and relative to its first position,
1907
+ * to reduce cancellation for narrow rings far from the origin. A ring that rounds to no area is left as it is, and
1908
+ * other changes rounding makes to a ring are not repaired.
1909
+ */
1910
+ function isWindingFlipped(coords, offset, first, ringEnd, tolerance, isReversed, ring) {
1751
1911
  const sqTolerance = tolerance * tolerance;
1752
- if (tolerance > 0 && geom.size < (isPolygon ? sqTolerance : tolerance)) {
1753
- tile.numPoints += geom.points.length / 3;
1754
- return;
1755
- }
1756
- const ring = [];
1757
- for (let i = 0; i < geom.points.length; i += 3) {
1758
- if (tolerance === 0 || geom.points[i + 2] > sqTolerance) {
1759
- tile.numSimplified++;
1760
- ring.push(geom.points[i], geom.points[i + 1]);
1912
+ const step = isReversed ? -3 : 3;
1913
+ let originX = 0;
1914
+ let originY = 0;
1915
+ let previousX = 0;
1916
+ let previousY = 0;
1917
+ let previousRoundedX = 0;
1918
+ let previousRoundedY = 0;
1919
+ let sourceArea = 0;
1920
+ let roundedArea = 0;
1921
+ let next = 0;
1922
+ for (let i = isReversed ? ringEnd - 3 : first; i >= first && i < ringEnd; i += step) {
1923
+ if (tolerance > 0 && !(coords[i + 2] > sqTolerance)) continue;
1924
+ if (next === 0) {
1925
+ originX = coords[i] + offset;
1926
+ originY = coords[i + 1];
1927
+ } else {
1928
+ const x = coords[i] + offset - originX;
1929
+ const y = coords[i + 1] - originY;
1930
+ const roundedX = ring[next] - ring[0];
1931
+ const roundedY = ring[next + 1] - ring[1];
1932
+ sourceArea += previousX * y - x * previousY;
1933
+ roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1934
+ previousX = x;
1935
+ previousY = y;
1936
+ previousRoundedX = roundedX;
1937
+ previousRoundedY = roundedY;
1761
1938
  }
1762
- tile.numPoints++;
1939
+ next += 2;
1763
1940
  }
1764
- if (isPolygon) rewind(ring, isOuter);
1765
- result.push(ring);
1941
+ return sourceArea * roundedArea < 0;
1766
1942
  }
1767
- function rewind(ring, clockwise) {
1768
- let area = 0;
1769
- 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]);
1770
- if (area > 0 !== clockwise) return;
1771
- for (let i = 0, len = ring.length; i < len / 2; i += 2) {
1772
- const x = ring[i];
1773
- const y = ring[i + 1];
1774
- ring[i] = ring[len - 2 - i];
1775
- ring[i + 1] = ring[len - 1 - i];
1776
- ring[len - 2 - i] = x;
1777
- ring[len - 1 - i] = y;
1943
+ function reverse(coords) {
1944
+ for (let i = 0, j = coords.length - 2; i < j; i += 2, j -= 2) {
1945
+ const x = coords[i];
1946
+ const y = coords[i + 1];
1947
+ coords[i] = coords[j];
1948
+ coords[i + 1] = coords[j + 1];
1949
+ coords[j] = x;
1950
+ coords[j + 1] = y;
1778
1951
  }
1779
1952
  }
1780
-
1781
- // src/transform.ts
1782
- /**
1783
- * Transforms the coordinates of each feature in the given tile from
1784
- * mercator-projected space into (extent x extent) tile space.
1785
- * @param tile - the tile to transform, this gets modified in place
1786
- * @param extent - the tile extent (usually 4096)
1787
- * @returns the transformed tile
1788
- */
1789
- function transformTile(tile, extent) {
1790
- if (tile.transformed) return tile;
1791
- const z2 = 1 << tile.z;
1792
- const tx = tile.x;
1793
- const ty = tile.y;
1794
- for (const feature of tile.features) if (feature.type === 1) transformPointFeature(feature, extent, z2, tx, ty);
1795
- else transformNonPointFeature(feature, extent, z2, tx, ty);
1796
- tile.transformed = true;
1797
- return tile;
1798
- }
1799
1953
  /**
1800
- * Transforms a single point feature from mercator-projected space into (extent x extent) tile space.
1954
+ * A position in the world, on one axis, as a coordinate in the tile that starts at `t` on that axis.
1801
1955
  */
1802
- function transformPointFeature(feature, extent, z2, tx, ty) {
1803
- const transformed = feature;
1804
- const geometry = feature.geometry;
1805
- const point = [];
1806
- for (let i = 0; i < geometry.length; i += 2) point.push(transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty));
1807
- transformed.geometry = point;
1808
- return transformed;
1956
+ function project(position, t, frame) {
1957
+ return Math.round(frame.extent * (position * frame.z2 - t));
1809
1958
  }
1810
1959
  /**
1811
- * Transforms a single non-point feature from mercator-projected space into (extent x extent) tile space.
1960
+ * The positions of a MultiPoint as coordinates in the tile. They go into a `Float64Array` when one of them doesn't
1961
+ * fit the tile's array, which takes a position far outside the tile.
1962
+ * @param offset - what to add to every x of `positions`
1812
1963
  */
1813
- function transformNonPointFeature(feature, extent, z2, tx, ty) {
1814
- const transformed = feature;
1815
- const geometry = feature.geometry;
1816
- const nonPoint = [];
1817
- for (const geom of geometry) if (feature.type === 3) nonPoint.push(transformPolygonRing(geom, extent, z2, tx, ty));
1818
- else {
1819
- const line = [];
1820
- for (let i = 0; i < geom.length; i += 2) line.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
1821
- nonPoint.push(line);
1822
- }
1823
- transformed.geometry = nonPoint;
1824
- return transformed;
1964
+ function projectCoords(positions, offset, frame, CoordArray = frame.CoordArray) {
1965
+ const limit = CoordArray === Int16Array ? 32767 : 2147483647;
1966
+ const coords = new CoordArray(positions.length / 3 * 2);
1967
+ for (let i = 0, j = 0; i < positions.length; i += 3, j += 2) {
1968
+ const x = project(positions[i] + offset, frame.tx, frame);
1969
+ const y = project(positions[i + 1], frame.ty, frame);
1970
+ if (CoordArray !== Float64Array && !(Math.abs(x) <= limit && Math.abs(y) <= limit)) return projectCoords(positions, offset, frame, Float64Array);
1971
+ coords[j] = x;
1972
+ coords[j + 1] = y;
1973
+ }
1974
+ return coords;
1825
1975
  }
1826
- /**
1827
- * Quantizes a polygon ring while preserving its winding so a hole does not become
1828
- * a shell, or vice versa. Signed areas use coordinates relative to the first
1829
- * vertex to reduce cancellation for narrow rings far from the origin.
1830
- * Zero-area results are left unchanged; other quantization topology changes
1831
- * are not repaired.
1976
+
1977
+ // src/transform.ts
1978
+ /**
1979
+ * Turns a stored tile, which keeps its coordinates in flat arrays, into the tile `getTile` returns,
1980
+ * with every position as an `[x, y]` pair. The stored tile stays as it is.
1981
+ * @param tile - the stored tile
1982
+ * @returns a new tile with the coordinates as pairs
1832
1983
  */
1833
- function transformPolygonRing(geometry, extent, z2, tx, ty) {
1834
- if (!geometry.length) return [];
1835
- const originX = geometry[0];
1836
- const originY = geometry[1];
1837
- const origin = transformPoint(originX, originY, extent, z2, tx, ty);
1838
- const ring = [origin];
1839
- let previousX = 0;
1840
- let previousY = 0;
1841
- let previousRoundedX = 0;
1842
- let previousRoundedY = 0;
1843
- let sourceArea = 0;
1844
- let roundedArea = 0;
1845
- for (let i = 2; i < geometry.length; i += 2) {
1846
- const point = transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty);
1847
- const x = geometry[i] - originX;
1848
- const y = geometry[i + 1] - originY;
1849
- const roundedX = point[0] - origin[0];
1850
- const roundedY = point[1] - origin[1];
1851
- sourceArea += previousX * y - x * previousY;
1852
- roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1853
- previousX = x;
1854
- previousY = y;
1855
- previousRoundedX = roundedX;
1856
- previousRoundedY = roundedY;
1857
- ring.push(point);
1858
- }
1859
- if (sourceArea * roundedArea < 0) ring.reverse();
1860
- return ring;
1984
+ function transformTile(tile) {
1985
+ const features = [];
1986
+ for (const feature of tile.features) features.push(transformFeature(feature));
1987
+ return { features };
1988
+ }
1989
+ function transformFeature(feature) {
1990
+ let transformed;
1991
+ if (feature.type === 4) transformed = {
1992
+ type: 1,
1993
+ tags: feature.tags,
1994
+ geometry: [[feature.x, feature.y]]
1995
+ };
1996
+ else if (feature.type === 1) transformed = {
1997
+ type: 1,
1998
+ tags: feature.tags,
1999
+ geometry: toPairs(feature.geometry)
2000
+ };
2001
+ else transformed = {
2002
+ type: feature.type,
2003
+ tags: feature.tags,
2004
+ geometry: feature.geometry.map(toPairs)
2005
+ };
2006
+ if (feature.id !== void 0) transformed.id = feature.id;
2007
+ return transformed;
1861
2008
  }
1862
- function transformPoint(x, y, extent, z2, tx, ty) {
1863
- return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))];
2009
+ function toPairs(coords) {
2010
+ const pairs = [];
2011
+ for (let i = 0; i < coords.length; i += 2) pairs.push([coords[i], coords[i + 1]]);
2012
+ return pairs;
1864
2013
  }
1865
2014
 
1866
2015
  // src/tile-index.ts
@@ -1926,12 +2075,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1926
2075
  }
1927
2076
  /** {@inheritdoc} */
1928
2077
  getTile(z, x, y) {
1929
- const { extent, debug } = this.options;
2078
+ const { debug } = this.options;
1930
2079
  const z2 = 1 << z;
1931
2080
  if (!this.options.wrap && (x < 0 || x >= z2)) return null;
1932
2081
  x = x + z2 & z2 - 1;
1933
2082
  const id = toID(z, x, y);
1934
- if (this.tiles[id]) return transformTile(this.tiles[id], extent);
2083
+ if (this.tiles[id]) return transformTile(this.tiles[id]);
1935
2084
  if (debug > 1) console.log("drilling down to z%d-%d-%d", z, x, y);
1936
2085
  let z0 = z;
1937
2086
  let x0 = x;
@@ -1951,7 +2100,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1951
2100
  this.splitTile(parent.source, z0, x0, y0, z, x, y);
1952
2101
  if (debug > 1) console.timeEnd("drilling down");
1953
2102
  if (!this.tiles[id]) return null;
1954
- return transformTile(this.tiles[id], extent);
2103
+ return transformTile(this.tiles[id]);
1955
2104
  }
1956
2105
  /**
1957
2106
  * The features the top tile starts from. A world that wraps keeps them all left and right, where wrapping already
@@ -2068,10 +2217,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2068
2217
  let minY = 1e400;
2069
2218
  let maxY = -1e400;
2070
2219
  for (const feature of features) {
2071
- minX = Math.min(minX, feature.minX);
2072
- maxX = Math.max(maxX, feature.maxX);
2073
- minY = Math.min(minY, feature.minY);
2074
- maxY = Math.max(maxY, feature.maxY);
2220
+ minX = Math.min(minX, getMinX(feature));
2221
+ maxX = Math.max(maxX, getMaxX(feature));
2222
+ minY = Math.min(minY, getMinY(feature));
2223
+ maxY = Math.max(maxY, getMaxY(feature));
2075
2224
  }
2076
2225
  const k1 = options.buffer / options.extent;
2077
2226
  const removedLookup = /* @__PURE__ */ new Set();
@@ -2084,7 +2233,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2084
2233
  const tileMaxY = (tile.y + 1 + k1) / z2;
2085
2234
  if (maxX < tileMinX || minX >= tileMaxX || maxY < tileMinY || minY >= tileMaxY) continue;
2086
2235
  let intersects = false;
2087
- for (const feature of features) if (feature.maxX >= tileMinX && feature.minX < tileMaxX && feature.maxY >= tileMinY && feature.minY < tileMaxY) {
2236
+ for (const feature of features) if (getMaxX(feature) >= tileMinX && getMinX(feature) < tileMaxX && getMaxY(feature) >= tileMinY && getMinY(feature) < tileMaxY) {
2088
2237
  intersects = true;
2089
2238
  break;
2090
2239
  }
@@ -2307,7 +2456,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2307
2456
  const left = clip(features, pow2, x - buffer, x + 1 + buffer, 0, -1, 2, options);
2308
2457
  features = clip(left || [], pow2, y - buffer, y + 1 + buffer, 1, -1, 2, options);
2309
2458
  }
2310
- return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
2459
+ return transformTile(createTile(features ?? [], z, x, y, options, Float64Array));
2311
2460
  }
2312
2461
 
2313
2462
  exports.GEOJSONVT_CLIP_END = GEOJSONVT_CLIP_END;