@maplibre/geojson-vt 6.3.1 → 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.
@@ -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.1",
3
+ "version": "6.3.2",
4
4
  "description": "Slice GeoJSON data into vector tiles efficiently",
5
5
  "type": "module",
6
6
  "exports": {
@@ -31,11 +31,11 @@
31
31
  "@vitest/coverage-v8": "^5.1.0",
32
32
  "benchmark": "^2.1.4",
33
33
  "eslint": "^10.14.0",
34
- "rolldown": "^1.3.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.72.1",
38
+ "typescript-eslint": "^8.72.2",
39
39
  "vitest": "^5.0.3"
40
40
  },
41
41
  "license": "ISC",
@@ -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)),