@maplibre/geojson-vt 6.3.0 → 6.3.2

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
@@ -91,10 +90,10 @@ function createFeature(id, type, geom, tags) {
91
90
  type: data.type,
92
91
  geometry: data.geom,
93
92
  tags,
94
- minX: Infinity,
95
- minY: Infinity,
96
- maxX: -Infinity,
97
- maxY: -Infinity
93
+ minX: 1e400,
94
+ minY: 1e400,
95
+ maxX: -1e400,
96
+ maxY: -1e400
98
97
  };
99
98
  switch (data.type) {
100
99
  case "Point":
@@ -126,8 +125,7 @@ function calcLineBBox(feature, geom) {
126
125
  feature.maxY = Math.max(feature.maxY, geom[i + 1]);
127
126
  }
128
127
  }
129
- //#endregion
130
- //#region src/world-coordinate-helper.ts
128
+ // src/world-coordinate-helper.ts
131
129
  /**
132
130
  * Longitude and latitude, which spherical mercator puts in the world square.
133
131
  */
@@ -170,8 +168,7 @@ const identityWorldCoordinateHelper = new IdentityWorldCoordinateHelper();
170
168
  function getWorldCoordinateHelper(coordinates) {
171
169
  return coordinates === "unitSquare" ? identityWorldCoordinateHelper : mercatorWorldCoordinateHelper;
172
170
  }
173
- //#endregion
174
- //#region src/convert.ts
171
+ // src/convert.ts
175
172
  const MAX_GEOMETRY_COLLECTION_DEPTH = 1024;
176
173
  /**
177
174
  * converts GeoJSON to internal source features (an intermediate projected JSON vector format with simplification data)
@@ -328,8 +325,7 @@ function convertLines(rings, out, tolerance, isPolygon, worldCoordinateHelper) {
328
325
  out.push(geom);
329
326
  }
330
327
  }
331
- //#endregion
332
- //#region src/deconvert.ts
328
+ // src/deconvert.ts
333
329
  /**
334
330
  * Converts internal source features back to GeoJSON format.
335
331
  */
@@ -388,8 +384,7 @@ function unprojectPoints(coords, worldCoordinateHelper = mercatorWorldCoordinate
388
384
  function unprojectPoint(x, y, worldCoordinateHelper) {
389
385
  return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
390
386
  }
391
- //#endregion
392
- //#region src/clip.ts
387
+ // src/clip.ts
393
388
  /**
394
389
  * clip features between two vertical or horizontal axis-parallel lines:
395
390
  * | |
@@ -420,7 +415,6 @@ function clip(features, scale, start, end, axis, minAll, maxAll, options) {
420
415
  }
421
416
  if (max < start || min >= end) continue;
422
417
  switch (feature.type) {
423
- case "Point":
424
418
  case "MultiPoint":
425
419
  clipPointFeature(feature, clipped, start, end, axis);
426
420
  continue;
@@ -573,8 +567,7 @@ function intersectY(out, ax, ay, bx, by, y) {
573
567
  addPoint(out.points, ax + (bx - ax) * t, y, 1);
574
568
  return t;
575
569
  }
576
- //#endregion
577
- //#region src/wrap.ts
570
+ // src/wrap.ts
578
571
  function wrap(features, options) {
579
572
  if (!options.wrap) return features;
580
573
  const buffer = options.buffer / options.extent;
@@ -639,8 +632,7 @@ function shiftLineCoords(line, offset) {
639
632
  optimizeLineMemory(newLine);
640
633
  return newLine;
641
634
  }
642
- //#endregion
643
- //#region src/difference.ts
635
+ // src/difference.ts
644
636
  /**
645
637
  * Applies a GeoJSON Source Diff to an existing set of simplified features
646
638
  * @param source
@@ -670,7 +662,10 @@ function applySourceDiff(source, dataDiff, options, unlocated) {
670
662
  let addFeatures = convertToInternal({
671
663
  type: "FeatureCollection",
672
664
  features: Array.from(diff.add.values())
673
- }, options, unlocated);
665
+ }, {
666
+ ...options,
667
+ generateId: false
668
+ }, unlocated);
674
669
  addFeatures = wrap(addFeatures, options);
675
670
  affected = affected.concat(addFeatures);
676
671
  source = source.concat(addFeatures);
@@ -791,8 +786,7 @@ function diffToHashed(diff, options) {
791
786
  update: new Map(diff.update?.map((update) => [update.id, update]))
792
787
  };
793
788
  }
794
- //#endregion
795
- //#region node_modules/kdbush/index.js
789
+ // node_modules/kdbush/index.js
796
790
  const ARRAY_TYPES = [
797
791
  Int8Array,
798
792
  Uint8Array,
@@ -1076,8 +1070,7 @@ function sqDist(ax, ay, bx, by) {
1076
1070
  const dy = ay - by;
1077
1071
  return dx * dx + dy * dy;
1078
1072
  }
1079
- //#endregion
1080
- //#region src/cluster-tile-index.ts
1073
+ // src/cluster-tile-index.ts
1081
1074
  const defaultClusterOptions = {
1082
1075
  minZoom: 0,
1083
1076
  maxZoom: 16,
@@ -1621,8 +1614,7 @@ function getClusterProperties(data, i, clusterProps) {
1621
1614
  function isInWorld(geometry) {
1622
1615
  return geometry[0] >= 0 && geometry[0] <= 1 && geometry[1] >= 0 && geometry[1] <= 1;
1623
1616
  }
1624
- //#endregion
1625
- //#region src/tile.ts
1617
+ // src/tile.ts
1626
1618
  const GEOJSONVT_CLIP_START = "geojsonvt_clip_start";
1627
1619
  const GEOJSONVT_CLIP_END = "geojsonvt_clip_end";
1628
1620
  /**
@@ -1768,8 +1760,7 @@ function rewind(ring, clockwise) {
1768
1760
  ring[len - 1 - i] = y;
1769
1761
  }
1770
1762
  }
1771
- //#endregion
1772
- //#region src/transform.ts
1763
+ // src/transform.ts
1773
1764
  /**
1774
1765
  * Transforms the coordinates of each feature in the given tile from
1775
1766
  * mercator-projected space into (extent x extent) tile space.
@@ -1805,19 +1796,55 @@ function transformNonPointFeature(feature, extent, z2, tx, ty) {
1805
1796
  const transformed = feature;
1806
1797
  const geometry = feature.geometry;
1807
1798
  const nonPoint = [];
1808
- for (const geom of geometry) {
1809
- const ring = [];
1810
- for (let i = 0; i < geom.length; i += 2) ring.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
1811
- nonPoint.push(ring);
1799
+ for (const geom of geometry) if (feature.type === 3) nonPoint.push(transformPolygonRing(geom, extent, z2, tx, ty));
1800
+ else {
1801
+ const line = [];
1802
+ for (let i = 0; i < geom.length; i += 2) line.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
1803
+ nonPoint.push(line);
1812
1804
  }
1813
1805
  transformed.geometry = nonPoint;
1814
1806
  return transformed;
1815
1807
  }
1808
+ /**
1809
+ * Quantizes a polygon ring while preserving its winding so a hole does not become
1810
+ * a shell, or vice versa. Signed areas use coordinates relative to the first
1811
+ * vertex to reduce cancellation for narrow rings far from the origin.
1812
+ * Zero-area results are left unchanged; other quantization topology changes
1813
+ * are not repaired.
1814
+ */
1815
+ function transformPolygonRing(geometry, extent, z2, tx, ty) {
1816
+ if (!geometry.length) return [];
1817
+ const originX = geometry[0];
1818
+ const originY = geometry[1];
1819
+ const origin = transformPoint(originX, originY, extent, z2, tx, ty);
1820
+ const ring = [origin];
1821
+ let previousX = 0;
1822
+ let previousY = 0;
1823
+ let previousRoundedX = 0;
1824
+ let previousRoundedY = 0;
1825
+ let sourceArea = 0;
1826
+ let roundedArea = 0;
1827
+ for (let i = 2; i < geometry.length; i += 2) {
1828
+ const point = transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty);
1829
+ const x = geometry[i] - originX;
1830
+ const y = geometry[i + 1] - originY;
1831
+ const roundedX = point[0] - origin[0];
1832
+ const roundedY = point[1] - origin[1];
1833
+ sourceArea += previousX * y - x * previousY;
1834
+ roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
1835
+ previousX = x;
1836
+ previousY = y;
1837
+ previousRoundedX = roundedX;
1838
+ previousRoundedY = roundedY;
1839
+ ring.push(point);
1840
+ }
1841
+ if (sourceArea * roundedArea < 0) ring.reverse();
1842
+ return ring;
1843
+ }
1816
1844
  function transformPoint(x, y, extent, z2, tx, ty) {
1817
1845
  return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))];
1818
1846
  }
1819
- //#endregion
1820
- //#region src/tile-index.ts
1847
+ // src/tile-index.ts
1821
1848
  var TileIndex = class {
1822
1849
  constructor(options) {
1823
1850
  this.options = options;
@@ -1917,7 +1944,7 @@ var TileIndex = class {
1917
1944
  const { wrap, coordinates } = this.options;
1918
1945
  if (wrap && coordinates !== "unitSquare") return features;
1919
1946
  const buffer = this.options.buffer / this.options.extent;
1920
- return clip(wrap ? features : clip(features, 1, -buffer, 1 + buffer, 0, -Infinity, Infinity, this.options) || [], 1, -buffer, 1 + buffer, 1, -Infinity, Infinity, this.options) || [];
1947
+ return clip(wrap ? features : clip(features, 1, -buffer, 1 + buffer, 0, -1e400, 1e400, this.options) || [], 1, -buffer, 1 + buffer, 1, -1e400, 1e400, this.options) || [];
1921
1948
  }
1922
1949
  /**
1923
1950
  * splits features from a parent tile to sub-tiles.
@@ -2016,10 +2043,10 @@ var TileIndex = class {
2016
2043
  if (!features.length) return;
2017
2044
  const options = this.options;
2018
2045
  const { debug } = options;
2019
- let minX = Infinity;
2020
- let maxX = -Infinity;
2021
- let minY = Infinity;
2022
- let maxY = -Infinity;
2046
+ let minX = 1e400;
2047
+ let maxX = -1e400;
2048
+ let minY = 1e400;
2049
+ let maxY = -1e400;
2023
2050
  for (const feature of features) {
2024
2051
  minX = Math.min(minX, feature.minX);
2025
2052
  maxX = Math.max(maxX, feature.maxX);
@@ -2057,8 +2084,7 @@ var TileIndex = class {
2057
2084
  function toID(z, x, y) {
2058
2085
  return ((1 << z) * y + x) * 32 + z;
2059
2086
  }
2060
- //#endregion
2061
- //#region src/geojsonvt.ts
2087
+ // src/geojsonvt.ts
2062
2088
  const defaultOptions = {
2063
2089
  maxZoom: 14,
2064
2090
  indexMaxZoom: 5,
@@ -2088,6 +2114,7 @@ var GeoJSONVT = class {
2088
2114
  if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error("maxZoom should be in the 0-24 range");
2089
2115
  if (options.promoteId && options.generateId) throw new Error("promoteId and generateId cannot be used together.");
2090
2116
  if (options.updateable) this.unlocated = /* @__PURE__ */ new Map();
2117
+ this.nextId = data.type === "FeatureCollection" ? data.features.length : 1;
2091
2118
  let features = convertToInternal(data, options, this.unlocated);
2092
2119
  if (debug) {
2093
2120
  console.timeEnd("preprocess data");
@@ -2128,6 +2155,13 @@ var GeoJSONVT = class {
2128
2155
  updateData(diff, filter) {
2129
2156
  const options = this.options;
2130
2157
  if (!options.updateable) throw new Error("to update tile geojson `updateable` option must be set to true");
2158
+ if (options.generateId && diff.add) diff = {
2159
+ ...diff,
2160
+ add: diff.add.map((feature) => ({
2161
+ ...feature,
2162
+ id: this.nextId++
2163
+ }))
2164
+ };
2131
2165
  let { affected, source } = applySourceDiff(this.source, diff, options, this.unlocated);
2132
2166
  if (filter) ({affected, source} = this.filterUpdate(source, affected, filter));
2133
2167
  if (!affected.length) return;
@@ -2203,8 +2237,7 @@ var GeoJSONVT = class {
2203
2237
  return this.tileIndex.getLeaves(clusterId, limit, offset);
2204
2238
  }
2205
2239
  };
2206
- //#endregion
2207
- //#region src/geojson-to-tile.ts
2240
+ // src/geojson-to-tile.ts
2208
2241
  /**
2209
2242
  * Converts GeoJSON data directly to a single vector tile without building a tile index.
2210
2243
  *
@@ -2253,7 +2286,6 @@ function geoJSONToTile(data, z, x, y, options = {}) {
2253
2286
  }
2254
2287
  return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
2255
2288
  }
2256
- //#endregion
2257
2289
  export { GEOJSONVT_CLIP_END, GEOJSONVT_CLIP_START, GeoJSONVT, ClusterTileIndex as Supercluster, geoJSONToTile };
2258
2290
 
2259
2291
  //# sourceMappingURL=geojson-vt.mjs.map