@vizhub/quadstream 0.0.0-stage → 0.3.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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +718 -2
- package/bin/quadstream.mjs +240 -0
- package/dist/adapters.d.ts +26 -0
- package/dist/adapters.js +229 -0
- package/dist/blg.d.ts +17 -0
- package/dist/blg.js +83 -0
- package/dist/dataset.d.ts +16 -0
- package/dist/dataset.js +61 -0
- package/dist/geometry-wire.d.ts +8 -0
- package/dist/geometry-wire.js +76 -0
- package/dist/geometry.d.ts +15 -0
- package/dist/geometry.js +236 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +22 -0
- package/dist/loader.d.ts +34 -0
- package/dist/loader.js +138 -0
- package/dist/lod-scheduler.d.ts +15 -0
- package/dist/lod-scheduler.js +85 -0
- package/dist/manifest-schema.d.ts +4 -0
- package/dist/manifest-schema.js +141 -0
- package/dist/maplibre-adapter.d.ts +30 -0
- package/dist/maplibre-adapter.js +84 -0
- package/dist/partition.d.ts +13 -0
- package/dist/partition.js +124 -0
- package/dist/progressive-map.d.ts +28 -0
- package/dist/progressive-map.js +112 -0
- package/dist/quadtree.d.ts +8 -0
- package/dist/quadtree.js +50 -0
- package/dist/reconstruct.d.ts +27 -0
- package/dist/reconstruct.js +145 -0
- package/dist/session.d.ts +24 -0
- package/dist/session.js +116 -0
- package/dist/spatial-index.d.ts +9 -0
- package/dist/spatial-index.js +63 -0
- package/dist/spherical-clip.d.ts +95 -0
- package/dist/spherical-clip.js +557 -0
- package/dist/spherical-cull.d.ts +25 -0
- package/dist/spherical-cull.js +93 -0
- package/dist/spherical-dataset.d.ts +55 -0
- package/dist/spherical-dataset.js +305 -0
- package/dist/spherical-geometry.d.ts +39 -0
- package/dist/spherical-geometry.js +229 -0
- package/dist/spherical-preprocess.d.ts +19 -0
- package/dist/spherical-preprocess.js +144 -0
- package/dist/spherical-repair.d.ts +17 -0
- package/dist/spherical-repair.js +100 -0
- package/dist/spherical-robust-dataset.d.ts +27 -0
- package/dist/spherical-robust-dataset.js +89 -0
- package/dist/spherical-view-clipper.d.ts +22 -0
- package/dist/spherical-view-clipper.js +85 -0
- package/dist/topology.d.ts +11 -0
- package/dist/topology.js +229 -0
- package/dist/types.d.ts +123 -0
- package/dist/types.js +1 -0
- package/package.json +79 -4
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
function polygons(g) {
|
|
2
|
+
if (g.type === "Polygon")
|
|
3
|
+
return [g.coordinates];
|
|
4
|
+
if (g.type === "MultiPolygon")
|
|
5
|
+
return g.coordinates;
|
|
6
|
+
throw new Error("Packed geometry supports only Polygon and MultiPolygon");
|
|
7
|
+
}
|
|
8
|
+
/** Transferable geometry only; feature order and metadata are supplied by the original. */
|
|
9
|
+
export function packGeometry(geojson) {
|
|
10
|
+
let metaLen = 0, coordCount = 0;
|
|
11
|
+
for (const f of geojson.features) {
|
|
12
|
+
const polys = polygons(f.geometry);
|
|
13
|
+
metaLen++;
|
|
14
|
+
for (const rings of polys) {
|
|
15
|
+
metaLen += 1 + rings.length;
|
|
16
|
+
for (const ring of rings)
|
|
17
|
+
coordCount += ring.length * 2;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const meta = new Int32Array(metaLen), coords = new Float64Array(coordCount);
|
|
21
|
+
let mi = 0, ci = 0;
|
|
22
|
+
for (const f of geojson.features) {
|
|
23
|
+
const polys = polygons(f.geometry);
|
|
24
|
+
meta[mi++] = polys.length;
|
|
25
|
+
for (const rings of polys) {
|
|
26
|
+
meta[mi++] = rings.length;
|
|
27
|
+
for (const ring of rings) {
|
|
28
|
+
meta[mi++] = ring.length;
|
|
29
|
+
for (const [x, y] of ring) {
|
|
30
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
31
|
+
throw new Error("Non-finite coordinate");
|
|
32
|
+
coords[ci++] = x;
|
|
33
|
+
coords[ci++] = y;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return { coords, meta };
|
|
39
|
+
}
|
|
40
|
+
export function unpackGeometry(original, coords, meta) {
|
|
41
|
+
let mi = 0, ci = 0;
|
|
42
|
+
const next = (limit) => {
|
|
43
|
+
const n = meta[mi++];
|
|
44
|
+
if (n === undefined || n < 0 || n > limit)
|
|
45
|
+
throw new Error("Malformed geometry metadata");
|
|
46
|
+
return n;
|
|
47
|
+
};
|
|
48
|
+
const features = original.features.map((f) => {
|
|
49
|
+
polygons(f.geometry);
|
|
50
|
+
const count = next(meta.length - mi - 1);
|
|
51
|
+
if (f.geometry.type === "Polygon" && count !== 1)
|
|
52
|
+
throw new Error("Malformed polygon");
|
|
53
|
+
const polys = [];
|
|
54
|
+
for (let p = 0; p < count; p++) {
|
|
55
|
+
const rings = [], nr = next(meta.length - mi - 1);
|
|
56
|
+
for (let r = 0; r < nr; r++) {
|
|
57
|
+
const ring = [], np = next(Math.floor((coords.length - ci) / 2));
|
|
58
|
+
for (let k = 0; k < np; k++) {
|
|
59
|
+
const x = coords[ci++], y = coords[ci++];
|
|
60
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
61
|
+
throw new Error("Non-finite coordinate");
|
|
62
|
+
ring.push([x, y]);
|
|
63
|
+
}
|
|
64
|
+
rings.push(ring);
|
|
65
|
+
}
|
|
66
|
+
polys.push(rings);
|
|
67
|
+
}
|
|
68
|
+
const geometry = f.geometry.type === "Polygon"
|
|
69
|
+
? { type: "Polygon", coordinates: polys[0] }
|
|
70
|
+
: { type: "MultiPolygon", coordinates: polys };
|
|
71
|
+
return { ...f, geometry };
|
|
72
|
+
});
|
|
73
|
+
if (mi !== meta.length || ci !== coords.length)
|
|
74
|
+
throw new Error("Trailing geometry data");
|
|
75
|
+
return { type: "FeatureCollection", features };
|
|
76
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Bounds, Position } from "./types.js";
|
|
2
|
+
export declare function assertPosition(value: unknown): Position;
|
|
3
|
+
export declare function boundsOf(points: readonly Position[]): Bounds;
|
|
4
|
+
export declare function unionBounds(a: Bounds, b: Bounds): Bounds;
|
|
5
|
+
export declare function intersects(a: Bounds, b: Bounds): boolean;
|
|
6
|
+
export declare function assertBounds(b: Bounds): Bounds;
|
|
7
|
+
export declare function signedArea(ring: readonly Position[]): number;
|
|
8
|
+
/** Includes touching endpoints; callers skip adjacent polygon edges. */
|
|
9
|
+
export declare function segmentsIntersect(a: Position, b: Position, c: Position, d: Position): boolean;
|
|
10
|
+
/** O(n^2) reference predicate; retained as the oracle for the indexed validator. */
|
|
11
|
+
export declare function simpleClosedRingReference(ring: readonly Position[]): boolean;
|
|
12
|
+
/** Sweep-and-prune over segment boxes; exact predicates only on overlapping candidates. */
|
|
13
|
+
export declare function simpleClosedRing(ring: readonly Position[]): boolean;
|
|
14
|
+
export declare function pointInRing(p: Position, ring: readonly Position[]): boolean;
|
|
15
|
+
export declare function validPolygon(rings: readonly Position[][]): boolean;
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
export function assertPosition(value) {
|
|
2
|
+
if (!Array.isArray(value) ||
|
|
3
|
+
value.length !== 2 ||
|
|
4
|
+
!value.every((v) => typeof v === "number" && Number.isFinite(v))) {
|
|
5
|
+
throw new Error("Coordinates must be finite planar [x, y] pairs");
|
|
6
|
+
}
|
|
7
|
+
return [
|
|
8
|
+
Object.is(value[0], -0) ? 0 : value[0],
|
|
9
|
+
Object.is(value[1], -0) ? 0 : value[1],
|
|
10
|
+
];
|
|
11
|
+
}
|
|
12
|
+
export function boundsOf(points) {
|
|
13
|
+
if (!points.length)
|
|
14
|
+
throw new Error("Cannot construct bounds of empty coordinates");
|
|
15
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
16
|
+
for (const [x, y] of points) {
|
|
17
|
+
x0 = Math.min(x0, x);
|
|
18
|
+
y0 = Math.min(y0, y);
|
|
19
|
+
x1 = Math.max(x1, x);
|
|
20
|
+
y1 = Math.max(y1, y);
|
|
21
|
+
}
|
|
22
|
+
return [x0, y0, x1, y1];
|
|
23
|
+
}
|
|
24
|
+
export function unionBounds(a, b) {
|
|
25
|
+
return [
|
|
26
|
+
Math.min(a[0], b[0]),
|
|
27
|
+
Math.min(a[1], b[1]),
|
|
28
|
+
Math.max(a[2], b[2]),
|
|
29
|
+
Math.max(a[3], b[3]),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
export function intersects(a, b) {
|
|
33
|
+
return a[0] <= b[2] && a[2] >= b[0] && a[1] <= b[3] && a[3] >= b[1];
|
|
34
|
+
}
|
|
35
|
+
export function assertBounds(b) {
|
|
36
|
+
if (!Array.isArray(b) ||
|
|
37
|
+
b.length !== 4 ||
|
|
38
|
+
!b.every(Number.isFinite) ||
|
|
39
|
+
b[0] > b[2] ||
|
|
40
|
+
b[1] > b[3])
|
|
41
|
+
throw new Error("Invalid [xmin,ymin,xmax,ymax] bounds");
|
|
42
|
+
return b;
|
|
43
|
+
}
|
|
44
|
+
export function signedArea(ring) {
|
|
45
|
+
let a = 0;
|
|
46
|
+
for (let i = 0; i < ring.length - 1; i++) {
|
|
47
|
+
const p = ring[i], q = ring[i + 1];
|
|
48
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
49
|
+
}
|
|
50
|
+
return a / 2;
|
|
51
|
+
}
|
|
52
|
+
function orientation(a, b, c) {
|
|
53
|
+
return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
54
|
+
}
|
|
55
|
+
function within(a, b, p) {
|
|
56
|
+
return (Math.min(a[0], b[0]) <= p[0] &&
|
|
57
|
+
p[0] <= Math.max(a[0], b[0]) &&
|
|
58
|
+
Math.min(a[1], b[1]) <= p[1] &&
|
|
59
|
+
p[1] <= Math.max(a[1], b[1]));
|
|
60
|
+
}
|
|
61
|
+
/** Includes touching endpoints; callers skip adjacent polygon edges. */
|
|
62
|
+
export function segmentsIntersect(a, b, c, d) {
|
|
63
|
+
// Inclusive AABB rejection: disjoint boxes cannot touch or cross.
|
|
64
|
+
if (Math.max(a[0], b[0]) < Math.min(c[0], d[0]) ||
|
|
65
|
+
Math.max(c[0], d[0]) < Math.min(a[0], b[0]) ||
|
|
66
|
+
Math.max(a[1], b[1]) < Math.min(c[1], d[1]) ||
|
|
67
|
+
Math.max(c[1], d[1]) < Math.min(a[1], b[1]))
|
|
68
|
+
return false;
|
|
69
|
+
const abC = orientation(a, b, c), abD = orientation(a, b, d), cdA = orientation(c, d, a), cdB = orientation(c, d, b);
|
|
70
|
+
if (((abC > 0 && abD < 0) || (abC < 0 && abD > 0)) &&
|
|
71
|
+
((cdA > 0 && cdB < 0) || (cdA < 0 && cdB > 0)))
|
|
72
|
+
return true;
|
|
73
|
+
return ((abC === 0 && within(a, b, c)) ||
|
|
74
|
+
(abD === 0 && within(a, b, d)) ||
|
|
75
|
+
(cdA === 0 && within(c, d, a)) ||
|
|
76
|
+
(cdB === 0 && within(c, d, b)));
|
|
77
|
+
}
|
|
78
|
+
function same(a, b) {
|
|
79
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
80
|
+
}
|
|
81
|
+
/** O(n^2) reference predicate; retained as the oracle for the indexed validator. */
|
|
82
|
+
export function simpleClosedRingReference(ring) {
|
|
83
|
+
const n = ring.length - 1;
|
|
84
|
+
if (n < 3 || !same(ring[0], ring[n]) || signedArea(ring) === 0)
|
|
85
|
+
return false;
|
|
86
|
+
for (let i = 0; i < n; i++) {
|
|
87
|
+
if (same(ring[i], ring[i + 1]))
|
|
88
|
+
return false;
|
|
89
|
+
for (let j = i + 1; j < n; j++) {
|
|
90
|
+
if (j === i + 1 || (i === 0 && j === n - 1))
|
|
91
|
+
continue;
|
|
92
|
+
if (segmentsIntersect(ring[i], ring[i + 1], ring[j], ring[j + 1]))
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
/** Rings below this many segments use the quadratic loops directly. */
|
|
99
|
+
const INDEX_MIN_SEGMENTS = 24;
|
|
100
|
+
function segmentBoxes(ring) {
|
|
101
|
+
const count = ring.length - 1, minX = new Float64Array(count), maxX = new Float64Array(count), minY = new Float64Array(count), maxY = new Float64Array(count);
|
|
102
|
+
for (let i = 0; i < count; i++) {
|
|
103
|
+
const a = ring[i], b = ring[i + 1];
|
|
104
|
+
if (!Number.isFinite(a[0] + a[1] + b[0] + b[1]) ||
|
|
105
|
+
a.length !== 2 ||
|
|
106
|
+
b.length !== 2)
|
|
107
|
+
return null;
|
|
108
|
+
minX[i] = Math.min(a[0], b[0]);
|
|
109
|
+
maxX[i] = Math.max(a[0], b[0]);
|
|
110
|
+
minY[i] = Math.min(a[1], b[1]);
|
|
111
|
+
maxY[i] = Math.max(a[1], b[1]);
|
|
112
|
+
}
|
|
113
|
+
const order = new Uint32Array(count);
|
|
114
|
+
for (let i = 0; i < count; i++)
|
|
115
|
+
order[i] = i;
|
|
116
|
+
order.sort((p, q) => minX[p] - minX[q] || p - q);
|
|
117
|
+
return { count, minX, maxX, minY, maxY, order };
|
|
118
|
+
}
|
|
119
|
+
/** Each unordered segment pair with overlapping boxes is visited once; true stops. */
|
|
120
|
+
function sweepSelf(s, visit) {
|
|
121
|
+
const { count, minX, maxX, minY, maxY, order } = s;
|
|
122
|
+
for (let a = 0; a < count; a++) {
|
|
123
|
+
const i = order[a], reach = maxX[i];
|
|
124
|
+
for (let b = a + 1; b < count; b++) {
|
|
125
|
+
const j = order[b];
|
|
126
|
+
if (minX[j] > reach)
|
|
127
|
+
break;
|
|
128
|
+
if (minY[j] > maxY[i] || maxY[j] < minY[i])
|
|
129
|
+
continue;
|
|
130
|
+
if (visit(i, j))
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
function sweepBetween(A, B, visit) {
|
|
137
|
+
let ia = 0, ib = 0;
|
|
138
|
+
while (ia < A.count && ib < B.count) {
|
|
139
|
+
const i = A.order[ia], j = B.order[ib];
|
|
140
|
+
if (A.minX[i] <= B.minX[j]) {
|
|
141
|
+
for (let k = ib; k < B.count; k++) {
|
|
142
|
+
const m = B.order[k];
|
|
143
|
+
if (B.minX[m] > A.maxX[i])
|
|
144
|
+
break;
|
|
145
|
+
if (B.minY[m] > A.maxY[i] || B.maxY[m] < A.minY[i])
|
|
146
|
+
continue;
|
|
147
|
+
if (visit(i, m))
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
ia++;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
for (let k = ia; k < A.count; k++) {
|
|
154
|
+
const m = A.order[k];
|
|
155
|
+
if (A.minX[m] > B.maxX[j])
|
|
156
|
+
break;
|
|
157
|
+
if (A.minY[m] > B.maxY[j] || A.maxY[m] < B.minY[j])
|
|
158
|
+
continue;
|
|
159
|
+
if (visit(m, j))
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
ib++;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
/** Sweep-and-prune over segment boxes; exact predicates only on overlapping candidates. */
|
|
168
|
+
export function simpleClosedRing(ring) {
|
|
169
|
+
const n = ring.length - 1;
|
|
170
|
+
if (n < INDEX_MIN_SEGMENTS)
|
|
171
|
+
return simpleClosedRingReference(ring);
|
|
172
|
+
if (!same(ring[0], ring[n]) || signedArea(ring) === 0)
|
|
173
|
+
return false;
|
|
174
|
+
for (let i = 0; i < n; i++)
|
|
175
|
+
if (same(ring[i], ring[i + 1]))
|
|
176
|
+
return false;
|
|
177
|
+
const boxes = segmentBoxes(ring);
|
|
178
|
+
if (!boxes)
|
|
179
|
+
return simpleClosedRingReference(ring);
|
|
180
|
+
return !sweepSelf(boxes, (p, q) => {
|
|
181
|
+
const i = Math.min(p, q), j = Math.max(p, q);
|
|
182
|
+
if (j === i + 1 || (i === 0 && j === n - 1))
|
|
183
|
+
return false;
|
|
184
|
+
return segmentsIntersect(ring[i], ring[i + 1], ring[j], ring[j + 1]);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
export function pointInRing(p, ring) {
|
|
188
|
+
let inside = false;
|
|
189
|
+
for (let i = 0, j = ring.length - 2; i < ring.length - 1; j = i++) {
|
|
190
|
+
const a = ring[i], b = ring[j];
|
|
191
|
+
if (orientation(a, b, p) === 0 && within(a, b, p))
|
|
192
|
+
return true;
|
|
193
|
+
if (a[1] > p[1] !== b[1] > p[1] &&
|
|
194
|
+
p[0] < ((b[0] - a[0]) * (p[1] - a[1])) / (b[1] - a[1]) + a[0])
|
|
195
|
+
inside = !inside;
|
|
196
|
+
}
|
|
197
|
+
return inside;
|
|
198
|
+
}
|
|
199
|
+
function ringsIntersectReference(a, b) {
|
|
200
|
+
for (let i = 0; i < a.length - 1; i++)
|
|
201
|
+
for (let j = 0; j < b.length - 1; j++)
|
|
202
|
+
if (segmentsIntersect(a[i], a[i + 1], b[j], b[j + 1]))
|
|
203
|
+
return true;
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
/** Touching counts as intersecting. Boxes are cached per validPolygon call. */
|
|
207
|
+
function ringsIntersect(a, b, cache) {
|
|
208
|
+
if (a.length - 1 < INDEX_MIN_SEGMENTS && b.length - 1 < INDEX_MIN_SEGMENTS)
|
|
209
|
+
return ringsIntersectReference(a, b);
|
|
210
|
+
const boxesOf = (r) => {
|
|
211
|
+
if (!cache.has(r))
|
|
212
|
+
cache.set(r, segmentBoxes(r));
|
|
213
|
+
return cache.get(r);
|
|
214
|
+
};
|
|
215
|
+
const A = boxesOf(a), B = boxesOf(b);
|
|
216
|
+
if (!A || !B)
|
|
217
|
+
return ringsIntersectReference(a, b);
|
|
218
|
+
return sweepBetween(A, B, (i, j) => segmentsIntersect(a[i], a[i + 1], b[j], b[j + 1]));
|
|
219
|
+
}
|
|
220
|
+
export function validPolygon(rings) {
|
|
221
|
+
if (!rings.length || rings.some((r) => !simpleClosedRing(r)))
|
|
222
|
+
return false;
|
|
223
|
+
const cache = new Map();
|
|
224
|
+
for (let i = 1; i < rings.length; i++) {
|
|
225
|
+
if (!pointInRing(rings[i][0], rings[0]) ||
|
|
226
|
+
ringsIntersect(rings[0], rings[i], cache))
|
|
227
|
+
return false;
|
|
228
|
+
for (let j = 1; j < i; j++) {
|
|
229
|
+
if (ringsIntersect(rings[j], rings[i], cache) ||
|
|
230
|
+
pointInRing(rings[i][0], rings[j]) ||
|
|
231
|
+
pointInRing(rings[j][0], rings[i]))
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export { buildQuadstream, QuadstreamDataset } from "./dataset.js";
|
|
2
|
+
export { buildTopology } from "./topology.js";
|
|
3
|
+
export { buildSparseQuadtree, cellKey, cellAddress } from "./quadtree.js";
|
|
4
|
+
export { buildBlgTree, selectBlgPositions, stableBlgRandom, assertBlgInvariants, } from "./blg.js";
|
|
5
|
+
export { createSpatialIndex } from "./spatial-index.js";
|
|
6
|
+
export { exportPartitions, encodePartition, decodePartition, checksum, requiredPartitionKeys, partitionsWithVerticesInBounds, } from "./partition.js";
|
|
7
|
+
export { QuadstreamSession } from "./session.js";
|
|
8
|
+
export { QuadstreamLoader, fetchFromBundle } from "./loader.js";
|
|
9
|
+
export type { PartitionFetcher, LoaderOptions } from "./loader.js";
|
|
10
|
+
export { validPolygon, simpleClosedRing, simpleClosedRingReference, segmentsIntersect, pointInRing, intersects, boundsOf, } from "./geometry.js";
|
|
11
|
+
export type * from "./types.js";
|
|
12
|
+
export { importGeoJSON, exportGeoJSON, importTopoJSON, exportTopoJSON, } from "./adapters.js";
|
|
13
|
+
export type { TopoJSONTopology } from "./adapters.js";
|
|
14
|
+
export { validateManifest, MANIFEST_VERSION } from "./manifest-schema.js";
|
|
15
|
+
export { ProgressiveMap } from "./progressive-map.js";
|
|
16
|
+
export type { MapQuery, MapFrame, ProgressiveMapClient, } from "./progressive-map.js";
|
|
17
|
+
export { attachMapLibre, mapLibreBounds } from "./maplibre-adapter.js";
|
|
18
|
+
export type { MapLibreLike, MapLibreAdapterOptions, } from "./maplibre-adapter.js";
|
|
19
|
+
export { canonicalLonLat, lonLatVector, angularDistance, validGreatCirclePath, sphericalCellAddress, buildSphericalPriorities, sphericalChart, projectSpherical, sphericalPolygonSignature, validSphericalPolygon, } from "./spherical-geometry.js";
|
|
20
|
+
export type { UnitVector, CubeFace, SphericalCell, SphericalPriorities, SphericalChart, SphericalPolygonSignature, } from "./spherical-geometry.js";
|
|
21
|
+
export { SphericalQuadstreamDataset, buildSphericalQuadstream, importSphericalTopoJSON, } from "./spherical-dataset.js";
|
|
22
|
+
export type { SphericalQueryOptions } from "./spherical-dataset.js";
|
|
23
|
+
export { computeFeatureCap, computeFeatureCaps, isCapVisible, filterSphericalFeatures, } from "./spherical-cull.js";
|
|
24
|
+
export type { SphericalCap, SphericalViewCone } from "./spherical-cull.js";
|
|
25
|
+
export { clipWindow, clipToWindow } from "./spherical-clip.js";
|
|
26
|
+
export type { SphericalClipWindow, SphericalClipStats, SphericalClipResult, } from "./spherical-clip.js";
|
|
27
|
+
export { repairRing, repairGeometry } from "./spherical-repair.js";
|
|
28
|
+
export type { SphericalRepairOptions } from "./spherical-repair.js";
|
|
29
|
+
export { createLatestGeometry, improvesLod } from "./lod-scheduler.js";
|
|
30
|
+
export type { LatestGeometryOptions } from "./lod-scheduler.js";
|
|
31
|
+
export { sphericalConnectedComponents, buildRobustSphericalGroups, } from "./spherical-robust-dataset.js";
|
|
32
|
+
export type { SphericalBuildGroup, RobustSphericalOptions, } from "./spherical-robust-dataset.js";
|
|
33
|
+
export { packGeometry, unpackGeometry } from "./geometry-wire.js";
|
|
34
|
+
export type { PackedGeometry } from "./geometry-wire.js";
|
|
35
|
+
export { createSphericalViewClipper } from "./spherical-view-clipper.js";
|
|
36
|
+
export type { SphericalViewClipperOptions, CachedSphericalClipResult, } from "./spherical-view-clipper.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { buildQuadstream, QuadstreamDataset } from "./dataset.js";
|
|
2
|
+
export { buildTopology } from "./topology.js";
|
|
3
|
+
export { buildSparseQuadtree, cellKey, cellAddress } from "./quadtree.js";
|
|
4
|
+
export { buildBlgTree, selectBlgPositions, stableBlgRandom, assertBlgInvariants, } from "./blg.js";
|
|
5
|
+
export { createSpatialIndex } from "./spatial-index.js";
|
|
6
|
+
export { exportPartitions, encodePartition, decodePartition, checksum, requiredPartitionKeys, partitionsWithVerticesInBounds, } from "./partition.js";
|
|
7
|
+
export { QuadstreamSession } from "./session.js";
|
|
8
|
+
export { QuadstreamLoader, fetchFromBundle } from "./loader.js";
|
|
9
|
+
export { validPolygon, simpleClosedRing, simpleClosedRingReference, segmentsIntersect, pointInRing, intersects, boundsOf, } from "./geometry.js";
|
|
10
|
+
export { importGeoJSON, exportGeoJSON, importTopoJSON, exportTopoJSON, } from "./adapters.js";
|
|
11
|
+
export { validateManifest, MANIFEST_VERSION } from "./manifest-schema.js";
|
|
12
|
+
export { ProgressiveMap } from "./progressive-map.js";
|
|
13
|
+
export { attachMapLibre, mapLibreBounds } from "./maplibre-adapter.js";
|
|
14
|
+
export { canonicalLonLat, lonLatVector, angularDistance, validGreatCirclePath, sphericalCellAddress, buildSphericalPriorities, sphericalChart, projectSpherical, sphericalPolygonSignature, validSphericalPolygon, } from "./spherical-geometry.js";
|
|
15
|
+
export { SphericalQuadstreamDataset, buildSphericalQuadstream, importSphericalTopoJSON, } from "./spherical-dataset.js";
|
|
16
|
+
export { computeFeatureCap, computeFeatureCaps, isCapVisible, filterSphericalFeatures, } from "./spherical-cull.js";
|
|
17
|
+
export { clipWindow, clipToWindow } from "./spherical-clip.js";
|
|
18
|
+
export { repairRing, repairGeometry } from "./spherical-repair.js";
|
|
19
|
+
export { createLatestGeometry, improvesLod } from "./lod-scheduler.js";
|
|
20
|
+
export { sphericalConnectedComponents, buildRobustSphericalGroups, } from "./spherical-robust-dataset.js";
|
|
21
|
+
export { packGeometry, unpackGeometry } from "./geometry-wire.js";
|
|
22
|
+
export { createSphericalViewClipper } from "./spherical-view-clipper.js";
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { QuadstreamSession } from "./session.js";
|
|
2
|
+
import type { QueryOptions, PartitionBundle } from "./types.js";
|
|
3
|
+
export type PartitionFetcher = (key: string, context: {
|
|
4
|
+
signal: AbortSignal;
|
|
5
|
+
}) => Promise<Uint8Array>;
|
|
6
|
+
export interface LoaderOptions {
|
|
7
|
+
/** Maximum concurrent fetches *per request*; default 4. */
|
|
8
|
+
concurrency?: number;
|
|
9
|
+
/** Limit for the optional raw-byte LRU. Parsed vertices remain in the session. */
|
|
10
|
+
cacheBytes?: number;
|
|
11
|
+
}
|
|
12
|
+
/** Optional, transport-independent loader around a validated progressive session. */
|
|
13
|
+
export declare class QuadstreamLoader {
|
|
14
|
+
readonly session: QuadstreamSession;
|
|
15
|
+
private readonly fetchPartition;
|
|
16
|
+
private readonly concurrency;
|
|
17
|
+
private readonly cacheLimit;
|
|
18
|
+
private readonly cache;
|
|
19
|
+
private cachedBytes;
|
|
20
|
+
private latestRequest?;
|
|
21
|
+
constructor(session: QuadstreamSession, fetchPartition: PartitionFetcher, options?: LoaderOptions);
|
|
22
|
+
get cachedPartitionKeys(): string[];
|
|
23
|
+
get cacheSizeBytes(): number;
|
|
24
|
+
private remember;
|
|
25
|
+
/** Lazily fetch partitions required for this level. Already ingested keys are skipped. */
|
|
26
|
+
ensureLevel(level: number, options?: {
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
/** Supersede the preceding latest-view request without clearing valid stored geometry. */
|
|
30
|
+
loadLatest(query?: QueryOptions): Promise<ReturnType<QuadstreamSession["query"]>>;
|
|
31
|
+
cancelLatest(): void;
|
|
32
|
+
}
|
|
33
|
+
/** In-memory adapter for examples and deterministic end-to-end tests. */
|
|
34
|
+
export declare function fetchFromBundle(bundle: PartitionBundle): PartitionFetcher;
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
function abortError() {
|
|
2
|
+
const e = new Error("Quadstream load aborted");
|
|
3
|
+
e.name = "AbortError";
|
|
4
|
+
return e;
|
|
5
|
+
}
|
|
6
|
+
function abortable(promise, signal) {
|
|
7
|
+
if (signal.aborted)
|
|
8
|
+
return Promise.reject(abortError());
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const onAbort = () => {
|
|
11
|
+
signal.removeEventListener("abort", onAbort);
|
|
12
|
+
reject(abortError());
|
|
13
|
+
};
|
|
14
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
15
|
+
promise.then((value) => {
|
|
16
|
+
signal.removeEventListener("abort", onAbort);
|
|
17
|
+
resolve(value);
|
|
18
|
+
}, (error) => {
|
|
19
|
+
signal.removeEventListener("abort", onAbort);
|
|
20
|
+
reject(error);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Optional, transport-independent loader around a validated progressive session. */
|
|
25
|
+
export class QuadstreamLoader {
|
|
26
|
+
session;
|
|
27
|
+
fetchPartition;
|
|
28
|
+
concurrency;
|
|
29
|
+
cacheLimit;
|
|
30
|
+
cache = new Map();
|
|
31
|
+
cachedBytes = 0;
|
|
32
|
+
latestRequest;
|
|
33
|
+
constructor(session, fetchPartition, options = {}) {
|
|
34
|
+
this.session = session;
|
|
35
|
+
this.fetchPartition = fetchPartition;
|
|
36
|
+
const { concurrency = 4, cacheBytes = 16 * 1024 * 1024 } = options;
|
|
37
|
+
if (!Number.isInteger(concurrency) || concurrency < 1 || concurrency > 32)
|
|
38
|
+
throw new Error("concurrency must be an integer between 1 and 32");
|
|
39
|
+
if (!Number.isSafeInteger(cacheBytes) || cacheBytes < 0)
|
|
40
|
+
throw new Error("cacheBytes must be a nonnegative safe integer");
|
|
41
|
+
this.concurrency = concurrency;
|
|
42
|
+
this.cacheLimit = cacheBytes;
|
|
43
|
+
}
|
|
44
|
+
get cachedPartitionKeys() {
|
|
45
|
+
return [...this.cache.keys()];
|
|
46
|
+
}
|
|
47
|
+
get cacheSizeBytes() {
|
|
48
|
+
return this.cachedBytes;
|
|
49
|
+
}
|
|
50
|
+
remember(key, bytes) {
|
|
51
|
+
if (bytes.byteLength > this.cacheLimit)
|
|
52
|
+
return;
|
|
53
|
+
const old = this.cache.get(key);
|
|
54
|
+
if (old) {
|
|
55
|
+
this.cachedBytes -= old.byteLength;
|
|
56
|
+
this.cache.delete(key);
|
|
57
|
+
}
|
|
58
|
+
this.cache.set(key, bytes.slice());
|
|
59
|
+
this.cachedBytes += bytes.byteLength;
|
|
60
|
+
while (this.cachedBytes > this.cacheLimit) {
|
|
61
|
+
const oldest = this.cache.keys().next().value;
|
|
62
|
+
this.cachedBytes -= this.cache.get(oldest).byteLength;
|
|
63
|
+
this.cache.delete(oldest);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Lazily fetch partitions required for this level. Already ingested keys are skipped. */
|
|
67
|
+
async ensureLevel(level, options = {}) {
|
|
68
|
+
const keys = this.session.missingKeys(level);
|
|
69
|
+
if (!keys.length)
|
|
70
|
+
return;
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
const onAbort = () => controller.abort();
|
|
73
|
+
if (options.signal?.aborted)
|
|
74
|
+
throw abortError();
|
|
75
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
76
|
+
let next = 0;
|
|
77
|
+
try {
|
|
78
|
+
const worker = async () => {
|
|
79
|
+
while (next < keys.length) {
|
|
80
|
+
if (controller.signal.aborted)
|
|
81
|
+
throw abortError();
|
|
82
|
+
const key = keys[next++];
|
|
83
|
+
try {
|
|
84
|
+
let bytes = this.cache.get(key);
|
|
85
|
+
if (bytes) {
|
|
86
|
+
this.cache.delete(key);
|
|
87
|
+
this.cache.set(key, bytes); // refresh LRU
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
bytes = await abortable(this.fetchPartition(key, { signal: controller.signal }), controller.signal);
|
|
91
|
+
}
|
|
92
|
+
if (controller.signal.aborted)
|
|
93
|
+
throw abortError();
|
|
94
|
+
// load() validates checksum, length, record IDs, levels and duplicate records.
|
|
95
|
+
this.session.load(key, bytes);
|
|
96
|
+
this.remember(key, bytes);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
controller.abort();
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
await Promise.all(Array.from({ length: Math.min(this.concurrency, keys.length) }, () => worker()));
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** Supersede the preceding latest-view request without clearing valid stored geometry. */
|
|
111
|
+
async loadLatest(query = {}) {
|
|
112
|
+
this.latestRequest?.abort();
|
|
113
|
+
const controller = new AbortController();
|
|
114
|
+
this.latestRequest = controller;
|
|
115
|
+
try {
|
|
116
|
+
await this.ensureLevel(query.level ?? this.session.manifest.maxLevel - 1, { signal: controller.signal });
|
|
117
|
+
if (controller.signal.aborted)
|
|
118
|
+
throw abortError();
|
|
119
|
+
return this.session.query(query);
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
if (this.latestRequest === controller)
|
|
123
|
+
this.latestRequest = undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
cancelLatest() {
|
|
127
|
+
this.latestRequest?.abort();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** In-memory adapter for examples and deterministic end-to-end tests. */
|
|
131
|
+
export function fetchFromBundle(bundle) {
|
|
132
|
+
return async (key) => {
|
|
133
|
+
const bytes = bundle.partitions.get(key);
|
|
134
|
+
if (!bytes)
|
|
135
|
+
throw new Error(`Missing partition in bundle: ${key}`);
|
|
136
|
+
return bytes.slice();
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface LatestGeometryOptions<T> {
|
|
2
|
+
request: (level: number) => Promise<T>;
|
|
3
|
+
onFrame: (frame: T) => void;
|
|
4
|
+
onError?: (error: unknown) => void;
|
|
5
|
+
cacheCapacity?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function improvesLod(level: number, rendered: number | null, target: number | null): boolean;
|
|
8
|
+
export declare function createLatestGeometry<T extends {
|
|
9
|
+
seq?: number;
|
|
10
|
+
}>({ request, onFrame, onError, cacheCapacity, }: LatestGeometryOptions<T>): {
|
|
11
|
+
select(level: number): Promise<void>;
|
|
12
|
+
invalidate: () => void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
readonly target: number | null;
|
|
15
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Compare against the current target, not the target at dispatch time.
|
|
2
|
+
// A first frame is useful; subsequent frames must strictly reduce LOD error.
|
|
3
|
+
export function improvesLod(level, rendered, target) {
|
|
4
|
+
return (target != null &&
|
|
5
|
+
(rendered == null || Math.abs(level - target) < Math.abs(rendered - target)));
|
|
6
|
+
}
|
|
7
|
+
// Completion-driven throttle: one active query, one replaceable desired level.
|
|
8
|
+
// Completed obsolete frames remain cached but cannot regress visible detail.
|
|
9
|
+
export function createLatestGeometry({ request, onFrame, onError = () => { }, cacheCapacity = 4, }) {
|
|
10
|
+
if (!Number.isInteger(cacheCapacity) || cacheCapacity < 0)
|
|
11
|
+
throw new Error("Invalid cache capacity");
|
|
12
|
+
let generation = 0, lastSeq = 0, desired = null, rendered = null, running = false, disposed = false;
|
|
13
|
+
const cache = new Map();
|
|
14
|
+
async function drain() {
|
|
15
|
+
if (running || disposed)
|
|
16
|
+
return;
|
|
17
|
+
running = true;
|
|
18
|
+
try {
|
|
19
|
+
while (!disposed && desired !== null && desired !== rendered) {
|
|
20
|
+
const level = desired, id = generation;
|
|
21
|
+
try {
|
|
22
|
+
let frame = cache.get(level);
|
|
23
|
+
if (!frame) {
|
|
24
|
+
frame = await request(level);
|
|
25
|
+
if (disposed || id !== generation)
|
|
26
|
+
continue;
|
|
27
|
+
const seq = frame?.seq;
|
|
28
|
+
if (seq != null) {
|
|
29
|
+
if (seq < lastSeq)
|
|
30
|
+
throw new Error("Out-of-order geometry frame");
|
|
31
|
+
lastSeq = seq;
|
|
32
|
+
}
|
|
33
|
+
cache.set(level, frame);
|
|
34
|
+
if (cache.size > cacheCapacity)
|
|
35
|
+
cache.delete(cache.keys().next().value);
|
|
36
|
+
}
|
|
37
|
+
if (improvesLod(level, rendered, desired)) {
|
|
38
|
+
rendered = level;
|
|
39
|
+
onFrame(frame);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
if (disposed || id !== generation)
|
|
44
|
+
continue;
|
|
45
|
+
// An obsolete failure must not block the newest target. A failure
|
|
46
|
+
// of the current target may be retried by a subsequent selection.
|
|
47
|
+
if (desired === level) {
|
|
48
|
+
desired = null;
|
|
49
|
+
onError(e);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
running = false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function invalidate() {
|
|
59
|
+
generation++;
|
|
60
|
+
lastSeq = 0;
|
|
61
|
+
desired = null;
|
|
62
|
+
rendered = null;
|
|
63
|
+
cache.clear();
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
async select(level) {
|
|
67
|
+
if (!Number.isFinite(level))
|
|
68
|
+
throw new Error("Invalid LOD");
|
|
69
|
+
if (disposed)
|
|
70
|
+
return;
|
|
71
|
+
desired = level;
|
|
72
|
+
// While busy this records intent only; the existing drain owns the
|
|
73
|
+
// query and will consume the latest target when that query completes.
|
|
74
|
+
await drain();
|
|
75
|
+
},
|
|
76
|
+
invalidate,
|
|
77
|
+
dispose() {
|
|
78
|
+
disposed = true;
|
|
79
|
+
invalidate();
|
|
80
|
+
},
|
|
81
|
+
get target() {
|
|
82
|
+
return desired;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|