@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
package/src/clip.ts CHANGED
@@ -1,13 +1,15 @@
1
1
 
2
- import {createFeature, optimizeLineMemory} from './feature';
3
- import type { GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, SliceArray, SliceFixedArray } from './definitions';
2
+ import {createFeature, createPointFeature, isHole} from './feature';
3
+ import type { GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, GeoJSONVTSourceCoords } from './definitions';
4
4
 
5
5
  export const enum AxisType {
6
6
  X = 0,
7
7
  Y = 1
8
8
  }
9
9
 
10
- /**
10
+ type RingsFeature = GeoJSONVTInternalLineStringFeature | GeoJSONVTInternalMultiLineStringFeature | GeoJSONVTInternalPolygonFeature | GeoJSONVTInternalMultiPolygonFeature;
11
+
12
+ /**
11
13
  * clip features between two vertical or horizontal axis-parallel lines:
12
14
  * | |
13
15
  * ___|___ | /
@@ -37,6 +39,12 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
37
39
  const clipped: GeoJSONVTInternalFeature[] = [];
38
40
 
39
41
  for (const feature of features) {
42
+ if (feature.type === 'Point') {
43
+ const a = axis === AxisType.X ? feature.x : feature.y;
44
+ if (a >= start && a < end) clipped.push(feature);
45
+ continue;
46
+ }
47
+
40
48
  const min = axis === AxisType.X ? feature.minX : feature.minY;
41
49
  const max = axis === AxisType.X ? feature.maxX : feature.maxY;
42
50
 
@@ -49,32 +57,12 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
49
57
  continue;
50
58
  }
51
59
 
52
- switch (feature.type) {
53
- case 'MultiPoint': {
54
- clipPointFeature(feature, clipped, start, end, axis);
55
- continue;
56
- }
57
-
58
- case 'LineString': {
59
- clipLineStringFeature(feature, clipped, start, end, axis, options);
60
- continue;
61
- }
62
-
63
- case 'MultiLineString': {
64
- clipMultiLineStringFeature(feature, clipped, start, end, axis);
65
- continue;
66
- }
67
-
68
- case 'Polygon': {
69
- clipPolygonFeature(feature, clipped, start, end, axis);
70
- continue;
71
- }
72
-
73
- case 'MultiPolygon': {
74
- clipMultiPolygonFeature(feature, clipped, start, end, axis);
75
- continue;
76
- }
60
+ if (feature.type === 'MultiPoint') {
61
+ clipPointsFeature(feature, clipped, start, end, axis);
62
+ continue;
77
63
  }
64
+
65
+ clipRingsFeature(feature, clipped, start, end, axis, options);
78
66
  }
79
67
 
80
68
  if (!clipped.length) return null;
@@ -82,190 +70,284 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
82
70
  return clipped;
83
71
  }
84
72
 
85
- function clipPointFeature(feature: GeoJSONVTInternalPointFeature | GeoJSONVTInternalMultiPointFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType) {
86
- const geom: number[] = [];
87
-
88
- clipPoints(feature.geometry, geom, start, end, axis);
89
- if (!geom.length) return;
90
-
91
- const type = geom.length === 3 ? 'Point' : 'MultiPoint';
92
- clipped.push(createFeature(feature.id, type, geom, feature.tags));
93
- }
94
-
95
- function clipLineStringFeature(feature: GeoJSONVTInternalLineStringFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType, options: GeoJSONVTOptions) {
96
- const geom: SliceArray[] = [];
73
+ function clipPointsFeature(feature: GeoJSONVTInternalMultiPointFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType) {
74
+ const geom = feature.geometry;
75
+ const offset = feature.worldOffset;
76
+ const axisOffset = axis === AxisType.X ? offset : 0;
97
77
 
98
- clipLine(feature.geometry, geom, start, end, axis, false, options.lineMetrics);
99
- if (!geom.length) return;
100
-
101
- if (options.lineMetrics) {
102
- for (const line of geom) {
103
- clipped.push(createFeature(feature.id, 'LineString', line, feature.tags));
78
+ let count = 0;
79
+ let lastInside = 0;
80
+ for (let i = 0; i < geom.length; i += 3) {
81
+ const a = geom[i + axis] + axisOffset;
82
+ if (a >= start && a <= end) {
83
+ count++;
84
+ lastInside = i;
104
85
  }
105
- return;
106
86
  }
87
+ if (!count) return;
107
88
 
108
- if (geom.length > 1) {
109
- clipped.push(createFeature(feature.id, 'MultiLineString', geom, feature.tags));
89
+ if (count === 1) {
90
+ clipped.push(createPointFeature(feature.id, geom[lastInside] + offset, geom[lastInside + 1], feature.tags));
110
91
  return;
111
92
  }
112
93
 
113
- clipped.push(createFeature(feature.id, 'LineString', geom[0], feature.tags));
114
- }
94
+ const out = new Float64Array(count * 3);
95
+ let next = 0;
96
+ for (let i = 0; i < geom.length; i += 3) {
97
+ const a = geom[i + axis] + axisOffset;
98
+ if (a >= start && a <= end) {
99
+ out[next] = geom[i] + offset;
100
+ out[next + 1] = geom[i + 1];
101
+ out[next + 2] = geom[i + 2];
102
+ next += 3;
103
+ }
104
+ }
115
105
 
116
- function clipMultiLineStringFeature(feature: GeoJSONVTInternalMultiLineStringFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType) {
117
- const geom: SliceArray[] = [];
106
+ clipped.push(createFeature(feature.id, 'MultiPoint', out, feature.tags));
107
+ }
118
108
 
119
- clipLines(feature.geometry, geom, start, end, axis, false);
120
- if (!geom.length) return;
109
+ function clipRingsFeature(feature: RingsFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType, options: GeoJSONVTOptions) {
110
+ const geom = feature.geometry;
111
+ const offset = feature.worldOffset;
112
+ const isPolygon = feature.type === 'Polygon' || feature.type === 'MultiPolygon';
113
+
114
+ // count first, so that all clipped rings go into one array of the right size
115
+ let length = 0;
116
+ for (let i = 0; i < geom.length;) {
117
+ const ringEnd = i + 2 + geom[i] * 3;
118
+ length += countClippedRing(geom, offset, i + 2, ringEnd, start, end, axis, isPolygon);
119
+ i = ringEnd;
120
+ }
121
+ if (!length) return;
122
+
123
+ const metricsOf = options.lineMetrics && feature.type === 'LineString' ? feature : null;
124
+ const metrics: number[] = [];
125
+ let out = new Float64Array(length);
126
+ let next = 0;
127
+ let hasOuter = false;
128
+ for (let i = 0; i < geom.length;) {
129
+ const ringEnd = i + 2 + geom[i] * 3;
130
+ let size = geom[i + 1];
131
+ if (isPolygon) {
132
+ // the first ring that is left of a polygon is its outer ring, also when that was a hole
133
+ if (!isHole(size)) hasOuter = false;
134
+ if (!hasOuter) size = Math.abs(size);
135
+ }
136
+ const ringStart = next;
137
+ next = clipRing(geom, offset, i + 2, ringEnd, size, out, next, start, end, axis, isPolygon, metricsOf, metrics);
138
+ if (next > ringStart) hasOuter = true;
139
+ i = ringEnd;
140
+ }
141
+ // a polygon ring that crosses a clip line has room for a position that closes it, which it does not always need
142
+ if (next < length) out = out.subarray(0, next);
143
+
144
+ if (metricsOf) {
145
+ // each part of the line becomes a feature, to be able to track its metrics
146
+ for (let i = 0, part = 0; i < out.length; part += 2) {
147
+ const ringEnd = i + 2 + out[i] * 3;
148
+ const line = createFeature(feature.id, 'LineString', i === 0 && ringEnd === out.length ? out : out.slice(i, ringEnd), feature.tags);
149
+ line.start = metrics[part];
150
+ line.end = metrics[part + 1];
151
+ line.size = metricsOf.size;
152
+ clipped.push(line);
153
+ i = ringEnd;
154
+ }
155
+ return;
156
+ }
121
157
 
122
- if (geom.length === 1) {
123
- clipped.push(createFeature(feature.id, 'LineString', geom[0], feature.tags));
158
+ if (isPolygon) {
159
+ clipped.push(createFeature(feature.id, feature.type, out, feature.tags));
124
160
  return;
125
161
  }
126
162
 
127
- clipped.push(createFeature(feature.id,'MultiLineString', geom, feature.tags));
163
+ const isSingleLine = 2 + out[0] * 3 === out.length;
164
+ clipped.push(createFeature(feature.id, isSingleLine ? 'LineString' : 'MultiLineString', out, feature.tags));
128
165
  }
129
166
 
130
- function clipPolygonFeature(feature: GeoJSONVTInternalPolygonFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType) {
131
- const geom: SliceArray[] = [];
132
-
133
- clipLines(feature.geometry, geom, start, end, axis, true);
134
- if (!geom.length) return;
135
-
136
- clipped.push(createFeature(feature.id, 'Polygon', geom, feature.tags));
137
- }
167
+ /**
168
+ * How many values `clipRing` writes for a ring, without working out the intersections.
169
+ * @param offset - what to add to every x of `geom`
170
+ */
171
+ function countClippedRing(geom: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, start: number, end: number, axis: AxisType, isPolygon: boolean): number {
172
+ const axisOffset = axis === AxisType.X ? offset : 0;
173
+ let parts = 0;
174
+ let total = 0;
175
+ let count = 0;
176
+
177
+ for (let i = first; i < ringEnd - 3; i += 3) {
178
+ const a = geom[i + axis] + axisOffset;
179
+ const b = geom[i + 3 + axis] + axisOffset;
180
+ let exited = false;
138
181
 
139
- function clipMultiPolygonFeature(feature: GeoJSONVTInternalMultiPolygonFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType) {
140
- const geom: SliceArray[][] = [];
182
+ if (a < start) {
183
+ if (b >= start) {
184
+ count++;
185
+ }
186
+ } else if (a > end) {
187
+ if (b <= end) {
188
+ count++;
189
+ }
190
+ } else {
191
+ count++;
192
+ }
141
193
 
142
- for (const polygon of feature.geometry) {
143
- const newPolygon: SliceArray[] = [];
194
+ if (b < start && a >= start) {
195
+ count++;
196
+ exited = true;
197
+ }
144
198
 
145
- clipLines(polygon, newPolygon, start, end, axis, true);
146
- if (!newPolygon.length) continue;
199
+ if (b > end && a <= end) {
200
+ count++;
201
+ exited = true;
202
+ }
147
203
 
148
- geom.push(newPolygon);
204
+ if (!isPolygon && exited) {
205
+ total += count;
206
+ parts++;
207
+ count = 0;
208
+ }
149
209
  }
150
- if (!geom.length) return;
151
210
 
152
- clipped.push(createFeature(feature.id, 'MultiPolygon', geom, feature.tags));
153
- }
211
+ const last = ringEnd - 3;
212
+ const a = geom[last + axis] + axisOffset;
213
+ if (a >= start && a <= end) count++;
154
214
 
155
- function clipPoints(geom: number[], newGeom: number[], start: number, end: number, axis: AxisType) {
156
- for (let i = 0; i < geom.length; i += 3) {
157
- const a = geom[i + axis];
215
+ // a polygon ring gets a position that closes it when its ends differ, which only shows once it is written
216
+ if (isPolygon && count >= 2) count++;
158
217
 
159
- if (a >= start && a <= end) {
160
- addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
161
- }
218
+ if (count > 0) {
219
+ total += count;
220
+ parts++;
162
221
  }
163
- }
164
222
 
165
- function clipLine(geom: SliceFixedArray, newGeom: SliceArray[], start: number, end: number, axis: AxisType, isPolygon: boolean, trackMetrics: boolean) {
223
+ return total * 3 + parts * 2;
224
+ }
166
225
 
167
- let slice = newSlice(geom);
168
- const intersect = axis === AxisType.X ? intersectX : intersectY;
169
- let len = geom.start;
226
+ /**
227
+ * Clips a ring and writes what is left of it to `out`, each part with the two values before it,
228
+ * see {@link GeoJSONVTSourceCoords}. A polygon ring stays one ring, and a line can come out in several parts.
229
+ * @param offset - what to add to every x of `geom`, which `out` has added already
230
+ * @param metricsOf - the line whose metrics to track, which adds where each part starts and ends to `metrics`
231
+ * @returns where the next ring starts in `out`
232
+ */
233
+ function clipRing(geom: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, size: number, out: GeoJSONVTSourceCoords, next: number, start: number, end: number, axis: AxisType, isPolygon: boolean, metricsOf: GeoJSONVTInternalLineStringFeature | null, metrics: number[]): number {
234
+ let header = next;
235
+ out[next + 1] = size;
236
+ next += 2;
237
+ let partStart = next;
238
+
239
+ let len = metricsOf ? metricsOf.start : 0;
240
+ let metricStart = len;
170
241
  let segLen, t;
171
242
 
172
- for (let i = 0; i < geom.points.length - 3; i += 3) {
173
- const ax = geom.points[i];
174
- const ay = geom.points[i + 1];
175
- const az = geom.points[i + 2];
176
- const bx = geom.points[i + 3];
177
- const by = geom.points[i + 4];
243
+ for (let i = first; i < ringEnd - 3; i += 3) {
244
+ const ax = geom[i] + offset;
245
+ const ay = geom[i + 1];
246
+ const az = geom[i + 2];
247
+ const bx = geom[i + 3] + offset;
248
+ const by = geom[i + 4];
178
249
  const a = axis === AxisType.X ? ax : ay;
179
250
  const b = axis === AxisType.X ? bx : by;
180
251
  let exited = false;
181
252
 
182
- if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
253
+ if (metricsOf) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
183
254
 
184
255
  if (a < start) {
185
256
  // ---|--> | (line enters the clip region from the left)
186
257
  if (b >= start) {
187
- t = intersect(slice, ax, ay, bx, by, start);
188
- if (trackMetrics) slice.start = len + segLen * t;
258
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
259
+ next += 3;
260
+ if (metricsOf) metricStart = len + segLen * t;
189
261
  }
190
262
  } else if (a > end) {
191
263
  // | <--|--- (line enters the clip region from the right)
192
264
  if (b <= end) {
193
- t = intersect(slice, ax, ay, bx, by, end);
194
- if (trackMetrics) slice.start = len + segLen * t;
265
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
266
+ next += 3;
267
+ if (metricsOf) metricStart = len + segLen * t;
195
268
  }
196
269
  } else {
197
- addPoint(slice.points, ax, ay, az);
270
+ out[next] = ax;
271
+ out[next + 1] = ay;
272
+ out[next + 2] = az;
273
+ next += 3;
198
274
  }
199
275
 
200
276
  if (b < start && a >= start) {
201
277
  // <--|--- | or <--|-----|--- (line exits the clip region on the left)
202
- t = intersect(slice, ax, ay, bx, by, start);
278
+ t = intersect(out, next, ax, ay, bx, by, start, axis);
279
+ next += 3;
203
280
  exited = true;
204
281
  }
205
282
 
206
283
  if (b > end && a <= end) {
207
284
  // | ---|--> or ---|-----|--> (line exits the clip region on the right)
208
- t = intersect(slice, ax, ay, bx, by, end);
285
+ t = intersect(out, next, ax, ay, bx, by, end, axis);
286
+ next += 3;
209
287
  exited = true;
210
288
  }
211
289
 
212
290
  if (!isPolygon && exited) {
213
- if (trackMetrics) slice.end = len + segLen * t;
214
- newGeom.push(slice);
215
- slice = newSlice(geom);
291
+ // the part ends here, and the next one starts
292
+ out[header] = (next - partStart) / 3;
293
+ if (metricsOf) {
294
+ metrics.push(metricStart, len + segLen * t);
295
+ metricStart = metricsOf.start;
296
+ }
297
+ header = next;
298
+ out[next + 1] = size;
299
+ next += 2;
300
+ partStart = next;
216
301
  }
217
302
 
218
- if (trackMetrics) len += segLen;
303
+ if (metricsOf) len += segLen;
219
304
  }
220
305
 
221
306
  // add the last point
222
- let last = geom.points.length - 3;
223
- const ax = geom.points[last];
224
- const ay = geom.points[last + 1];
225
- const az = geom.points[last + 2];
307
+ const last = ringEnd - 3;
308
+ const ax = geom[last] + offset;
309
+ const ay = geom[last + 1];
226
310
  const a = axis === AxisType.X ? ax : ay;
227
- if (a >= start && a <= end) addPoint(slice.points, ax, ay, az);
228
-
229
- // close the polygon if its endpoints are not the same after clipping
230
- last = slice.points.length - 3;
231
- if (isPolygon && last >= 3 && (slice.points[last] !== slice.points[0] || slice.points[last + 1] !== slice.points[1])) {
232
- addPoint(slice.points, slice.points[0], slice.points[1], slice.points[2]);
311
+ if (a >= start && a <= end) {
312
+ out[next] = ax;
313
+ out[next + 1] = ay;
314
+ out[next + 2] = geom[last + 2];
315
+ next += 3;
233
316
  }
234
317
 
235
- // add the final slice
236
- if (slice.points.length) {
237
- optimizeLineMemory(slice);
238
- newGeom.push(slice);
318
+ // close the polygon if its endpoints are not the same after clipping
319
+ if (isPolygon && next - partStart >= 6 && (out[next - 3] !== out[partStart] || out[next - 2] !== out[partStart + 1])) {
320
+ out[next] = out[partStart];
321
+ out[next + 1] = out[partStart + 1];
322
+ out[next + 2] = out[partStart + 2];
323
+ next += 3;
239
324
  }
240
- }
241
325
 
242
- function newSlice(line: SliceFixedArray): SliceArray {
243
- return {
244
- points: [],
245
- size: line.size,
246
- start: line.start,
247
- end: line.end
248
- };
249
- }
326
+ // add the final part, or give back the two values before it when it is empty
327
+ if (next === partStart) return header;
250
328
 
251
- function clipLines(geom: SliceFixedArray[], newGeom: SliceArray[], start: number, end: number, axis: AxisType, isPolygon: boolean) {
252
- for (const line of geom) {
253
- clipLine(line, newGeom, start, end, axis, isPolygon, false);
254
- }
255
- }
329
+ out[header] = (next - partStart) / 3;
330
+ if (metricsOf) metrics.push(metricStart, metricsOf.end);
256
331
 
257
- function addPoint(out: number[], x: number, y: number, z: number) {
258
- out.push(x, y, z);
332
+ return next;
259
333
  }
260
334
 
261
- function intersectX(out: SliceArray, ax: number, ay: number, bx: number, by: number, x: number) {
262
- const t = (x - ax) / (bx - ax);
263
- addPoint(out.points, x, ay + (by - ay) * t, 1);
264
- return t;
265
- }
335
+ /**
336
+ * Writes the position where a segment crosses the clip line at `k`.
337
+ * @returns how far along the segment that is, from 0 to 1
338
+ */
339
+ function intersect(out: GeoJSONVTSourceCoords, next: number, ax: number, ay: number, bx: number, by: number, k: number, axis: AxisType): number {
340
+ let t;
341
+ if (axis === AxisType.X) {
342
+ t = (k - ax) / (bx - ax);
343
+ out[next] = k;
344
+ out[next + 1] = ay + (by - ay) * t;
345
+ } else {
346
+ t = (k - ay) / (by - ay);
347
+ out[next] = ax + (bx - ax) * t;
348
+ out[next + 1] = k;
349
+ }
350
+ out[next + 2] = 1;
266
351
 
267
- function intersectY(out: SliceArray, ax: number, ay: number, bx: number, by: number, y: number) {
268
- const t = (y - ay) / (by - ay);
269
- addPoint(out.points, ax + (bx - ax) * t, y, 1);
270
352
  return t;
271
353
  }
@@ -83,11 +83,13 @@ const OFFSET_PROP = 4;
83
83
  export class ClusterTileIndex implements GeoJSONVTTileIndex {
84
84
  options: Required<SuperclusterOptions>;
85
85
  /**
86
+ * @internal
86
87
  * A tree per zoom level. A zoom level where no clusters form shares the tree of the zoom level above it.
87
88
  */
88
89
  trees: KDBushWithData[];
89
90
  stride: number;
90
91
  clusterProps: Record<string, unknown>[];
92
+ /** @internal */
91
93
  points: GeoJSONVTInternalPointFeature[];
92
94
  private worldCoordinateHelper: WorldCoordinateHelper;
93
95
 
@@ -117,12 +119,13 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
117
119
  const positions = point.geometry.type === 'MultiPoint' ? point.geometry.coordinates : [point.geometry.coordinates];
118
120
  for (const [lng, lat] of positions) {
119
121
  const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
120
- if (!this.options.wrap && !isInWorld([x, y])) continue;
122
+ if (!this.options.wrap && !isInWorld(x, y)) continue;
121
123
 
122
124
  const feature: GeoJSONVTInternalPointFeature = {
123
125
  id: point.id,
124
126
  type: 'Point',
125
- geometry: [x, y],
127
+ x,
128
+ y,
126
129
  tags: point.properties
127
130
  };
128
131
 
@@ -144,22 +147,32 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
144
147
 
145
148
  for (const feature of features) {
146
149
  if (feature.type === 'Point') {
147
- if (!this.options.wrap && !isInWorld(feature.geometry)) continue;
150
+ if (!this.isClustered(feature.x, feature.y)) continue;
148
151
  points.push(feature);
149
152
  continue;
150
153
  }
151
154
  if (feature.type !== 'MultiPoint') continue;
152
155
 
153
156
  for (let i = 0; i < feature.geometry.length; i += 3) {
154
- const geometry = [feature.geometry[i], feature.geometry[i + 1]];
155
- if (!this.options.wrap && !isInWorld(geometry)) continue;
156
- points.push({id: feature.id, type: 'Point', geometry, tags: feature.tags});
157
+ const x = feature.geometry[i] + feature.worldOffset;
158
+ const y = feature.geometry[i + 1];
159
+ if (!this.isClustered(x, y)) continue;
160
+ points.push({id: feature.id, type: 'Point', x, y, tags: feature.tags});
157
161
  }
158
162
  }
159
163
 
160
164
  this.createIndex(points);
161
165
  }
162
166
 
167
+ /**
168
+ * Whether a position of the data source goes into the clusters. In a world that wraps, the source also has a copy
169
+ * of each position near the antimeridian on the other side of it. Only the one inside the world is clustered, since
170
+ * `getTile` looks across the antimeridian by itself. In a world that doesn't wrap, the positions outside it are left out.
171
+ */
172
+ private isClustered(x: number, y: number): boolean {
173
+ return this.options.wrap ? x >= 0 && x < 1 : isInWorld(x, y);
174
+ }
175
+
163
176
  /**
164
177
  * @internal
165
178
  * Updates the cluster data by rebuilding.
@@ -199,11 +212,11 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
199
212
 
200
213
  for (let i = 0; i < points.length; i++) {
201
214
  const p = points[i];
202
- if (!p?.geometry || !fitsInteger(p.geometry[0]) || !fitsInteger(p.geometry[1])) continue;
215
+ if (!fitsInteger(p.x) || !fitsInteger(p.y)) continue;
203
216
 
204
217
  // store internal point/cluster data in flat numeric arrays for performance
205
- coords[numItems * 2] = Math.round(encode(p.geometry[0])); // projected point coordinates
206
- coords[numItems * 2 + 1] = Math.round(encode(p.geometry[1]));
218
+ coords[numItems * 2] = Math.round(encode(p.x)); // projected point coordinates
219
+ coords[numItems * 2 + 1] = Math.round(encode(p.y));
207
220
  const k = numItems * stride;
208
221
  data[k + OFFSET_ZOOM] = notProcessed; // the last zoom the point was processed at
209
222
  data[k + OFFSET_ID] = i; // index of the source feature in the original input array
@@ -377,14 +390,7 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
377
390
  const top = encode((y - p) / z2);
378
391
  const bottom = encode((y + 1 + p) / z2);
379
392
 
380
- const tile: GeoJSONVTTile = {
381
- transformed: true,
382
- features: [],
383
- source: null,
384
- x: x,
385
- y: y,
386
- z: z
387
- };
393
+ const tile: GeoJSONVTTile = {features: []};
388
394
 
389
395
  this.addTileFeatures(
390
396
  tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom),
@@ -480,7 +486,8 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
480
486
  } else {
481
487
  const p = this.points[data[k + OFFSET_ID]];
482
488
  tags = p.tags;
483
- [px, py] = p.geometry;
489
+ px = p.x;
490
+ py = p.y;
484
491
  }
485
492
 
486
493
  const f: GeoJSONVTFeature = {
@@ -650,7 +657,8 @@ function getClusterFeature(coords: Int32Array, index: number, data: Int32Array,
650
657
  id: data[i + OFFSET_ID],
651
658
  type: 'Point',
652
659
  tags: getClusterProperties(data, i, clusterProps),
653
- geometry: [decode(coords[index * 2]), decode(coords[index * 2 + 1])]
660
+ x: decode(coords[index * 2]),
661
+ y: decode(coords[index * 2 + 1])
654
662
  };
655
663
  }
656
664
 
@@ -682,6 +690,6 @@ function getClusterProperties(data: Int32Array, i: number, clusterProps: Record<
682
690
  });
683
691
  }
684
692
 
685
- function isInWorld(geometry: number[]): boolean {
686
- return geometry[0] >= 0 && geometry[0] <= 1 && geometry[1] >= 0 && geometry[1] <= 1;
693
+ function isInWorld(x: number, y: number): boolean {
694
+ return x >= 0 && x <= 1 && y >= 0 && y <= 1;
687
695
  }