@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
@@ -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,59 +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
- if (!line.length) continue;
282
- const out = { points: [] };
283
- convertLine(line, out, tolerance, false, worldCoordinateHelper);
284
- features.push(createFeature(id, "LineString", out, properties));
285
- }
286
- else {
287
- const out = [];
288
- convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
289
- if (!out.length) return;
290
- features.push(createFeature(id, "MultiLineString", out, properties));
291
- }
292
- }
293
- function convertPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
294
- const out = [];
295
- convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
296
- if (!out.length) return;
297
- features.push(createFeature(id, "Polygon", out, properties));
298
- }
299
- function convertMultiPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
300
- const out = [];
301
- for (const polygon of geom.coordinates) {
302
- const polygonOut = [];
303
- convertLines(polygon, polygonOut, tolerance, true, worldCoordinateHelper);
304
- if (!polygonOut.length) continue;
305
- out.push(polygonOut);
306
- }
307
- if (!out.length) return;
308
- features.push(createFeature(id, "MultiPolygon", out, properties));
309
- }
310
- function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
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];
299
+ }
300
+ features.push(feature);
301
+ }
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
+ }
320
+ }
321
+ features.push(createFeature(id, type, out, properties));
322
+ }
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;
311
338
  let x0, y0;
312
339
  let size = 0;
340
+ let next = first;
313
341
  for (let j = 0; j < ring.length; j++) {
314
342
  const x = worldCoordinateHelper.projectX(ring[j][0]);
315
343
  const y = worldCoordinateHelper.projectY(ring[j][1]);
316
- out.points.push(x, y, 0);
344
+ out[next] = x;
345
+ out[next + 1] = y;
346
+ next += 3;
317
347
  if (j > 0) {
318
348
  if (isPolygon) size += (x0 * y - x * y0) / 2;
319
349
  else size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2));
@@ -321,22 +351,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
321
351
  x0 = x;
322
352
  y0 = y;
323
353
  }
324
- const last = out.points.length - 3;
325
- out.points[2] = 1;
326
- if (tolerance > 0) simplify(out.points, 0, last, tolerance);
327
- out.points[last + 2] = 1;
328
- optimizeLineMemory(out);
329
- out.size = Math.abs(size);
330
- out.start = 0;
331
- out.end = out.size;
332
- }
333
- function convertLines(rings, out, tolerance, isPolygon, worldCoordinateHelper) {
334
- for (let i = 0; i < rings.length; i++) {
335
- if (!rings[i].length) continue;
336
- const geom = { points: [] };
337
- convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
338
- out.push(geom);
339
- }
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;
340
361
  }
341
362
 
342
363
  // src/deconvert.ts
@@ -365,42 +386,64 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
365
386
  * Converts a single internal feature geometry to GeoJSON format.
366
387
  */
367
388
  function geometryToGeoJSON(feature, worldCoordinateHelper) {
368
- const { type, geometry } = feature;
369
- switch (type) {
389
+ switch (feature.type) {
370
390
  case "Point": return {
371
- type,
372
- coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinateHelper)
391
+ type: feature.type,
392
+ coordinates: [worldCoordinateHelper.unprojectX(feature.x), worldCoordinateHelper.unprojectY(feature.y)]
373
393
  };
374
394
  case "MultiPoint": return {
375
- type,
376
- coordinates: unprojectPoints(geometry, worldCoordinateHelper)
395
+ type: feature.type,
396
+ coordinates: unprojectPoints(feature.geometry, feature.worldOffset, 0, feature.geometry.length, worldCoordinateHelper)
377
397
  };
378
398
  case "LineString": return {
379
- type,
380
- coordinates: unprojectPoints(geometry.points, worldCoordinateHelper)
399
+ type: feature.type,
400
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)[0]
381
401
  };
382
402
  case "MultiLineString":
383
403
  case "Polygon": return {
384
- type,
385
- coordinates: geometry.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper))
404
+ type: feature.type,
405
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)
386
406
  };
387
407
  case "MultiPolygon": return {
388
- type,
389
- coordinates: geometry.map((polygon) => polygon.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper)))
408
+ type: feature.type,
409
+ coordinates: unprojectPolygons(feature.geometry, feature.worldOffset, worldCoordinateHelper)
390
410
  };
391
411
  }
392
412
  }
393
- 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) {
394
440
  const result = [];
395
- 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])]);
396
442
  return result;
397
443
  }
398
- function unprojectPoint(x, y, worldCoordinateHelper) {
399
- return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
400
- }
401
444
 
402
445
  // src/clip.ts
403
- /**
446
+ /**
404
447
  * clip features between two vertical or horizontal axis-parallel lines:
405
448
  * | |
406
449
  * ___|___ | /
@@ -422,6 +465,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
422
465
  if (maxAll < start || minAll >= end) return null;
423
466
  const clipped = [];
424
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
+ }
425
473
  const min = axis === 0 ? feature.minX : feature.minY;
426
474
  const max = axis === 0 ? feature.maxX : feature.maxY;
427
475
  if (min >= start && max < end) {
@@ -429,157 +477,238 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
429
477
  continue;
430
478
  }
431
479
  if (max < start || min >= end) continue;
432
- switch (feature.type) {
433
- case "MultiPoint":
434
- clipPointFeature(feature, clipped, start, end, axis);
435
- continue;
436
- case "LineString":
437
- clipLineStringFeature(feature, clipped, start, end, axis, options);
438
- continue;
439
- case "MultiLineString":
440
- clipMultiLineStringFeature(feature, clipped, start, end, axis);
441
- continue;
442
- case "Polygon":
443
- clipPolygonFeature(feature, clipped, start, end, axis);
444
- continue;
445
- case "MultiPolygon":
446
- clipMultiPolygonFeature(feature, clipped, start, end, axis);
447
- continue;
480
+ if (feature.type === "MultiPoint") {
481
+ clipPointsFeature(feature, clipped, start, end, axis);
482
+ continue;
448
483
  }
484
+ clipRingsFeature(feature, clipped, start, end, axis, options);
449
485
  }
450
486
  if (!clipped.length) return null;
451
487
  return clipped;
452
488
  }
453
- function clipPointFeature(feature, clipped, start, end, axis) {
454
- const geom = [];
455
- clipPoints(feature.geometry, geom, start, end, axis);
456
- if (!geom.length) return;
457
- const type = geom.length === 3 ? "Point" : "MultiPoint";
458
- clipped.push(createFeature(feature.id, type, geom, feature.tags));
459
- }
460
- function clipLineStringFeature(feature, clipped, start, end, axis, options) {
461
- const geom = [];
462
- clipLine(feature.geometry, geom, start, end, axis, false, options.lineMetrics);
463
- if (!geom.length) return;
464
- if (options.lineMetrics) {
465
- 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));
466
505
  return;
467
506
  }
468
- if (geom.length > 1) {
469
- 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
+ }
470
559
  return;
471
560
  }
472
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
473
- }
474
- function clipMultiLineStringFeature(feature, clipped, start, end, axis) {
475
- const geom = [];
476
- clipLines(feature.geometry, geom, start, end, axis, false);
477
- if (!geom.length) return;
478
- if (geom.length === 1) {
479
- clipped.push(createFeature(feature.id, "LineString", geom[0], feature.tags));
561
+ if (isPolygon) {
562
+ clipped.push(createFeature(feature.id, feature.type, out, feature.tags));
480
563
  return;
481
564
  }
482
- clipped.push(createFeature(feature.id, "MultiLineString", geom, feature.tags));
483
- }
484
- function clipPolygonFeature(feature, clipped, start, end, axis) {
485
- const geom = [];
486
- clipLines(feature.geometry, geom, start, end, axis, true);
487
- if (!geom.length) return;
488
- clipped.push(createFeature(feature.id, "Polygon", geom, feature.tags));
565
+ const isSingleLine = 2 + out[0] * 3 === out.length;
566
+ clipped.push(createFeature(feature.id, isSingleLine ? "LineString" : "MultiLineString", out, feature.tags));
489
567
  }
490
- function clipMultiPolygonFeature(feature, clipped, start, end, axis) {
491
- const geom = [];
492
- for (const polygon of feature.geometry) {
493
- const newPolygon = [];
494
- clipLines(polygon, newPolygon, start, end, axis, true);
495
- if (!newPolygon.length) continue;
496
- 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
+ }
497
599
  }
498
- if (!geom.length) return;
499
- clipped.push(createFeature(feature.id, "MultiPolygon", geom, feature.tags));
500
- }
501
- function clipPoints(geom, newGeom, start, end, axis) {
502
- for (let i = 0; i < geom.length; i += 3) {
503
- const a = geom[i + axis];
504
- 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++;
505
606
  }
607
+ return total * 3 + parts * 2;
506
608
  }
507
- function clipLine(geom, newGeom, start, end, axis, isPolygon, trackMetrics) {
508
- let slice = newSlice(geom);
509
- const intersect = axis === 0 ? intersectX : intersectY;
510
- 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;
511
623
  let segLen, t;
512
- for (let i = 0; i < geom.points.length - 3; i += 3) {
513
- const ax = geom.points[i];
514
- const ay = geom.points[i + 1];
515
- const az = geom.points[i + 2];
516
- const bx = geom.points[i + 3];
517
- 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];
518
630
  const a = axis === 0 ? ax : ay;
519
631
  const b = axis === 0 ? bx : by;
520
632
  let exited = false;
521
- 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));
522
634
  if (a < start) {
523
635
  if (b >= start) {
524
- t = intersect(slice, ax, ay, bx, by, start);
525
- 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;
526
639
  }
527
640
  } else if (a > end) {
528
641
  if (b <= end) {
529
- t = intersect(slice, ax, ay, bx, by, end);
530
- 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;
531
645
  }
532
- } 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
+ }
533
652
  if (b < start && a >= start) {
534
- t = intersect(slice, ax, ay, bx, by, start);
653
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
654
+ next += 3;
535
655
  exited = true;
536
656
  }
537
657
  if (b > end && a <= end) {
538
- t = intersect(slice, ax, ay, bx, by, end);
658
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
659
+ next += 3;
539
660
  exited = true;
540
661
  }
541
662
  if (!isPolygon && exited) {
542
- if (trackMetrics) slice.end = len + segLen * t;
543
- newGeom.push(slice);
544
- 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;
545
672
  }
546
- if (trackMetrics) len += segLen;
673
+ if (metricsOf) len += segLen;
547
674
  }
548
- let last = geom.points.length - 3;
549
- const ax = geom.points[last];
550
- const ay = geom.points[last + 1];
551
- const az = geom.points[last + 2];
675
+ const last = ringEnd - 3;
676
+ const ax = geom[last] + offset;
677
+ const ay = geom[last + 1];
552
678
  const a = axis === 0 ? ax : ay;
553
- if (a >= start && a <= end) addPoint(slice.points, ax, ay, az);
554
- last = slice.points.length - 3;
555
- 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]);
556
- if (slice.points.length) {
557
- optimizeLineMemory(slice);
558
- newGeom.push(slice);
559
- }
560
- }
561
- function newSlice(line) {
562
- return {
563
- points: [],
564
- size: line.size,
565
- start: line.start,
566
- end: line.end
567
- };
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;
568
695
  }
569
- function clipLines(geom, newGeom, start, end, axis, isPolygon) {
570
- for (const line of geom) clipLine(line, newGeom, start, end, axis, isPolygon, false);
571
- }
572
- function addPoint(out, x, y, z) {
573
- out.push(x, y, z);
574
- }
575
- function intersectX(out, ax, ay, bx, by, x) {
576
- const t = (x - ax) / (bx - ax);
577
- addPoint(out.points, x, ay + (by - ay) * t, 1);
578
- return t;
579
- }
580
- function intersectY(out, ax, ay, bx, by, y) {
581
- const t = (y - ay) / (by - ay);
582
- 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;
583
712
  return t;
584
713
  }
585
714
 
@@ -596,58 +725,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
596
725
  if (right) merged = merged.concat(shiftFeatureCoords(right, -1));
597
726
  return merged;
598
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
+ */
599
732
  function shiftFeatureCoords(features, offset) {
600
733
  const newFeatures = [];
601
- for (const feature of features) switch (feature.type) {
602
- case "Point":
603
- case "MultiPoint": {
604
- const newGeometry = shiftPointCoords(feature.geometry, offset);
605
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
606
- continue;
607
- }
608
- case "LineString": {
609
- const newGeometry = shiftLineCoords(feature.geometry, offset);
610
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
611
- continue;
612
- }
613
- case "MultiLineString":
614
- case "Polygon": {
615
- const newGeometry = [];
616
- for (const line of feature.geometry) newGeometry.push(shiftLineCoords(line, offset));
617
- newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
618
- continue;
619
- }
620
- case "MultiPolygon": {
621
- const newGeometry = [];
622
- for (const polygon of feature.geometry) {
623
- const newPolygon = [];
624
- for (const line of polygon) newPolygon.push(shiftLineCoords(line, offset));
625
- newGeometry.push(newPolygon);
626
- }
627
- 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));
628
737
  continue;
629
738
  }
739
+ newFeatures.push({
740
+ ...feature,
741
+ minX: feature.minX + offset,
742
+ maxX: feature.maxX + offset,
743
+ worldOffset: feature.worldOffset + offset
744
+ });
630
745
  }
631
746
  return newFeatures;
632
747
  }
633
- function shiftPointCoords(coords, offset) {
634
- const newCoords = [];
635
- for (let i = 0; i < coords.length; i += 3) newCoords.push(coords[i] + offset, coords[i + 1], coords[i + 2]);
636
- return newCoords;
637
- }
638
- function shiftLineCoords(line, offset) {
639
- const newLine = {
640
- points: [],
641
- size: line.size
642
- };
643
- if (line.start !== void 0) {
644
- newLine.start = line.start;
645
- newLine.end = line.end;
646
- }
647
- 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]);
648
- optimizeLineMemory(newLine);
649
- return newLine;
650
- }
651
748
 
652
749
  // src/difference.ts
653
750
  /**
@@ -1163,11 +1260,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1163
1260
  const positions = point.geometry.type === "MultiPoint" ? point.geometry.coordinates : [point.geometry.coordinates];
1164
1261
  for (const [lng, lat] of positions) {
1165
1262
  const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
1166
- if (!this.options.wrap && !isInWorld([x, y])) continue;
1263
+ if (!this.options.wrap && !isInWorld(x, y)) continue;
1167
1264
  const feature = {
1168
1265
  id: point.id,
1169
1266
  type: "Point",
1170
- geometry: [x, y],
1267
+ x,
1268
+ y,
1171
1269
  tags: point.properties
1172
1270
  };
1173
1271
  features.push(feature);
@@ -1185,18 +1283,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1185
1283
  const points = [];
1186
1284
  for (const feature of features) {
1187
1285
  if (feature.type === "Point") {
1188
- if (!this.options.wrap && !isInWorld(feature.geometry)) continue;
1286
+ if (!this.isClustered(feature.x, feature.y)) continue;
1189
1287
  points.push(feature);
1190
1288
  continue;
1191
1289
  }
1192
1290
  if (feature.type !== "MultiPoint") continue;
1193
1291
  for (let i = 0; i < feature.geometry.length; i += 3) {
1194
- const geometry = [feature.geometry[i], feature.geometry[i + 1]];
1195
- 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;
1196
1295
  points.push({
1197
1296
  id: feature.id,
1198
1297
  type: "Point",
1199
- geometry,
1298
+ x,
1299
+ y,
1200
1300
  tags: feature.tags
1201
1301
  });
1202
1302
  }
@@ -1204,6 +1304,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1204
1304
  this.createIndex(points);
1205
1305
  }
1206
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
+ /**
1207
1315
  * @internal
1208
1316
  * Updates the cluster data by rebuilding.
1209
1317
  * @param features
@@ -1236,9 +1344,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1236
1344
  let numItems = 0;
1237
1345
  for (let i = 0; i < points.length; i++) {
1238
1346
  const p = points[i];
1239
- if (!p?.geometry || !fitsInteger(p.geometry[0]) || !fitsInteger(p.geometry[1])) continue;
1240
- coords[numItems * 2] = Math.round(encode(p.geometry[0]));
1241
- 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));
1242
1350
  const k = numItems * stride;
1243
1351
  data[k + OFFSET_ZOOM] = notProcessed;
1244
1352
  data[k + OFFSET_ID] = i;
@@ -1415,14 +1523,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1415
1523
  const p = radius / extent;
1416
1524
  const top = encode((y - p) / z2);
1417
1525
  const bottom = encode((y + 1 + p) / z2);
1418
- const tile = {
1419
- transformed: true,
1420
- features: [],
1421
- source: null,
1422
- x,
1423
- y,
1424
- z
1425
- };
1526
+ const tile = { features: [] };
1426
1527
  this.addTileFeatures(tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom), tree, x, y, z2, tile);
1427
1528
  if (this.options.wrap && x === 0) this.addTileFeatures(tree.range(encode(1 - p / z2), top, encode(1), bottom), tree, z2, y, z2, tile);
1428
1529
  if (this.options.wrap && x === z2 - 1) this.addTileFeatures(tree.range(encode(0), top, encode(p / z2), bottom), tree, -1, y, z2, tile);
@@ -1482,7 +1583,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1482
1583
  } else {
1483
1584
  const p = this.points[data[k + OFFSET_ID]];
1484
1585
  tags = p.tags;
1485
- [px, py] = p.geometry;
1586
+ px = p.x;
1587
+ py = p.y;
1486
1588
  }
1487
1589
  const f = {
1488
1590
  type: 1,
@@ -1605,7 +1707,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1605
1707
  id: data[i + OFFSET_ID],
1606
1708
  type: "Point",
1607
1709
  tags: getClusterProperties(data, i, clusterProps),
1608
- geometry: [decode(coords[index * 2]), decode(coords[index * 2 + 1])]
1710
+ x: decode(coords[index * 2]),
1711
+ y: decode(coords[index * 2 + 1])
1609
1712
  };
1610
1713
  }
1611
1714
  function getClusterGeoJSON(coords, index, data, i, clusterProps, worldCoordinateHelper) {
@@ -1631,26 +1734,32 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1631
1734
  point_count_abbreviated: abbrev
1632
1735
  });
1633
1736
  }
1634
- function isInWorld(geometry) {
1635
- 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;
1636
1739
  }
1637
1740
 
1638
1741
  // src/tile.ts
1639
1742
  const GEOJSONVT_CLIP_START = "geojsonvt_clip_start";
1640
1743
  const GEOJSONVT_CLIP_END = "geojsonvt_clip_end";
1641
1744
  /**
1642
- * 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
1643
1752
  * @param features - the features to include in the tile
1644
1753
  * @param z
1645
1754
  * @param tx
1646
1755
  * @param ty
1647
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
1648
1758
  * @returns the created tile
1649
1759
  */
1650
- function createTile(features, z, tx, ty, options) {
1760
+ function createTile(features, z, tx, ty, options, CoordArray = getTileCoordsConstructor(options)) {
1651
1761
  const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
1652
1762
  const tile = {
1653
- transformed: false,
1654
1763
  features: [],
1655
1764
  source: null,
1656
1765
  x: tx,
@@ -1664,39 +1773,53 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1664
1773
  numSimplified: 0,
1665
1774
  numFeatures: features.length
1666
1775
  };
1667
- 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);
1668
1784
  return tile;
1669
1785
  }
1670
- 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
+ }
1671
1791
  tile.minX = Math.min(tile.minX, feature.minX);
1672
1792
  tile.minY = Math.min(tile.minY, feature.minY);
1673
1793
  tile.maxX = Math.max(tile.maxX, feature.maxX);
1674
1794
  tile.maxY = Math.max(tile.maxY, feature.maxY);
1675
- switch (feature.type) {
1676
- case "Point":
1677
- case "MultiPoint":
1678
- addPointsTileFeature(tile, feature);
1679
- return;
1680
- case "LineString":
1681
- addLineTileFeautre(tile, feature, tolerance, options);
1682
- return;
1683
- case "MultiLineString":
1684
- case "Polygon":
1685
- addLinesTileFeature(tile, feature, tolerance);
1686
- return;
1687
- case "MultiPolygon":
1688
- addMultiPolygonTileFeature(tile, feature, tolerance);
1689
- return;
1795
+ if (feature.type === "MultiPoint") {
1796
+ addPointsTileFeature(tile, feature, frame);
1797
+ return;
1690
1798
  }
1799
+ addRingsTileFeature(tile, feature, tolerance, options, frame);
1691
1800
  }
1692
- function addPointsTileFeature(tile, feature) {
1693
- const geometry = [];
1694
- for (let i = 0; i < feature.geometry.length; i += 3) {
1695
- geometry.push(feature.geometry[i], feature.geometry[i + 1]);
1696
- tile.numPoints++;
1697
- tile.numSimplified++;
1698
- }
1699
- 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;
1700
1823
  const tileFeature = {
1701
1824
  type: 1,
1702
1825
  tags: feature.tags || null,
@@ -1705,166 +1828,188 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1705
1828
  if (feature.id !== null) tileFeature.id = feature.id;
1706
1829
  tile.features.push(tileFeature);
1707
1830
  }
1708
- 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;
1709
1834
  const geometry = [];
1710
- 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
+ }
1711
1840
  if (!geometry.length) return;
1712
1841
  let tags = feature.tags || null;
1713
- if (options.lineMetrics) {
1842
+ if (feature.type === "LineString" && options.lineMetrics) {
1714
1843
  tags = {};
1715
1844
  for (const key in feature.tags) tags[key] = feature.tags[key];
1716
- tags[GEOJSONVT_CLIP_START] = feature.geometry.start / feature.geometry.size;
1717
- 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;
1718
1847
  }
1719
1848
  const tileFeature = {
1720
- type: 2,
1849
+ type: isPolygon ? 3 : 2,
1721
1850
  tags,
1722
1851
  geometry
1723
1852
  };
1724
1853
  if (feature.id !== null) tileFeature.id = feature.id;
1725
1854
  tile.features.push(tileFeature);
1726
1855
  }
1727
- function addLinesTileFeature(tile, feature, tolerance) {
1728
- const geometry = [];
1729
- for (let i = 0; i < feature.geometry.length; i++) addLine(geometry, feature.geometry[i], tile, tolerance, feature.type === "Polygon", i === 0);
1730
- if (!geometry.length) return;
1731
- const tileFeature = {
1732
- type: feature.type === "Polygon" ? 3 : 2,
1733
- tags: feature.tags || null,
1734
- geometry
1735
- };
1736
- if (feature.id !== null) tileFeature.id = feature.id;
1737
- 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);
1738
1883
  }
1739
- function addMultiPolygonTileFeature(tile, feature, tolerance) {
1740
- const geometry = [];
1741
- for (let k = 0; k < feature.geometry.length; k++) {
1742
- const polygon = feature.geometry[k];
1743
- 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;
1744
1901
  }
1745
- if (!geometry.length) return;
1746
- const tileFeature = {
1747
- type: 3,
1748
- tags: feature.tags || null,
1749
- geometry
1750
- };
1751
- if (feature.id !== null) tileFeature.id = feature.id;
1752
- tile.features.push(tileFeature);
1902
+ return ring;
1753
1903
  }
1754
- 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) {
1755
1911
  const sqTolerance = tolerance * tolerance;
1756
- if (tolerance > 0 && geom.size < (isPolygon ? sqTolerance : tolerance)) {
1757
- tile.numPoints += geom.points.length / 3;
1758
- return;
1759
- }
1760
- const ring = [];
1761
- for (let i = 0; i < geom.points.length; i += 3) {
1762
- if (tolerance === 0 || geom.points[i + 2] > sqTolerance) {
1763
- tile.numSimplified++;
1764
- 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;
1765
1938
  }
1766
- tile.numPoints++;
1939
+ next += 2;
1767
1940
  }
1768
- if (isPolygon) rewind(ring, isOuter);
1769
- result.push(ring);
1941
+ return sourceArea * roundedArea < 0;
1770
1942
  }
1771
- function rewind(ring, clockwise) {
1772
- let area = 0;
1773
- 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]);
1774
- if (area > 0 !== clockwise) return;
1775
- for (let i = 0, len = ring.length; i < len / 2; i += 2) {
1776
- const x = ring[i];
1777
- const y = ring[i + 1];
1778
- ring[i] = ring[len - 2 - i];
1779
- ring[i + 1] = ring[len - 1 - i];
1780
- ring[len - 2 - i] = x;
1781
- 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;
1782
1951
  }
1783
1952
  }
1784
-
1785
- // src/transform.ts
1786
- /**
1787
- * Transforms the coordinates of each feature in the given tile from
1788
- * mercator-projected space into (extent x extent) tile space.
1789
- * @param tile - the tile to transform, this gets modified in place
1790
- * @param extent - the tile extent (usually 4096)
1791
- * @returns the transformed tile
1792
- */
1793
- function transformTile(tile, extent) {
1794
- if (tile.transformed) return tile;
1795
- const z2 = 1 << tile.z;
1796
- const tx = tile.x;
1797
- const ty = tile.y;
1798
- for (const feature of tile.features) if (feature.type === 1) transformPointFeature(feature, extent, z2, tx, ty);
1799
- else transformNonPointFeature(feature, extent, z2, tx, ty);
1800
- tile.transformed = true;
1801
- return tile;
1802
- }
1803
1953
  /**
1804
- * 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.
1805
1955
  */
1806
- function transformPointFeature(feature, extent, z2, tx, ty) {
1807
- const transformed = feature;
1808
- const geometry = feature.geometry;
1809
- const point = [];
1810
- for (let i = 0; i < geometry.length; i += 2) point.push(transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty));
1811
- transformed.geometry = point;
1812
- return transformed;
1956
+ function project(position, t, frame) {
1957
+ return Math.round(frame.extent * (position * frame.z2 - t));
1813
1958
  }
1814
1959
  /**
1815
- * 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`
1816
1963
  */
1817
- function transformNonPointFeature(feature, extent, z2, tx, ty) {
1818
- const transformed = feature;
1819
- const geometry = feature.geometry;
1820
- const nonPoint = [];
1821
- for (const geom of geometry) if (feature.type === 3) nonPoint.push(transformPolygonRing(geom, extent, z2, tx, ty));
1822
- else {
1823
- const line = [];
1824
- for (let i = 0; i < geom.length; i += 2) line.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
1825
- nonPoint.push(line);
1826
- }
1827
- transformed.geometry = nonPoint;
1828
- 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;
1829
1975
  }
1830
- /**
1831
- * Quantizes a polygon ring while preserving its winding so a hole does not become
1832
- * a shell, or vice versa. Signed areas use coordinates relative to the first
1833
- * vertex to reduce cancellation for narrow rings far from the origin.
1834
- * Zero-area results are left unchanged; other quantization topology changes
1835
- * 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
1836
1983
  */
1837
- function transformPolygonRing(geometry, extent, z2, tx, ty) {
1838
- if (!geometry.length) return [];
1839
- const originX = geometry[0];
1840
- const originY = geometry[1];
1841
- const origin = transformPoint(originX, originY, extent, z2, tx, ty);
1842
- const ring = [origin];
1843
- let previousX = 0;
1844
- let previousY = 0;
1845
- let previousRoundedX = 0;
1846
- let previousRoundedY = 0;
1847
- let sourceArea = 0;
1848
- let roundedArea = 0;
1849
- for (let i = 2; i < geometry.length; i += 2) {
1850
- const point = transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty);
1851
- const x = geometry[i] - originX;
1852
- const y = geometry[i + 1] - originY;
1853
- const roundedX = point[0] - origin[0];
1854
- const roundedY = point[1] - origin[1];
1855
- sourceArea += previousX * y - x * previousY;
1856
- roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1857
- previousX = x;
1858
- previousY = y;
1859
- previousRoundedX = roundedX;
1860
- previousRoundedY = roundedY;
1861
- ring.push(point);
1862
- }
1863
- if (sourceArea * roundedArea < 0) ring.reverse();
1864
- 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;
1865
2008
  }
1866
- function transformPoint(x, y, extent, z2, tx, ty) {
1867
- 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;
1868
2013
  }
1869
2014
 
1870
2015
  // src/tile-index.ts
@@ -1930,12 +2075,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1930
2075
  }
1931
2076
  /** {@inheritdoc} */
1932
2077
  getTile(z, x, y) {
1933
- const { extent, debug } = this.options;
2078
+ const { debug } = this.options;
1934
2079
  const z2 = 1 << z;
1935
2080
  if (!this.options.wrap && (x < 0 || x >= z2)) return null;
1936
2081
  x = x + z2 & z2 - 1;
1937
2082
  const id = toID(z, x, y);
1938
- if (this.tiles[id]) return transformTile(this.tiles[id], extent);
2083
+ if (this.tiles[id]) return transformTile(this.tiles[id]);
1939
2084
  if (debug > 1) console.log("drilling down to z%d-%d-%d", z, x, y);
1940
2085
  let z0 = z;
1941
2086
  let x0 = x;
@@ -1955,7 +2100,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1955
2100
  this.splitTile(parent.source, z0, x0, y0, z, x, y);
1956
2101
  if (debug > 1) console.timeEnd("drilling down");
1957
2102
  if (!this.tiles[id]) return null;
1958
- return transformTile(this.tiles[id], extent);
2103
+ return transformTile(this.tiles[id]);
1959
2104
  }
1960
2105
  /**
1961
2106
  * The features the top tile starts from. A world that wraps keeps them all left and right, where wrapping already
@@ -2072,10 +2217,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2072
2217
  let minY = 1e400;
2073
2218
  let maxY = -1e400;
2074
2219
  for (const feature of features) {
2075
- minX = Math.min(minX, feature.minX);
2076
- maxX = Math.max(maxX, feature.maxX);
2077
- minY = Math.min(minY, feature.minY);
2078
- 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));
2079
2224
  }
2080
2225
  const k1 = options.buffer / options.extent;
2081
2226
  const removedLookup = /* @__PURE__ */ new Set();
@@ -2088,7 +2233,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2088
2233
  const tileMaxY = (tile.y + 1 + k1) / z2;
2089
2234
  if (maxX < tileMinX || minX >= tileMaxX || maxY < tileMinY || minY >= tileMaxY) continue;
2090
2235
  let intersects = false;
2091
- 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) {
2092
2237
  intersects = true;
2093
2238
  break;
2094
2239
  }
@@ -2311,7 +2456,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2311
2456
  const left = clip(features, pow2, x - buffer, x + 1 + buffer, 0, -1, 2, options);
2312
2457
  features = clip(left || [], pow2, y - buffer, y + 1 + buffer, 1, -1, 2, options);
2313
2458
  }
2314
- return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
2459
+ return transformTile(createTile(features ?? [], z, x, y, options, Float64Array));
2315
2460
  }
2316
2461
 
2317
2462
  exports.GEOJSONVT_CLIP_END = GEOJSONVT_CLIP_END;