@maplibre/geojson-vt 5.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/LICENSE +39 -0
- package/README.md +128 -0
- package/dist/geojson-vt-dev.js +1185 -0
- package/dist/geojson-vt.js +1 -0
- package/package.json +42 -0
- package/src/clip.js +200 -0
- package/src/convert.js +139 -0
- package/src/difference.js +180 -0
- package/src/feature.js +43 -0
- package/src/index.js +334 -0
- package/src/simplify.js +65 -0
- package/src/tile.js +123 -0
- package/src/transform.js +41 -0
- package/src/wrap.js +68 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
|
|
2
|
+
import convert from './convert.js'; // GeoJSON conversion and preprocessing
|
|
3
|
+
import clip from './clip.js'; // stripe clipping algorithm
|
|
4
|
+
import wrap from './wrap.js'; // date line processing
|
|
5
|
+
import transform from './transform.js'; // coordinate transformation
|
|
6
|
+
import createTile from './tile.js'; // final simplified tile generation
|
|
7
|
+
import {applySourceDiff} from './difference.js'; // diff utilities
|
|
8
|
+
|
|
9
|
+
const defaultOptions = {
|
|
10
|
+
maxZoom: 14, // max zoom to preserve detail on
|
|
11
|
+
indexMaxZoom: 5, // max zoom in the tile index
|
|
12
|
+
indexMaxPoints: 100000, // max number of points per tile in the tile index
|
|
13
|
+
tolerance: 3, // simplification tolerance (higher means simpler)
|
|
14
|
+
extent: 4096, // tile extent
|
|
15
|
+
buffer: 64, // tile buffer on each side
|
|
16
|
+
lineMetrics: false, // whether to calculate line metrics
|
|
17
|
+
promoteId: null, // name of a feature property to be promoted to feature.id
|
|
18
|
+
generateId: false, // whether to generate feature ids. Cannot be used with promoteId
|
|
19
|
+
updateable: false, // whether geojson can be updated (with caveat of a stored simplified copy)
|
|
20
|
+
debug: 0 // logging level (0, 1 or 2)
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
class GeoJSONVT {
|
|
24
|
+
constructor(data, options) {
|
|
25
|
+
options = this.options = extend(Object.create(defaultOptions), options);
|
|
26
|
+
|
|
27
|
+
const debug = options.debug;
|
|
28
|
+
|
|
29
|
+
if (debug) console.time('preprocess data');
|
|
30
|
+
|
|
31
|
+
if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error('maxZoom should be in the 0-24 range');
|
|
32
|
+
if (options.promoteId && options.generateId) throw new Error('promoteId and generateId cannot be used together.');
|
|
33
|
+
|
|
34
|
+
// projects and adds simplification info
|
|
35
|
+
let features = convert(data, options);
|
|
36
|
+
|
|
37
|
+
// tiles and tileCoords are part of the public API
|
|
38
|
+
this.tiles = {};
|
|
39
|
+
this.tileCoords = [];
|
|
40
|
+
|
|
41
|
+
if (debug) {
|
|
42
|
+
console.timeEnd('preprocess data');
|
|
43
|
+
console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
|
|
44
|
+
console.time('generate tiles');
|
|
45
|
+
this.stats = {};
|
|
46
|
+
this.total = 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// wraps features (ie extreme west and extreme east)
|
|
50
|
+
features = wrap(features, options);
|
|
51
|
+
|
|
52
|
+
// start slicing from the top tile down
|
|
53
|
+
if (features.length) this.splitTile(features, 0, 0, 0);
|
|
54
|
+
|
|
55
|
+
// for updateable indexes, store a copy of the original simplified features
|
|
56
|
+
if (options.updateable) {
|
|
57
|
+
this.source = features;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (debug) {
|
|
61
|
+
if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
|
|
62
|
+
console.timeEnd('generate tiles');
|
|
63
|
+
console.log('tiles generated:', this.total, JSON.stringify(this.stats));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// splits features from a parent tile to sub-tiles.
|
|
68
|
+
// z, x, and y are the coordinates of the parent tile
|
|
69
|
+
// cz, cx, and cy are the coordinates of the target tile
|
|
70
|
+
//
|
|
71
|
+
// If no target tile is specified, splitting stops when we reach the maximum
|
|
72
|
+
// zoom or the number of points is low as specified in the options.
|
|
73
|
+
splitTile(features, z, x, y, cz, cx, cy) {
|
|
74
|
+
|
|
75
|
+
const stack = [features, z, x, y];
|
|
76
|
+
const options = this.options;
|
|
77
|
+
const debug = options.debug;
|
|
78
|
+
|
|
79
|
+
// avoid recursion by using a processing queue
|
|
80
|
+
while (stack.length) {
|
|
81
|
+
y = stack.pop();
|
|
82
|
+
x = stack.pop();
|
|
83
|
+
z = stack.pop();
|
|
84
|
+
features = stack.pop();
|
|
85
|
+
|
|
86
|
+
const z2 = 1 << z;
|
|
87
|
+
const id = toID(z, x, y);
|
|
88
|
+
let tile = this.tiles[id];
|
|
89
|
+
|
|
90
|
+
if (!tile) {
|
|
91
|
+
if (debug > 1) console.time('creation');
|
|
92
|
+
|
|
93
|
+
tile = this.tiles[id] = createTile(features, z, x, y, options);
|
|
94
|
+
this.tileCoords.push({z, x, y, id});
|
|
95
|
+
|
|
96
|
+
if (debug) {
|
|
97
|
+
if (debug > 1) {
|
|
98
|
+
console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
|
|
99
|
+
z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
|
|
100
|
+
console.timeEnd('creation');
|
|
101
|
+
}
|
|
102
|
+
const key = `z${ z}`;
|
|
103
|
+
this.stats[key] = (this.stats[key] || 0) + 1;
|
|
104
|
+
this.total++;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// save reference to original geometry in tile so that we can drill down later if we stop now
|
|
109
|
+
tile.source = features;
|
|
110
|
+
|
|
111
|
+
// if it's the first-pass tiling
|
|
112
|
+
if (cz == null) {
|
|
113
|
+
// stop tiling if we reached max zoom, or if the tile is too simple
|
|
114
|
+
if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
|
|
115
|
+
// if a drilldown to a specific tile
|
|
116
|
+
} else if (z === options.maxZoom || z === cz) {
|
|
117
|
+
// stop tiling if we reached base zoom or our target tile zoom
|
|
118
|
+
continue;
|
|
119
|
+
} else if (cz != null) {
|
|
120
|
+
// stop tiling if it's not an ancestor of the target tile
|
|
121
|
+
const zoomSteps = cz - z;
|
|
122
|
+
if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// if we slice further down, no need to keep source geometry
|
|
126
|
+
tile.source = null;
|
|
127
|
+
|
|
128
|
+
if (features.length === 0) continue;
|
|
129
|
+
|
|
130
|
+
if (debug > 1) console.time('clipping');
|
|
131
|
+
|
|
132
|
+
// values we'll use for clipping
|
|
133
|
+
const k1 = 0.5 * options.buffer / options.extent;
|
|
134
|
+
const k2 = 0.5 - k1;
|
|
135
|
+
const k3 = 0.5 + k1;
|
|
136
|
+
const k4 = 1 + k1;
|
|
137
|
+
|
|
138
|
+
let tl = null;
|
|
139
|
+
let bl = null;
|
|
140
|
+
let tr = null;
|
|
141
|
+
let br = null;
|
|
142
|
+
|
|
143
|
+
const left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
|
|
144
|
+
const right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
|
|
145
|
+
|
|
146
|
+
if (left) {
|
|
147
|
+
tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
|
|
148
|
+
bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (right) {
|
|
152
|
+
tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
|
|
153
|
+
br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (debug > 1) console.timeEnd('clipping');
|
|
157
|
+
|
|
158
|
+
stack.push(tl || [], z + 1, x * 2, y * 2);
|
|
159
|
+
stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
|
|
160
|
+
stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
|
|
161
|
+
stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
getTile(z, x, y) {
|
|
166
|
+
z = +z;
|
|
167
|
+
x = +x;
|
|
168
|
+
y = +y;
|
|
169
|
+
|
|
170
|
+
const options = this.options;
|
|
171
|
+
const {extent, debug} = options;
|
|
172
|
+
|
|
173
|
+
if (z < 0 || z > 24) return null;
|
|
174
|
+
|
|
175
|
+
const z2 = 1 << z;
|
|
176
|
+
x = (x + z2) & (z2 - 1); // wrap tile x coordinate
|
|
177
|
+
|
|
178
|
+
const id = toID(z, x, y);
|
|
179
|
+
if (this.tiles[id]) return transform(this.tiles[id], extent);
|
|
180
|
+
|
|
181
|
+
if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
|
|
182
|
+
|
|
183
|
+
let z0 = z;
|
|
184
|
+
let x0 = x;
|
|
185
|
+
let y0 = y;
|
|
186
|
+
let parent;
|
|
187
|
+
|
|
188
|
+
while (!parent && z0 > 0) {
|
|
189
|
+
z0--;
|
|
190
|
+
x0 = x0 >> 1;
|
|
191
|
+
y0 = y0 >> 1;
|
|
192
|
+
parent = this.tiles[toID(z0, x0, y0)];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (!parent || !parent.source) return null;
|
|
196
|
+
|
|
197
|
+
// if we found a parent tile containing the original geometry, we can drill down from it
|
|
198
|
+
if (debug > 1) {
|
|
199
|
+
console.log('found parent tile z%d-%d-%d', z0, x0, y0);
|
|
200
|
+
console.time('drilling down');
|
|
201
|
+
}
|
|
202
|
+
this.splitTile(parent.source, z0, x0, y0, z, x, y);
|
|
203
|
+
if (debug > 1) console.timeEnd('drilling down');
|
|
204
|
+
|
|
205
|
+
return this.tiles[id] ? transform(this.tiles[id], extent) : null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// invalidates (removes) tiles affected by the provided features
|
|
209
|
+
invalidateTiles(features) {
|
|
210
|
+
const options = this.options;
|
|
211
|
+
const {debug} = options;
|
|
212
|
+
|
|
213
|
+
// calculate bounding box of all features for trivial reject
|
|
214
|
+
let minX = Infinity;
|
|
215
|
+
let maxX = -Infinity;
|
|
216
|
+
let minY = Infinity;
|
|
217
|
+
let maxY = -Infinity;
|
|
218
|
+
|
|
219
|
+
for (const feature of features) {
|
|
220
|
+
minX = Math.min(minX, feature.minX);
|
|
221
|
+
maxX = Math.max(maxX, feature.maxX);
|
|
222
|
+
minY = Math.min(minY, feature.minY);
|
|
223
|
+
maxY = Math.max(maxY, feature.maxY);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// tile buffer clipping value - not halved as in splitTile above because checking against tile's own extent
|
|
227
|
+
const k1 = options.buffer / options.extent;
|
|
228
|
+
|
|
229
|
+
// track removed tile ids for o(1) lookup
|
|
230
|
+
const removedLookup = new Set();
|
|
231
|
+
|
|
232
|
+
// iterate through existing tiles and remove ones that are affected by features
|
|
233
|
+
for (const id in this.tiles) {
|
|
234
|
+
const tile = this.tiles[id];
|
|
235
|
+
|
|
236
|
+
// calculate tile bounds including buffer
|
|
237
|
+
const z2 = 1 << tile.z;
|
|
238
|
+
const tileMinX = (tile.x - k1) / z2;
|
|
239
|
+
const tileMaxX = (tile.x + 1 + k1) / z2;
|
|
240
|
+
const tileMinY = (tile.y - k1) / z2;
|
|
241
|
+
const tileMaxY = (tile.y + 1 + k1) / z2;
|
|
242
|
+
|
|
243
|
+
// trivial reject if feature bounds don't intersect tile
|
|
244
|
+
if (maxX < tileMinX || minX >= tileMaxX ||
|
|
245
|
+
maxY < tileMinY || minY >= tileMaxY) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// check if any feature intersects with the tile
|
|
250
|
+
let intersects = false;
|
|
251
|
+
for (const feature of features) {
|
|
252
|
+
if (feature.maxX >= tileMinX && feature.minX < tileMaxX &&
|
|
253
|
+
feature.maxY >= tileMinY && feature.minY < tileMaxY) {
|
|
254
|
+
intersects = true;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (!intersects) continue;
|
|
259
|
+
|
|
260
|
+
if (debug) {
|
|
261
|
+
if (debug > 1) {
|
|
262
|
+
console.log('invalidate tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
|
|
263
|
+
tile.z, tile.x, tile.y, tile.numFeatures, tile.numPoints, tile.numSimplified);
|
|
264
|
+
}
|
|
265
|
+
const key = `z${ tile.z}`;
|
|
266
|
+
this.stats[key] = (this.stats[key] || 0) - 1;
|
|
267
|
+
this.total--;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
delete this.tiles[id];
|
|
271
|
+
removedLookup.add(id);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// remove tile coords that are no longer in the index
|
|
275
|
+
if (removedLookup.size) this.tileCoords = this.tileCoords.filter(c => !removedLookup.has(c.id));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// updates the tile index by adding and/or removing geojson features
|
|
279
|
+
// invalidates tiles that are affected by the update for regeneration on next getTile call
|
|
280
|
+
// diff is an object with properties specified in difference.js
|
|
281
|
+
updateData(diff) {
|
|
282
|
+
const options = this.options;
|
|
283
|
+
const debug = options.debug;
|
|
284
|
+
|
|
285
|
+
if (!options.updateable) throw new Error('to update tile geojson `updateable` option must be set to true');
|
|
286
|
+
|
|
287
|
+
// apply diff and collect affected features and updated source that will be used to invalidate tiles
|
|
288
|
+
const {affected, source} = applySourceDiff(this.source, diff, options);
|
|
289
|
+
|
|
290
|
+
// nothing has changed
|
|
291
|
+
if (!affected.length) return;
|
|
292
|
+
|
|
293
|
+
// update source with new simplified feature set
|
|
294
|
+
this.source = source;
|
|
295
|
+
|
|
296
|
+
if (debug > 1) {
|
|
297
|
+
console.log('invalidating tiles');
|
|
298
|
+
console.time('invalidating');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
this.invalidateTiles(affected);
|
|
302
|
+
|
|
303
|
+
if (debug > 1) console.timeEnd('invalidating');
|
|
304
|
+
|
|
305
|
+
// re-generate root tile with updated feature set
|
|
306
|
+
const [z, x, y] = [0, 0, 0];
|
|
307
|
+
const rootTile = createTile(this.source, z, x, y, this.options);
|
|
308
|
+
rootTile.source = this.source;
|
|
309
|
+
|
|
310
|
+
// update tile index with new root tile - ready for getTile calls
|
|
311
|
+
const id = toID(z, x, y);
|
|
312
|
+
this.tiles[id] = rootTile;
|
|
313
|
+
this.tileCoords.push({z, x, y, id});
|
|
314
|
+
|
|
315
|
+
if (debug) {
|
|
316
|
+
const key = `z${ z}`;
|
|
317
|
+
this.stats[key] = (this.stats[key] || 0) + 1;
|
|
318
|
+
this.total++;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function toID(z, x, y) {
|
|
324
|
+
return (((1 << z) * y + x) * 32) + z;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function extend(dest, src) {
|
|
328
|
+
for (const i in src) dest[i] = src[i];
|
|
329
|
+
return dest;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export default function geojsonvt(data, options) {
|
|
333
|
+
return new GeoJSONVT(data, options);
|
|
334
|
+
}
|
package/src/simplify.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
|
|
2
|
+
// calculate simplification data using optimized Douglas-Peucker algorithm
|
|
3
|
+
|
|
4
|
+
export default function simplify(coords, first, last, sqTolerance) {
|
|
5
|
+
let maxSqDist = sqTolerance;
|
|
6
|
+
const mid = first + ((last - first) >> 1);
|
|
7
|
+
let minPosToMid = last - first;
|
|
8
|
+
let index;
|
|
9
|
+
|
|
10
|
+
const ax = coords[first];
|
|
11
|
+
const ay = coords[first + 1];
|
|
12
|
+
const bx = coords[last];
|
|
13
|
+
const by = coords[last + 1];
|
|
14
|
+
|
|
15
|
+
for (let i = first + 3; i < last; i += 3) {
|
|
16
|
+
const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
|
|
17
|
+
|
|
18
|
+
if (d > maxSqDist) {
|
|
19
|
+
index = i;
|
|
20
|
+
maxSqDist = d;
|
|
21
|
+
|
|
22
|
+
} else if (d === maxSqDist) {
|
|
23
|
+
// a workaround to ensure we choose a pivot close to the middle of the list,
|
|
24
|
+
// reducing recursion depth, for certain degenerate inputs
|
|
25
|
+
// https://github.com/mapbox/geojson-vt/issues/104
|
|
26
|
+
const posToMid = Math.abs(i - mid);
|
|
27
|
+
if (posToMid < minPosToMid) {
|
|
28
|
+
index = i;
|
|
29
|
+
minPosToMid = posToMid;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (maxSqDist > sqTolerance) {
|
|
35
|
+
if (index - first > 3) simplify(coords, first, index, sqTolerance);
|
|
36
|
+
coords[index + 2] = maxSqDist;
|
|
37
|
+
if (last - index > 3) simplify(coords, index, last, sqTolerance);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// square distance from a point to a segment
|
|
42
|
+
function getSqSegDist(px, py, x, y, bx, by) {
|
|
43
|
+
|
|
44
|
+
let dx = bx - x;
|
|
45
|
+
let dy = by - y;
|
|
46
|
+
|
|
47
|
+
if (dx !== 0 || dy !== 0) {
|
|
48
|
+
|
|
49
|
+
const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
|
|
50
|
+
|
|
51
|
+
if (t > 1) {
|
|
52
|
+
x = bx;
|
|
53
|
+
y = by;
|
|
54
|
+
|
|
55
|
+
} else if (t > 0) {
|
|
56
|
+
x += dx * t;
|
|
57
|
+
y += dy * t;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
dx = px - x;
|
|
62
|
+
dy = py - y;
|
|
63
|
+
|
|
64
|
+
return dx * dx + dy * dy;
|
|
65
|
+
}
|
package/src/tile.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
|
|
2
|
+
export default function createTile(features, z, tx, ty, options) {
|
|
3
|
+
const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
|
|
4
|
+
const tile = {
|
|
5
|
+
features: [],
|
|
6
|
+
numPoints: 0,
|
|
7
|
+
numSimplified: 0,
|
|
8
|
+
numFeatures: features.length,
|
|
9
|
+
source: null,
|
|
10
|
+
x: tx,
|
|
11
|
+
y: ty,
|
|
12
|
+
z,
|
|
13
|
+
transformed: false,
|
|
14
|
+
minX: 2,
|
|
15
|
+
minY: 1,
|
|
16
|
+
maxX: -1,
|
|
17
|
+
maxY: 0
|
|
18
|
+
};
|
|
19
|
+
for (const feature of features) {
|
|
20
|
+
addFeature(tile, feature, tolerance, options);
|
|
21
|
+
}
|
|
22
|
+
return tile;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function addFeature(tile, feature, tolerance, options) {
|
|
26
|
+
const geom = feature.geometry;
|
|
27
|
+
const type = feature.type;
|
|
28
|
+
const simplified = [];
|
|
29
|
+
|
|
30
|
+
tile.minX = Math.min(tile.minX, feature.minX);
|
|
31
|
+
tile.minY = Math.min(tile.minY, feature.minY);
|
|
32
|
+
tile.maxX = Math.max(tile.maxX, feature.maxX);
|
|
33
|
+
tile.maxY = Math.max(tile.maxY, feature.maxY);
|
|
34
|
+
|
|
35
|
+
if (type === 'Point' || type === 'MultiPoint') {
|
|
36
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
37
|
+
simplified.push(geom[i], geom[i + 1]);
|
|
38
|
+
tile.numPoints++;
|
|
39
|
+
tile.numSimplified++;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
} else if (type === 'LineString') {
|
|
43
|
+
addLine(simplified, geom, tile, tolerance, false, false);
|
|
44
|
+
|
|
45
|
+
} else if (type === 'MultiLineString' || type === 'Polygon') {
|
|
46
|
+
for (let i = 0; i < geom.length; i++) {
|
|
47
|
+
addLine(simplified, geom[i], tile, tolerance, type === 'Polygon', i === 0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
} else if (type === 'MultiPolygon') {
|
|
51
|
+
|
|
52
|
+
for (let k = 0; k < geom.length; k++) {
|
|
53
|
+
const polygon = geom[k];
|
|
54
|
+
for (let i = 0; i < polygon.length; i++) {
|
|
55
|
+
addLine(simplified, polygon[i], tile, tolerance, true, i === 0);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (simplified.length) {
|
|
61
|
+
let tags = feature.tags || null;
|
|
62
|
+
|
|
63
|
+
if (type === 'LineString' && options.lineMetrics) {
|
|
64
|
+
tags = {};
|
|
65
|
+
for (const key in feature.tags) tags[key] = feature.tags[key];
|
|
66
|
+
/* eslint-disable dot-notation */
|
|
67
|
+
tags['mapbox_clip_start'] = geom.start / geom.size;
|
|
68
|
+
tags['mapbox_clip_end'] = geom.end / geom.size;
|
|
69
|
+
/* eslint-enable dot-notation */
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const tileFeature = {
|
|
73
|
+
geometry: simplified,
|
|
74
|
+
type: type === 'Polygon' || type === 'MultiPolygon' ? 3 :
|
|
75
|
+
(type === 'LineString' || type === 'MultiLineString' ? 2 : 1),
|
|
76
|
+
tags
|
|
77
|
+
};
|
|
78
|
+
if (feature.id !== null) {
|
|
79
|
+
tileFeature.id = feature.id;
|
|
80
|
+
}
|
|
81
|
+
tile.features.push(tileFeature);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function addLine(result, geom, tile, tolerance, isPolygon, isOuter) {
|
|
86
|
+
const sqTolerance = tolerance * tolerance;
|
|
87
|
+
|
|
88
|
+
if (tolerance > 0 && (geom.size < (isPolygon ? sqTolerance : tolerance))) {
|
|
89
|
+
tile.numPoints += geom.length / 3;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const ring = [];
|
|
94
|
+
|
|
95
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
96
|
+
if (tolerance === 0 || geom[i + 2] > sqTolerance) {
|
|
97
|
+
tile.numSimplified++;
|
|
98
|
+
ring.push(geom[i], geom[i + 1]);
|
|
99
|
+
}
|
|
100
|
+
tile.numPoints++;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (isPolygon) rewind(ring, isOuter);
|
|
104
|
+
|
|
105
|
+
result.push(ring);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function rewind(ring, clockwise) {
|
|
109
|
+
let area = 0;
|
|
110
|
+
for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) {
|
|
111
|
+
area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
|
|
112
|
+
}
|
|
113
|
+
if (area > 0 === clockwise) {
|
|
114
|
+
for (let i = 0, len = ring.length; i < len / 2; i += 2) {
|
|
115
|
+
const x = ring[i];
|
|
116
|
+
const y = ring[i + 1];
|
|
117
|
+
ring[i] = ring[len - 2 - i];
|
|
118
|
+
ring[i + 1] = ring[len - 1 - i];
|
|
119
|
+
ring[len - 2 - i] = x;
|
|
120
|
+
ring[len - 1 - i] = y;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
package/src/transform.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
// Transforms the coordinates of each feature in the given tile from
|
|
3
|
+
// mercator-projected space into (extent x extent) tile space.
|
|
4
|
+
export default function transformTile(tile, extent) {
|
|
5
|
+
if (tile.transformed) return tile;
|
|
6
|
+
|
|
7
|
+
const z2 = 1 << tile.z;
|
|
8
|
+
const tx = tile.x;
|
|
9
|
+
const ty = tile.y;
|
|
10
|
+
|
|
11
|
+
for (const feature of tile.features) {
|
|
12
|
+
const geom = feature.geometry;
|
|
13
|
+
const type = feature.type;
|
|
14
|
+
|
|
15
|
+
feature.geometry = [];
|
|
16
|
+
|
|
17
|
+
if (type === 1) {
|
|
18
|
+
for (let j = 0; j < geom.length; j += 2) {
|
|
19
|
+
feature.geometry.push(transformPoint(geom[j], geom[j + 1], extent, z2, tx, ty));
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
for (let j = 0; j < geom.length; j++) {
|
|
23
|
+
const ring = [];
|
|
24
|
+
for (let k = 0; k < geom[j].length; k += 2) {
|
|
25
|
+
ring.push(transformPoint(geom[j][k], geom[j][k + 1], extent, z2, tx, ty));
|
|
26
|
+
}
|
|
27
|
+
feature.geometry.push(ring);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
tile.transformed = true;
|
|
33
|
+
|
|
34
|
+
return tile;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function transformPoint(x, y, extent, z2, tx, ty) {
|
|
38
|
+
return [
|
|
39
|
+
Math.round(extent * (x * z2 - tx)),
|
|
40
|
+
Math.round(extent * (y * z2 - ty))];
|
|
41
|
+
}
|
package/src/wrap.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
|
|
2
|
+
import clip from './clip.js';
|
|
3
|
+
import createFeature from './feature.js';
|
|
4
|
+
|
|
5
|
+
export default function wrap(features, options) {
|
|
6
|
+
const buffer = options.buffer / options.extent;
|
|
7
|
+
let merged = features;
|
|
8
|
+
const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
|
|
9
|
+
const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
|
|
10
|
+
|
|
11
|
+
if (left || right) {
|
|
12
|
+
merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
|
|
13
|
+
|
|
14
|
+
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
|
|
15
|
+
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return merged;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function shiftFeatureCoords(features, offset) {
|
|
22
|
+
const newFeatures = [];
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < features.length; i++) {
|
|
25
|
+
const feature = features[i];
|
|
26
|
+
const type = feature.type;
|
|
27
|
+
|
|
28
|
+
let newGeometry;
|
|
29
|
+
|
|
30
|
+
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
|
|
31
|
+
newGeometry = shiftCoords(feature.geometry, offset);
|
|
32
|
+
|
|
33
|
+
} else if (type === 'MultiLineString' || type === 'Polygon') {
|
|
34
|
+
newGeometry = [];
|
|
35
|
+
for (const line of feature.geometry) {
|
|
36
|
+
newGeometry.push(shiftCoords(line, offset));
|
|
37
|
+
}
|
|
38
|
+
} else if (type === 'MultiPolygon') {
|
|
39
|
+
newGeometry = [];
|
|
40
|
+
for (const polygon of feature.geometry) {
|
|
41
|
+
const newPolygon = [];
|
|
42
|
+
for (const line of polygon) {
|
|
43
|
+
newPolygon.push(shiftCoords(line, offset));
|
|
44
|
+
}
|
|
45
|
+
newGeometry.push(newPolygon);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
newFeatures.push(createFeature(feature.id, type, newGeometry, feature.tags));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return newFeatures;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function shiftCoords(points, offset) {
|
|
56
|
+
const newPoints = [];
|
|
57
|
+
newPoints.size = points.size;
|
|
58
|
+
|
|
59
|
+
if (points.start !== undefined) {
|
|
60
|
+
newPoints.start = points.start;
|
|
61
|
+
newPoints.end = points.end;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < points.length; i += 3) {
|
|
65
|
+
newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
|
|
66
|
+
}
|
|
67
|
+
return newPoints;
|
|
68
|
+
}
|