@maplibre/geojson-vt 6.3.1 → 6.3.3

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.3",
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",
package/src/convert.ts CHANGED
@@ -140,6 +140,8 @@ function convertMultiLineStringFeature(features: GeoJSONVTInternalFeature[], id:
140
140
  if (options.lineMetrics) {
141
141
  // explode into linestrings to be able to track metrics
142
142
  for (const line of geom.coordinates) {
143
+ if (!line.length) continue;
144
+
143
145
  const out: SliceArray = {points: []};
144
146
  convertLine(line, out, tolerance, false, worldCoordinateHelper);
145
147
  features.push(createFeature(id, 'LineString', out, properties));
@@ -147,6 +149,8 @@ function convertMultiLineStringFeature(features: GeoJSONVTInternalFeature[], id:
147
149
  } else {
148
150
  const out: SliceArray[] = [];
149
151
  convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
152
+ if (!out.length) return;
153
+
150
154
  features.push(createFeature(id, 'MultiLineString', out, properties));
151
155
  }
152
156
  }
@@ -154,6 +158,8 @@ function convertMultiLineStringFeature(features: GeoJSONVTInternalFeature[], id:
154
158
  function convertPolygonFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.Polygon, tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
155
159
  const out: SliceArray[] = [];
156
160
  convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
161
+ if (!out.length) return;
162
+
157
163
  features.push(createFeature(id, 'Polygon', out, properties));
158
164
  }
159
165
 
@@ -205,6 +211,8 @@ function convertLine(ring: GeoJSON.Position[], out: SliceArray, tolerance: numbe
205
211
 
206
212
  function convertLines(rings: GeoJSON.Position[][], out: SliceArray[], tolerance: number, isPolygon: boolean, worldCoordinateHelper: WorldCoordinateHelper) {
207
213
  for (let i = 0; i < rings.length; i++) {
214
+ if (!rings[i].length) continue;
215
+
208
216
  const geom: SliceArray = {points: []};
209
217
  convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
210
218
  out.push(geom);
@@ -435,3 +435,41 @@ test('a unit square without wrap leaves the positions of a MultiPoint outside th
435
435
 
436
436
  expect(index.getTile(0, 0, 0).features.map(feature => feature.geometry)).toEqual([[[507, 256]]]);
437
437
  });
438
+
439
+ test('leaves out the empty rings of a polygon and the empty lines of a MultiLineString', () => {
440
+ const ring = [[0, 0], [10, 0], [10, 10], [0, 0]];
441
+ const data: GeoJSON.FeatureCollection = {
442
+ type: 'FeatureCollection',
443
+ features: [
444
+ {type: 'Feature', id: 'polygon', properties: {}, geometry: {type: 'Polygon', coordinates: [[], ring, []]}},
445
+ {type: 'Feature', id: 'polygons', properties: {}, geometry: {type: 'MultiPolygon', coordinates: [[[]], [ring, []]]}},
446
+ {type: 'Feature', id: 'lines', properties: {}, geometry: {type: 'MultiLineString', coordinates: [[], [[20, 0], [30, 5]]]}}
447
+ ]
448
+ };
449
+ const ringsOf = (index: GeoJSONVT) => (index.getData() as GeoJSON.FeatureCollection<GeoJSON.Polygon>).features.map(feature => [feature.id, feature.geometry.type, feature.geometry.coordinates.length]);
450
+
451
+ const index = new GeoJSONVT(data, {updateable: true, tolerance: 0});
452
+ expect(ringsOf(index)).toEqual([['polygon', 'Polygon', 1], ['polygons', 'MultiPolygon', 1], ['lines', 'MultiLineString', 1]]);
453
+ expect(index.getTile(0, 0, 0).features.map(feature => feature.geometry.length)).toEqual([1, 1, 1]);
454
+
455
+ const withMetrics = new GeoJSONVT(data, {updateable: true, tolerance: 0, lineMetrics: true});
456
+ expect(ringsOf(withMetrics)).toEqual([['polygon', 'Polygon', 1], ['polygons', 'MultiPolygon', 1], ['lines', 'LineString', 2]]);
457
+ });
458
+
459
+ test('takes a polygon or a MultiLineString with only empty rings as a feature without coordinates', () => {
460
+ const data: GeoJSON.FeatureCollection = {
461
+ type: 'FeatureCollection',
462
+ features: [
463
+ {type: 'Feature', id: 'polygon', properties: {}, geometry: {type: 'Polygon', coordinates: [[]]}},
464
+ {type: 'Feature', id: 'lines', properties: {}, geometry: {type: 'MultiLineString', coordinates: [[]]}}
465
+ ]
466
+ };
467
+ const index = new GeoJSONVT(data, {updateable: true, tolerance: 0});
468
+
469
+ expect((index.getData() as GeoJSON.FeatureCollection).features).toEqual([]);
470
+ expect(index.getTile(0, 0, 0)).toBeNull();
471
+
472
+ index.updateData({update: [{id: 'polygon', newGeometry: {type: 'Point', coordinates: [0, 0]}}]});
473
+ expect((index.getData() as GeoJSON.FeatureCollection).features.map(feature => feature.id)).toEqual(['polygon']);
474
+ });
475
+
@@ -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)),