@maplibre/geojson-vt 6.3.2 → 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.
- package/dist/clip.d.ts.map +1 -1
- package/dist/cluster-tile-index.d.ts +8 -0
- package/dist/cluster-tile-index.d.ts.map +1 -1
- package/dist/convert.d.ts.map +1 -1
- package/dist/deconvert.d.ts +0 -1
- package/dist/deconvert.d.ts.map +1 -1
- package/dist/definitions.d.ts +66 -25
- package/dist/definitions.d.ts.map +1 -1
- package/dist/feature.d.ts +16 -12
- package/dist/feature.d.ts.map +1 -1
- package/dist/geojson-vt-dev.js +602 -453
- package/dist/geojson-vt.js +1 -1
- package/dist/geojson-vt.mjs +599 -450
- package/dist/geojson-vt.mjs.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/simplify.d.ts +1 -1
- package/dist/simplify.d.ts.map +1 -1
- package/dist/tile-index.d.ts.map +1 -1
- package/dist/tile.d.ts +5 -2
- package/dist/tile.d.ts.map +1 -1
- package/dist/transform.d.ts +5 -6
- package/dist/transform.d.ts.map +1 -1
- package/dist/wrap.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/clip.test.ts +52 -32
- package/src/clip.ts +225 -143
- package/src/cluster-tile-index.ts +29 -21
- package/src/convert.ts +78 -65
- package/src/deconvert.test.ts +29 -28
- package/src/deconvert.ts +51 -23
- package/src/definitions.ts +69 -17
- package/src/difference.test.ts +38 -75
- package/src/feature.ts +53 -51
- package/src/geojson-to-tile.ts +1 -1
- package/src/geojsonvt.test.ts +113 -0
- package/src/index.ts +1 -17
- package/src/simplify.ts +2 -2
- package/src/tile-index.test.ts +3 -9
- package/src/tile-index.ts +11 -10
- package/src/tile.ts +212 -92
- package/src/transform.test.ts +2 -3
- package/src/transform.ts +24 -99
- package/src/wrap.ts +12 -73
package/src/tile.ts
CHANGED
|
@@ -1,22 +1,43 @@
|
|
|
1
|
-
import type { GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTOptions,
|
|
1
|
+
import type { GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTOptions, GeoJSONVTSourceCoords, GeoJSONVTTileCoords } from "./definitions";
|
|
2
|
+
import { isHole } from "./feature";
|
|
2
3
|
|
|
3
4
|
export const GEOJSONVT_CLIP_START = 'geojsonvt_clip_start';
|
|
4
5
|
export const GEOJSONVT_CLIP_END = 'geojsonvt_clip_end';
|
|
5
6
|
|
|
7
|
+
type TileCoordsConstructor = Int16ArrayConstructor | Int32ArrayConstructor | Float64ArrayConstructor;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where a tile lies in the world, and what holds the coordinates of its features.
|
|
11
|
+
*/
|
|
12
|
+
type TileFrame = {
|
|
13
|
+
z2: number;
|
|
14
|
+
tx: number;
|
|
15
|
+
ty: number;
|
|
16
|
+
extent: number;
|
|
17
|
+
CoordArray: TileCoordsConstructor;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The smallest array type that holds the coordinates of a tile whose features are cut at its buffer.
|
|
22
|
+
*/
|
|
23
|
+
function getTileCoordsConstructor(options: GeoJSONVTOptions): TileCoordsConstructor {
|
|
24
|
+
return options.extent + options.buffer <= 0x7fff ? Int16Array : Int32Array;
|
|
25
|
+
}
|
|
26
|
+
|
|
6
27
|
/**
|
|
7
|
-
* Creates a tile object from the given features
|
|
28
|
+
* Creates a tile object from the given features, with their coordinates in the tile's extent
|
|
8
29
|
* @param features - the features to include in the tile
|
|
9
30
|
* @param z
|
|
10
31
|
* @param tx
|
|
11
32
|
* @param ty
|
|
12
33
|
* @param options - the options object
|
|
34
|
+
* @param CoordArray - what holds the coordinates, which has to be a `Float64Array` when the features are not cut at the tile's buffer
|
|
13
35
|
* @returns the created tile
|
|
14
36
|
*/
|
|
15
|
-
export function createTile(features: GeoJSONVTInternalFeature[], z: number, tx: number, ty: number, options: GeoJSONVTOptions): GeoJSONVTInternalTile {
|
|
37
|
+
export function createTile(features: GeoJSONVTInternalFeature[], z: number, tx: number, ty: number, options: GeoJSONVTOptions, CoordArray = getTileCoordsConstructor(options)): GeoJSONVTInternalTile {
|
|
16
38
|
const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
|
|
17
39
|
|
|
18
40
|
const tile = {
|
|
19
|
-
transformed: false,
|
|
20
41
|
features: [] as GeoJSONVTInternalTileFeature[],
|
|
21
42
|
source: null as GeoJSONVTInternalFeature[],
|
|
22
43
|
x: tx,
|
|
@@ -31,49 +52,53 @@ export function createTile(features: GeoJSONVTInternalFeature[], z: number, tx:
|
|
|
31
52
|
numFeatures: features.length
|
|
32
53
|
};
|
|
33
54
|
|
|
55
|
+
const frame: TileFrame = {
|
|
56
|
+
z2: 1 << z,
|
|
57
|
+
tx,
|
|
58
|
+
ty,
|
|
59
|
+
extent: options.extent,
|
|
60
|
+
CoordArray
|
|
61
|
+
};
|
|
62
|
+
|
|
34
63
|
for (const feature of features) {
|
|
35
|
-
addFeature(tile, feature, tolerance, options);
|
|
64
|
+
addFeature(tile, feature, tolerance, options, frame);
|
|
36
65
|
}
|
|
37
66
|
|
|
38
67
|
return tile;
|
|
39
68
|
}
|
|
40
69
|
|
|
41
|
-
function addFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInternalFeature, tolerance: number, options: GeoJSONVTOptions) {
|
|
70
|
+
function addFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInternalFeature, tolerance: number, options: GeoJSONVTOptions, frame: TileFrame) {
|
|
71
|
+
if (feature.type === 'Point') {
|
|
72
|
+
addPointTileFeature(tile, feature, frame);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
42
76
|
tile.minX = Math.min(tile.minX, feature.minX);
|
|
43
77
|
tile.minY = Math.min(tile.minY, feature.minY);
|
|
44
78
|
tile.maxX = Math.max(tile.maxX, feature.maxX);
|
|
45
79
|
tile.maxY = Math.max(tile.maxY, feature.maxY);
|
|
46
80
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
addPointsTileFeature(tile, feature);
|
|
51
|
-
return;
|
|
52
|
-
case 'LineString':
|
|
53
|
-
addLineTileFeautre(tile, feature, tolerance, options);
|
|
54
|
-
return;
|
|
55
|
-
case 'MultiLineString':
|
|
56
|
-
case 'Polygon':
|
|
57
|
-
addLinesTileFeature(tile, feature, tolerance);
|
|
58
|
-
return;
|
|
59
|
-
case 'MultiPolygon':
|
|
60
|
-
addMultiPolygonTileFeature(tile, feature, tolerance);
|
|
61
|
-
return;
|
|
81
|
+
if (feature.type === 'MultiPoint') {
|
|
82
|
+
addPointsTileFeature(tile, feature, frame);
|
|
83
|
+
return;
|
|
62
84
|
}
|
|
85
|
+
|
|
86
|
+
addRingsTileFeature(tile, feature, tolerance, options, frame);
|
|
63
87
|
}
|
|
64
88
|
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
89
|
+
function addPointTileFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInternalPointFeature, frame: TileFrame) {
|
|
90
|
+
tile.minX = Math.min(tile.minX, feature.x);
|
|
91
|
+
tile.minY = Math.min(tile.minY, feature.y);
|
|
92
|
+
tile.maxX = Math.max(tile.maxX, feature.x);
|
|
93
|
+
tile.maxY = Math.max(tile.maxY, feature.y);
|
|
94
|
+
tile.numPoints++;
|
|
95
|
+
tile.numSimplified++;
|
|
96
|
+
|
|
73
97
|
const tileFeature: GeoJSONVTInternalTileFeature = {
|
|
74
|
-
type:
|
|
98
|
+
type: 4,
|
|
75
99
|
tags: feature.tags || null,
|
|
76
|
-
|
|
100
|
+
x: project(feature.x, frame.tx, frame),
|
|
101
|
+
y: project(feature.y, frame.ty, frame)
|
|
77
102
|
};
|
|
78
103
|
if (feature.id !== null) {
|
|
79
104
|
tileFeature.id = feature.id;
|
|
@@ -81,37 +106,46 @@ function addPointsTileFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInt
|
|
|
81
106
|
tile.features.push(tileFeature);
|
|
82
107
|
}
|
|
83
108
|
|
|
84
|
-
function
|
|
85
|
-
const geometry
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
tags[GEOJSONVT_CLIP_START] = feature.geometry.start / feature.geometry.size;
|
|
93
|
-
tags[GEOJSONVT_CLIP_END] = feature.geometry.end / feature.geometry.size;
|
|
94
|
-
}
|
|
109
|
+
function addPointsTileFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInternalMultiPointFeature, frame: TileFrame) {
|
|
110
|
+
const numPoints = feature.geometry.length / 3;
|
|
111
|
+
if (!numPoints) return;
|
|
112
|
+
|
|
113
|
+
const geometry = projectCoords(feature.geometry, feature.worldOffset, frame);
|
|
114
|
+
tile.numPoints += numPoints;
|
|
115
|
+
tile.numSimplified += numPoints;
|
|
116
|
+
|
|
95
117
|
const tileFeature: GeoJSONVTInternalTileFeature = {
|
|
96
|
-
type:
|
|
97
|
-
tags: tags,
|
|
118
|
+
type: 1,
|
|
119
|
+
tags: feature.tags || null,
|
|
98
120
|
geometry: geometry
|
|
99
|
-
}
|
|
121
|
+
};
|
|
100
122
|
if (feature.id !== null) {
|
|
101
123
|
tileFeature.id = feature.id;
|
|
102
124
|
}
|
|
103
125
|
tile.features.push(tileFeature);
|
|
104
126
|
}
|
|
105
127
|
|
|
106
|
-
function
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
128
|
+
function addRingsTileFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInternalLineStringFeature | GeoJSONVTInternalMultiLineStringFeature | GeoJSONVTInternalPolygonFeature | GeoJSONVTInternalMultiPolygonFeature, tolerance: number, options: GeoJSONVTOptions, frame: TileFrame) {
|
|
129
|
+
const isPolygon = feature.type === 'Polygon' || feature.type === 'MultiPolygon';
|
|
130
|
+
const coords = feature.geometry;
|
|
131
|
+
const geometry: GeoJSONVTTileCoords[] = [];
|
|
132
|
+
for (let i = 0; i < coords.length;) {
|
|
133
|
+
const ringEnd = i + 2 + coords[i] * 3;
|
|
134
|
+
addLine(geometry, coords, feature.worldOffset, i + 2, ringEnd, coords[i + 1], tile, tolerance, isPolygon, frame);
|
|
135
|
+
i = ringEnd;
|
|
110
136
|
}
|
|
111
137
|
if (!geometry.length) return;
|
|
138
|
+
|
|
139
|
+
let tags = feature.tags || null;
|
|
140
|
+
if (feature.type === 'LineString' && options.lineMetrics) {
|
|
141
|
+
tags = {};
|
|
142
|
+
for (const key in feature.tags) tags[key] = feature.tags[key];
|
|
143
|
+
tags[GEOJSONVT_CLIP_START] = feature.start / feature.size;
|
|
144
|
+
tags[GEOJSONVT_CLIP_END] = feature.end / feature.size;
|
|
145
|
+
}
|
|
112
146
|
const tileFeature: GeoJSONVTInternalTileFeature = {
|
|
113
|
-
type:
|
|
114
|
-
tags:
|
|
147
|
+
type: isPolygon ? 3 : 2,
|
|
148
|
+
tags: tags,
|
|
115
149
|
geometry: geometry
|
|
116
150
|
}
|
|
117
151
|
if (feature.id !== null) {
|
|
@@ -120,65 +154,151 @@ function addLinesTileFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInte
|
|
|
120
154
|
tile.features.push(tileFeature);
|
|
121
155
|
}
|
|
122
156
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Adds a ring to a tile feature, with the positions that matter on the zoom level of the tile.
|
|
159
|
+
* @param offset - what to add to every x of `coords`
|
|
160
|
+
* @param size - the size of the ring, which is negative for the hole of a polygon
|
|
161
|
+
*/
|
|
162
|
+
function addLine(result: GeoJSONVTTileCoords[], coords: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, size: number, tile: GeoJSONVTInternalTile, tolerance: number, isPolygon: boolean, frame: TileFrame) {
|
|
163
|
+
const sqTolerance = tolerance * tolerance;
|
|
164
|
+
const numPoints = (ringEnd - first) / 3;
|
|
165
|
+
|
|
166
|
+
tile.numPoints += numPoints;
|
|
167
|
+
if (tolerance > 0 && (Math.abs(size) < (isPolygon ? sqTolerance : tolerance))) return;
|
|
168
|
+
|
|
169
|
+
// the area of what is kept tells which way the ring winds, starting from the last position that is kept
|
|
170
|
+
let last = ringEnd - 3;
|
|
171
|
+
while (last > first && tolerance > 0 && coords[last + 2] <= sqTolerance) last -= 3;
|
|
172
|
+
let x0 = coords[last] + offset;
|
|
173
|
+
let y0 = coords[last + 1];
|
|
174
|
+
let area = 0;
|
|
175
|
+
let numKept = 0;
|
|
176
|
+
|
|
177
|
+
for (let i = first; i < ringEnd; i += 3) {
|
|
178
|
+
if (tolerance === 0 || coords[i + 2] > sqTolerance) {
|
|
179
|
+
area += (coords[i] + offset - x0) * (coords[i + 1] + y0);
|
|
180
|
+
x0 = coords[i] + offset;
|
|
181
|
+
y0 = coords[i + 1];
|
|
182
|
+
numKept++;
|
|
129
183
|
}
|
|
130
184
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
tile.features.push(tileFeature);
|
|
185
|
+
tile.numSimplified += numKept;
|
|
186
|
+
|
|
187
|
+
// an outer ring goes clockwise and a hole the other way
|
|
188
|
+
const isReversed = isPolygon && (area > 0) === !isHole(size);
|
|
189
|
+
const ring = projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame);
|
|
190
|
+
|
|
191
|
+
if (isPolygon && isWindingFlipped(coords, offset, first, ringEnd, tolerance, isReversed, ring)) reverse(ring);
|
|
192
|
+
|
|
193
|
+
result.push(ring);
|
|
141
194
|
}
|
|
142
195
|
|
|
143
|
-
|
|
196
|
+
/**
|
|
197
|
+
* The positions of a ring that matter on the zoom level of the tile, as coordinates in the tile, last to first when
|
|
198
|
+
* the ring is reversed. They go into a `Float64Array` when one of them doesn't fit the tile's array, which takes a
|
|
199
|
+
* position far outside the tile.
|
|
200
|
+
*/
|
|
201
|
+
function projectRing(coords: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, tolerance: number, numKept: number, isReversed: boolean, frame: TileFrame, CoordArray = frame.CoordArray): GeoJSONVTTileCoords {
|
|
144
202
|
const sqTolerance = tolerance * tolerance;
|
|
203
|
+
const limit = CoordArray === Int16Array ? 0x7fff : 0x7fffffff;
|
|
204
|
+
const ring = new CoordArray(numKept * 2);
|
|
205
|
+
let next = isReversed ? ring.length - 2 : 0;
|
|
145
206
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
207
|
+
for (let i = first; i < ringEnd; i += 3) {
|
|
208
|
+
if (tolerance === 0 || coords[i + 2] > sqTolerance) {
|
|
209
|
+
const x = project(coords[i] + offset, frame.tx, frame);
|
|
210
|
+
const y = project(coords[i + 1], frame.ty, frame);
|
|
211
|
+
if (CoordArray !== Float64Array && !(Math.abs(x) <= limit && Math.abs(y) <= limit)) {
|
|
212
|
+
return projectRing(coords, offset, first, ringEnd, tolerance, numKept, isReversed, frame, Float64Array);
|
|
213
|
+
}
|
|
214
|
+
ring[next] = x;
|
|
215
|
+
ring[next + 1] = y;
|
|
216
|
+
next += isReversed ? -2 : 2;
|
|
217
|
+
}
|
|
149
218
|
}
|
|
150
219
|
|
|
151
|
-
|
|
220
|
+
return ring;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Whether rounding to tile coordinates turned a polygon ring around, so that a hole would read as a shell, or the
|
|
225
|
+
* other way round. Both signed areas are summed in the order of the tile's ring and relative to its first position,
|
|
226
|
+
* to reduce cancellation for narrow rings far from the origin. A ring that rounds to no area is left as it is, and
|
|
227
|
+
* other changes rounding makes to a ring are not repaired.
|
|
228
|
+
*/
|
|
229
|
+
function isWindingFlipped(coords: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, tolerance: number, isReversed: boolean, ring: GeoJSONVTTileCoords): boolean {
|
|
230
|
+
const sqTolerance = tolerance * tolerance;
|
|
231
|
+
const step = isReversed ? -3 : 3;
|
|
232
|
+
let originX = 0;
|
|
233
|
+
let originY = 0;
|
|
234
|
+
let previousX = 0;
|
|
235
|
+
let previousY = 0;
|
|
236
|
+
let previousRoundedX = 0;
|
|
237
|
+
let previousRoundedY = 0;
|
|
238
|
+
let sourceArea = 0;
|
|
239
|
+
let roundedArea = 0;
|
|
240
|
+
let next = 0;
|
|
152
241
|
|
|
153
|
-
for (let i =
|
|
154
|
-
if (tolerance
|
|
155
|
-
|
|
156
|
-
|
|
242
|
+
for (let i = isReversed ? ringEnd - 3 : first; i >= first && i < ringEnd; i += step) {
|
|
243
|
+
if (tolerance > 0 && !(coords[i + 2] > sqTolerance)) continue;
|
|
244
|
+
if (next === 0) {
|
|
245
|
+
originX = coords[i] + offset;
|
|
246
|
+
originY = coords[i + 1];
|
|
247
|
+
} else {
|
|
248
|
+
const x = coords[i] + offset - originX;
|
|
249
|
+
const y = coords[i + 1] - originY;
|
|
250
|
+
const roundedX = ring[next] - ring[0];
|
|
251
|
+
const roundedY = ring[next + 1] - ring[1];
|
|
252
|
+
sourceArea += previousX * y - x * previousY;
|
|
253
|
+
roundedArea += previousRoundedX * roundedY - roundedX * previousRoundedY;
|
|
254
|
+
previousX = x;
|
|
255
|
+
previousY = y;
|
|
256
|
+
previousRoundedX = roundedX;
|
|
257
|
+
previousRoundedY = roundedY;
|
|
157
258
|
}
|
|
158
|
-
|
|
259
|
+
next += 2;
|
|
159
260
|
}
|
|
160
261
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
result.push(ring);
|
|
262
|
+
return sourceArea * roundedArea < 0;
|
|
164
263
|
}
|
|
165
264
|
|
|
166
|
-
function
|
|
167
|
-
let
|
|
265
|
+
function reverse(coords: GeoJSONVTTileCoords) {
|
|
266
|
+
for (let i = 0, j = coords.length - 2; i < j; i += 2, j -= 2) {
|
|
267
|
+
const x = coords[i];
|
|
268
|
+
const y = coords[i + 1];
|
|
168
269
|
|
|
169
|
-
|
|
170
|
-
|
|
270
|
+
coords[i] = coords[j];
|
|
271
|
+
coords[i + 1] = coords[j + 1];
|
|
272
|
+
coords[j] = x;
|
|
273
|
+
coords[j + 1] = y;
|
|
171
274
|
}
|
|
275
|
+
}
|
|
172
276
|
|
|
173
|
-
|
|
277
|
+
/**
|
|
278
|
+
* A position in the world, on one axis, as a coordinate in the tile that starts at `t` on that axis.
|
|
279
|
+
*/
|
|
280
|
+
function project(position: number, t: number, frame: TileFrame): number {
|
|
281
|
+
return Math.round(frame.extent * (position * frame.z2 - t));
|
|
282
|
+
}
|
|
174
283
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
284
|
+
/**
|
|
285
|
+
* The positions of a MultiPoint as coordinates in the tile. They go into a `Float64Array` when one of them doesn't
|
|
286
|
+
* fit the tile's array, which takes a position far outside the tile.
|
|
287
|
+
* @param offset - what to add to every x of `positions`
|
|
288
|
+
*/
|
|
289
|
+
function projectCoords(positions: GeoJSONVTSourceCoords, offset: number, frame: TileFrame, CoordArray = frame.CoordArray): GeoJSONVTTileCoords {
|
|
290
|
+
const limit = CoordArray === Int16Array ? 0x7fff : 0x7fffffff;
|
|
291
|
+
const coords = new CoordArray(positions.length / 3 * 2);
|
|
178
292
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
293
|
+
for (let i = 0, j = 0; i < positions.length; i += 3, j += 2) {
|
|
294
|
+
const x = project(positions[i] + offset, frame.tx, frame);
|
|
295
|
+
const y = project(positions[i + 1], frame.ty, frame);
|
|
296
|
+
if (CoordArray !== Float64Array && !(Math.abs(x) <= limit && Math.abs(y) <= limit)) {
|
|
297
|
+
return projectCoords(positions, offset, frame, Float64Array);
|
|
298
|
+
}
|
|
299
|
+
coords[j] = x;
|
|
300
|
+
coords[j + 1] = y;
|
|
183
301
|
}
|
|
302
|
+
|
|
303
|
+
return coords;
|
|
184
304
|
}
|
package/src/transform.test.ts
CHANGED
|
@@ -26,11 +26,10 @@ test('quantization preserves shell and hole winding without changing ordinary or
|
|
|
26
26
|
|
|
27
27
|
test('transformTile retains empty polygon rings', () => {
|
|
28
28
|
const tile = transformTile({
|
|
29
|
-
transformed: false,
|
|
30
29
|
source: null,
|
|
31
30
|
x: 0, y: 0, z: 0,
|
|
32
|
-
features: [{type: 3, tags: null, geometry: [
|
|
33
|
-
}
|
|
31
|
+
features: [{type: 3, tags: null, geometry: [new Int16Array(0)]}]
|
|
32
|
+
});
|
|
34
33
|
|
|
35
34
|
expect(tile.features[0].geometry).toEqual([[]]);
|
|
36
35
|
});
|
package/src/transform.ts
CHANGED
|
@@ -1,116 +1,41 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { GeoJSONVTFeature, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTTile, GeoJSONVTTileCoords } from "./definitions";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @param tile - the tile
|
|
7
|
-
* @
|
|
8
|
-
* @returns the transformed tile
|
|
4
|
+
* Turns a stored tile, which keeps its coordinates in flat arrays, into the tile `getTile` returns,
|
|
5
|
+
* with every position as an `[x, y]` pair. The stored tile stays as it is.
|
|
6
|
+
* @param tile - the stored tile
|
|
7
|
+
* @returns a new tile with the coordinates as pairs
|
|
9
8
|
*/
|
|
10
|
-
export function transformTile(tile: GeoJSONVTInternalTile
|
|
11
|
-
|
|
12
|
-
return tile as GeoJSONVTTile;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const z2 = 1 << tile.z;
|
|
16
|
-
const tx = tile.x;
|
|
17
|
-
const ty = tile.y;
|
|
18
|
-
|
|
9
|
+
export function transformTile(tile: GeoJSONVTInternalTile): GeoJSONVTTile {
|
|
10
|
+
const features: GeoJSONVTFeature[] = [];
|
|
19
11
|
for (const feature of tile.features) {
|
|
20
|
-
|
|
21
|
-
transformPointFeature(feature, extent, z2, tx, ty);
|
|
22
|
-
} else {
|
|
23
|
-
transformNonPointFeature(feature, extent, z2, tx, ty);
|
|
24
|
-
}
|
|
12
|
+
features.push(transformFeature(feature));
|
|
25
13
|
}
|
|
26
|
-
tile.transformed = true;
|
|
27
14
|
|
|
28
|
-
return
|
|
15
|
+
return {features};
|
|
29
16
|
}
|
|
30
17
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
for (let i = 0; i < geometry.length; i += 2) {
|
|
40
|
-
point.push(transformPoint(geometry[i], geometry[i + 1], extent, z2, tx, ty));
|
|
18
|
+
function transformFeature(feature: GeoJSONVTInternalTileFeature): GeoJSONVTFeature {
|
|
19
|
+
let transformed: GeoJSONVTFeature;
|
|
20
|
+
if (feature.type === 4) {
|
|
21
|
+
transformed = {type: 1, tags: feature.tags, geometry: [[feature.x, feature.y]]};
|
|
22
|
+
} else if (feature.type === 1) {
|
|
23
|
+
transformed = {type: 1, tags: feature.tags, geometry: toPairs(feature.geometry)};
|
|
24
|
+
} else {
|
|
25
|
+
transformed = {type: feature.type, tags: feature.tags, geometry: feature.geometry.map(toPairs)};
|
|
41
26
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return transformed;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Transforms a single non-point feature from mercator-projected space into (extent x extent) tile space.
|
|
49
|
-
*/
|
|
50
|
-
function transformNonPointFeature(feature: GeoJSONVTInternalTileFeatureNonPoint, extent: number, z2: number, tx: number, ty: number): GeoJSONVTFeatureNonPoint {
|
|
51
|
-
const transformed = feature as unknown as GeoJSONVTFeatureNonPoint;
|
|
52
|
-
|
|
53
|
-
const geometry = feature.geometry;
|
|
54
|
-
const nonPoint: GeoJSONVTFeatureNonPoint["geometry"] = [];
|
|
55
|
-
for (const geom of geometry) {
|
|
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);
|
|
64
|
-
}
|
|
27
|
+
if (feature.id !== undefined) {
|
|
28
|
+
transformed.id = feature.id;
|
|
65
29
|
}
|
|
66
|
-
transformed.geometry = nonPoint;
|
|
67
30
|
|
|
68
31
|
return transformed;
|
|
69
32
|
}
|
|
70
33
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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);
|
|
34
|
+
function toPairs(coords: GeoJSONVTTileCoords): [number, number][] {
|
|
35
|
+
const pairs: [number, number][] = [];
|
|
36
|
+
for (let i = 0; i < coords.length; i += 2) {
|
|
37
|
+
pairs.push([coords[i], coords[i + 1]]);
|
|
105
38
|
}
|
|
106
39
|
|
|
107
|
-
|
|
108
|
-
return ring;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function transformPoint(x: number, y: number, extent: number, z2: number, tx: number, ty: number): [number, number] {
|
|
112
|
-
return [
|
|
113
|
-
Math.round(extent * (x * z2 - tx)),
|
|
114
|
-
Math.round(extent * (y * z2 - ty))
|
|
115
|
-
];
|
|
40
|
+
return pairs;
|
|
116
41
|
}
|
package/src/wrap.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import {AxisType, clip} from './clip';
|
|
3
|
-
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions
|
|
4
|
-
import {
|
|
3
|
+
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';
|
|
4
|
+
import {createPointFeature} from './feature';
|
|
5
5
|
|
|
6
6
|
export function wrap(features: GeoJSONVTInternalFeature[], options: GeoJSONVTOptions): GeoJSONVTInternalFeature[] {
|
|
7
7
|
if (!options.wrap) return features;
|
|
@@ -22,82 +22,21 @@ export function wrap(features: GeoJSONVTInternalFeature[], options: GeoJSONVTOpt
|
|
|
22
22
|
return merged;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* The features on the other side of the antimeridian. A feature with a coordinates array shares it with its copy,
|
|
27
|
+
* which only gets the offset.
|
|
28
|
+
*/
|
|
25
29
|
function shiftFeatureCoords(features: GeoJSONVTInternalFeature[], offset: number): GeoJSONVTInternalFeature[] {
|
|
26
|
-
const newFeatures = [];
|
|
30
|
+
const newFeatures: GeoJSONVTInternalFeature[] = [];
|
|
27
31
|
|
|
28
32
|
for (const feature of features) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const newGeometry = shiftPointCoords(feature.geometry, offset);
|
|
33
|
-
|
|
34
|
-
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
case 'LineString': {
|
|
39
|
-
const newGeometry = shiftLineCoords(feature.geometry, offset);
|
|
40
|
-
|
|
41
|
-
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
case 'MultiLineString':
|
|
46
|
-
case 'Polygon': {
|
|
47
|
-
const newGeometry: SliceFixedArray[] = [];
|
|
48
|
-
for (const line of feature.geometry) {
|
|
49
|
-
newGeometry.push(shiftLineCoords(line, offset));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
case 'MultiPolygon': {
|
|
57
|
-
const newGeometry: SliceFixedArray[][] = [];
|
|
58
|
-
for (const polygon of feature.geometry) {
|
|
59
|
-
const newPolygon: SliceFixedArray[] = [];
|
|
60
|
-
for (const line of polygon) {
|
|
61
|
-
newPolygon.push(shiftLineCoords(line, offset));
|
|
62
|
-
}
|
|
63
|
-
newGeometry.push(newPolygon);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
33
|
+
if (feature.type === 'Point') {
|
|
34
|
+
newFeatures.push(createPointFeature(feature.id, feature.x + offset, feature.y, feature.tags));
|
|
35
|
+
continue;
|
|
69
36
|
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return newFeatures;
|
|
73
|
-
}
|
|
74
37
|
|
|
75
|
-
|
|
76
|
-
const newCoords: number[] = [];
|
|
77
|
-
|
|
78
|
-
for (let i = 0; i < coords.length; i += 3) {
|
|
79
|
-
newCoords.push(coords[i] + offset, coords[i + 1], coords[i + 2]);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return newCoords;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function shiftLineCoords(line: SliceArray | SliceFixedArray, offset: number): SliceFixedArray {
|
|
86
|
-
const newLine: SliceArray = {
|
|
87
|
-
points: [],
|
|
88
|
-
size: line.size
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
if (line.start !== undefined) {
|
|
92
|
-
newLine.start = line.start;
|
|
93
|
-
newLine.end = line.end;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
for (let i = 0; i < line.points.length; i += 3) {
|
|
97
|
-
newLine.points.push(line.points[i] + offset, line.points[i + 1], line.points[i + 2]);
|
|
38
|
+
newFeatures.push({...feature, minX: feature.minX + offset, maxX: feature.maxX + offset, worldOffset: feature.worldOffset + offset});
|
|
98
39
|
}
|
|
99
40
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return newLine;
|
|
41
|
+
return newFeatures;
|
|
103
42
|
}
|