@maplibre/geojson-vt 6.2.0 → 6.3.1

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.
@@ -1,4 +1,4 @@
1
- //#region src/simplify.ts
1
+ // src/simplify.ts
2
2
  /**
3
3
  * calculate simplification data using optimized Douglas-Peucker algorithm
4
4
  * @param coords - flat array of coordinates
@@ -71,8 +71,7 @@ function getSqSegDist(px, py, x, y, bx, by) {
71
71
  dy = py - y;
72
72
  return dx * dx + dy * dy;
73
73
  }
74
- //#endregion
75
- //#region src/feature.ts
74
+ // src/feature.ts
76
75
  /**
77
76
  *
78
77
  * @param id - the feature's ID
@@ -126,32 +125,87 @@ function calcLineBBox(feature, geom) {
126
125
  feature.maxY = Math.max(feature.maxY, geom[i + 1]);
127
126
  }
128
127
  }
129
- //#endregion
130
- //#region src/convert.ts
128
+ // src/world-coordinate-helper.ts
129
+ /**
130
+ * Longitude and latitude, which spherical mercator puts in the world square.
131
+ */
132
+ var MercatorWorldCoordinateHelper = class {
133
+ projectX(lng) {
134
+ return lng / 360 + .5;
135
+ }
136
+ projectY(lat) {
137
+ const sin = Math.sin(lat * Math.PI / 180);
138
+ const y = .5 - .25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
139
+ return y < 0 ? 0 : y > 1 ? 1 : y;
140
+ }
141
+ unprojectX(x) {
142
+ return (x - .5) * 360;
143
+ }
144
+ unprojectY(y) {
145
+ const y2 = (180 - y * 360) * Math.PI / 180;
146
+ return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90;
147
+ }
148
+ };
149
+ /**
150
+ * Positions in the unit square already, see `coordinates` in {@link GeoJSONVTOptions}.
151
+ */
152
+ var IdentityWorldCoordinateHelper = class {
153
+ projectX(x) {
154
+ return x;
155
+ }
156
+ projectY(y) {
157
+ return y;
158
+ }
159
+ unprojectX(x) {
160
+ return x;
161
+ }
162
+ unprojectY(y) {
163
+ return y;
164
+ }
165
+ };
166
+ const mercatorWorldCoordinateHelper = new MercatorWorldCoordinateHelper();
167
+ const identityWorldCoordinateHelper = new IdentityWorldCoordinateHelper();
168
+ function getWorldCoordinateHelper(coordinates) {
169
+ return coordinates === "unitSquare" ? identityWorldCoordinateHelper : mercatorWorldCoordinateHelper;
170
+ }
171
+ // src/convert.ts
131
172
  const MAX_GEOMETRY_COLLECTION_DEPTH = 1024;
132
173
  /**
133
174
  * converts GeoJSON to internal source features (an intermediate projected JSON vector format with simplification data)
134
175
  * @param data
135
176
  * @param options
177
+ * @param unlocated - the features without coordinates, by id: a feature with an id that converts to nothing is set in it, and one that converts to something is deleted from it
136
178
  * @returns
137
179
  */
138
- function convertToInternal(data, options) {
180
+ function convertToInternal(data, options, unlocated) {
139
181
  const features = [];
140
182
  switch (data.type) {
141
183
  case "FeatureCollection":
142
- for (let i = 0; i < data.features.length; i++) featureToInternal(features, data.features[i], options, i);
184
+ for (let i = 0; i < data.features.length; i++) convertFeature(features, data.features[i], options, unlocated, i);
143
185
  break;
144
186
  case "Feature":
145
- featureToInternal(features, data, options);
187
+ convertFeature(features, data, options, unlocated);
146
188
  break;
147
- default: featureToInternal(features, {
189
+ default: convertFeature(features, {
148
190
  type: "Feature",
149
191
  geometry: data,
150
192
  properties: void 0
151
- }, options);
193
+ }, options, unlocated);
152
194
  }
153
195
  return features;
154
196
  }
197
+ function convertFeature(features, geojson, options, unlocated, index) {
198
+ const count = features.length;
199
+ featureToInternal(features, geojson, options, index);
200
+ if (!unlocated) return;
201
+ const id = getFeatureId(geojson, options, index);
202
+ if (id == null) return;
203
+ if (features.length > count) unlocated.delete(id);
204
+ else unlocated.set(id, {
205
+ ...geojson,
206
+ id
207
+ });
208
+ }
155
209
  function featureToInternal(features, geojson, options, index, depth = 0) {
156
210
  if (!geojson.geometry) return;
157
211
  if (depth > MAX_GEOMETRY_COLLECTION_DEPTH) throw new Error("GeometryCollection nesting exceeds supported depth: 1024");
@@ -162,25 +216,25 @@ function featureToInternal(features, geojson, options, index, depth = 0) {
162
216
  if (!geojson.geometry.coordinates?.length) return;
163
217
  const id = getFeatureId(geojson, options, index);
164
218
  const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
165
- const worldCoordinates = options.worldCoordinates === true;
219
+ const worldCoordinateHelper = getWorldCoordinateHelper(options.coordinates);
166
220
  switch (geojson.geometry.type) {
167
221
  case "Point":
168
- convertPointFeature(features, id, geojson.geometry, worldCoordinates, geojson.properties);
222
+ convertPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
169
223
  return;
170
224
  case "MultiPoint":
171
- convertMultiPointFeature(features, id, geojson.geometry, worldCoordinates, geojson.properties);
225
+ convertMultiPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
172
226
  return;
173
227
  case "LineString":
174
- convertLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinates, geojson.properties);
228
+ convertLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
175
229
  return;
176
230
  case "MultiLineString":
177
- convertMultiLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinates, options, geojson.properties);
231
+ convertMultiLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, options, geojson.properties);
178
232
  return;
179
233
  case "Polygon":
180
- convertPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinates, geojson.properties);
234
+ convertPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
181
235
  return;
182
236
  case "MultiPolygon":
183
- convertMultiPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinates, geojson.properties);
237
+ convertMultiPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
184
238
  return;
185
239
  default: throw new Error("Input data is not a valid GeoJSON object.");
186
240
  }
@@ -198,55 +252,55 @@ function convertGeometryCollection(features, geojson, geometry, options, index,
198
252
  properties: geojson.properties
199
253
  }, options, index, depth);
200
254
  }
201
- function convertPointFeature(features, id, geom, worldCoordinates, properties) {
255
+ function convertPointFeature(features, id, geom, worldCoordinateHelper, properties) {
202
256
  const out = [];
203
- out.push(projectX(geom.coordinates[0], worldCoordinates), projectY(geom.coordinates[1], worldCoordinates), 0);
257
+ out.push(worldCoordinateHelper.projectX(geom.coordinates[0]), worldCoordinateHelper.projectY(geom.coordinates[1]), 0);
204
258
  features.push(createFeature(id, "Point", out, properties));
205
259
  }
206
- function convertMultiPointFeature(features, id, geom, worldCoordinates, properties) {
260
+ function convertMultiPointFeature(features, id, geom, worldCoordinateHelper, properties) {
207
261
  const out = [];
208
- for (const coords of geom.coordinates) out.push(projectX(coords[0], worldCoordinates), projectY(coords[1], worldCoordinates), 0);
262
+ for (const coords of geom.coordinates) out.push(worldCoordinateHelper.projectX(coords[0]), worldCoordinateHelper.projectY(coords[1]), 0);
209
263
  features.push(createFeature(id, "MultiPoint", out, properties));
210
264
  }
211
- function convertLineStringFeature(features, id, geom, tolerance, worldCoordinates, properties) {
265
+ function convertLineStringFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
212
266
  const out = { points: [] };
213
- convertLine(geom.coordinates, out, tolerance, false, worldCoordinates);
267
+ convertLine(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
214
268
  features.push(createFeature(id, "LineString", out, properties));
215
269
  }
216
- function convertMultiLineStringFeature(features, id, geom, tolerance, worldCoordinates, options, properties) {
270
+ function convertMultiLineStringFeature(features, id, geom, tolerance, worldCoordinateHelper, options, properties) {
217
271
  if (options.lineMetrics) for (const line of geom.coordinates) {
218
272
  const out = { points: [] };
219
- convertLine(line, out, tolerance, false, worldCoordinates);
273
+ convertLine(line, out, tolerance, false, worldCoordinateHelper);
220
274
  features.push(createFeature(id, "LineString", out, properties));
221
275
  }
222
276
  else {
223
277
  const out = [];
224
- convertLines(geom.coordinates, out, tolerance, false, worldCoordinates);
278
+ convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
225
279
  features.push(createFeature(id, "MultiLineString", out, properties));
226
280
  }
227
281
  }
228
- function convertPolygonFeature(features, id, geom, tolerance, worldCoordinates, properties) {
282
+ function convertPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
229
283
  const out = [];
230
- convertLines(geom.coordinates, out, tolerance, true, worldCoordinates);
284
+ convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
231
285
  features.push(createFeature(id, "Polygon", out, properties));
232
286
  }
233
- function convertMultiPolygonFeature(features, id, geom, tolerance, worldCoordinates, properties) {
287
+ function convertMultiPolygonFeature(features, id, geom, tolerance, worldCoordinateHelper, properties) {
234
288
  const out = [];
235
289
  for (const polygon of geom.coordinates) {
236
290
  const polygonOut = [];
237
- convertLines(polygon, polygonOut, tolerance, true, worldCoordinates);
291
+ convertLines(polygon, polygonOut, tolerance, true, worldCoordinateHelper);
238
292
  if (!polygonOut.length) continue;
239
293
  out.push(polygonOut);
240
294
  }
241
295
  if (!out.length) return;
242
296
  features.push(createFeature(id, "MultiPolygon", out, properties));
243
297
  }
244
- function convertLine(ring, out, tolerance, isPolygon, worldCoordinates) {
298
+ function convertLine(ring, out, tolerance, isPolygon, worldCoordinateHelper) {
245
299
  let x0, y0;
246
300
  let size = 0;
247
301
  for (let j = 0; j < ring.length; j++) {
248
- const x = projectX(ring[j][0], worldCoordinates);
249
- const y = projectY(ring[j][1], worldCoordinates);
302
+ const x = worldCoordinateHelper.projectX(ring[j][0]);
303
+ const y = worldCoordinateHelper.projectY(ring[j][1]);
250
304
  out.points.push(x, y, 0);
251
305
  if (j > 0) {
252
306
  if (isPolygon) size += (x0 * y - x * y0) / 2;
@@ -264,47 +318,30 @@ function convertLine(ring, out, tolerance, isPolygon, worldCoordinates) {
264
318
  out.start = 0;
265
319
  out.end = out.size;
266
320
  }
267
- function convertLines(rings, out, tolerance, isPolygon, worldCoordinates) {
321
+ function convertLines(rings, out, tolerance, isPolygon, worldCoordinateHelper) {
268
322
  for (let i = 0; i < rings.length; i++) {
269
323
  const geom = { points: [] };
270
- convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinates);
324
+ convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
271
325
  out.push(geom);
272
326
  }
273
327
  }
274
- /**
275
- * Convert longitude to spherical mercator in [0..1] range, or keep a world coordinate as it is
276
- */
277
- function projectX(x, worldCoordinates = false) {
278
- if (worldCoordinates) return x;
279
- return x / 360 + .5;
280
- }
281
- /**
282
- * Convert latitude to spherical mercator in [0..1] range, or keep a world coordinate as it is
283
- */
284
- function projectY(y, worldCoordinates = false) {
285
- if (worldCoordinates) return y;
286
- const sin = Math.sin(y * Math.PI / 180);
287
- const y2 = .5 - .25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
288
- return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
289
- }
290
- //#endregion
291
- //#region src/deconvert.ts
328
+ // src/deconvert.ts
292
329
  /**
293
330
  * Converts internal source features back to GeoJSON format.
294
331
  */
295
- function convertToGeoJSON(source, worldCoordinates = false) {
332
+ function convertToGeoJSON(source, worldCoordinateHelper = mercatorWorldCoordinateHelper) {
296
333
  return {
297
334
  type: "FeatureCollection",
298
- features: source.map((feature) => featureToGeoJSON(feature, worldCoordinates))
335
+ features: source.map((feature) => featureToGeoJSON(feature, worldCoordinateHelper))
299
336
  };
300
337
  }
301
338
  /**
302
339
  * Converts a single internal feature to GeoJSON format.
303
340
  */
304
- function featureToGeoJSON(feature, worldCoordinates = false) {
341
+ function featureToGeoJSON(feature, worldCoordinateHelper = mercatorWorldCoordinateHelper) {
305
342
  const geojsonFeature = {
306
343
  type: "Feature",
307
- geometry: geometryToGeoJSON(feature, worldCoordinates),
344
+ geometry: geometryToGeoJSON(feature, worldCoordinateHelper),
308
345
  properties: feature.tags
309
346
  };
310
347
  if (feature.id != null) geojsonFeature.id = feature.id;
@@ -313,57 +350,41 @@ function featureToGeoJSON(feature, worldCoordinates = false) {
313
350
  /**
314
351
  * Converts a single internal feature geometry to GeoJSON format.
315
352
  */
316
- function geometryToGeoJSON(feature, worldCoordinates) {
353
+ function geometryToGeoJSON(feature, worldCoordinateHelper) {
317
354
  const { type, geometry } = feature;
318
355
  switch (type) {
319
356
  case "Point": return {
320
357
  type,
321
- coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinates)
358
+ coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinateHelper)
322
359
  };
323
360
  case "MultiPoint": return {
324
361
  type,
325
- coordinates: unprojectPoints(geometry, worldCoordinates)
362
+ coordinates: unprojectPoints(geometry, worldCoordinateHelper)
326
363
  };
327
364
  case "LineString": return {
328
365
  type,
329
- coordinates: unprojectPoints(geometry.points, worldCoordinates)
366
+ coordinates: unprojectPoints(geometry.points, worldCoordinateHelper)
330
367
  };
331
368
  case "MultiLineString":
332
369
  case "Polygon": return {
333
370
  type,
334
- coordinates: geometry.map((ring) => unprojectPoints(ring.points, worldCoordinates))
371
+ coordinates: geometry.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper))
335
372
  };
336
373
  case "MultiPolygon": return {
337
374
  type,
338
- coordinates: geometry.map((polygon) => polygon.map((ring) => unprojectPoints(ring.points, worldCoordinates)))
375
+ coordinates: geometry.map((polygon) => polygon.map((ring) => unprojectPoints(ring.points, worldCoordinateHelper)))
339
376
  };
340
377
  }
341
378
  }
342
- function unprojectPoints(coords, worldCoordinates = false) {
379
+ function unprojectPoints(coords, worldCoordinateHelper = mercatorWorldCoordinateHelper) {
343
380
  const result = [];
344
- for (let i = 0; i < coords.length; i += 3) result.push(unprojectPoint(coords[i], coords[i + 1], worldCoordinates));
381
+ for (let i = 0; i < coords.length; i += 3) result.push(unprojectPoint(coords[i], coords[i + 1], worldCoordinateHelper));
345
382
  return result;
346
383
  }
347
- function unprojectPoint(x, y, worldCoordinates) {
348
- return [unprojectX(x, worldCoordinates), unprojectY(y, worldCoordinates)];
384
+ function unprojectPoint(x, y, worldCoordinateHelper) {
385
+ return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
349
386
  }
350
- /**
351
- * Convert spherical mercator in [0..1] range to longitude, or keep a world coordinate as it is
352
- */
353
- function unprojectX(x, worldCoordinates = false) {
354
- if (worldCoordinates) return x;
355
- return (x - .5) * 360;
356
- }
357
- /**
358
- * Convert spherical mercator in [0..1] range to latitude, or keep a world coordinate as it is
359
- */
360
- function unprojectY(y, worldCoordinates = false) {
361
- if (worldCoordinates) return y;
362
- const y2 = (180 - y * 360) * Math.PI / 180;
363
- return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90;
364
- }
365
- //#endregion
366
- //#region src/clip.ts
387
+ // src/clip.ts
367
388
  /**
368
389
  * clip features between two vertical or horizontal axis-parallel lines:
369
390
  * | |
@@ -394,7 +415,6 @@ function clip(features, scale, start, end, axis, minAll, maxAll, options) {
394
415
  }
395
416
  if (max < start || min >= end) continue;
396
417
  switch (feature.type) {
397
- case "Point":
398
418
  case "MultiPoint":
399
419
  clipPointFeature(feature, clipped, start, end, axis);
400
420
  continue;
@@ -547,10 +567,9 @@ function intersectY(out, ax, ay, bx, by, y) {
547
567
  addPoint(out.points, ax + (bx - ax) * t, y, 1);
548
568
  return t;
549
569
  }
550
- //#endregion
551
- //#region src/wrap.ts
570
+ // src/wrap.ts
552
571
  function wrap(features, options) {
553
- if (options.worldCoordinates) return features;
572
+ if (!options.wrap) return features;
554
573
  const buffer = options.buffer / options.extent;
555
574
  let merged = features;
556
575
  const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options);
@@ -613,23 +632,25 @@ function shiftLineCoords(line, offset) {
613
632
  optimizeLineMemory(newLine);
614
633
  return newLine;
615
634
  }
616
- //#endregion
617
- //#region src/difference.ts
635
+ // src/difference.ts
618
636
  /**
619
637
  * Applies a GeoJSON Source Diff to an existing set of simplified features
620
638
  * @param source
621
639
  * @param dataDiff
622
640
  * @param options
641
+ * @param unlocated - the features without coordinates, by id, so that an update can give them some
623
642
  * @returns
624
643
  */
625
- function applySourceDiff(source, dataDiff, options) {
644
+ function applySourceDiff(source, dataDiff, options, unlocated) {
626
645
  const diff = diffToHashed(dataDiff, options);
627
646
  let affected = [];
628
647
  if (diff.removeAll) {
629
648
  affected = source;
630
649
  source = [];
650
+ unlocated?.clear();
631
651
  }
632
652
  if (diff.remove.size || diff.add.size) {
653
+ for (const id of diff.remove) unlocated?.delete(id);
633
654
  const removeFeatures = [];
634
655
  for (const feature of source) if (diff.remove.has(feature.id) || diff.add.has(feature.id)) removeFeatures.push(feature);
635
656
  if (removeFeatures.length) {
@@ -641,7 +662,10 @@ function applySourceDiff(source, dataDiff, options) {
641
662
  let addFeatures = convertToInternal({
642
663
  type: "FeatureCollection",
643
664
  features: Array.from(diff.add.values())
644
- }, options);
665
+ }, {
666
+ ...options,
667
+ generateId: false
668
+ }, unlocated);
645
669
  addFeatures = wrap(addFeatures, options);
646
670
  affected = affected.concat(addFeatures);
647
671
  source = source.concat(addFeatures);
@@ -654,8 +678,14 @@ function applySourceDiff(source, dataDiff, options) {
654
678
  else keepFeatures.push(feature);
655
679
  for (const [id, update] of diff.update) {
656
680
  const oldFeatures = oldFeaturesMap.get(id);
657
- if (!oldFeatures || oldFeatures.length === 0) continue;
658
- const updatedFeatures = getUpdatedFeatures(oldFeatures, update, options);
681
+ if (!oldFeatures || oldFeatures.length === 0) {
682
+ if (!unlocated?.has(id)) continue;
683
+ const locatedFeatures = getUpdatedUnlocatedFeatures(id, update, options, unlocated);
684
+ affected = affected.concat(locatedFeatures);
685
+ keepFeatures = keepFeatures.concat(locatedFeatures);
686
+ continue;
687
+ }
688
+ const updatedFeatures = getUpdatedFeatures(oldFeatures, update, options, unlocated);
659
689
  affected = affected.concat(oldFeatures, updatedFeatures);
660
690
  keepFeatures = keepFeatures.concat(updatedFeatures);
661
691
  }
@@ -671,28 +701,16 @@ function applySourceDiff(source, dataDiff, options) {
671
701
  * @param vtFeatures - the original features
672
702
  * @param update - the update object to apply
673
703
  * @param options - the options to use for the wrap method
704
+ * @param unlocated - the features without coordinates, by id, which a new geometry without coordinates adds the feature to
674
705
  * @returns Updated features. If geometry is updated, returns new feature(s) converted from geojson and wrapped, with the id the feature already has. If only properties are updated, returns feature(s) with tags updated.
675
706
  */
676
- function getUpdatedFeatures(vtFeatures, update, options) {
707
+ function getUpdatedFeatures(vtFeatures, update, options, unlocated) {
677
708
  const changeGeometry = !!update.newGeometry;
678
- const changeProps = update.removeAllProperties || update.removeProperties?.length > 0 || update.addOrUpdateProperties?.length > 0;
709
+ const changeProps = hasPropertyChanges(update);
679
710
  if (changeGeometry) {
680
711
  const vtFeature = vtFeatures[0];
681
- let features = convertToInternal({
682
- type: "FeatureCollection",
683
- features: [{
684
- type: "Feature",
685
- id: vtFeature.id,
686
- geometry: update.newGeometry,
687
- properties: changeProps ? applyPropertyUpdates(vtFeature.tags, update) : vtFeature.tags
688
- }]
689
- }, {
690
- ...options,
691
- promoteId: void 0,
692
- generateId: false
693
- });
694
- features = wrap(features, options);
695
- return features;
712
+ const properties = changeProps ? applyPropertyUpdates(vtFeature.tags, update) : vtFeature.tags;
713
+ return convertNewGeometry(vtFeature.id, update.newGeometry, properties, options, unlocated);
696
714
  }
697
715
  if (changeProps) {
698
716
  const updated = [];
@@ -706,6 +724,43 @@ function getUpdatedFeatures(vtFeatures, update, options) {
706
724
  return vtFeatures;
707
725
  }
708
726
  /**
727
+ * Gets the features an update gives a feature without coordinates: none, unless its new geometry has some.
728
+ * Its property changes apply either way.
729
+ */
730
+ function getUpdatedUnlocatedFeatures(id, update, options, unlocated) {
731
+ const feature = unlocated.get(id);
732
+ const properties = hasPropertyChanges(update) ? applyPropertyUpdates(feature.properties, update) : feature.properties;
733
+ if (!update.newGeometry) {
734
+ unlocated.set(id, {
735
+ ...feature,
736
+ properties
737
+ });
738
+ return [];
739
+ }
740
+ return convertNewGeometry(id, update.newGeometry, properties, options, unlocated);
741
+ }
742
+ /**
743
+ * Converts the new geometry of a feature to internal format, keeping the id the feature already has
744
+ */
745
+ function convertNewGeometry(id, geometry, properties, options, unlocated) {
746
+ return wrap(convertToInternal({
747
+ type: "FeatureCollection",
748
+ features: [{
749
+ type: "Feature",
750
+ id,
751
+ geometry,
752
+ properties
753
+ }]
754
+ }, {
755
+ ...options,
756
+ promoteId: void 0,
757
+ generateId: false
758
+ }, unlocated), options);
759
+ }
760
+ function hasPropertyChanges(update) {
761
+ return update.removeAllProperties || update.removeProperties?.length > 0 || update.addOrUpdateProperties?.length > 0;
762
+ }
763
+ /**
709
764
  * helper to apply property updates from a diff update object to a properties object
710
765
  */
711
766
  function applyPropertyUpdates(tags, update) {
@@ -731,8 +786,7 @@ function diffToHashed(diff, options) {
731
786
  update: new Map(diff.update?.map((update) => [update.id, update]))
732
787
  };
733
788
  }
734
- //#endregion
735
- //#region node_modules/kdbush/index.js
789
+ // node_modules/kdbush/index.js
736
790
  const ARRAY_TYPES = [
737
791
  Int8Array,
738
792
  Uint8Array,
@@ -1016,8 +1070,7 @@ function sqDist(ax, ay, bx, by) {
1016
1070
  const dy = ay - by;
1017
1071
  return dx * dx + dy * dy;
1018
1072
  }
1019
- //#endregion
1020
- //#region src/cluster-tile-index.ts
1073
+ // src/cluster-tile-index.ts
1021
1074
  const defaultClusterOptions = {
1022
1075
  minZoom: 0,
1023
1076
  maxZoom: 16,
@@ -1028,7 +1081,9 @@ const defaultClusterOptions = {
1028
1081
  log: false,
1029
1082
  generateId: false,
1030
1083
  reduce: null,
1031
- map: (props) => props
1084
+ map: (props) => props,
1085
+ coordinates: "lngLat",
1086
+ wrap: true
1032
1087
  };
1033
1088
  /**
1034
1089
  * The highest zoom level to cluster on. A cluster id keeps its zoom level in five bits.
@@ -1068,9 +1123,9 @@ const OFFSET_PROP = 4;
1068
1123
  * This class allow clustering of geojson points.
1069
1124
  */
1070
1125
  var ClusterTileIndex = class {
1071
- constructor(options, worldCoordinates = false) {
1126
+ constructor(options) {
1072
1127
  this.options = createOptions(options);
1073
- this.worldCoordinates = worldCoordinates;
1128
+ this.worldCoordinateHelper = getWorldCoordinateHelper(this.options.coordinates);
1074
1129
  this.trees = new Array(this.options.maxZoom + 1);
1075
1130
  this.stride = this.options.reduce ? 5 : 4;
1076
1131
  this.clusterProps = [];
@@ -1087,8 +1142,8 @@ var ClusterTileIndex = class {
1087
1142
  if (!point.geometry) continue;
1088
1143
  const positions = point.geometry.type === "MultiPoint" ? point.geometry.coordinates : [point.geometry.coordinates];
1089
1144
  for (const [lng, lat] of positions) {
1090
- const [x, y] = [projectX(lng, this.worldCoordinates), projectY(lat, this.worldCoordinates)];
1091
- if (this.worldCoordinates && !isInWorld([x, y])) continue;
1145
+ const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
1146
+ if (!this.options.wrap && !isInWorld([x, y])) continue;
1092
1147
  const feature = {
1093
1148
  id: point.id,
1094
1149
  type: "Point",
@@ -1110,14 +1165,14 @@ var ClusterTileIndex = class {
1110
1165
  const points = [];
1111
1166
  for (const feature of features) {
1112
1167
  if (feature.type === "Point") {
1113
- if (this.worldCoordinates && !isInWorld(feature.geometry)) continue;
1168
+ if (!this.options.wrap && !isInWorld(feature.geometry)) continue;
1114
1169
  points.push(feature);
1115
1170
  continue;
1116
1171
  }
1117
1172
  if (feature.type !== "MultiPoint") continue;
1118
1173
  for (let i = 0; i < feature.geometry.length; i += 3) {
1119
1174
  const geometry = [feature.geometry[i], feature.geometry[i + 1]];
1120
- if (this.worldCoordinates && !isInWorld(geometry)) continue;
1175
+ if (!this.options.wrap && !isInWorld(geometry)) continue;
1121
1176
  points.push({
1122
1177
  id: feature.id,
1123
1178
  type: "Point",
@@ -1134,7 +1189,11 @@ var ClusterTileIndex = class {
1134
1189
  * @param features
1135
1190
  */
1136
1191
  updateIndex(features, _affected, options) {
1137
- this.options = createOptions(options.clusterOptions);
1192
+ this.options = createOptions({
1193
+ ...options.clusterOptions,
1194
+ coordinates: this.options.coordinates,
1195
+ wrap: this.options.wrap
1196
+ });
1138
1197
  this.initialize(features);
1139
1198
  }
1140
1199
  /**
@@ -1182,11 +1241,61 @@ var ClusterTileIndex = class {
1182
1241
  }
1183
1242
  /**
1184
1243
  * Returns clusters and/or points within a bounding box at a given zoom level.
1185
- * @param bbox - Bounding box in `[westLng, southLat, eastLng, northLat]` order, or `[minX, minY, maxX, maxY]` in world coordinates.
1244
+ * @param bbox - Bounding box in `[westLng, southLat, eastLng, northLat]` order, or `[minX, minY, maxX, maxY]` in the unit square with `coordinates: 'unitSquare'`.
1245
+ * In a world that wraps, the box can reach across the antimeridian. In one that doesn't, only its part inside the world counts.
1186
1246
  * @param zoom - Zoom level to query.
1187
1247
  */
1188
1248
  getClusters(bbox, zoom) {
1189
- return (this.worldCoordinates ? this.getClustersInRange(bbox, zoom) : this.getClustersInternal(bbox, zoom)).map((f) => featureToGeoJSON(f, this.worldCoordinates));
1249
+ return (!this.options.wrap ? this.getClustersInWorld(bbox, zoom) : this.options.coordinates === "unitSquare" ? this.getClustersAcrossEdge(bbox, zoom) : this.getClustersInternal(bbox, zoom)).map((f) => featureToGeoJSON(f, this.worldCoordinateHelper));
1250
+ }
1251
+ /**
1252
+ * The clusters and points in the part of a box inside a world that ends at its edges.
1253
+ */
1254
+ getClustersInWorld(bbox, zoom) {
1255
+ const lngLat = this.options.coordinates !== "unitSquare";
1256
+ const y1 = this.worldCoordinateHelper.projectY(lngLat ? Math.max(-90, Math.min(90, bbox[1])) : bbox[1]);
1257
+ const y2 = this.worldCoordinateHelper.projectY(lngLat ? Math.max(-90, Math.min(90, bbox[3])) : bbox[3]);
1258
+ const minX = Math.max(0, this.worldCoordinateHelper.projectX(bbox[0]));
1259
+ const maxX = Math.min(1, this.worldCoordinateHelper.projectX(bbox[2]));
1260
+ if (minX > maxX) return [];
1261
+ return this.getClustersInRange([
1262
+ minX,
1263
+ Math.min(y1, y2),
1264
+ maxX,
1265
+ Math.max(y1, y2)
1266
+ ], zoom);
1267
+ }
1268
+ /**
1269
+ * The clusters and points in a box of a unit square that wraps, where the box can reach across its left or right edge.
1270
+ */
1271
+ getClustersAcrossEdge([minX, minY, maxX, maxY], zoom) {
1272
+ if (maxX < minX) maxX += 1;
1273
+ if (maxX - minX >= 1) return this.getClustersInRange([
1274
+ 0,
1275
+ minY,
1276
+ 1,
1277
+ maxY
1278
+ ], zoom);
1279
+ const shift = Math.floor(minX);
1280
+ const west = minX - shift;
1281
+ const east = maxX - shift;
1282
+ if (east <= 1) return this.getClustersInRange([
1283
+ west,
1284
+ minY,
1285
+ east,
1286
+ maxY
1287
+ ], zoom);
1288
+ return this.getClustersInRange([
1289
+ west,
1290
+ minY,
1291
+ 1,
1292
+ maxY
1293
+ ], zoom).concat(this.getClustersInRange([
1294
+ 0,
1295
+ minY,
1296
+ east - 1,
1297
+ maxY
1298
+ ], zoom));
1190
1299
  }
1191
1300
  getClustersInternal(bbox, zoom) {
1192
1301
  let minLng = ((bbox[0] + 180) % 360 + 360) % 360 - 180;
@@ -1212,10 +1321,10 @@ var ClusterTileIndex = class {
1212
1321
  return easternHem.concat(westernHem);
1213
1322
  }
1214
1323
  return this.getClustersInRange([
1215
- projectX(minLng),
1216
- projectY(maxLat),
1217
- projectX(maxLng),
1218
- projectY(minLat)
1324
+ this.worldCoordinateHelper.projectX(minLng),
1325
+ this.worldCoordinateHelper.projectY(maxLat),
1326
+ this.worldCoordinateHelper.projectX(maxLng),
1327
+ this.worldCoordinateHelper.projectY(minLat)
1219
1328
  ], zoom);
1220
1329
  }
1221
1330
  /**
@@ -1253,7 +1362,7 @@ var ClusterTileIndex = class {
1253
1362
  const children = [];
1254
1363
  for (const id of ids) {
1255
1364
  const k = id * this.stride;
1256
- if (data[k + OFFSET_PARENT] === clusterId) children.push(data[k + OFFSET_NUM] > 1 ? getClusterGeoJSON(coords, id, data, k, this.clusterProps, this.worldCoordinates) : featureToGeoJSON(this.points[data[k + OFFSET_ID]], this.worldCoordinates));
1365
+ if (data[k + OFFSET_PARENT] === clusterId) children.push(data[k + OFFSET_NUM] > 1 ? getClusterGeoJSON(coords, id, data, k, this.clusterProps, this.worldCoordinateHelper) : featureToGeoJSON(this.points[data[k + OFFSET_ID]], this.worldCoordinateHelper));
1257
1366
  }
1258
1367
  if (children.length === 0) throw clusterError;
1259
1368
  return children;
@@ -1281,7 +1390,7 @@ var ClusterTileIndex = class {
1281
1390
  const tree = this.trees[this.limitZoom(z)];
1282
1391
  if (!tree) return null;
1283
1392
  const z2 = Math.pow(2, z);
1284
- if (this.worldCoordinates && (x < 0 || x >= z2 || y < 0 || y >= z2)) return null;
1393
+ if (!this.options.wrap && (x < 0 || x >= z2 || y < 0 || y >= z2)) return null;
1285
1394
  const { extent, radius } = this.options;
1286
1395
  const p = radius / extent;
1287
1396
  const top = encode((y - p) / z2);
@@ -1295,8 +1404,8 @@ var ClusterTileIndex = class {
1295
1404
  z
1296
1405
  };
1297
1406
  this.addTileFeatures(tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom), tree, x, y, z2, tile);
1298
- if (!this.worldCoordinates && x === 0) this.addTileFeatures(tree.range(encode(1 - p / z2), top, encode(1), bottom), tree, z2, y, z2, tile);
1299
- if (!this.worldCoordinates && x === z2 - 1) this.addTileFeatures(tree.range(encode(0), top, encode(p / z2), bottom), tree, -1, y, z2, tile);
1407
+ if (this.options.wrap && x === 0) this.addTileFeatures(tree.range(encode(1 - p / z2), top, encode(1), bottom), tree, z2, y, z2, tile);
1408
+ if (this.options.wrap && x === z2 - 1) this.addTileFeatures(tree.range(encode(0), top, encode(p / z2), bottom), tree, -1, y, z2, tile);
1300
1409
  return tile;
1301
1410
  }
1302
1411
  /**
@@ -1479,14 +1588,14 @@ function getClusterFeature(coords, index, data, i, clusterProps) {
1479
1588
  geometry: [decode(coords[index * 2]), decode(coords[index * 2 + 1])]
1480
1589
  };
1481
1590
  }
1482
- function getClusterGeoJSON(coords, index, data, i, clusterProps, worldCoordinates) {
1591
+ function getClusterGeoJSON(coords, index, data, i, clusterProps, worldCoordinateHelper) {
1483
1592
  return {
1484
1593
  type: "Feature",
1485
1594
  id: data[i + OFFSET_ID],
1486
1595
  properties: getClusterProperties(data, i, clusterProps),
1487
1596
  geometry: {
1488
1597
  type: "Point",
1489
- coordinates: [unprojectX(decode(coords[index * 2]), worldCoordinates), unprojectY(decode(coords[index * 2 + 1]), worldCoordinates)]
1598
+ coordinates: [worldCoordinateHelper.unprojectX(decode(coords[index * 2])), worldCoordinateHelper.unprojectY(decode(coords[index * 2 + 1]))]
1490
1599
  }
1491
1600
  };
1492
1601
  }
@@ -1505,8 +1614,7 @@ function getClusterProperties(data, i, clusterProps) {
1505
1614
  function isInWorld(geometry) {
1506
1615
  return geometry[0] >= 0 && geometry[0] <= 1 && geometry[1] >= 0 && geometry[1] <= 1;
1507
1616
  }
1508
- //#endregion
1509
- //#region src/tile.ts
1617
+ // src/tile.ts
1510
1618
  const GEOJSONVT_CLIP_START = "geojsonvt_clip_start";
1511
1619
  const GEOJSONVT_CLIP_END = "geojsonvt_clip_end";
1512
1620
  /**
@@ -1652,8 +1760,7 @@ function rewind(ring, clockwise) {
1652
1760
  ring[len - 1 - i] = y;
1653
1761
  }
1654
1762
  }
1655
- //#endregion
1656
- //#region src/transform.ts
1763
+ // src/transform.ts
1657
1764
  /**
1658
1765
  * Transforms the coordinates of each feature in the given tile from
1659
1766
  * mercator-projected space into (extent x extent) tile space.
@@ -1700,8 +1807,7 @@ function transformNonPointFeature(feature, extent, z2, tx, ty) {
1700
1807
  function transformPoint(x, y, extent, z2, tx, ty) {
1701
1808
  return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))];
1702
1809
  }
1703
- //#endregion
1704
- //#region src/tile-index.ts
1810
+ // src/tile-index.ts
1705
1811
  var TileIndex = class {
1706
1812
  constructor(options) {
1707
1813
  this.options = options;
@@ -1766,7 +1872,7 @@ var TileIndex = class {
1766
1872
  getTile(z, x, y) {
1767
1873
  const { extent, debug } = this.options;
1768
1874
  const z2 = 1 << z;
1769
- if (this.options.worldCoordinates && (x < 0 || x >= z2)) return null;
1875
+ if (!this.options.wrap && (x < 0 || x >= z2)) return null;
1770
1876
  x = x + z2 & z2 - 1;
1771
1877
  const id = toID(z, x, y);
1772
1878
  if (this.tiles[id]) return transformTile(this.tiles[id], extent);
@@ -1792,14 +1898,16 @@ var TileIndex = class {
1792
1898
  return transformTile(this.tiles[id], extent);
1793
1899
  }
1794
1900
  /**
1795
- * The features the top tile starts from: all of them in a wrapping world, where wrapping already put them inside,
1796
- * and only what lies inside the world square and its buffer with `worldCoordinates`. The index keeps the rest, so
1797
- * an update can still move a feature in from outside.
1901
+ * The features the top tile starts from. A world that wraps keeps them all left and right, where wrapping already
1902
+ * put them inside, and one that doesn't only what lies inside the world square and its buffer. Positions in the
1903
+ * unit square are also cut above and below in a world that wraps, where lng/lat is clamped already. The index keeps
1904
+ * the rest, so an update can still move a feature in from outside.
1798
1905
  */
1799
1906
  clipToWorld(features) {
1800
- if (!this.options.worldCoordinates) return features;
1907
+ const { wrap, coordinates } = this.options;
1908
+ if (wrap && coordinates !== "unitSquare") return features;
1801
1909
  const buffer = this.options.buffer / this.options.extent;
1802
- return clip(clip(features, 1, -buffer, 1 + buffer, 0, -Infinity, Infinity, this.options) || [], 1, -buffer, 1 + buffer, 1, -Infinity, Infinity, this.options) || [];
1910
+ return clip(wrap ? features : clip(features, 1, -buffer, 1 + buffer, 0, -Infinity, Infinity, this.options) || [], 1, -buffer, 1 + buffer, 1, -Infinity, Infinity, this.options) || [];
1803
1911
  }
1804
1912
  /**
1805
1913
  * splits features from a parent tile to sub-tiles.
@@ -1939,8 +2047,7 @@ var TileIndex = class {
1939
2047
  function toID(z, x, y) {
1940
2048
  return ((1 << z) * y + x) * 32 + z;
1941
2049
  }
1942
- //#endregion
1943
- //#region src/geojsonvt.ts
2050
+ // src/geojsonvt.ts
1944
2051
  const defaultOptions = {
1945
2052
  maxZoom: 14,
1946
2053
  indexMaxZoom: 5,
@@ -1955,7 +2062,8 @@ const defaultOptions = {
1955
2062
  cluster: false,
1956
2063
  clusterOptions: defaultClusterOptions,
1957
2064
  debug: 0,
1958
- worldCoordinates: false
2065
+ coordinates: "lngLat",
2066
+ wrap: true
1959
2067
  };
1960
2068
  /**
1961
2069
  * Main class for creating and managing a vector tile index from GeoJSON data.
@@ -1963,11 +2071,14 @@ const defaultOptions = {
1963
2071
  var GeoJSONVT = class {
1964
2072
  constructor(data, options) {
1965
2073
  options = this.options = Object.assign({}, defaultOptions, options);
2074
+ this.worldCoordinateHelper = getWorldCoordinateHelper(options.coordinates);
1966
2075
  const debug = options.debug;
1967
2076
  if (debug) console.time("preprocess data");
1968
2077
  if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error("maxZoom should be in the 0-24 range");
1969
2078
  if (options.promoteId && options.generateId) throw new Error("promoteId and generateId cannot be used together.");
1970
- let features = convertToInternal(data, options);
2079
+ if (options.updateable) this.unlocated = /* @__PURE__ */ new Map();
2080
+ this.nextId = data.type === "FeatureCollection" ? data.features.length : 1;
2081
+ let features = convertToInternal(data, options, this.unlocated);
1971
2082
  if (debug) {
1972
2083
  console.timeEnd("preprocess data");
1973
2084
  console.log("index: maxZoom: %d, maxPoints: %d", options.indexMaxZoom, options.indexMaxPoints);
@@ -1978,7 +2089,11 @@ var GeoJSONVT = class {
1978
2089
  this.initializeIndex(features, options);
1979
2090
  }
1980
2091
  initializeIndex(features, options) {
1981
- this.tileIndex = options.cluster ? new ClusterTileIndex(options.clusterOptions, options.worldCoordinates) : new TileIndex(options);
2092
+ this.tileIndex = options.cluster ? new ClusterTileIndex({
2093
+ ...options.clusterOptions,
2094
+ coordinates: options.coordinates,
2095
+ wrap: options.wrap
2096
+ }) : new TileIndex(options);
1982
2097
  if (!features.length) return;
1983
2098
  this.tileIndex.initialize(features);
1984
2099
  }
@@ -2003,24 +2118,34 @@ var GeoJSONVT = class {
2003
2118
  updateData(diff, filter) {
2004
2119
  const options = this.options;
2005
2120
  if (!options.updateable) throw new Error("to update tile geojson `updateable` option must be set to true");
2006
- let { affected, source } = applySourceDiff(this.source, diff, options);
2121
+ if (options.generateId && diff.add) diff = {
2122
+ ...diff,
2123
+ add: diff.add.map((feature) => ({
2124
+ ...feature,
2125
+ id: this.nextId++
2126
+ }))
2127
+ };
2128
+ let { affected, source } = applySourceDiff(this.source, diff, options, this.unlocated);
2007
2129
  if (filter) ({affected, source} = this.filterUpdate(source, affected, filter));
2008
2130
  if (!affected.length) return;
2009
2131
  this.source = source;
2010
2132
  this.tileIndex.updateIndex(source, affected, options);
2011
2133
  }
2012
2134
  /**
2013
- * Filter an update using a predicate function. Returns the affected and updated source features.
2135
+ * Filter an update using a predicate function, the features an update emptied included, so that one the filter
2136
+ * rejects can't come back with a later update. A feature without a geometry at all isn't passed to the predicate.
2137
+ * Returns the affected and updated source features.
2014
2138
  */
2015
2139
  filterUpdate(source, affected, predicate) {
2016
2140
  const removeIds = /* @__PURE__ */ new Set();
2017
2141
  for (const feature of source) {
2018
2142
  if (feature.id == void 0) continue;
2019
- if (predicate(featureToGeoJSON(feature, this.options.worldCoordinates))) continue;
2143
+ if (predicate(featureToGeoJSON(feature, this.worldCoordinateHelper))) continue;
2020
2144
  affected.push(feature);
2021
2145
  removeIds.add(feature.id);
2022
2146
  }
2023
2147
  source = source.filter((feature) => !removeIds.has(feature.id));
2148
+ for (const [id, feature] of this.unlocated ?? []) if (feature.geometry && !predicate(feature)) this.unlocated.delete(id);
2024
2149
  return {
2025
2150
  affected,
2026
2151
  source
@@ -2031,12 +2156,12 @@ var GeoJSONVT = class {
2031
2156
  */
2032
2157
  getData() {
2033
2158
  if (!this.options.updateable) throw new Error("to retrieve data the `updateable` option must be set to true");
2034
- return convertToGeoJSON(this.source, this.options.worldCoordinates);
2159
+ return convertToGeoJSON(this.source, this.worldCoordinateHelper);
2035
2160
  }
2036
2161
  /**
2037
2162
  * Update supercluster options and regenerate the index.
2038
2163
  * @param cluster - whether to enable clustering
2039
- * @param clusterOptions - {@link SuperclusterOptions}
2164
+ * @param clusterOptions - {@link SuperclusterOptions} without `coordinates` and `wrap`, which come from the index options
2040
2165
  */
2041
2166
  updateClusterOptions(cluster, clusterOptions) {
2042
2167
  const wasCluster = this.options.cluster;
@@ -2075,8 +2200,7 @@ var GeoJSONVT = class {
2075
2200
  return this.tileIndex.getLeaves(clusterId, limit, offset);
2076
2201
  }
2077
2202
  };
2078
- //#endregion
2079
- //#region src/geojson-to-tile.ts
2203
+ // src/geojson-to-tile.ts
2080
2204
  /**
2081
2205
  * Converts GeoJSON data directly to a single vector tile without building a tile index.
2082
2206
  *
@@ -2112,9 +2236,10 @@ var GeoJSONVT = class {
2112
2236
  function geoJSONToTile(data, z, x, y, options = {}) {
2113
2237
  options = {
2114
2238
  ...defaultOptions,
2239
+ wrap: false,
2115
2240
  ...options
2116
2241
  };
2117
- const { wrap: shouldWrap = false, clip: shouldClip = false } = options;
2242
+ const { wrap: shouldWrap, clip: shouldClip = false } = options;
2118
2243
  let features = convertToInternal(data, options);
2119
2244
  if (shouldWrap) features = wrap(features, options);
2120
2245
  if (shouldClip || options.lineMetrics) {
@@ -2124,7 +2249,6 @@ function geoJSONToTile(data, z, x, y, options = {}) {
2124
2249
  }
2125
2250
  return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
2126
2251
  }
2127
- //#endregion
2128
2252
  export { GEOJSONVT_CLIP_END, GEOJSONVT_CLIP_START, GeoJSONVT, ClusterTileIndex as Supercluster, geoJSONToTile };
2129
2253
 
2130
2254
  //# sourceMappingURL=geojson-vt.mjs.map