@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
|
@@ -0,0 +1,1185 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.geojsonvt = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
// calculate simplification data using optimized Douglas-Peucker algorithm
|
|
8
|
+
|
|
9
|
+
function simplify(coords, first, last, sqTolerance) {
|
|
10
|
+
let maxSqDist = sqTolerance;
|
|
11
|
+
const mid = first + ((last - first) >> 1);
|
|
12
|
+
let minPosToMid = last - first;
|
|
13
|
+
let index;
|
|
14
|
+
|
|
15
|
+
const ax = coords[first];
|
|
16
|
+
const ay = coords[first + 1];
|
|
17
|
+
const bx = coords[last];
|
|
18
|
+
const by = coords[last + 1];
|
|
19
|
+
|
|
20
|
+
for (let i = first + 3; i < last; i += 3) {
|
|
21
|
+
const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
|
|
22
|
+
|
|
23
|
+
if (d > maxSqDist) {
|
|
24
|
+
index = i;
|
|
25
|
+
maxSqDist = d;
|
|
26
|
+
|
|
27
|
+
} else if (d === maxSqDist) {
|
|
28
|
+
// a workaround to ensure we choose a pivot close to the middle of the list,
|
|
29
|
+
// reducing recursion depth, for certain degenerate inputs
|
|
30
|
+
// https://github.com/mapbox/geojson-vt/issues/104
|
|
31
|
+
const posToMid = Math.abs(i - mid);
|
|
32
|
+
if (posToMid < minPosToMid) {
|
|
33
|
+
index = i;
|
|
34
|
+
minPosToMid = posToMid;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (maxSqDist > sqTolerance) {
|
|
40
|
+
if (index - first > 3) simplify(coords, first, index, sqTolerance);
|
|
41
|
+
coords[index + 2] = maxSqDist;
|
|
42
|
+
if (last - index > 3) simplify(coords, index, last, sqTolerance);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// square distance from a point to a segment
|
|
47
|
+
function getSqSegDist(px, py, x, y, bx, by) {
|
|
48
|
+
|
|
49
|
+
let dx = bx - x;
|
|
50
|
+
let dy = by - y;
|
|
51
|
+
|
|
52
|
+
if (dx !== 0 || dy !== 0) {
|
|
53
|
+
|
|
54
|
+
const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
|
|
55
|
+
|
|
56
|
+
if (t > 1) {
|
|
57
|
+
x = bx;
|
|
58
|
+
y = by;
|
|
59
|
+
|
|
60
|
+
} else if (t > 0) {
|
|
61
|
+
x += dx * t;
|
|
62
|
+
y += dy * t;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
dx = px - x;
|
|
67
|
+
dy = py - y;
|
|
68
|
+
|
|
69
|
+
return dx * dx + dy * dy;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function createFeature(id, type, geom, tags) {
|
|
73
|
+
const feature = {
|
|
74
|
+
id: id == null ? null : id,
|
|
75
|
+
type,
|
|
76
|
+
geometry: geom,
|
|
77
|
+
tags,
|
|
78
|
+
minX: Infinity,
|
|
79
|
+
minY: Infinity,
|
|
80
|
+
maxX: -Infinity,
|
|
81
|
+
maxY: -Infinity
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
|
|
85
|
+
calcLineBBox(feature, geom);
|
|
86
|
+
|
|
87
|
+
} else if (type === 'Polygon') {
|
|
88
|
+
// the outer ring (ie [0]) contains all inner rings
|
|
89
|
+
calcLineBBox(feature, geom[0]);
|
|
90
|
+
|
|
91
|
+
} else if (type === 'MultiLineString') {
|
|
92
|
+
for (const line of geom) {
|
|
93
|
+
calcLineBBox(feature, line);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
} else if (type === 'MultiPolygon') {
|
|
97
|
+
for (const polygon of geom) {
|
|
98
|
+
// the outer ring (ie [0]) contains all inner rings
|
|
99
|
+
calcLineBBox(feature, polygon[0]);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return feature;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function calcLineBBox(feature, geom) {
|
|
107
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
108
|
+
feature.minX = Math.min(feature.minX, geom[i]);
|
|
109
|
+
feature.minY = Math.min(feature.minY, geom[i + 1]);
|
|
110
|
+
feature.maxX = Math.max(feature.maxX, geom[i]);
|
|
111
|
+
feature.maxY = Math.max(feature.maxY, geom[i + 1]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
|
|
116
|
+
|
|
117
|
+
function convert(data, options) {
|
|
118
|
+
const features = [];
|
|
119
|
+
if (data.type === 'FeatureCollection') {
|
|
120
|
+
for (let i = 0; i < data.features.length; i++) {
|
|
121
|
+
convertFeature(features, data.features[i], options, i);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
} else if (data.type === 'Feature') {
|
|
125
|
+
convertFeature(features, data, options);
|
|
126
|
+
|
|
127
|
+
} else {
|
|
128
|
+
// single geometry or a geometry collection
|
|
129
|
+
convertFeature(features, {geometry: data}, options);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return features;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function convertFeature(features, geojson, options, index) {
|
|
136
|
+
if (!geojson.geometry) return;
|
|
137
|
+
|
|
138
|
+
const coords = geojson.geometry.coordinates;
|
|
139
|
+
if (coords && coords.length === 0) return;
|
|
140
|
+
|
|
141
|
+
const type = geojson.geometry.type;
|
|
142
|
+
const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
|
|
143
|
+
let geometry = [];
|
|
144
|
+
let id = geojson.id;
|
|
145
|
+
if (options.promoteId) {
|
|
146
|
+
id = geojson.properties[options.promoteId];
|
|
147
|
+
} else if (options.generateId) {
|
|
148
|
+
id = index || 0;
|
|
149
|
+
}
|
|
150
|
+
if (type === 'Point') {
|
|
151
|
+
convertPoint(coords, geometry);
|
|
152
|
+
|
|
153
|
+
} else if (type === 'MultiPoint') {
|
|
154
|
+
for (const p of coords) {
|
|
155
|
+
convertPoint(p, geometry);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
} else if (type === 'LineString') {
|
|
159
|
+
convertLine(coords, geometry, tolerance, false);
|
|
160
|
+
|
|
161
|
+
} else if (type === 'MultiLineString') {
|
|
162
|
+
if (options.lineMetrics) {
|
|
163
|
+
// explode into linestrings to be able to track metrics
|
|
164
|
+
for (const line of coords) {
|
|
165
|
+
geometry = [];
|
|
166
|
+
convertLine(line, geometry, tolerance, false);
|
|
167
|
+
features.push(createFeature(id, 'LineString', geometry, geojson.properties));
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
convertLines(coords, geometry, tolerance, false);
|
|
172
|
+
|
|
173
|
+
} else if (type === 'Polygon') {
|
|
174
|
+
convertLines(coords, geometry, tolerance, true);
|
|
175
|
+
|
|
176
|
+
} else if (type === 'MultiPolygon') {
|
|
177
|
+
for (const polygon of coords) {
|
|
178
|
+
const newPolygon = [];
|
|
179
|
+
convertLines(polygon, newPolygon, tolerance, true);
|
|
180
|
+
geometry.push(newPolygon);
|
|
181
|
+
}
|
|
182
|
+
} else if (type === 'GeometryCollection') {
|
|
183
|
+
for (const singleGeometry of geojson.geometry.geometries) {
|
|
184
|
+
convertFeature(features, {
|
|
185
|
+
id,
|
|
186
|
+
geometry: singleGeometry,
|
|
187
|
+
properties: geojson.properties
|
|
188
|
+
}, options, index);
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
} else {
|
|
192
|
+
throw new Error('Input data is not a valid GeoJSON object.');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
features.push(createFeature(id, type, geometry, geojson.properties));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function convertPoint(coords, out) {
|
|
199
|
+
out.push(projectX(coords[0]), projectY(coords[1]), 0);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function convertLine(ring, out, tolerance, isPolygon) {
|
|
203
|
+
let x0, y0;
|
|
204
|
+
let size = 0;
|
|
205
|
+
|
|
206
|
+
for (let j = 0; j < ring.length; j++) {
|
|
207
|
+
const x = projectX(ring[j][0]);
|
|
208
|
+
const y = projectY(ring[j][1]);
|
|
209
|
+
|
|
210
|
+
out.push(x, y, 0);
|
|
211
|
+
|
|
212
|
+
if (j > 0) {
|
|
213
|
+
if (isPolygon) {
|
|
214
|
+
size += (x0 * y - x * y0) / 2; // area
|
|
215
|
+
} else {
|
|
216
|
+
size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)); // length
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
x0 = x;
|
|
220
|
+
y0 = y;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const last = out.length - 3;
|
|
224
|
+
out[2] = 1;
|
|
225
|
+
if (tolerance > 0) simplify(out, 0, last, tolerance);
|
|
226
|
+
out[last + 2] = 1;
|
|
227
|
+
|
|
228
|
+
out.size = Math.abs(size);
|
|
229
|
+
out.start = 0;
|
|
230
|
+
out.end = out.size;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function convertLines(rings, out, tolerance, isPolygon) {
|
|
234
|
+
for (let i = 0; i < rings.length; i++) {
|
|
235
|
+
const geom = [];
|
|
236
|
+
convertLine(rings[i], geom, tolerance, isPolygon);
|
|
237
|
+
out.push(geom);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function projectX(x) {
|
|
242
|
+
return x / 360 + 0.5;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function projectY(y) {
|
|
246
|
+
const sin = Math.sin(y * Math.PI / 180);
|
|
247
|
+
const y2 = 0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
|
|
248
|
+
return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/* clip features between two vertical or horizontal axis-parallel lines:
|
|
252
|
+
* | |
|
|
253
|
+
* ___|___ | /
|
|
254
|
+
* / | \____|____/
|
|
255
|
+
* | |
|
|
256
|
+
*
|
|
257
|
+
* k1 and k2 are the line coordinates
|
|
258
|
+
* axis: 0 for x, 1 for y
|
|
259
|
+
* minAll and maxAll: minimum and maximum coordinate value for all features
|
|
260
|
+
*/
|
|
261
|
+
function clip(features, scale, k1, k2, axis, minAll, maxAll, options) {
|
|
262
|
+
k1 /= scale;
|
|
263
|
+
k2 /= scale;
|
|
264
|
+
|
|
265
|
+
if (minAll >= k1 && maxAll < k2) return features; // trivial accept
|
|
266
|
+
else if (maxAll < k1 || minAll >= k2) return null; // trivial reject
|
|
267
|
+
|
|
268
|
+
const clipped = [];
|
|
269
|
+
|
|
270
|
+
for (const feature of features) {
|
|
271
|
+
const geometry = feature.geometry;
|
|
272
|
+
let type = feature.type;
|
|
273
|
+
|
|
274
|
+
const min = axis === 0 ? feature.minX : feature.minY;
|
|
275
|
+
const max = axis === 0 ? feature.maxX : feature.maxY;
|
|
276
|
+
|
|
277
|
+
if (min >= k1 && max < k2) { // trivial accept
|
|
278
|
+
clipped.push(feature);
|
|
279
|
+
continue;
|
|
280
|
+
} else if (max < k1 || min >= k2) { // trivial reject
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let newGeometry = [];
|
|
285
|
+
|
|
286
|
+
if (type === 'Point' || type === 'MultiPoint') {
|
|
287
|
+
clipPoints(geometry, newGeometry, k1, k2, axis);
|
|
288
|
+
|
|
289
|
+
} else if (type === 'LineString') {
|
|
290
|
+
clipLine(geometry, newGeometry, k1, k2, axis, false, options.lineMetrics);
|
|
291
|
+
|
|
292
|
+
} else if (type === 'MultiLineString') {
|
|
293
|
+
clipLines(geometry, newGeometry, k1, k2, axis, false);
|
|
294
|
+
|
|
295
|
+
} else if (type === 'Polygon') {
|
|
296
|
+
clipLines(geometry, newGeometry, k1, k2, axis, true);
|
|
297
|
+
|
|
298
|
+
} else if (type === 'MultiPolygon') {
|
|
299
|
+
for (const polygon of geometry) {
|
|
300
|
+
const newPolygon = [];
|
|
301
|
+
clipLines(polygon, newPolygon, k1, k2, axis, true);
|
|
302
|
+
if (newPolygon.length) {
|
|
303
|
+
newGeometry.push(newPolygon);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (newGeometry.length) {
|
|
309
|
+
if (options.lineMetrics && type === 'LineString') {
|
|
310
|
+
for (const line of newGeometry) {
|
|
311
|
+
clipped.push(createFeature(feature.id, type, line, feature.tags));
|
|
312
|
+
}
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (type === 'LineString' || type === 'MultiLineString') {
|
|
317
|
+
if (newGeometry.length === 1) {
|
|
318
|
+
type = 'LineString';
|
|
319
|
+
newGeometry = newGeometry[0];
|
|
320
|
+
} else {
|
|
321
|
+
type = 'MultiLineString';
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (type === 'Point' || type === 'MultiPoint') {
|
|
325
|
+
type = newGeometry.length === 3 ? 'Point' : 'MultiPoint';
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
clipped.push(createFeature(feature.id, type, newGeometry, feature.tags));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return clipped.length ? clipped : null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function clipPoints(geom, newGeom, k1, k2, axis) {
|
|
336
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
337
|
+
const a = geom[i + axis];
|
|
338
|
+
|
|
339
|
+
if (a >= k1 && a <= k2) {
|
|
340
|
+
addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function clipLine(geom, newGeom, k1, k2, axis, isPolygon, trackMetrics) {
|
|
346
|
+
|
|
347
|
+
let slice = newSlice(geom);
|
|
348
|
+
const intersect = axis === 0 ? intersectX : intersectY;
|
|
349
|
+
let len = geom.start;
|
|
350
|
+
let segLen, t;
|
|
351
|
+
|
|
352
|
+
for (let i = 0; i < geom.length - 3; i += 3) {
|
|
353
|
+
const ax = geom[i];
|
|
354
|
+
const ay = geom[i + 1];
|
|
355
|
+
const az = geom[i + 2];
|
|
356
|
+
const bx = geom[i + 3];
|
|
357
|
+
const by = geom[i + 4];
|
|
358
|
+
const a = axis === 0 ? ax : ay;
|
|
359
|
+
const b = axis === 0 ? bx : by;
|
|
360
|
+
let exited = false;
|
|
361
|
+
|
|
362
|
+
if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
|
|
363
|
+
|
|
364
|
+
if (a < k1) {
|
|
365
|
+
// ---|--> | (line enters the clip region from the left)
|
|
366
|
+
if (b > k1) {
|
|
367
|
+
t = intersect(slice, ax, ay, bx, by, k1);
|
|
368
|
+
if (trackMetrics) slice.start = len + segLen * t;
|
|
369
|
+
}
|
|
370
|
+
} else if (a > k2) {
|
|
371
|
+
// | <--|--- (line enters the clip region from the right)
|
|
372
|
+
if (b < k2) {
|
|
373
|
+
t = intersect(slice, ax, ay, bx, by, k2);
|
|
374
|
+
if (trackMetrics) slice.start = len + segLen * t;
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
addPoint(slice, ax, ay, az);
|
|
378
|
+
}
|
|
379
|
+
if (b < k1 && a >= k1) {
|
|
380
|
+
// <--|--- | or <--|-----|--- (line exits the clip region on the left)
|
|
381
|
+
t = intersect(slice, ax, ay, bx, by, k1);
|
|
382
|
+
exited = true;
|
|
383
|
+
}
|
|
384
|
+
if (b > k2 && a <= k2) {
|
|
385
|
+
// | ---|--> or ---|-----|--> (line exits the clip region on the right)
|
|
386
|
+
t = intersect(slice, ax, ay, bx, by, k2);
|
|
387
|
+
exited = true;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (!isPolygon && exited) {
|
|
391
|
+
if (trackMetrics) slice.end = len + segLen * t;
|
|
392
|
+
newGeom.push(slice);
|
|
393
|
+
slice = newSlice(geom);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (trackMetrics) len += segLen;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// add the last point
|
|
400
|
+
let last = geom.length - 3;
|
|
401
|
+
const ax = geom[last];
|
|
402
|
+
const ay = geom[last + 1];
|
|
403
|
+
const az = geom[last + 2];
|
|
404
|
+
const a = axis === 0 ? ax : ay;
|
|
405
|
+
if (a >= k1 && a <= k2) addPoint(slice, ax, ay, az);
|
|
406
|
+
|
|
407
|
+
// close the polygon if its endpoints are not the same after clipping
|
|
408
|
+
last = slice.length - 3;
|
|
409
|
+
if (isPolygon && last >= 3 && (slice[last] !== slice[0] || slice[last + 1] !== slice[1])) {
|
|
410
|
+
addPoint(slice, slice[0], slice[1], slice[2]);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// add the final slice
|
|
414
|
+
if (slice.length) {
|
|
415
|
+
newGeom.push(slice);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function newSlice(line) {
|
|
420
|
+
const slice = [];
|
|
421
|
+
slice.size = line.size;
|
|
422
|
+
slice.start = line.start;
|
|
423
|
+
slice.end = line.end;
|
|
424
|
+
return slice;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function clipLines(geom, newGeom, k1, k2, axis, isPolygon) {
|
|
428
|
+
for (const line of geom) {
|
|
429
|
+
clipLine(line, newGeom, k1, k2, axis, isPolygon, false);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function addPoint(out, x, y, z) {
|
|
434
|
+
out.push(x, y, z);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function intersectX(out, ax, ay, bx, by, x) {
|
|
438
|
+
const t = (x - ax) / (bx - ax);
|
|
439
|
+
addPoint(out, x, ay + (by - ay) * t, 1);
|
|
440
|
+
return t;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function intersectY(out, ax, ay, bx, by, y) {
|
|
444
|
+
const t = (y - ay) / (by - ay);
|
|
445
|
+
addPoint(out, ax + (bx - ax) * t, y, 1);
|
|
446
|
+
return t;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function wrap(features, options) {
|
|
450
|
+
const buffer = options.buffer / options.extent;
|
|
451
|
+
let merged = features;
|
|
452
|
+
const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
|
|
453
|
+
const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
|
|
454
|
+
|
|
455
|
+
if (left || right) {
|
|
456
|
+
merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
|
|
457
|
+
|
|
458
|
+
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
|
|
459
|
+
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return merged;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function shiftFeatureCoords(features, offset) {
|
|
466
|
+
const newFeatures = [];
|
|
467
|
+
|
|
468
|
+
for (let i = 0; i < features.length; i++) {
|
|
469
|
+
const feature = features[i];
|
|
470
|
+
const type = feature.type;
|
|
471
|
+
|
|
472
|
+
let newGeometry;
|
|
473
|
+
|
|
474
|
+
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
|
|
475
|
+
newGeometry = shiftCoords(feature.geometry, offset);
|
|
476
|
+
|
|
477
|
+
} else if (type === 'MultiLineString' || type === 'Polygon') {
|
|
478
|
+
newGeometry = [];
|
|
479
|
+
for (const line of feature.geometry) {
|
|
480
|
+
newGeometry.push(shiftCoords(line, offset));
|
|
481
|
+
}
|
|
482
|
+
} else if (type === 'MultiPolygon') {
|
|
483
|
+
newGeometry = [];
|
|
484
|
+
for (const polygon of feature.geometry) {
|
|
485
|
+
const newPolygon = [];
|
|
486
|
+
for (const line of polygon) {
|
|
487
|
+
newPolygon.push(shiftCoords(line, offset));
|
|
488
|
+
}
|
|
489
|
+
newGeometry.push(newPolygon);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
newFeatures.push(createFeature(feature.id, type, newGeometry, feature.tags));
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return newFeatures;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function shiftCoords(points, offset) {
|
|
500
|
+
const newPoints = [];
|
|
501
|
+
newPoints.size = points.size;
|
|
502
|
+
|
|
503
|
+
if (points.start !== undefined) {
|
|
504
|
+
newPoints.start = points.start;
|
|
505
|
+
newPoints.end = points.end;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
for (let i = 0; i < points.length; i += 3) {
|
|
509
|
+
newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
|
|
510
|
+
}
|
|
511
|
+
return newPoints;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Transforms the coordinates of each feature in the given tile from
|
|
515
|
+
// mercator-projected space into (extent x extent) tile space.
|
|
516
|
+
function transformTile(tile, extent) {
|
|
517
|
+
if (tile.transformed) return tile;
|
|
518
|
+
|
|
519
|
+
const z2 = 1 << tile.z;
|
|
520
|
+
const tx = tile.x;
|
|
521
|
+
const ty = tile.y;
|
|
522
|
+
|
|
523
|
+
for (const feature of tile.features) {
|
|
524
|
+
const geom = feature.geometry;
|
|
525
|
+
const type = feature.type;
|
|
526
|
+
|
|
527
|
+
feature.geometry = [];
|
|
528
|
+
|
|
529
|
+
if (type === 1) {
|
|
530
|
+
for (let j = 0; j < geom.length; j += 2) {
|
|
531
|
+
feature.geometry.push(transformPoint(geom[j], geom[j + 1], extent, z2, tx, ty));
|
|
532
|
+
}
|
|
533
|
+
} else {
|
|
534
|
+
for (let j = 0; j < geom.length; j++) {
|
|
535
|
+
const ring = [];
|
|
536
|
+
for (let k = 0; k < geom[j].length; k += 2) {
|
|
537
|
+
ring.push(transformPoint(geom[j][k], geom[j][k + 1], extent, z2, tx, ty));
|
|
538
|
+
}
|
|
539
|
+
feature.geometry.push(ring);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
tile.transformed = true;
|
|
545
|
+
|
|
546
|
+
return tile;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function transformPoint(x, y, extent, z2, tx, ty) {
|
|
550
|
+
return [
|
|
551
|
+
Math.round(extent * (x * z2 - tx)),
|
|
552
|
+
Math.round(extent * (y * z2 - ty))];
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function createTile(features, z, tx, ty, options) {
|
|
556
|
+
const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
|
|
557
|
+
const tile = {
|
|
558
|
+
features: [],
|
|
559
|
+
numPoints: 0,
|
|
560
|
+
numSimplified: 0,
|
|
561
|
+
numFeatures: features.length,
|
|
562
|
+
source: null,
|
|
563
|
+
x: tx,
|
|
564
|
+
y: ty,
|
|
565
|
+
z,
|
|
566
|
+
transformed: false,
|
|
567
|
+
minX: 2,
|
|
568
|
+
minY: 1,
|
|
569
|
+
maxX: -1,
|
|
570
|
+
maxY: 0
|
|
571
|
+
};
|
|
572
|
+
for (const feature of features) {
|
|
573
|
+
addFeature(tile, feature, tolerance, options);
|
|
574
|
+
}
|
|
575
|
+
return tile;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function addFeature(tile, feature, tolerance, options) {
|
|
579
|
+
const geom = feature.geometry;
|
|
580
|
+
const type = feature.type;
|
|
581
|
+
const simplified = [];
|
|
582
|
+
|
|
583
|
+
tile.minX = Math.min(tile.minX, feature.minX);
|
|
584
|
+
tile.minY = Math.min(tile.minY, feature.minY);
|
|
585
|
+
tile.maxX = Math.max(tile.maxX, feature.maxX);
|
|
586
|
+
tile.maxY = Math.max(tile.maxY, feature.maxY);
|
|
587
|
+
|
|
588
|
+
if (type === 'Point' || type === 'MultiPoint') {
|
|
589
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
590
|
+
simplified.push(geom[i], geom[i + 1]);
|
|
591
|
+
tile.numPoints++;
|
|
592
|
+
tile.numSimplified++;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
} else if (type === 'LineString') {
|
|
596
|
+
addLine(simplified, geom, tile, tolerance, false, false);
|
|
597
|
+
|
|
598
|
+
} else if (type === 'MultiLineString' || type === 'Polygon') {
|
|
599
|
+
for (let i = 0; i < geom.length; i++) {
|
|
600
|
+
addLine(simplified, geom[i], tile, tolerance, type === 'Polygon', i === 0);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
} else if (type === 'MultiPolygon') {
|
|
604
|
+
|
|
605
|
+
for (let k = 0; k < geom.length; k++) {
|
|
606
|
+
const polygon = geom[k];
|
|
607
|
+
for (let i = 0; i < polygon.length; i++) {
|
|
608
|
+
addLine(simplified, polygon[i], tile, tolerance, true, i === 0);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (simplified.length) {
|
|
614
|
+
let tags = feature.tags || null;
|
|
615
|
+
|
|
616
|
+
if (type === 'LineString' && options.lineMetrics) {
|
|
617
|
+
tags = {};
|
|
618
|
+
for (const key in feature.tags) tags[key] = feature.tags[key];
|
|
619
|
+
/* eslint-disable dot-notation */
|
|
620
|
+
tags['mapbox_clip_start'] = geom.start / geom.size;
|
|
621
|
+
tags['mapbox_clip_end'] = geom.end / geom.size;
|
|
622
|
+
/* eslint-enable dot-notation */
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const tileFeature = {
|
|
626
|
+
geometry: simplified,
|
|
627
|
+
type: type === 'Polygon' || type === 'MultiPolygon' ? 3 :
|
|
628
|
+
(type === 'LineString' || type === 'MultiLineString' ? 2 : 1),
|
|
629
|
+
tags
|
|
630
|
+
};
|
|
631
|
+
if (feature.id !== null) {
|
|
632
|
+
tileFeature.id = feature.id;
|
|
633
|
+
}
|
|
634
|
+
tile.features.push(tileFeature);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function addLine(result, geom, tile, tolerance, isPolygon, isOuter) {
|
|
639
|
+
const sqTolerance = tolerance * tolerance;
|
|
640
|
+
|
|
641
|
+
if (tolerance > 0 && (geom.size < (isPolygon ? sqTolerance : tolerance))) {
|
|
642
|
+
tile.numPoints += geom.length / 3;
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const ring = [];
|
|
647
|
+
|
|
648
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
649
|
+
if (tolerance === 0 || geom[i + 2] > sqTolerance) {
|
|
650
|
+
tile.numSimplified++;
|
|
651
|
+
ring.push(geom[i], geom[i + 1]);
|
|
652
|
+
}
|
|
653
|
+
tile.numPoints++;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
if (isPolygon) rewind(ring, isOuter);
|
|
657
|
+
|
|
658
|
+
result.push(ring);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function rewind(ring, clockwise) {
|
|
662
|
+
let area = 0;
|
|
663
|
+
for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) {
|
|
664
|
+
area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
|
|
665
|
+
}
|
|
666
|
+
if (area > 0 === clockwise) {
|
|
667
|
+
for (let i = 0, len = ring.length; i < len / 2; i += 2) {
|
|
668
|
+
const x = ring[i];
|
|
669
|
+
const y = ring[i + 1];
|
|
670
|
+
ring[i] = ring[len - 2 - i];
|
|
671
|
+
ring[i + 1] = ring[len - 1 - i];
|
|
672
|
+
ring[len - 2 - i] = x;
|
|
673
|
+
ring[len - 1 - i] = y;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// This file provides a set of helper functions for managing "diffs" (changes)
|
|
679
|
+
// to GeoJSON data structures. These diffs describe additions, removals,
|
|
680
|
+
// and updates of features in a GeoJSON source in an efficient way.
|
|
681
|
+
|
|
682
|
+
// GeoJSON Source Diff:
|
|
683
|
+
// {
|
|
684
|
+
// removeAll: true, // If true, clear all existing features
|
|
685
|
+
// remove: [featureId, ...], // Array of feature IDs to remove
|
|
686
|
+
// add: [feature, ...], // Array of GeoJSON features to add
|
|
687
|
+
// update: [GeoJSON Feature Diff, ...] // Array of per-feature updates
|
|
688
|
+
// }
|
|
689
|
+
|
|
690
|
+
// GeoJSON Feature Diff:
|
|
691
|
+
// {
|
|
692
|
+
// id: featureId, // ID of the feature being updated
|
|
693
|
+
// newGeometry: GeoJSON.Geometry, // Optional new geometry
|
|
694
|
+
// removeAllProperties: true, // Remove all properties if true
|
|
695
|
+
// removeProperties: [key, ...], // Specific properties to delete
|
|
696
|
+
// addOrUpdateProperties: [ // Properties to add or update
|
|
697
|
+
// { key: "name", value: "New name" }
|
|
698
|
+
// ]
|
|
699
|
+
// }
|
|
700
|
+
|
|
701
|
+
/* eslint @stylistic/comma-spacing: 0, no-shadow: 0 */
|
|
702
|
+
|
|
703
|
+
// applies a diff to the geojsonvt source simplified features array
|
|
704
|
+
// returns an object with the affected features and new source array for invalidation
|
|
705
|
+
function applySourceDiff(source, dataDiff, options) {
|
|
706
|
+
|
|
707
|
+
// convert diff to sets/maps for o(1) lookups
|
|
708
|
+
const diff = diffToHashed(dataDiff);
|
|
709
|
+
|
|
710
|
+
// collection for features that will be affected by this update
|
|
711
|
+
let affected = [];
|
|
712
|
+
|
|
713
|
+
// full removal - clear everything before applying diff
|
|
714
|
+
if (diff.removeAll) {
|
|
715
|
+
affected = source;
|
|
716
|
+
source = [];
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// remove/add features and collect affected ones
|
|
720
|
+
if (diff.remove.size || diff.add.size) {
|
|
721
|
+
const removeFeatures = [];
|
|
722
|
+
|
|
723
|
+
// collect source features to be removed
|
|
724
|
+
for (const feature of source) {
|
|
725
|
+
const {id} = feature;
|
|
726
|
+
|
|
727
|
+
// explicit feature removal
|
|
728
|
+
if (diff.remove.has(id)) {
|
|
729
|
+
removeFeatures.push(feature);
|
|
730
|
+
// feature with duplicate id being added
|
|
731
|
+
} else if (diff.add.has(id)) {
|
|
732
|
+
removeFeatures.push(feature);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// collect affected and remove from source
|
|
737
|
+
if (removeFeatures.length) {
|
|
738
|
+
affected.push(...removeFeatures);
|
|
739
|
+
|
|
740
|
+
const removeIds = new Set(removeFeatures.map(f => f.id));
|
|
741
|
+
source = source.filter(f => !removeIds.has(f.id));
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// convert and add new features
|
|
745
|
+
if (diff.add.size) {
|
|
746
|
+
// projects and adds simplification info
|
|
747
|
+
let addFeatures = convert({type: 'FeatureCollection', features: Array.from(diff.add.values())}, options);
|
|
748
|
+
|
|
749
|
+
// wraps features (ie extreme west and extreme east)
|
|
750
|
+
addFeatures = wrap(addFeatures, options);
|
|
751
|
+
|
|
752
|
+
affected.push(...addFeatures);
|
|
753
|
+
source.push(...addFeatures);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
if (diff.update.size) {
|
|
758
|
+
for (const [id, update] of diff.update) {
|
|
759
|
+
const featureIndex = source.findIndex(f => f.id === id);
|
|
760
|
+
if (featureIndex === -1) continue;
|
|
761
|
+
|
|
762
|
+
const feature = source[featureIndex];
|
|
763
|
+
|
|
764
|
+
// get updated geojsonvt simplified feature
|
|
765
|
+
const updatedFeature = getUpdatedFeature(feature, update, options);
|
|
766
|
+
if (!updatedFeature) continue;
|
|
767
|
+
|
|
768
|
+
// track both features for invalidation
|
|
769
|
+
affected.push(feature, updatedFeature);
|
|
770
|
+
|
|
771
|
+
// replace old feature with updated feature
|
|
772
|
+
source[featureIndex] = updatedFeature;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return {affected, source};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// return an updated geojsonvt simplified feature
|
|
780
|
+
function getUpdatedFeature(vtFeature, update, options) {
|
|
781
|
+
const changeGeometry = !!update.newGeometry;
|
|
782
|
+
|
|
783
|
+
const changeProps =
|
|
784
|
+
update.removeAllProperties ||
|
|
785
|
+
update.removeProperties?.length > 0 ||
|
|
786
|
+
update.addOrUpdateProperties?.length > 0;
|
|
787
|
+
|
|
788
|
+
// nothing to do
|
|
789
|
+
if (!changeGeometry && !changeProps) return null;
|
|
790
|
+
|
|
791
|
+
// if geometry changed, need to create new geojson feature and convert to simplified format
|
|
792
|
+
if (changeGeometry) {
|
|
793
|
+
const geojsonFeature = {
|
|
794
|
+
type: 'Feature',
|
|
795
|
+
id: vtFeature.id,
|
|
796
|
+
geometry: update.newGeometry,
|
|
797
|
+
properties: changeProps ? applyPropertyUpdates(vtFeature.tags, update) : vtFeature.tags
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
// projects and adds simplification info
|
|
801
|
+
let features = convert({type: 'FeatureCollection', features: [geojsonFeature]}, options);
|
|
802
|
+
|
|
803
|
+
// wraps features (ie extreme west and extreme east)
|
|
804
|
+
features = wrap(features, options);
|
|
805
|
+
|
|
806
|
+
return features[0];
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// only properties changed - update tags directly
|
|
810
|
+
if (changeProps) {
|
|
811
|
+
const feature = {...vtFeature};
|
|
812
|
+
feature.tags = applyPropertyUpdates(feature.tags, update);
|
|
813
|
+
return feature;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
return null;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// helper to apply property updates from a diff update object to a properties object
|
|
820
|
+
function applyPropertyUpdates(tags, update) {
|
|
821
|
+
if (update.removeAllProperties) {
|
|
822
|
+
return {};
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const properties = {...tags || {}};
|
|
826
|
+
|
|
827
|
+
if (update.removeProperties) {
|
|
828
|
+
for (const key of update.removeProperties) {
|
|
829
|
+
delete properties[key];
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
if (update.addOrUpdateProperties) {
|
|
834
|
+
for (const {key, value} of update.addOrUpdateProperties) {
|
|
835
|
+
properties[key] = value;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
return properties;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// Convert a GeoJSON Source Diff to an idempotent hashed representation using Sets and Maps
|
|
843
|
+
function diffToHashed(diff) {
|
|
844
|
+
if (!diff) return {};
|
|
845
|
+
|
|
846
|
+
const hashed = {};
|
|
847
|
+
|
|
848
|
+
hashed.removeAll = diff.removeAll;
|
|
849
|
+
hashed.remove = new Set(diff.remove || []);
|
|
850
|
+
hashed.add = new Map(diff.add?.map(feature => [feature.id, feature]));
|
|
851
|
+
hashed.update = new Map(diff.update?.map(update => [update.id, update]));
|
|
852
|
+
|
|
853
|
+
return hashed;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
const defaultOptions = {
|
|
857
|
+
maxZoom: 14, // max zoom to preserve detail on
|
|
858
|
+
indexMaxZoom: 5, // max zoom in the tile index
|
|
859
|
+
indexMaxPoints: 100000, // max number of points per tile in the tile index
|
|
860
|
+
tolerance: 3, // simplification tolerance (higher means simpler)
|
|
861
|
+
extent: 4096, // tile extent
|
|
862
|
+
buffer: 64, // tile buffer on each side
|
|
863
|
+
lineMetrics: false, // whether to calculate line metrics
|
|
864
|
+
promoteId: null, // name of a feature property to be promoted to feature.id
|
|
865
|
+
generateId: false, // whether to generate feature ids. Cannot be used with promoteId
|
|
866
|
+
updateable: false, // whether geojson can be updated (with caveat of a stored simplified copy)
|
|
867
|
+
debug: 0 // logging level (0, 1 or 2)
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
class GeoJSONVT {
|
|
871
|
+
constructor(data, options) {
|
|
872
|
+
options = this.options = extend(Object.create(defaultOptions), options);
|
|
873
|
+
|
|
874
|
+
const debug = options.debug;
|
|
875
|
+
|
|
876
|
+
if (debug) console.time('preprocess data');
|
|
877
|
+
|
|
878
|
+
if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error('maxZoom should be in the 0-24 range');
|
|
879
|
+
if (options.promoteId && options.generateId) throw new Error('promoteId and generateId cannot be used together.');
|
|
880
|
+
|
|
881
|
+
// projects and adds simplification info
|
|
882
|
+
let features = convert(data, options);
|
|
883
|
+
|
|
884
|
+
// tiles and tileCoords are part of the public API
|
|
885
|
+
this.tiles = {};
|
|
886
|
+
this.tileCoords = [];
|
|
887
|
+
|
|
888
|
+
if (debug) {
|
|
889
|
+
console.timeEnd('preprocess data');
|
|
890
|
+
console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
|
|
891
|
+
console.time('generate tiles');
|
|
892
|
+
this.stats = {};
|
|
893
|
+
this.total = 0;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// wraps features (ie extreme west and extreme east)
|
|
897
|
+
features = wrap(features, options);
|
|
898
|
+
|
|
899
|
+
// start slicing from the top tile down
|
|
900
|
+
if (features.length) this.splitTile(features, 0, 0, 0);
|
|
901
|
+
|
|
902
|
+
// for updateable indexes, store a copy of the original simplified features
|
|
903
|
+
if (options.updateable) {
|
|
904
|
+
this.source = features;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
if (debug) {
|
|
908
|
+
if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
|
|
909
|
+
console.timeEnd('generate tiles');
|
|
910
|
+
console.log('tiles generated:', this.total, JSON.stringify(this.stats));
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// splits features from a parent tile to sub-tiles.
|
|
915
|
+
// z, x, and y are the coordinates of the parent tile
|
|
916
|
+
// cz, cx, and cy are the coordinates of the target tile
|
|
917
|
+
//
|
|
918
|
+
// If no target tile is specified, splitting stops when we reach the maximum
|
|
919
|
+
// zoom or the number of points is low as specified in the options.
|
|
920
|
+
splitTile(features, z, x, y, cz, cx, cy) {
|
|
921
|
+
|
|
922
|
+
const stack = [features, z, x, y];
|
|
923
|
+
const options = this.options;
|
|
924
|
+
const debug = options.debug;
|
|
925
|
+
|
|
926
|
+
// avoid recursion by using a processing queue
|
|
927
|
+
while (stack.length) {
|
|
928
|
+
y = stack.pop();
|
|
929
|
+
x = stack.pop();
|
|
930
|
+
z = stack.pop();
|
|
931
|
+
features = stack.pop();
|
|
932
|
+
|
|
933
|
+
const z2 = 1 << z;
|
|
934
|
+
const id = toID(z, x, y);
|
|
935
|
+
let tile = this.tiles[id];
|
|
936
|
+
|
|
937
|
+
if (!tile) {
|
|
938
|
+
if (debug > 1) console.time('creation');
|
|
939
|
+
|
|
940
|
+
tile = this.tiles[id] = createTile(features, z, x, y, options);
|
|
941
|
+
this.tileCoords.push({z, x, y, id});
|
|
942
|
+
|
|
943
|
+
if (debug) {
|
|
944
|
+
if (debug > 1) {
|
|
945
|
+
console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
|
|
946
|
+
z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
|
|
947
|
+
console.timeEnd('creation');
|
|
948
|
+
}
|
|
949
|
+
const key = `z${ z}`;
|
|
950
|
+
this.stats[key] = (this.stats[key] || 0) + 1;
|
|
951
|
+
this.total++;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
// save reference to original geometry in tile so that we can drill down later if we stop now
|
|
956
|
+
tile.source = features;
|
|
957
|
+
|
|
958
|
+
// if it's the first-pass tiling
|
|
959
|
+
if (cz == null) {
|
|
960
|
+
// stop tiling if we reached max zoom, or if the tile is too simple
|
|
961
|
+
if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
|
|
962
|
+
// if a drilldown to a specific tile
|
|
963
|
+
} else if (z === options.maxZoom || z === cz) {
|
|
964
|
+
// stop tiling if we reached base zoom or our target tile zoom
|
|
965
|
+
continue;
|
|
966
|
+
} else if (cz != null) {
|
|
967
|
+
// stop tiling if it's not an ancestor of the target tile
|
|
968
|
+
const zoomSteps = cz - z;
|
|
969
|
+
if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// if we slice further down, no need to keep source geometry
|
|
973
|
+
tile.source = null;
|
|
974
|
+
|
|
975
|
+
if (features.length === 0) continue;
|
|
976
|
+
|
|
977
|
+
if (debug > 1) console.time('clipping');
|
|
978
|
+
|
|
979
|
+
// values we'll use for clipping
|
|
980
|
+
const k1 = 0.5 * options.buffer / options.extent;
|
|
981
|
+
const k2 = 0.5 - k1;
|
|
982
|
+
const k3 = 0.5 + k1;
|
|
983
|
+
const k4 = 1 + k1;
|
|
984
|
+
|
|
985
|
+
let tl = null;
|
|
986
|
+
let bl = null;
|
|
987
|
+
let tr = null;
|
|
988
|
+
let br = null;
|
|
989
|
+
|
|
990
|
+
const left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
|
|
991
|
+
const right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
|
|
992
|
+
|
|
993
|
+
if (left) {
|
|
994
|
+
tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
|
|
995
|
+
bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
if (right) {
|
|
999
|
+
tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
|
|
1000
|
+
br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
if (debug > 1) console.timeEnd('clipping');
|
|
1004
|
+
|
|
1005
|
+
stack.push(tl || [], z + 1, x * 2, y * 2);
|
|
1006
|
+
stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
|
|
1007
|
+
stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
|
|
1008
|
+
stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
getTile(z, x, y) {
|
|
1013
|
+
z = +z;
|
|
1014
|
+
x = +x;
|
|
1015
|
+
y = +y;
|
|
1016
|
+
|
|
1017
|
+
const options = this.options;
|
|
1018
|
+
const {extent, debug} = options;
|
|
1019
|
+
|
|
1020
|
+
if (z < 0 || z > 24) return null;
|
|
1021
|
+
|
|
1022
|
+
const z2 = 1 << z;
|
|
1023
|
+
x = (x + z2) & (z2 - 1); // wrap tile x coordinate
|
|
1024
|
+
|
|
1025
|
+
const id = toID(z, x, y);
|
|
1026
|
+
if (this.tiles[id]) return transformTile(this.tiles[id], extent);
|
|
1027
|
+
|
|
1028
|
+
if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
|
|
1029
|
+
|
|
1030
|
+
let z0 = z;
|
|
1031
|
+
let x0 = x;
|
|
1032
|
+
let y0 = y;
|
|
1033
|
+
let parent;
|
|
1034
|
+
|
|
1035
|
+
while (!parent && z0 > 0) {
|
|
1036
|
+
z0--;
|
|
1037
|
+
x0 = x0 >> 1;
|
|
1038
|
+
y0 = y0 >> 1;
|
|
1039
|
+
parent = this.tiles[toID(z0, x0, y0)];
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
if (!parent || !parent.source) return null;
|
|
1043
|
+
|
|
1044
|
+
// if we found a parent tile containing the original geometry, we can drill down from it
|
|
1045
|
+
if (debug > 1) {
|
|
1046
|
+
console.log('found parent tile z%d-%d-%d', z0, x0, y0);
|
|
1047
|
+
console.time('drilling down');
|
|
1048
|
+
}
|
|
1049
|
+
this.splitTile(parent.source, z0, x0, y0, z, x, y);
|
|
1050
|
+
if (debug > 1) console.timeEnd('drilling down');
|
|
1051
|
+
|
|
1052
|
+
return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// invalidates (removes) tiles affected by the provided features
|
|
1056
|
+
invalidateTiles(features) {
|
|
1057
|
+
const options = this.options;
|
|
1058
|
+
const {debug} = options;
|
|
1059
|
+
|
|
1060
|
+
// calculate bounding box of all features for trivial reject
|
|
1061
|
+
let minX = Infinity;
|
|
1062
|
+
let maxX = -Infinity;
|
|
1063
|
+
let minY = Infinity;
|
|
1064
|
+
let maxY = -Infinity;
|
|
1065
|
+
|
|
1066
|
+
for (const feature of features) {
|
|
1067
|
+
minX = Math.min(minX, feature.minX);
|
|
1068
|
+
maxX = Math.max(maxX, feature.maxX);
|
|
1069
|
+
minY = Math.min(minY, feature.minY);
|
|
1070
|
+
maxY = Math.max(maxY, feature.maxY);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
// tile buffer clipping value - not halved as in splitTile above because checking against tile's own extent
|
|
1074
|
+
const k1 = options.buffer / options.extent;
|
|
1075
|
+
|
|
1076
|
+
// track removed tile ids for o(1) lookup
|
|
1077
|
+
const removedLookup = new Set();
|
|
1078
|
+
|
|
1079
|
+
// iterate through existing tiles and remove ones that are affected by features
|
|
1080
|
+
for (const id in this.tiles) {
|
|
1081
|
+
const tile = this.tiles[id];
|
|
1082
|
+
|
|
1083
|
+
// calculate tile bounds including buffer
|
|
1084
|
+
const z2 = 1 << tile.z;
|
|
1085
|
+
const tileMinX = (tile.x - k1) / z2;
|
|
1086
|
+
const tileMaxX = (tile.x + 1 + k1) / z2;
|
|
1087
|
+
const tileMinY = (tile.y - k1) / z2;
|
|
1088
|
+
const tileMaxY = (tile.y + 1 + k1) / z2;
|
|
1089
|
+
|
|
1090
|
+
// trivial reject if feature bounds don't intersect tile
|
|
1091
|
+
if (maxX < tileMinX || minX >= tileMaxX ||
|
|
1092
|
+
maxY < tileMinY || minY >= tileMaxY) {
|
|
1093
|
+
continue;
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// check if any feature intersects with the tile
|
|
1097
|
+
let intersects = false;
|
|
1098
|
+
for (const feature of features) {
|
|
1099
|
+
if (feature.maxX >= tileMinX && feature.minX < tileMaxX &&
|
|
1100
|
+
feature.maxY >= tileMinY && feature.minY < tileMaxY) {
|
|
1101
|
+
intersects = true;
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
if (!intersects) continue;
|
|
1106
|
+
|
|
1107
|
+
if (debug) {
|
|
1108
|
+
if (debug > 1) {
|
|
1109
|
+
console.log('invalidate tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
|
|
1110
|
+
tile.z, tile.x, tile.y, tile.numFeatures, tile.numPoints, tile.numSimplified);
|
|
1111
|
+
}
|
|
1112
|
+
const key = `z${ tile.z}`;
|
|
1113
|
+
this.stats[key] = (this.stats[key] || 0) - 1;
|
|
1114
|
+
this.total--;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
delete this.tiles[id];
|
|
1118
|
+
removedLookup.add(id);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
// remove tile coords that are no longer in the index
|
|
1122
|
+
if (removedLookup.size) this.tileCoords = this.tileCoords.filter(c => !removedLookup.has(c.id));
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// updates the tile index by adding and/or removing geojson features
|
|
1126
|
+
// invalidates tiles that are affected by the update for regeneration on next getTile call
|
|
1127
|
+
// diff is an object with properties specified in difference.js
|
|
1128
|
+
updateData(diff) {
|
|
1129
|
+
const options = this.options;
|
|
1130
|
+
const debug = options.debug;
|
|
1131
|
+
|
|
1132
|
+
if (!options.updateable) throw new Error('to update tile geojson `updateable` option must be set to true');
|
|
1133
|
+
|
|
1134
|
+
// apply diff and collect affected features and updated source that will be used to invalidate tiles
|
|
1135
|
+
const {affected, source} = applySourceDiff(this.source, diff, options);
|
|
1136
|
+
|
|
1137
|
+
// nothing has changed
|
|
1138
|
+
if (!affected.length) return;
|
|
1139
|
+
|
|
1140
|
+
// update source with new simplified feature set
|
|
1141
|
+
this.source = source;
|
|
1142
|
+
|
|
1143
|
+
if (debug > 1) {
|
|
1144
|
+
console.log('invalidating tiles');
|
|
1145
|
+
console.time('invalidating');
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
this.invalidateTiles(affected);
|
|
1149
|
+
|
|
1150
|
+
if (debug > 1) console.timeEnd('invalidating');
|
|
1151
|
+
|
|
1152
|
+
// re-generate root tile with updated feature set
|
|
1153
|
+
const [z, x, y] = [0, 0, 0];
|
|
1154
|
+
const rootTile = createTile(this.source, z, x, y, this.options);
|
|
1155
|
+
rootTile.source = this.source;
|
|
1156
|
+
|
|
1157
|
+
// update tile index with new root tile - ready for getTile calls
|
|
1158
|
+
const id = toID(z, x, y);
|
|
1159
|
+
this.tiles[id] = rootTile;
|
|
1160
|
+
this.tileCoords.push({z, x, y, id});
|
|
1161
|
+
|
|
1162
|
+
if (debug) {
|
|
1163
|
+
const key = `z${ z}`;
|
|
1164
|
+
this.stats[key] = (this.stats[key] || 0) + 1;
|
|
1165
|
+
this.total++;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
function toID(z, x, y) {
|
|
1171
|
+
return (((1 << z) * y + x) * 32) + z;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
function extend(dest, src) {
|
|
1175
|
+
for (const i in src) dest[i] = src[i];
|
|
1176
|
+
return dest;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
function geojsonvt(data, options) {
|
|
1180
|
+
return new GeoJSONVT(data, options);
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
return geojsonvt;
|
|
1184
|
+
|
|
1185
|
+
}));
|