@maplibre/geojson-vt 6.3.3 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +606 -461
- package/dist/geojson-vt.js +1 -1
- package/dist/geojson-vt.mjs +603 -458
- 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 +77 -72
- 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 +75 -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/clip.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
|
|
2
|
-
import {createFeature,
|
|
3
|
-
import type { GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature,
|
|
2
|
+
import {createFeature, createPointFeature, isHole} from './feature';
|
|
3
|
+
import type { GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, GeoJSONVTSourceCoords } from './definitions';
|
|
4
4
|
|
|
5
5
|
export const enum AxisType {
|
|
6
6
|
X = 0,
|
|
7
7
|
Y = 1
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
type RingsFeature = GeoJSONVTInternalLineStringFeature | GeoJSONVTInternalMultiLineStringFeature | GeoJSONVTInternalPolygonFeature | GeoJSONVTInternalMultiPolygonFeature;
|
|
11
|
+
|
|
12
|
+
/**
|
|
11
13
|
* clip features between two vertical or horizontal axis-parallel lines:
|
|
12
14
|
* | |
|
|
13
15
|
* ___|___ | /
|
|
@@ -37,6 +39,12 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
|
|
|
37
39
|
const clipped: GeoJSONVTInternalFeature[] = [];
|
|
38
40
|
|
|
39
41
|
for (const feature of features) {
|
|
42
|
+
if (feature.type === 'Point') {
|
|
43
|
+
const a = axis === AxisType.X ? feature.x : feature.y;
|
|
44
|
+
if (a >= start && a < end) clipped.push(feature);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
const min = axis === AxisType.X ? feature.minX : feature.minY;
|
|
41
49
|
const max = axis === AxisType.X ? feature.maxX : feature.maxY;
|
|
42
50
|
|
|
@@ -49,32 +57,12 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
|
|
|
49
57
|
continue;
|
|
50
58
|
}
|
|
51
59
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
case 'LineString': {
|
|
59
|
-
clipLineStringFeature(feature, clipped, start, end, axis, options);
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
case 'MultiLineString': {
|
|
64
|
-
clipMultiLineStringFeature(feature, clipped, start, end, axis);
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
case 'Polygon': {
|
|
69
|
-
clipPolygonFeature(feature, clipped, start, end, axis);
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
case 'MultiPolygon': {
|
|
74
|
-
clipMultiPolygonFeature(feature, clipped, start, end, axis);
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
60
|
+
if (feature.type === 'MultiPoint') {
|
|
61
|
+
clipPointsFeature(feature, clipped, start, end, axis);
|
|
62
|
+
continue;
|
|
77
63
|
}
|
|
64
|
+
|
|
65
|
+
clipRingsFeature(feature, clipped, start, end, axis, options);
|
|
78
66
|
}
|
|
79
67
|
|
|
80
68
|
if (!clipped.length) return null;
|
|
@@ -82,190 +70,284 @@ export function clip(features: GeoJSONVTInternalFeature[], scale: number, start:
|
|
|
82
70
|
return clipped;
|
|
83
71
|
}
|
|
84
72
|
|
|
85
|
-
function
|
|
86
|
-
const geom
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (!geom.length) return;
|
|
90
|
-
|
|
91
|
-
const type = geom.length === 3 ? 'Point' : 'MultiPoint';
|
|
92
|
-
clipped.push(createFeature(feature.id, type, geom, feature.tags));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function clipLineStringFeature(feature: GeoJSONVTInternalLineStringFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType, options: GeoJSONVTOptions) {
|
|
96
|
-
const geom: SliceArray[] = [];
|
|
73
|
+
function clipPointsFeature(feature: GeoJSONVTInternalMultiPointFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType) {
|
|
74
|
+
const geom = feature.geometry;
|
|
75
|
+
const offset = feature.worldOffset;
|
|
76
|
+
const axisOffset = axis === AxisType.X ? offset : 0;
|
|
97
77
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
78
|
+
let count = 0;
|
|
79
|
+
let lastInside = 0;
|
|
80
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
81
|
+
const a = geom[i + axis] + axisOffset;
|
|
82
|
+
if (a >= start && a <= end) {
|
|
83
|
+
count++;
|
|
84
|
+
lastInside = i;
|
|
104
85
|
}
|
|
105
|
-
return;
|
|
106
86
|
}
|
|
87
|
+
if (!count) return;
|
|
107
88
|
|
|
108
|
-
if (
|
|
109
|
-
clipped.push(
|
|
89
|
+
if (count === 1) {
|
|
90
|
+
clipped.push(createPointFeature(feature.id, geom[lastInside] + offset, geom[lastInside + 1], feature.tags));
|
|
110
91
|
return;
|
|
111
92
|
}
|
|
112
93
|
|
|
113
|
-
|
|
114
|
-
|
|
94
|
+
const out = new Float64Array(count * 3);
|
|
95
|
+
let next = 0;
|
|
96
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
97
|
+
const a = geom[i + axis] + axisOffset;
|
|
98
|
+
if (a >= start && a <= end) {
|
|
99
|
+
out[next] = geom[i] + offset;
|
|
100
|
+
out[next + 1] = geom[i + 1];
|
|
101
|
+
out[next + 2] = geom[i + 2];
|
|
102
|
+
next += 3;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
115
105
|
|
|
116
|
-
|
|
117
|
-
|
|
106
|
+
clipped.push(createFeature(feature.id, 'MultiPoint', out, feature.tags));
|
|
107
|
+
}
|
|
118
108
|
|
|
119
|
-
|
|
120
|
-
|
|
109
|
+
function clipRingsFeature(feature: RingsFeature, clipped: GeoJSONVTInternalFeature[], start: number, end: number, axis: AxisType, options: GeoJSONVTOptions) {
|
|
110
|
+
const geom = feature.geometry;
|
|
111
|
+
const offset = feature.worldOffset;
|
|
112
|
+
const isPolygon = feature.type === 'Polygon' || feature.type === 'MultiPolygon';
|
|
113
|
+
|
|
114
|
+
// count first, so that all clipped rings go into one array of the right size
|
|
115
|
+
let length = 0;
|
|
116
|
+
for (let i = 0; i < geom.length;) {
|
|
117
|
+
const ringEnd = i + 2 + geom[i] * 3;
|
|
118
|
+
length += countClippedRing(geom, offset, i + 2, ringEnd, start, end, axis, isPolygon);
|
|
119
|
+
i = ringEnd;
|
|
120
|
+
}
|
|
121
|
+
if (!length) return;
|
|
122
|
+
|
|
123
|
+
const metricsOf = options.lineMetrics && feature.type === 'LineString' ? feature : null;
|
|
124
|
+
const metrics: number[] = [];
|
|
125
|
+
let out = new Float64Array(length);
|
|
126
|
+
let next = 0;
|
|
127
|
+
let hasOuter = false;
|
|
128
|
+
for (let i = 0; i < geom.length;) {
|
|
129
|
+
const ringEnd = i + 2 + geom[i] * 3;
|
|
130
|
+
let size = geom[i + 1];
|
|
131
|
+
if (isPolygon) {
|
|
132
|
+
// the first ring that is left of a polygon is its outer ring, also when that was a hole
|
|
133
|
+
if (!isHole(size)) hasOuter = false;
|
|
134
|
+
if (!hasOuter) size = Math.abs(size);
|
|
135
|
+
}
|
|
136
|
+
const ringStart = next;
|
|
137
|
+
next = clipRing(geom, offset, i + 2, ringEnd, size, out, next, start, end, axis, isPolygon, metricsOf, metrics);
|
|
138
|
+
if (next > ringStart) hasOuter = true;
|
|
139
|
+
i = ringEnd;
|
|
140
|
+
}
|
|
141
|
+
// a polygon ring that crosses a clip line has room for a position that closes it, which it does not always need
|
|
142
|
+
if (next < length) out = out.subarray(0, next);
|
|
143
|
+
|
|
144
|
+
if (metricsOf) {
|
|
145
|
+
// each part of the line becomes a feature, to be able to track its metrics
|
|
146
|
+
for (let i = 0, part = 0; i < out.length; part += 2) {
|
|
147
|
+
const ringEnd = i + 2 + out[i] * 3;
|
|
148
|
+
const line = createFeature(feature.id, 'LineString', i === 0 && ringEnd === out.length ? out : out.slice(i, ringEnd), feature.tags);
|
|
149
|
+
line.start = metrics[part];
|
|
150
|
+
line.end = metrics[part + 1];
|
|
151
|
+
line.size = metricsOf.size;
|
|
152
|
+
clipped.push(line);
|
|
153
|
+
i = ringEnd;
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
121
157
|
|
|
122
|
-
if (
|
|
123
|
-
clipped.push(createFeature(feature.id,
|
|
158
|
+
if (isPolygon) {
|
|
159
|
+
clipped.push(createFeature(feature.id, feature.type, out, feature.tags));
|
|
124
160
|
return;
|
|
125
161
|
}
|
|
126
162
|
|
|
127
|
-
|
|
163
|
+
const isSingleLine = 2 + out[0] * 3 === out.length;
|
|
164
|
+
clipped.push(createFeature(feature.id, isSingleLine ? 'LineString' : 'MultiLineString', out, feature.tags));
|
|
128
165
|
}
|
|
129
166
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
167
|
+
/**
|
|
168
|
+
* How many values `clipRing` writes for a ring, without working out the intersections.
|
|
169
|
+
* @param offset - what to add to every x of `geom`
|
|
170
|
+
*/
|
|
171
|
+
function countClippedRing(geom: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, start: number, end: number, axis: AxisType, isPolygon: boolean): number {
|
|
172
|
+
const axisOffset = axis === AxisType.X ? offset : 0;
|
|
173
|
+
let parts = 0;
|
|
174
|
+
let total = 0;
|
|
175
|
+
let count = 0;
|
|
176
|
+
|
|
177
|
+
for (let i = first; i < ringEnd - 3; i += 3) {
|
|
178
|
+
const a = geom[i + axis] + axisOffset;
|
|
179
|
+
const b = geom[i + 3 + axis] + axisOffset;
|
|
180
|
+
let exited = false;
|
|
138
181
|
|
|
139
|
-
|
|
140
|
-
|
|
182
|
+
if (a < start) {
|
|
183
|
+
if (b >= start) {
|
|
184
|
+
count++;
|
|
185
|
+
}
|
|
186
|
+
} else if (a > end) {
|
|
187
|
+
if (b <= end) {
|
|
188
|
+
count++;
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
count++;
|
|
192
|
+
}
|
|
141
193
|
|
|
142
|
-
|
|
143
|
-
|
|
194
|
+
if (b < start && a >= start) {
|
|
195
|
+
count++;
|
|
196
|
+
exited = true;
|
|
197
|
+
}
|
|
144
198
|
|
|
145
|
-
|
|
146
|
-
|
|
199
|
+
if (b > end && a <= end) {
|
|
200
|
+
count++;
|
|
201
|
+
exited = true;
|
|
202
|
+
}
|
|
147
203
|
|
|
148
|
-
|
|
204
|
+
if (!isPolygon && exited) {
|
|
205
|
+
total += count;
|
|
206
|
+
parts++;
|
|
207
|
+
count = 0;
|
|
208
|
+
}
|
|
149
209
|
}
|
|
150
|
-
if (!geom.length) return;
|
|
151
210
|
|
|
152
|
-
|
|
153
|
-
|
|
211
|
+
const last = ringEnd - 3;
|
|
212
|
+
const a = geom[last + axis] + axisOffset;
|
|
213
|
+
if (a >= start && a <= end) count++;
|
|
154
214
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const a = geom[i + axis];
|
|
215
|
+
// a polygon ring gets a position that closes it when its ends differ, which only shows once it is written
|
|
216
|
+
if (isPolygon && count >= 2) count++;
|
|
158
217
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
218
|
+
if (count > 0) {
|
|
219
|
+
total += count;
|
|
220
|
+
parts++;
|
|
162
221
|
}
|
|
163
|
-
}
|
|
164
222
|
|
|
165
|
-
|
|
223
|
+
return total * 3 + parts * 2;
|
|
224
|
+
}
|
|
166
225
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Clips a ring and writes what is left of it to `out`, each part with the two values before it,
|
|
228
|
+
* see {@link GeoJSONVTSourceCoords}. A polygon ring stays one ring, and a line can come out in several parts.
|
|
229
|
+
* @param offset - what to add to every x of `geom`, which `out` has added already
|
|
230
|
+
* @param metricsOf - the line whose metrics to track, which adds where each part starts and ends to `metrics`
|
|
231
|
+
* @returns where the next ring starts in `out`
|
|
232
|
+
*/
|
|
233
|
+
function clipRing(geom: GeoJSONVTSourceCoords, offset: number, first: number, ringEnd: number, size: number, out: GeoJSONVTSourceCoords, next: number, start: number, end: number, axis: AxisType, isPolygon: boolean, metricsOf: GeoJSONVTInternalLineStringFeature | null, metrics: number[]): number {
|
|
234
|
+
let header = next;
|
|
235
|
+
out[next + 1] = size;
|
|
236
|
+
next += 2;
|
|
237
|
+
let partStart = next;
|
|
238
|
+
|
|
239
|
+
let len = metricsOf ? metricsOf.start : 0;
|
|
240
|
+
let metricStart = len;
|
|
170
241
|
let segLen, t;
|
|
171
242
|
|
|
172
|
-
for (let i =
|
|
173
|
-
const ax = geom
|
|
174
|
-
const ay = geom
|
|
175
|
-
const az = geom
|
|
176
|
-
const bx = geom
|
|
177
|
-
const by = geom
|
|
243
|
+
for (let i = first; i < ringEnd - 3; i += 3) {
|
|
244
|
+
const ax = geom[i] + offset;
|
|
245
|
+
const ay = geom[i + 1];
|
|
246
|
+
const az = geom[i + 2];
|
|
247
|
+
const bx = geom[i + 3] + offset;
|
|
248
|
+
const by = geom[i + 4];
|
|
178
249
|
const a = axis === AxisType.X ? ax : ay;
|
|
179
250
|
const b = axis === AxisType.X ? bx : by;
|
|
180
251
|
let exited = false;
|
|
181
252
|
|
|
182
|
-
if (
|
|
253
|
+
if (metricsOf) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
|
|
183
254
|
|
|
184
255
|
if (a < start) {
|
|
185
256
|
// ---|--> | (line enters the clip region from the left)
|
|
186
257
|
if (b >= start) {
|
|
187
|
-
t = intersect(
|
|
188
|
-
|
|
258
|
+
t = intersect(out, next, ax, ay, bx, by, start, axis);
|
|
259
|
+
next += 3;
|
|
260
|
+
if (metricsOf) metricStart = len + segLen * t;
|
|
189
261
|
}
|
|
190
262
|
} else if (a > end) {
|
|
191
263
|
// | <--|--- (line enters the clip region from the right)
|
|
192
264
|
if (b <= end) {
|
|
193
|
-
t = intersect(
|
|
194
|
-
|
|
265
|
+
t = intersect(out, next, ax, ay, bx, by, end, axis);
|
|
266
|
+
next += 3;
|
|
267
|
+
if (metricsOf) metricStart = len + segLen * t;
|
|
195
268
|
}
|
|
196
269
|
} else {
|
|
197
|
-
|
|
270
|
+
out[next] = ax;
|
|
271
|
+
out[next + 1] = ay;
|
|
272
|
+
out[next + 2] = az;
|
|
273
|
+
next += 3;
|
|
198
274
|
}
|
|
199
275
|
|
|
200
276
|
if (b < start && a >= start) {
|
|
201
277
|
// <--|--- | or <--|-----|--- (line exits the clip region on the left)
|
|
202
|
-
t = intersect(
|
|
278
|
+
t = intersect(out, next, ax, ay, bx, by, start, axis);
|
|
279
|
+
next += 3;
|
|
203
280
|
exited = true;
|
|
204
281
|
}
|
|
205
282
|
|
|
206
283
|
if (b > end && a <= end) {
|
|
207
284
|
// | ---|--> or ---|-----|--> (line exits the clip region on the right)
|
|
208
|
-
t = intersect(
|
|
285
|
+
t = intersect(out, next, ax, ay, bx, by, end, axis);
|
|
286
|
+
next += 3;
|
|
209
287
|
exited = true;
|
|
210
288
|
}
|
|
211
289
|
|
|
212
290
|
if (!isPolygon && exited) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
291
|
+
// the part ends here, and the next one starts
|
|
292
|
+
out[header] = (next - partStart) / 3;
|
|
293
|
+
if (metricsOf) {
|
|
294
|
+
metrics.push(metricStart, len + segLen * t);
|
|
295
|
+
metricStart = metricsOf.start;
|
|
296
|
+
}
|
|
297
|
+
header = next;
|
|
298
|
+
out[next + 1] = size;
|
|
299
|
+
next += 2;
|
|
300
|
+
partStart = next;
|
|
216
301
|
}
|
|
217
302
|
|
|
218
|
-
if (
|
|
303
|
+
if (metricsOf) len += segLen;
|
|
219
304
|
}
|
|
220
305
|
|
|
221
306
|
// add the last point
|
|
222
|
-
|
|
223
|
-
const ax = geom
|
|
224
|
-
const ay = geom
|
|
225
|
-
const az = geom.points[last + 2];
|
|
307
|
+
const last = ringEnd - 3;
|
|
308
|
+
const ax = geom[last] + offset;
|
|
309
|
+
const ay = geom[last + 1];
|
|
226
310
|
const a = axis === AxisType.X ? ax : ay;
|
|
227
|
-
if (a >= start && a <= end)
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
addPoint(slice.points, slice.points[0], slice.points[1], slice.points[2]);
|
|
311
|
+
if (a >= start && a <= end) {
|
|
312
|
+
out[next] = ax;
|
|
313
|
+
out[next + 1] = ay;
|
|
314
|
+
out[next + 2] = geom[last + 2];
|
|
315
|
+
next += 3;
|
|
233
316
|
}
|
|
234
317
|
|
|
235
|
-
//
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
|
|
318
|
+
// close the polygon if its endpoints are not the same after clipping
|
|
319
|
+
if (isPolygon && next - partStart >= 6 && (out[next - 3] !== out[partStart] || out[next - 2] !== out[partStart + 1])) {
|
|
320
|
+
out[next] = out[partStart];
|
|
321
|
+
out[next + 1] = out[partStart + 1];
|
|
322
|
+
out[next + 2] = out[partStart + 2];
|
|
323
|
+
next += 3;
|
|
239
324
|
}
|
|
240
|
-
}
|
|
241
325
|
|
|
242
|
-
|
|
243
|
-
return
|
|
244
|
-
points: [],
|
|
245
|
-
size: line.size,
|
|
246
|
-
start: line.start,
|
|
247
|
-
end: line.end
|
|
248
|
-
};
|
|
249
|
-
}
|
|
326
|
+
// add the final part, or give back the two values before it when it is empty
|
|
327
|
+
if (next === partStart) return header;
|
|
250
328
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
clipLine(line, newGeom, start, end, axis, isPolygon, false);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
329
|
+
out[header] = (next - partStart) / 3;
|
|
330
|
+
if (metricsOf) metrics.push(metricStart, metricsOf.end);
|
|
256
331
|
|
|
257
|
-
|
|
258
|
-
out.push(x, y, z);
|
|
332
|
+
return next;
|
|
259
333
|
}
|
|
260
334
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Writes the position where a segment crosses the clip line at `k`.
|
|
337
|
+
* @returns how far along the segment that is, from 0 to 1
|
|
338
|
+
*/
|
|
339
|
+
function intersect(out: GeoJSONVTSourceCoords, next: number, ax: number, ay: number, bx: number, by: number, k: number, axis: AxisType): number {
|
|
340
|
+
let t;
|
|
341
|
+
if (axis === AxisType.X) {
|
|
342
|
+
t = (k - ax) / (bx - ax);
|
|
343
|
+
out[next] = k;
|
|
344
|
+
out[next + 1] = ay + (by - ay) * t;
|
|
345
|
+
} else {
|
|
346
|
+
t = (k - ay) / (by - ay);
|
|
347
|
+
out[next] = ax + (bx - ax) * t;
|
|
348
|
+
out[next + 1] = k;
|
|
349
|
+
}
|
|
350
|
+
out[next + 2] = 1;
|
|
266
351
|
|
|
267
|
-
function intersectY(out: SliceArray, ax: number, ay: number, bx: number, by: number, y: number) {
|
|
268
|
-
const t = (y - ay) / (by - ay);
|
|
269
|
-
addPoint(out.points, ax + (bx - ax) * t, y, 1);
|
|
270
352
|
return t;
|
|
271
353
|
}
|
|
@@ -83,11 +83,13 @@ const OFFSET_PROP = 4;
|
|
|
83
83
|
export class ClusterTileIndex implements GeoJSONVTTileIndex {
|
|
84
84
|
options: Required<SuperclusterOptions>;
|
|
85
85
|
/**
|
|
86
|
+
* @internal
|
|
86
87
|
* A tree per zoom level. A zoom level where no clusters form shares the tree of the zoom level above it.
|
|
87
88
|
*/
|
|
88
89
|
trees: KDBushWithData[];
|
|
89
90
|
stride: number;
|
|
90
91
|
clusterProps: Record<string, unknown>[];
|
|
92
|
+
/** @internal */
|
|
91
93
|
points: GeoJSONVTInternalPointFeature[];
|
|
92
94
|
private worldCoordinateHelper: WorldCoordinateHelper;
|
|
93
95
|
|
|
@@ -117,12 +119,13 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
|
|
|
117
119
|
const positions = point.geometry.type === 'MultiPoint' ? point.geometry.coordinates : [point.geometry.coordinates];
|
|
118
120
|
for (const [lng, lat] of positions) {
|
|
119
121
|
const [x, y] = [this.worldCoordinateHelper.projectX(lng), this.worldCoordinateHelper.projectY(lat)];
|
|
120
|
-
if (!this.options.wrap && !isInWorld(
|
|
122
|
+
if (!this.options.wrap && !isInWorld(x, y)) continue;
|
|
121
123
|
|
|
122
124
|
const feature: GeoJSONVTInternalPointFeature = {
|
|
123
125
|
id: point.id,
|
|
124
126
|
type: 'Point',
|
|
125
|
-
|
|
127
|
+
x,
|
|
128
|
+
y,
|
|
126
129
|
tags: point.properties
|
|
127
130
|
};
|
|
128
131
|
|
|
@@ -144,22 +147,32 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
|
|
|
144
147
|
|
|
145
148
|
for (const feature of features) {
|
|
146
149
|
if (feature.type === 'Point') {
|
|
147
|
-
if (!this.
|
|
150
|
+
if (!this.isClustered(feature.x, feature.y)) continue;
|
|
148
151
|
points.push(feature);
|
|
149
152
|
continue;
|
|
150
153
|
}
|
|
151
154
|
if (feature.type !== 'MultiPoint') continue;
|
|
152
155
|
|
|
153
156
|
for (let i = 0; i < feature.geometry.length; i += 3) {
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
+
const x = feature.geometry[i] + feature.worldOffset;
|
|
158
|
+
const y = feature.geometry[i + 1];
|
|
159
|
+
if (!this.isClustered(x, y)) continue;
|
|
160
|
+
points.push({id: feature.id, type: 'Point', x, y, tags: feature.tags});
|
|
157
161
|
}
|
|
158
162
|
}
|
|
159
163
|
|
|
160
164
|
this.createIndex(points);
|
|
161
165
|
}
|
|
162
166
|
|
|
167
|
+
/**
|
|
168
|
+
* Whether a position of the data source goes into the clusters. In a world that wraps, the source also has a copy
|
|
169
|
+
* of each position near the antimeridian on the other side of it. Only the one inside the world is clustered, since
|
|
170
|
+
* `getTile` looks across the antimeridian by itself. In a world that doesn't wrap, the positions outside it are left out.
|
|
171
|
+
*/
|
|
172
|
+
private isClustered(x: number, y: number): boolean {
|
|
173
|
+
return this.options.wrap ? x >= 0 && x < 1 : isInWorld(x, y);
|
|
174
|
+
}
|
|
175
|
+
|
|
163
176
|
/**
|
|
164
177
|
* @internal
|
|
165
178
|
* Updates the cluster data by rebuilding.
|
|
@@ -199,11 +212,11 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
|
|
|
199
212
|
|
|
200
213
|
for (let i = 0; i < points.length; i++) {
|
|
201
214
|
const p = points[i];
|
|
202
|
-
if (!
|
|
215
|
+
if (!fitsInteger(p.x) || !fitsInteger(p.y)) continue;
|
|
203
216
|
|
|
204
217
|
// store internal point/cluster data in flat numeric arrays for performance
|
|
205
|
-
coords[numItems * 2] = Math.round(encode(p.
|
|
206
|
-
coords[numItems * 2 + 1] = Math.round(encode(p.
|
|
218
|
+
coords[numItems * 2] = Math.round(encode(p.x)); // projected point coordinates
|
|
219
|
+
coords[numItems * 2 + 1] = Math.round(encode(p.y));
|
|
207
220
|
const k = numItems * stride;
|
|
208
221
|
data[k + OFFSET_ZOOM] = notProcessed; // the last zoom the point was processed at
|
|
209
222
|
data[k + OFFSET_ID] = i; // index of the source feature in the original input array
|
|
@@ -377,14 +390,7 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
|
|
|
377
390
|
const top = encode((y - p) / z2);
|
|
378
391
|
const bottom = encode((y + 1 + p) / z2);
|
|
379
392
|
|
|
380
|
-
const tile: GeoJSONVTTile = {
|
|
381
|
-
transformed: true,
|
|
382
|
-
features: [],
|
|
383
|
-
source: null,
|
|
384
|
-
x: x,
|
|
385
|
-
y: y,
|
|
386
|
-
z: z
|
|
387
|
-
};
|
|
393
|
+
const tile: GeoJSONVTTile = {features: []};
|
|
388
394
|
|
|
389
395
|
this.addTileFeatures(
|
|
390
396
|
tree.range(encode((x - p) / z2), top, encode((x + 1 + p) / z2), bottom),
|
|
@@ -480,7 +486,8 @@ export class ClusterTileIndex implements GeoJSONVTTileIndex {
|
|
|
480
486
|
} else {
|
|
481
487
|
const p = this.points[data[k + OFFSET_ID]];
|
|
482
488
|
tags = p.tags;
|
|
483
|
-
|
|
489
|
+
px = p.x;
|
|
490
|
+
py = p.y;
|
|
484
491
|
}
|
|
485
492
|
|
|
486
493
|
const f: GeoJSONVTFeature = {
|
|
@@ -650,7 +657,8 @@ function getClusterFeature(coords: Int32Array, index: number, data: Int32Array,
|
|
|
650
657
|
id: data[i + OFFSET_ID],
|
|
651
658
|
type: 'Point',
|
|
652
659
|
tags: getClusterProperties(data, i, clusterProps),
|
|
653
|
-
|
|
660
|
+
x: decode(coords[index * 2]),
|
|
661
|
+
y: decode(coords[index * 2 + 1])
|
|
654
662
|
};
|
|
655
663
|
}
|
|
656
664
|
|
|
@@ -682,6 +690,6 @@ function getClusterProperties(data: Int32Array, i: number, clusterProps: Record<
|
|
|
682
690
|
});
|
|
683
691
|
}
|
|
684
692
|
|
|
685
|
-
function isInWorld(
|
|
686
|
-
return
|
|
693
|
+
function isInWorld(x: number, y: number): boolean {
|
|
694
|
+
return x >= 0 && x <= 1 && y >= 0 && y <= 1;
|
|
687
695
|
}
|