@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.
@@ -10,6 +10,7 @@ export declare class GeoJSONVT {
10
10
  private source?;
11
11
  private unlocated?;
12
12
  private tileIndex;
13
+ private nextId;
13
14
  constructor(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions);
14
15
  private initializeIndex;
15
16
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"geojsonvt.d.ts","sourceRoot":"","sources":["../src/geojsonvt.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,KAAK,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAGvE,OAAO,KAAK,EAAC,qBAAqB,EAAgD,gBAAgB,EAAE,aAAa,EAAC,MAAM,eAAe,CAAC;AAExI,eAAO,MAAM,cAAc,EAAE,gBAgB5B,CAAC;AAEF;;GAEG;AACH,qBAAa,SAAS;IAClB,OAAO,CAAC,OAAO,CAAmB;IAClC,OAAO,CAAC,qBAAqB,CAAwB;IAErD,OAAO,CAAC,MAAM,CAAC,CAA6B;IAC5C,OAAO,CAAC,SAAS,CAAC,CAAwC;IAC1D,OAAO,CAAC,SAAS,CAAqB;gBAE1B,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,gBAAgB;IAkC7D,OAAO,CAAC,eAAe;IAMvB;;;;;;OAMG;IACH,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,aAAa,GAAG,IAAI;IAUzF;;;OAGG;IACH,UAAU,CAAC,IAAI,EAAE,mBAAmB,EAAE,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,KAAK,OAAO;IAqBpF;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAkBpB;;OAEG;IACH,OAAO,IAAI,OAAO,C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AGG;IACH,UAAU,CAAC,IAAI,EAAE,mBAAmB,EAAE,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,KAAK,OAAO;IAwBpF;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAkBpB;;OAEG;IACH,OAAO,IAAI,OAAO,CAAC,OAAO;IAK1B;;;;OAIG;IACH,oBAAoB,CAAC,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,gBAAgB,CAAC,gBAAgB,CAAC;IAazF;;;;OAIG;IACH,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIzD;;;;OAIG;IACH,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,qBAAqB,EAAE,GAAG,IAAI;IAIrE;;;;;;OAMG;IACH,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI;CAG9G"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=transform.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.test.d.ts","sourceRoot":"","sources":["../src/transform.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maplibre/geojson-vt",
3
- "version": "6.3.0",
3
+ "version": "6.3.2",
4
4
  "description": "Slice GeoJSON data into vector tiles efficiently",
5
5
  "type": "module",
6
6
  "exports": {
@@ -27,15 +27,15 @@
27
27
  "@eslint/js": "^10.0.1",
28
28
  "@types/benchmark": "^2.1.5",
29
29
  "@types/geojson": "^7946.0.16",
30
- "@types/node": "^26.6.5",
31
- "@vitest/coverage-v8": "^5.0.3",
30
+ "@types/node": "^26.6.6",
31
+ "@vitest/coverage-v8": "^5.1.0",
32
32
  "benchmark": "^2.1.4",
33
- "eslint": "^10.12.0",
34
- "rolldown": "^1.2.13",
33
+ "eslint": "^10.14.0",
34
+ "rolldown": "^1.3.2",
35
35
  "tsx": "^4.23.15",
36
36
  "typedoc": "^0.28.20",
37
37
  "typescript": "^6.0.3",
38
- "typescript-eslint": "^8.71.1",
38
+ "typescript-eslint": "^8.72.2",
39
39
  "vitest": "^5.0.3"
40
40
  },
41
41
  "license": "ISC",
package/src/clip.ts CHANGED
@@ -50,7 +50,6 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
50
50
  }
51
51
 
52
52
  switch (feature.type) {
53
- case 'Point':
54
53
  case 'MultiPoint': {
55
54
  clipPointFeature(feature, clipped, start, end, axis);
56
55
  continue;
package/src/difference.ts CHANGED
@@ -105,7 +105,8 @@ export function applySourceDiff(source: GeoJSONVTInternalFeature[], dataDiff: Ge
105
105
  }
106
106
 
107
107
  if (diff.add.size) {
108
- let addFeatures = convertToInternal({type: 'FeatureCollection', features: Array.from(diff.add.values())}, options, unlocated);
108
+ // added features keep their own ids, as generated ids are given to them before the diff is applied
109
+ let addFeatures = convertToInternal({type: 'FeatureCollection', features: Array.from(diff.add.values())}, {...options, generateId: false}, unlocated);
109
110
  addFeatures = wrap(addFeatures, options);
110
111
  affected = affected.concat(addFeatures);
111
112
  source = source.concat(addFeatures);
@@ -291,6 +291,18 @@ test('updateData leaves a feature without a geometry removed by removeAll when a
291
291
  expect(index.getTile(0, 0, 0).features.map(feature => feature.id)).toEqual(['kept']);
292
292
  });
293
293
 
294
+ test('updateData gives each feature added with generateId a new id that is not reused and can be removed', () => {
295
+ const point = (x: number): GeoJSON.Feature => ({type: 'Feature', properties: {x}, geometry: {type: 'Point', coordinates: [x, 0]}});
296
+ const index = new GeoJSONVT({type: 'FeatureCollection', features: [point(0), point(1)]}, {updateable: true, generateId: true});
297
+
298
+ index.updateData({add: [point(2), point(3)]});
299
+ index.updateData({remove: [0]});
300
+ index.updateData({add: [point(4)]});
301
+ index.updateData({remove: [2]});
302
+
303
+ expect((index.getData() as GeoJSON.FeatureCollection).features.map(feature => [feature.id, feature.properties.x])).toEqual([[1, 1], [3, 3], [4, 4]]);
304
+ });
305
+
294
306
  test('updateData keeps the property changes made to a feature while it has no geometry', () => {
295
307
  const renamed: GeoJSON.Feature = {type: 'Feature', id: 'renamed', properties: {name: 'before'}, geometry: null};
296
308
  const index = new GeoJSONVT({type: 'FeatureCollection', features: [renamed]}, {updateable: true});
package/src/geojsonvt.ts CHANGED
@@ -35,6 +35,7 @@ export class GeoJSONVT {
35
35
  private source?: GeoJSONVTInternalFeature[];
36
36
  private unlocated?: Map<string | number, GeoJSON.Feature>;
37
37
  private tileIndex: GeoJSONVTTileIndex;
38
+ private nextId: number;
38
39
 
39
40
  constructor(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions) {
40
41
  options = this.options = Object.assign({}, defaultOptions, options);
@@ -50,6 +51,9 @@ export class GeoJSONVT {
50
51
  // for updateable indexes, keep the properties of the features without coordinates, so that an update can give them some
51
52
  if (options.updateable) this.unlocated = new Map();
52
53
 
54
+ // with generateId, features added by an update get ids after the ones generated here
55
+ this.nextId = data.type === 'FeatureCollection' ? data.features.length : 1;
56
+
53
57
  // projects and adds simplification info
54
58
  let features = convertToInternal(data, options, this.unlocated);
55
59
 
@@ -102,6 +106,9 @@ export class GeoJSONVT {
102
106
 
103
107
  if (!options.updateable) throw new Error('to update tile geojson `updateable` option must be set to true');
104
108
 
109
+ // give added features new generated ids, so none of them share or reuse an id
110
+ if (options.generateId && diff.add) diff = {...diff, add: diff.add.map(feature => ({...feature, id: this.nextId++}))};
111
+
105
112
  // apply diff and collect affected features and updated source that will be used to invalidate tiles
106
113
  let {affected, source} = applySourceDiff(this.source, diff, options, this.unlocated);
107
114
 
@@ -0,0 +1,36 @@
1
+ import {expect, test} from 'vitest';
2
+ import {GeoJSONVT} from '.';
3
+ import {transformTile} from './transform';
4
+
5
+ test('quantization preserves shell and hole winding without changing ordinary or collapsed rings', () => {
6
+ const outer = [[100, 100], [900, 100], [900, 900], [100, 900], [100, 100]];
7
+ const hole = [[400, 400], [400, 600], [600, 600], [600, 400], [400, 400]];
8
+ // Signed area changes from -5 to +25 when these positions are rounded.
9
+ const thinHole = [[200.1, 200.1], [250.1, 200.4], [300.1, 200.6], [200.1, 200.1]];
10
+ const roundedThinHole = [[200, 200], [300, 201], [250, 200], [200, 200]];
11
+ const collapsed = [[200.1, 300.1], [250.1, 300.2], [300.1, 300.1], [200.1, 300.1]];
12
+ const thinShell = thinHole.slice().reverse().map(([x, y]) => [x + 2000, y]);
13
+ const roundedThinShell = roundedThinHole.slice().reverse().map(([x, y]) => [x + 2000, y]);
14
+ const unitSquare = (ring: number[][]) => ring.map(([x, y]) => [(4800 + x / 8192) / (1 << 13), (3000 + y / 8192) / (1 << 13)]);
15
+ const data: GeoJSON.MultiPolygon = {
16
+ type: 'MultiPolygon',
17
+ coordinates: [[outer, thinHole, hole, collapsed].map(unitSquare), [unitSquare(thinShell)]]
18
+ };
19
+ const index = new GeoJSONVT(data, {coordinates: 'unitSquare', wrap: false, extent: 8192, tolerance: 0});
20
+
21
+ expect(index.getTile(13, 4800, 3000).features[0].geometry).toEqual([
22
+ outer, roundedThinHole, hole,
23
+ [[200, 300], [250, 300], [300, 300], [200, 300]], roundedThinShell
24
+ ]);
25
+ });
26
+
27
+ test('transformTile retains empty polygon rings', () => {
28
+ const tile = transformTile({
29
+ transformed: false,
30
+ source: null,
31
+ x: 0, y: 0, z: 0,
32
+ features: [{type: 3, tags: null, geometry: [[]]}]
33
+ }, 8192);
34
+
35
+ expect(tile.features[0].geometry).toEqual([[]]);
36
+ });
package/src/transform.ts CHANGED
@@ -53,17 +53,61 @@ function transformNonPointFeature(feature: GeoJSONVTInternalTileFeatureNonPoint,
53
53
  const geometry = feature.geometry;
54
54
  const nonPoint: GeoJSONVTFeatureNonPoint["geometry"] = [];
55
55
  for (const geom of geometry) {
56
- const ring: GeoJSONVTFeaturePoint["geometry"] = [];
57
- for (let i = 0; i < geom.length; i += 2) {
58
- ring.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
56
+ if (feature.type === 3) {
57
+ nonPoint.push(transformPolygonRing(geom, extent, z2, tx, ty));
58
+ } else {
59
+ const line: GeoJSONVTFeaturePoint["geometry"] = [];
60
+ for (let i = 0; i < geom.length; i += 2) {
61
+ line.push(transformPoint(geom[i], geom[i + 1], extent, z2, tx, ty));
62
+ }
63
+ nonPoint.push(line);
59
64
  }
60
- nonPoint.push(ring);
61
65
  }
62
66
  transformed.geometry = nonPoint;
63
67
 
64
68
  return transformed;
65
69
  }
66
70
 
71
+ /**
72
+ * Quantizes a polygon ring while preserving its winding so a hole does not become
73
+ * a shell, or vice versa. Signed areas use coordinates relative to the first
74
+ * vertex to reduce cancellation for narrow rings far from the origin.
75
+ * Zero-area results are left unchanged; other quantization topology changes
76
+ * are not repaired.
77
+ */
78
+ function transformPolygonRing(geometry: number[], extent: number, z2: number, tx: number, ty: number): [number, number][] {
79
+ if (!geometry.length) return [];
80
+
81
+ const originX = geometry[0];
82
+ const originY = geometry[1];
83
+ const origin = transformPoint(originX, originY, extent, z2, tx, ty);
84
+ const ring = [origin];
85
+ let previousX = 0;
86
+ let previousY = 0;
87
+ let previousRoundedX = 0;
88
+ let previousRoundedY = 0;
89
+ let sourceArea = 0;
90
+ let roundedArea = 0;
91
+
92
+ for (let i = 2; i < geometry.length; i += 2) {
93
+ const point = transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty);
94
+ const x = geometry[i] - originX;
95
+ const y = geometry[i + 1] - originY;
96
+ const roundedX = point[0] - origin[0];
97
+ const roundedY = point[1] - origin[1];
98
+ sourceArea += previousX * y - x * previousY;
99
+ roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
100
+ previousX = x;
101
+ previousY = y;
102
+ previousRoundedX = roundedX;
103
+ previousRoundedY = roundedY;
104
+ ring.push(point);
105
+ }
106
+
107
+ if (sourceArea * roundedArea < 0) ring.reverse();
108
+ return ring;
109
+ }
110
+
67
111
  function transformPoint(x: number, y: number, extent: number, z2: number, tx: number, ty: number): [number, number] {
68
112
  return [
69
113
  Math.round(extent * (x * z2 - tx)),