@maplibre/geojson-vt 6.3.3 → 7.0.0

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.
Files changed (44) hide show
  1. package/dist/clip.d.ts.map +1 -1
  2. package/dist/cluster-tile-index.d.ts +8 -0
  3. package/dist/cluster-tile-index.d.ts.map +1 -1
  4. package/dist/convert.d.ts.map +1 -1
  5. package/dist/deconvert.d.ts +0 -1
  6. package/dist/deconvert.d.ts.map +1 -1
  7. package/dist/definitions.d.ts +66 -25
  8. package/dist/definitions.d.ts.map +1 -1
  9. package/dist/feature.d.ts +16 -12
  10. package/dist/feature.d.ts.map +1 -1
  11. package/dist/geojson-vt-dev.js +606 -461
  12. package/dist/geojson-vt.js +1 -1
  13. package/dist/geojson-vt.mjs +603 -458
  14. package/dist/geojson-vt.mjs.map +1 -1
  15. package/dist/index.d.ts +2 -3
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/simplify.d.ts +1 -1
  18. package/dist/simplify.d.ts.map +1 -1
  19. package/dist/tile-index.d.ts.map +1 -1
  20. package/dist/tile.d.ts +5 -2
  21. package/dist/tile.d.ts.map +1 -1
  22. package/dist/transform.d.ts +5 -6
  23. package/dist/transform.d.ts.map +1 -1
  24. package/dist/wrap.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/src/clip.test.ts +52 -32
  27. package/src/clip.ts +225 -143
  28. package/src/cluster-tile-index.ts +29 -21
  29. package/src/convert.ts +77 -72
  30. package/src/deconvert.test.ts +29 -28
  31. package/src/deconvert.ts +51 -23
  32. package/src/definitions.ts +69 -17
  33. package/src/difference.test.ts +38 -75
  34. package/src/feature.ts +53 -51
  35. package/src/geojson-to-tile.ts +1 -1
  36. package/src/geojsonvt.test.ts +75 -0
  37. package/src/index.ts +1 -17
  38. package/src/simplify.ts +2 -2
  39. package/src/tile-index.test.ts +3 -9
  40. package/src/tile-index.ts +11 -10
  41. package/src/tile.ts +212 -92
  42. package/src/transform.test.ts +2 -3
  43. package/src/transform.ts +24 -99
  44. package/src/wrap.ts +12 -73
package/src/convert.ts CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  import {simplify} from './simplify';
3
- import {createFeature, optimizeLineMemory} from './feature';
3
+ import {createFeature, createPointFeature} from './feature';
4
4
  import {getWorldCoordinateHelper, type WorldCoordinateHelper} from './world-coordinate-helper';
5
- import type {GeoJSONVTInternalFeature, GeoJSONVTOptions, SliceArray} from './definitions';
5
+ import type {GeoJSONVTInternalFeature, GeoJSONVTOptions, GeoJSONVTSourceCoords} from './definitions';
6
6
 
7
7
  const MAX_GEOMETRY_COLLECTION_DEPTH = 1024;
8
8
 
@@ -67,7 +67,7 @@ function featureToInternal(features: GeoJSONVTInternalFeature[], geojson: GeoJSO
67
67
 
68
68
  switch (geojson.geometry.type) {
69
69
  case 'Point':
70
- convertPointFeature(features, id, geojson.geometry, worldCoordinateHelper, geojson.properties);
70
+ features.push(createPointFeature(id, worldCoordinateHelper.projectX(geojson.geometry.coordinates[0]), worldCoordinateHelper.projectY(geojson.geometry.coordinates[1]), geojson.properties));
71
71
  return;
72
72
 
73
73
  case 'MultiPoint':
@@ -75,19 +75,26 @@ function featureToInternal(features: GeoJSONVTInternalFeature[], geojson: GeoJSO
75
75
  return;
76
76
 
77
77
  case 'LineString':
78
- convertLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
78
+ convertLineStringFeature(features, id, geojson.geometry.coordinates, tolerance, worldCoordinateHelper, options, geojson.properties);
79
79
  return;
80
80
 
81
81
  case 'MultiLineString':
82
- convertMultiLineStringFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, options, geojson.properties);
82
+ if (options.lineMetrics) {
83
+ // explode into linestrings to be able to track metrics
84
+ for (const line of geojson.geometry.coordinates) {
85
+ convertLineStringFeature(features, id, line, tolerance, worldCoordinateHelper, options, geojson.properties);
86
+ }
87
+ return;
88
+ }
89
+ convertRingsFeature(features, id, 'MultiLineString', [geojson.geometry.coordinates], tolerance, worldCoordinateHelper, geojson.properties);
83
90
  return;
84
91
 
85
92
  case 'Polygon':
86
- convertPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
93
+ convertRingsFeature(features, id, 'Polygon', [geojson.geometry.coordinates], tolerance, worldCoordinateHelper, geojson.properties);
87
94
  return;
88
95
 
89
96
  case 'MultiPolygon':
90
- convertMultiPolygonFeature(features, id, geojson.geometry, tolerance, worldCoordinateHelper, geojson.properties);
97
+ convertRingsFeature(features, id, 'MultiPolygon', geojson.geometry.coordinates, tolerance, worldCoordinateHelper, geojson.properties);
91
98
  return;
92
99
 
93
100
  default:
@@ -116,76 +123,84 @@ function convertGeometryCollection(features: GeoJSONVTInternalFeature[], geojson
116
123
  }
117
124
  }
118
125
 
119
- function convertPointFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.Point, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
120
- const out: number[] = [];
121
- out.push(worldCoordinateHelper.projectX(geom.coordinates[0]), worldCoordinateHelper.projectY(geom.coordinates[1]), 0);
122
- features.push(createFeature(id, 'Point', out, properties));
123
- }
124
-
125
126
  function convertMultiPointFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.MultiPoint, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
126
- const out: number[] = [];
127
- for (const coords of geom.coordinates) {
128
- out.push(worldCoordinateHelper.projectX(coords[0]), worldCoordinateHelper.projectY(coords[1]), 0);
127
+ const out = new Float64Array(geom.coordinates.length * 3);
128
+ for (let i = 0; i < geom.coordinates.length; i++) {
129
+ out[i * 3] = worldCoordinateHelper.projectX(geom.coordinates[i][0]);
130
+ out[i * 3 + 1] = worldCoordinateHelper.projectY(geom.coordinates[i][1]);
129
131
  }
130
132
  features.push(createFeature(id, 'MultiPoint', out, properties));
131
133
  }
132
134
 
133
- function convertLineStringFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.LineString, tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
134
- const out: SliceArray = {points: []};
135
- convertLine(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
136
- features.push(createFeature(id, 'LineString', out, properties));
137
- }
135
+ function convertLineStringFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, line: GeoJSON.Position[], tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, options: GeoJSONVTOptions, properties: GeoJSON.GeoJsonProperties) {
136
+ if (!line.length) return;
138
137
 
139
- function convertMultiLineStringFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.MultiLineString, tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, options: GeoJSONVTOptions, properties: GeoJSON.GeoJsonProperties) {
140
- if (options.lineMetrics) {
141
- // explode into linestrings to be able to track metrics
142
- for (const line of geom.coordinates) {
143
- if (!line.length) continue;
138
+ const out = new Float64Array(2 + line.length * 3);
139
+ convertRing(line, out, 0, tolerance, false, false, worldCoordinateHelper);
144
140
 
145
- const out: SliceArray = {points: []};
146
- convertLine(line, out, tolerance, false, worldCoordinateHelper);
147
- features.push(createFeature(id, 'LineString', out, properties));
148
- }
149
- } else {
150
- const out: SliceArray[] = [];
151
- convertLines(geom.coordinates, out, tolerance, false, worldCoordinateHelper);
152
- if (!out.length) return;
153
-
154
- features.push(createFeature(id, 'MultiLineString', out, properties));
141
+ const feature = createFeature(id, 'LineString', out, properties);
142
+ if (options.lineMetrics) {
143
+ feature.start = 0;
144
+ feature.end = feature.size = out[1];
155
145
  }
146
+ features.push(feature);
156
147
  }
157
148
 
158
- function convertPolygonFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.Polygon, tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
159
- const out: SliceArray[] = [];
160
- convertLines(geom.coordinates, out, tolerance, true, worldCoordinateHelper);
161
- if (!out.length) return;
149
+ /**
150
+ * Converts a MultiLineString, a Polygon or a MultiPolygon, whose rings come in groups: all lines in one,
151
+ * and the rings of one polygon in each. The first ring of a polygon is its outer ring, and the rest are its holes.
152
+ */
153
+ function convertRingsFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, type: 'MultiLineString' | 'Polygon' | 'MultiPolygon', groups: GeoJSON.Position[][][], tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
154
+ let length = 0;
155
+ for (const rings of groups) {
156
+ for (const ring of rings) {
157
+ if (ring.length) length += 2 + ring.length * 3;
158
+ }
159
+ }
160
+ if (!length) return;
161
+
162
+ const isPolygon = type !== 'MultiLineString';
163
+ const out = new Float64Array(length);
164
+ let index = 0;
165
+ for (const rings of groups) {
166
+ let isOuter = true;
167
+ for (const ring of rings) {
168
+ if (!ring.length) continue;
169
+ index = convertRing(ring, out, index, tolerance, isPolygon, isOuter, worldCoordinateHelper);
170
+ isOuter = false;
171
+ }
172
+ }
162
173
 
163
- features.push(createFeature(id, 'Polygon', out, properties));
174
+ features.push(createFeature(id, type, out, properties));
164
175
  }
165
176
 
166
- function convertMultiPolygonFeature(features: GeoJSONVTInternalFeature[], id: number | string | undefined, geom: GeoJSON.MultiPolygon, tolerance: number, worldCoordinateHelper: WorldCoordinateHelper, properties: GeoJSON.GeoJsonProperties) {
167
- const out: SliceArray[][] = [];
168
- for (const polygon of geom.coordinates) {
169
- const polygonOut: SliceArray[] = [];
170
- convertLines(polygon, polygonOut, tolerance, true, worldCoordinateHelper);
171
- if (!polygonOut.length) continue;
172
-
173
- out.push(polygonOut);
174
- }
175
- if (!out.length) return;
176
-
177
- features.push(createFeature(id, 'MultiPolygon', out, properties));
177
+ /**
178
+ * The size a hole is stored with, which has to be negative for {@link isHole} to tell it from an outer ring.
179
+ * A hole without area gets -0, and one whose area is not a number gets -Infinity. Both compare with a tolerance
180
+ * the way 0 and NaN do.
181
+ */
182
+ function toHoleSize(size: number): number {
183
+ const magnitude = Math.abs(size);
184
+ return Number.isNaN(magnitude) ? -Infinity : -magnitude;
178
185
  }
179
186
 
180
- function convertLine(ring: GeoJSON.Position[], out: SliceArray, tolerance: number, isPolygon: boolean, worldCoordinateHelper: WorldCoordinateHelper) {
187
+ /**
188
+ * Writes a ring and the two values before it, see {@link GeoJSONVTSourceCoords}.
189
+ * @returns where the next ring starts
190
+ */
191
+ function convertRing(ring: GeoJSON.Position[], out: GeoJSONVTSourceCoords, index: number, tolerance: number, isPolygon: boolean, isOuter: boolean, worldCoordinateHelper: WorldCoordinateHelper): number {
192
+ const first = index + 2;
181
193
  let x0, y0;
182
194
  let size = 0;
195
+ let next = first;
183
196
 
184
197
  for (let j = 0; j < ring.length; j++) {
185
198
  const x = worldCoordinateHelper.projectX(ring[j][0]);
186
199
  const y = worldCoordinateHelper.projectY(ring[j][1]);
187
200
 
188
- out.points.push(x, y, 0);
201
+ out[next] = x;
202
+ out[next + 1] = y;
203
+ next += 3;
189
204
 
190
205
  if (j > 0) {
191
206
  if (isPolygon) {
@@ -198,23 +213,13 @@ function convertLine(ring: GeoJSON.Position[], out: SliceArray, tolerance: numbe
198
213
  y0 = y;
199
214
  }
200
215
 
201
- const last = out.points.length - 3;
202
- out.points[2] = 1;
203
- if (tolerance > 0) simplify(out.points, 0, last, tolerance);
204
- out.points[last + 2] = 1;
216
+ const last = next - 3;
217
+ out[first + 2] = 1;
218
+ if (tolerance > 0) simplify(out, first, last, tolerance);
219
+ out[last + 2] = 1;
205
220
 
206
- optimizeLineMemory(out);
207
- out.size = Math.abs(size);
208
- out.start = 0;
209
- out.end = out.size;
210
- }
211
-
212
- function convertLines(rings: GeoJSON.Position[][], out: SliceArray[], tolerance: number, isPolygon: boolean, worldCoordinateHelper: WorldCoordinateHelper) {
213
- for (let i = 0; i < rings.length; i++) {
214
- if (!rings[i].length) continue;
221
+ out[index] = ring.length;
222
+ out[index + 1] = isPolygon && !isOuter ? toHoleSize(size) : Math.abs(size);
215
223
 
216
- const geom: SliceArray = {points: []};
217
- convertLine(rings[i], geom, tolerance, isPolygon, worldCoordinateHelper);
218
- out.push(geom);
219
- }
224
+ return next;
220
225
  }
@@ -28,9 +28,9 @@ test('featureToGeoJSON: converts Point geometry', () => {
28
28
  const feature: GeoJSONVTInternalFeature = {
29
29
  type: 'Point',
30
30
  id: 'point1',
31
- geometry: [0.5, 0.5, 0],
32
- tags: {name: 'Test Point'},
33
- minX: 0.5, minY: 0.5, maxX: 0.5, maxY: 0.5
31
+ x: 0.5,
32
+ y: 0.5,
33
+ tags: {name: 'Test Point'}
34
34
  };
35
35
 
36
36
  const result = featureToGeoJSON(feature);
@@ -46,9 +46,9 @@ test('featureToGeoJSON: converts MultiPoint geometry', () => {
46
46
  const feature: GeoJSONVTInternalFeature = {
47
47
  type: 'MultiPoint',
48
48
  id: 'multipoint1',
49
- geometry: [0.5, 0.5, 0, 0.525, 0.5, 0],
49
+ geometry: new Float64Array([0.5, 0.5, 0, 0.525, 0.5, 0]),
50
50
  tags: {},
51
- minX: 0.5, minY: 0.5, maxX: 0.525, maxY: 0.5
51
+ minX: 0.5, minY: 0.5, maxX: 0.525, maxY: 0.5, worldOffset: 0
52
52
  };
53
53
 
54
54
  const result = featureToGeoJSON(feature);
@@ -61,9 +61,9 @@ test('featureToGeoJSON: converts LineString geometry', () => {
61
61
  const feature: GeoJSONVTInternalFeature = {
62
62
  type: 'LineString',
63
63
  id: 'line1',
64
- geometry: {points: [0.5, 0.5, 0, 0.525, 0.5, 0, 0.525, 0.525, 0]},
64
+ geometry: new Float64Array([3, 0.05, 0.5, 0.5, 0, 0.525, 0.5, 0, 0.525, 0.525, 0]),
65
65
  tags: {highway: 'primary'},
66
- minX: 0.5, minY: 0.5, maxX: 0.525, maxY: 0.525
66
+ minX: 0.5, minY: 0.5, maxX: 0.525, maxY: 0.525, worldOffset: 0
67
67
  };
68
68
 
69
69
  const result = featureToGeoJSON(feature);
@@ -76,12 +76,12 @@ test('featureToGeoJSON: converts MultiLineString geometry', () => {
76
76
  const feature: GeoJSONVTInternalFeature = {
77
77
  type: 'MultiLineString',
78
78
  id: 'multiline1',
79
- geometry: [
80
- {points: [0.5, 0.5, 0, 0.525, 0.5, 0]},
81
- {points: [0.55, 0.55, 0, 0.575, 0.55, 0]}
82
- ],
79
+ geometry: new Float64Array([
80
+ 2, 0.025, 0.5, 0.5, 0, 0.525, 0.5, 0,
81
+ 2, 0.025, 0.55, 0.55, 0, 0.575, 0.55, 0
82
+ ]),
83
83
  tags: {},
84
- minX: 0.5, minY: 0.5, maxX: 0.575, maxY: 0.55
84
+ minX: 0.5, minY: 0.5, maxX: 0.575, maxY: 0.55, worldOffset: 0
85
85
  };
86
86
 
87
87
  const result = featureToGeoJSON(feature);
@@ -94,12 +94,12 @@ test('featureToGeoJSON: converts Polygon geometry', () => {
94
94
  const feature: GeoJSONVTInternalFeature = {
95
95
  type: 'Polygon',
96
96
  id: 'polygon1',
97
- geometry: [
98
- {points: [0.5, 0.5, 0, 0.6, 0.5, 0, 0.6, 0.6, 0, 0.5, 0.6, 0, 0.5, 0.5, 0]},
99
- {points: [0.52, 0.52, 0, 0.58, 0.52, 0, 0.58, 0.58, 0, 0.52, 0.58, 0, 0.52, 0.52, 0]}
100
- ],
97
+ geometry: new Float64Array([
98
+ 5, 0.01, 0.5, 0.5, 0, 0.6, 0.5, 0, 0.6, 0.6, 0, 0.5, 0.6, 0, 0.5, 0.5, 0,
99
+ 5, -0.0036, 0.52, 0.52, 0, 0.58, 0.52, 0, 0.58, 0.58, 0, 0.52, 0.58, 0, 0.52, 0.52, 0
100
+ ]),
101
101
  tags: {landuse: 'residential'},
102
- minX: 0.5, minY: 0.5, maxX: 0.6, maxY: 0.6
102
+ minX: 0.5, minY: 0.5, maxX: 0.6, maxY: 0.6, worldOffset: 0
103
103
  };
104
104
 
105
105
  const result = featureToGeoJSON(feature);
@@ -112,24 +112,25 @@ test('featureToGeoJSON: converts MultiPolygon geometry', () => {
112
112
  const feature: GeoJSONVTInternalFeature = {
113
113
  type: 'MultiPolygon',
114
114
  id: 'multipolygon1',
115
- geometry: [
116
- [{points: [0.5, 0.5, 0, 0.52, 0.5, 0, 0.52, 0.52, 0, 0.5, 0.52, 0, 0.5, 0.5, 0]}],
117
- [{points: [0.55, 0.55, 0, 0.57, 0.55, 0, 0.57, 0.57, 0, 0.55, 0.57, 0, 0.55, 0.55, 0]}]
118
- ],
115
+ geometry: new Float64Array([
116
+ 5, 0.0004, 0.5, 0.5, 0, 0.52, 0.5, 0, 0.52, 0.52, 0, 0.5, 0.52, 0, 0.5, 0.5, 0,
117
+ 4, -0.00005, 0.505, 0.505, 0, 0.515, 0.505, 0, 0.515, 0.515, 0, 0.505, 0.505, 0,
118
+ 5, 0.0004, 0.55, 0.55, 0, 0.57, 0.55, 0, 0.57, 0.57, 0, 0.55, 0.57, 0, 0.55, 0.55, 0
119
+ ]),
119
120
  tags: {},
120
- minX: 0.5, minY: 0.5, maxX: 0.57, maxY: 0.57
121
+ minX: 0.5, minY: 0.5, maxX: 0.57, maxY: 0.57, worldOffset: 0
121
122
  };
122
123
 
123
124
  const result = featureToGeoJSON(feature);
124
125
 
125
126
  expect(result.geometry.type).toBe('MultiPolygon');
126
- expect((result.geometry as GeoJSON.MultiPolygon).coordinates.length).toBe(2);
127
+ expect((result.geometry as GeoJSON.MultiPolygon).coordinates.map(polygon => polygon.length)).toEqual([2, 1]);
127
128
  });
128
129
 
129
130
  test('featureToGeoJSON: handles various id types', () => {
130
- const noId: GeoJSONVTInternalFeature = {type: 'Point', geometry: [0.5, 0.5, 0], tags: {}, minX: 0.5, minY: 0.5, maxX: 0.5, maxY: 0.5};
131
- const stringId: GeoJSONVTInternalFeature = {type: 'Point', id: 'string-id', geometry: [0.5, 0.5, 0], tags: {}, minX: 0.5, minY: 0.5, maxX: 0.5, maxY: 0.5};
132
- const numberId: GeoJSONVTInternalFeature = {type: 'Point', id: 42, geometry: [0.5, 0.5, 0], tags: {}, minX: 0.5, minY: 0.5, maxX: 0.5, maxY: 0.5};
131
+ const noId: GeoJSONVTInternalFeature = {type: 'Point', x: 0.5, y: 0.5, tags: {}};
132
+ const stringId: GeoJSONVTInternalFeature = {type: 'Point', id: 'string-id', x: 0.5, y: 0.5, tags: {}};
133
+ const numberId: GeoJSONVTInternalFeature = {type: 'Point', id: 42, x: 0.5, y: 0.5, tags: {}};
133
134
 
134
135
  expect(featureToGeoJSON(noId).id).toBeUndefined();
135
136
  expect(featureToGeoJSON(stringId).id).toBe('string-id');
@@ -139,9 +140,9 @@ test('featureToGeoJSON: handles various id types', () => {
139
140
  test('featureToGeoJSON: correctly unprojects known coordinates', () => {
140
141
  const feature: GeoJSONVTInternalFeature = {
141
142
  type: 'MultiPoint',
142
- geometry: [0.5, 0.5, 0, 0, 0.5, 0, 1, 0.5, 0],
143
+ geometry: new Float64Array([0.5, 0.5, 0, 0, 0.5, 0, 1, 0.5, 0]),
143
144
  tags: {},
144
- minX: 0, minY: 0.5, maxX: 1, maxY: 0.5
145
+ minX: 0, minY: 0.5, maxX: 1, maxY: 0.5, worldOffset: 0
145
146
  };
146
147
 
147
148
  const result = featureToGeoJSON(feature);
package/src/deconvert.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {mercatorWorldCoordinateHelper, type WorldCoordinateHelper} from './world-coordinate-helper';
2
- import type {GeoJSONVTInternalFeature} from './definitions';
2
+ import type {GeoJSONVTInternalFeature, GeoJSONVTSourceCoords} from './definitions';
3
+ import {isHole} from './feature';
3
4
 
4
5
  /**
5
6
  * Converts internal source features back to GeoJSON format.
@@ -33,50 +34,77 @@ export function featureToGeoJSON(feature: GeoJSONVTInternalFeature, worldCoordin
33
34
  * Converts a single internal feature geometry to GeoJSON format.
34
35
  */
35
36
  function geometryToGeoJSON(feature: GeoJSONVTInternalFeature, worldCoordinateHelper: WorldCoordinateHelper): GeoJSON.Geometry {
36
- const {type, geometry} = feature;
37
-
38
- switch (type) {
37
+ switch (feature.type) {
39
38
  case 'Point':
40
39
  return {
41
- type: type,
42
- coordinates: unprojectPoint(geometry[0], geometry[1], worldCoordinateHelper)
40
+ type: feature.type,
41
+ coordinates: [worldCoordinateHelper.unprojectX(feature.x), worldCoordinateHelper.unprojectY(feature.y)]
43
42
  };
44
43
  case 'MultiPoint':
45
44
  return {
46
- type: type,
47
- coordinates: unprojectPoints(geometry, worldCoordinateHelper)
45
+ type: feature.type,
46
+ coordinates: unprojectPoints(feature.geometry, feature.worldOffset, 0, feature.geometry.length, worldCoordinateHelper)
48
47
  };
49
48
  case 'LineString':
50
49
  return {
51
- type: type,
52
- coordinates: unprojectPoints(geometry.points, worldCoordinateHelper)
50
+ type: feature.type,
51
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)[0]
53
52
  };
54
53
  case 'MultiLineString':
55
54
  case 'Polygon':
56
55
  return {
57
- type: type,
58
- coordinates: geometry.map(ring => unprojectPoints(ring.points, worldCoordinateHelper))
56
+ type: feature.type,
57
+ coordinates: unprojectRings(feature.geometry, feature.worldOffset, worldCoordinateHelper)
59
58
  };
60
59
  case 'MultiPolygon':
61
60
  return {
62
- type: type,
63
- coordinates: geometry.map(polygon =>
64
- polygon.map(ring => unprojectPoints(ring.points, worldCoordinateHelper))
65
- )
61
+ type: feature.type,
62
+ coordinates: unprojectPolygons(feature.geometry, feature.worldOffset, worldCoordinateHelper)
66
63
  };
67
64
  }
68
65
  }
69
66
 
70
- export function unprojectPoints(coords: number[] | Float64Array, worldCoordinateHelper: WorldCoordinateHelper = mercatorWorldCoordinateHelper): GeoJSON.Position[] {
71
- const result: GeoJSON.Position[] = [];
67
+ function unprojectRings(coords: GeoJSONVTSourceCoords, offset: number, worldCoordinateHelper: WorldCoordinateHelper): GeoJSON.Position[][] {
68
+ const rings: GeoJSON.Position[][] = [];
72
69
 
73
- for (let i = 0; i < coords.length; i += 3) {
74
- result.push(unprojectPoint(coords[i], coords[i + 1], worldCoordinateHelper));
70
+ for (let i = 0; i < coords.length;) {
71
+ const ringEnd = i + 2 + coords[i] * 3;
72
+ rings.push(unprojectPoints(coords, offset, i + 2, ringEnd, worldCoordinateHelper));
73
+ i = ringEnd;
75
74
  }
76
75
 
77
- return result;
76
+ return rings;
78
77
  }
79
78
 
80
- function unprojectPoint(x: number, y: number, worldCoordinateHelper: WorldCoordinateHelper): GeoJSON.Position {
81
- return [worldCoordinateHelper.unprojectX(x), worldCoordinateHelper.unprojectY(y)];
79
+ /**
80
+ * The rings in groups of one polygon each, where a ring with a negative size is a hole of the polygon before it.
81
+ */
82
+ function unprojectPolygons(coords: GeoJSONVTSourceCoords, offset: number, worldCoordinateHelper: WorldCoordinateHelper): GeoJSON.Position[][][] {
83
+ const polygons: GeoJSON.Position[][][] = [];
84
+
85
+ for (let i = 0; i < coords.length;) {
86
+ const ringEnd = i + 2 + coords[i] * 3;
87
+ const ring = unprojectPoints(coords, offset, i + 2, ringEnd, worldCoordinateHelper);
88
+ if (!isHole(coords[i + 1]) || !polygons.length) {
89
+ polygons.push([ring]);
90
+ } else {
91
+ polygons[polygons.length - 1].push(ring);
92
+ }
93
+ i = ringEnd;
94
+ }
95
+
96
+ return polygons;
97
+ }
98
+
99
+ /**
100
+ * @param offset - what to add to every x of `coords`
101
+ */
102
+ function unprojectPoints(coords: GeoJSONVTSourceCoords, offset: number, start: number, end: number, worldCoordinateHelper: WorldCoordinateHelper): GeoJSON.Position[] {
103
+ const result: GeoJSON.Position[] = [];
104
+
105
+ for (let i = start; i < end; i += 3) {
106
+ result.push([worldCoordinateHelper.unprojectX(coords[i] + offset), worldCoordinateHelper.unprojectY(coords[i + 1])]);
107
+ }
108
+
109
+ return result;
82
110
  }
@@ -103,9 +103,15 @@ export type GeoJSONToTileOptions = GeoJSONVTOptions & {
103
103
  clip?: boolean;
104
104
  };
105
105
 
106
- export type SliceArray = { points: number[]; start?: number; end?: number; size?: number };
107
-
108
- export type SliceFixedArray = { points: number[] | Float64Array; start?: number; end?: number; size?: number };
106
+ /**
107
+ * The coordinates of a source feature in one flat array, with three values for a position: `x`, `y` and how much
108
+ * the position matters when the line is simplified.
109
+ *
110
+ * A MultiPoint is only its positions. A line or polygon has two values before the positions of each ring: how many
111
+ * positions the ring has, and its size. The size is the length of a line and the area of a polygon ring, and
112
+ * the ring is a hole of the ring before it when the size is negative, see `isHole`.
113
+ */
114
+ export type GeoJSONVTSourceCoords = Float64Array;
109
115
 
110
116
  export type PartialGeoJSONVTFeature = {
111
117
  id?: number | string | undefined;
@@ -114,36 +120,60 @@ export type PartialGeoJSONVTFeature = {
114
120
  minY?: number;
115
121
  maxX?: number;
116
122
  maxY?: number;
123
+ /**
124
+ * What to add to every x of `geometry`. The copy of a feature on the other side of the antimeridian shares the
125
+ * coordinates of the feature and has the width of the world here, where the feature itself has 0.
126
+ * The bounding box has it added already.
127
+ */
128
+ worldOffset: number;
117
129
  }
118
130
 
119
- export type GeoJSONVTInternalPointFeature = PartialGeoJSONVTFeature & {
131
+ /**
132
+ * A single point keeps its position on the feature, without a geometry array or a bounding box.
133
+ */
134
+ export type GeoJSONVTInternalPointFeature = {
135
+ id?: number | string | undefined;
120
136
  type: 'Point';
121
- geometry: number[];
137
+ x: number;
138
+ y: number;
139
+ tags: GeoJSON.GeoJsonProperties;
122
140
  };
123
141
 
124
142
  export type GeoJSONVTInternalMultiPointFeature = PartialGeoJSONVTFeature & {
125
143
  type: 'MultiPoint';
126
- geometry: number[];
144
+ geometry: GeoJSONVTSourceCoords;
127
145
  };
128
146
 
129
147
  export type GeoJSONVTInternalLineStringFeature = PartialGeoJSONVTFeature & {
130
148
  type: 'LineString';
131
- geometry: SliceFixedArray;
149
+ geometry: GeoJSONVTSourceCoords;
150
+ /**
151
+ * With `lineMetrics`, how far along the whole line this part of it starts.
152
+ */
153
+ start?: number;
154
+ /**
155
+ * With `lineMetrics`, how far along the whole line this part of it ends.
156
+ */
157
+ end?: number;
158
+ /**
159
+ * With `lineMetrics`, the length of the whole line.
160
+ */
161
+ size?: number;
132
162
  };
133
163
 
134
164
  export type GeoJSONVTInternalMultiLineStringFeature = PartialGeoJSONVTFeature & {
135
165
  type: 'MultiLineString';
136
- geometry: SliceFixedArray[];
166
+ geometry: GeoJSONVTSourceCoords;
137
167
  };
138
168
 
139
169
  export type GeoJSONVTInternalPolygonFeature = PartialGeoJSONVTFeature & {
140
170
  type: 'Polygon';
141
- geometry: SliceFixedArray[];
171
+ geometry: GeoJSONVTSourceCoords;
142
172
  };
143
173
 
144
174
  export type GeoJSONVTInternalMultiPolygonFeature = PartialGeoJSONVTFeature & {
145
175
  type: 'MultiPolygon';
146
- geometry: SliceFixedArray[][];
176
+ geometry: GeoJSONVTSourceCoords;
147
177
  };
148
178
 
149
179
  export type GeoJSONVTInternalFeature =
@@ -176,23 +206,38 @@ export type ClusterFeature = GeoJSON.Feature<GeoJSON.Point, ClusterProperties>;
176
206
  */
177
207
  export type ClusterOrPointFeature = ClusterFeature | GeoJSON.Feature<GeoJSON.Point>;
178
208
 
209
+ /**
210
+ * The coordinates of a tile feature as flat `x, y` values in the extent of the tile.
211
+ */
212
+ export type GeoJSONVTTileCoords = Int16Array | Int32Array | Float64Array;
213
+
214
+ /**
215
+ * A single point keeps its coordinates on the feature.
216
+ */
179
217
  export type GeoJSONVTInternalTileFeaturePoint = {
218
+ id? : number | string | undefined;
219
+ type: 4;
220
+ tags: GeoJSON.GeoJsonProperties | null;
221
+ x: number;
222
+ y: number;
223
+ }
224
+
225
+ export type GeoJSONVTInternalTileFeatureMultiPoint = {
180
226
  id? : number | string | undefined;
181
227
  type: 1;
182
228
  tags: GeoJSON.GeoJsonProperties | null;
183
- geometry: number[];
229
+ geometry: GeoJSONVTTileCoords;
184
230
  }
185
231
 
186
232
  export type GeoJSONVTInternalTileFeatureNonPoint = {
187
233
  id? : number | string | undefined;
188
234
  type: 2 | 3;
189
235
  tags: GeoJSON.GeoJsonProperties | null;
190
- geometry: number[][];
236
+ geometry: GeoJSONVTTileCoords[];
191
237
  }
192
- export type GeoJSONVTInternalTileFeature = GeoJSONVTInternalTileFeaturePoint | GeoJSONVTInternalTileFeatureNonPoint;
238
+ export type GeoJSONVTInternalTileFeature = GeoJSONVTInternalTileFeaturePoint | GeoJSONVTInternalTileFeatureMultiPoint | GeoJSONVTInternalTileFeatureNonPoint;
193
239
 
194
240
  export type GeoJSONVTInternalTile = {
195
- transformed: boolean;
196
241
  features: GeoJSONVTInternalTileFeature[];
197
242
  source: GeoJSONVTInternalFeature[] | null;
198
243
  x: number;
@@ -223,13 +268,20 @@ export type GeoJSONVTFeatureNonPoint = {
223
268
 
224
269
  export type GeoJSONVTFeature = GeoJSONVTFeaturePoint | GeoJSONVTFeatureNonPoint;
225
270
 
226
- export type GeoJSONVTTile = GeoJSONVTInternalTile & {
227
- transformed: true;
228
- features: GeoJSONVTFeature[]
271
+ /**
272
+ * A tile as `getTile` and `geoJSONToTile` return it.
273
+ */
274
+ export type GeoJSONVTTile = {
275
+ /**
276
+ * The features of the tile, with geometries in tile coordinates.
277
+ */
278
+ features: GeoJSONVTFeature[];
229
279
  }
230
280
 
231
281
  export interface GeoJSONVTTileIndex {
282
+ /** @internal */
232
283
  initialize(features: GeoJSONVTInternalFeature[]): void;
284
+ /** @internal */
233
285
  updateIndex(source: GeoJSONVTInternalFeature[], affected: GeoJSONVTInternalFeature[], options: GeoJSONVTOptions): void;
234
286
  getClusterExpansionZoom(clusterId: number): number | null;
235
287
  getChildren(clusterId: number): ClusterOrPointFeature[] | null;