Spaces:
Running
Running
Parquet decoding in Web Workers, phone layout, city markers, exact footer reads
Browse files- app.js +37 -3
- geoparquet.js +11 -4
- hf.js +5 -0
- index.html +4 -2
- pqworker.js +37 -0
app.js
CHANGED
|
@@ -5,9 +5,8 @@
|
|
| 5 |
// * vectors: GeoParquet 1.1 files, read row group by row group using the bbox statistics in their footers.
|
| 6 |
// There is no tile server or database: the browser computes which byte ranges it needs and asks for them.
|
| 7 |
|
| 8 |
-
import { stats, fetchJSON, RangeSource } from './hf.js';
|
| 9 |
import { COG } from './cog.js';
|
| 10 |
-
import { GeoParquet } from './geoparquet.js';
|
| 11 |
|
| 12 |
const HALF = 20037508.342789244;
|
| 13 |
const params = new URLSearchParams(location.search);
|
|
@@ -204,6 +203,41 @@ class Imagery {
|
|
| 204 |
}
|
| 205 |
|
| 206 |
// ------------------------------------------------------------------------------------------------ vectors (GeoParquet)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
class VectorSource {
|
| 208 |
constructor(def, info) {
|
| 209 |
this.def = def;
|
|
@@ -217,7 +251,7 @@ class VectorSource {
|
|
| 217 |
}
|
| 218 |
|
| 219 |
async ensureOpen() {
|
| 220 |
-
this.opening ??= new
|
| 221 |
this.pq = pq;
|
| 222 |
scheduleUpdate(true);
|
| 223 |
}).catch((e) => { this.status = `error: ${e.message}`; console.warn(e); });
|
|
|
|
| 5 |
// * vectors: GeoParquet 1.1 files, read row group by row group using the bbox statistics in their footers.
|
| 6 |
// There is no tile server or database: the browser computes which byte ranges it needs and asks for them.
|
| 7 |
|
| 8 |
+
import { stats, fetchJSON, RangeSource, ingest } from './hf.js';
|
| 9 |
import { COG } from './cog.js';
|
|
|
|
| 10 |
|
| 11 |
const HALF = 20037508.342789244;
|
| 12 |
const params = new URLSearchParams(location.search);
|
|
|
|
| 203 |
}
|
| 204 |
|
| 205 |
// ------------------------------------------------------------------------------------------------ vectors (GeoParquet)
|
| 206 |
+
// Parquet reading and decoding run in a small pool of module workers; the page only builds deck.gl layers.
|
| 207 |
+
const workers = Array.from({ length: Math.min(4, Math.max(2, (navigator.hardwareConcurrency || 4) - 2)) }, () => {
|
| 208 |
+
const w = new Worker(new URL('./pqworker.js', import.meta.url), { type: 'module' });
|
| 209 |
+
w.onmessage = ({ data: m }) => {
|
| 210 |
+
if (m.type === 'stat') { if (m.entry) ingest(m.entry); return; }
|
| 211 |
+
const p = calls.get(m.id);
|
| 212 |
+
calls.delete(m.id);
|
| 213 |
+
if (m.error) p.reject(new Error(m.error)); else p.resolve(m.result);
|
| 214 |
+
};
|
| 215 |
+
return w;
|
| 216 |
+
});
|
| 217 |
+
const calls = new Map();
|
| 218 |
+
let callId = 0;
|
| 219 |
+
function call(w, msg) {
|
| 220 |
+
const id = ++callId;
|
| 221 |
+
return new Promise((resolve, reject) => { calls.set(id, { resolve, reject }); w.postMessage({ ...msg, id }); });
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
/** Same interface as GeoParquet, backed by the worker pool. Each file's footer is read once per worker. */
|
| 225 |
+
class RemoteGeoParquet {
|
| 226 |
+
constructor(path, size, footer) {
|
| 227 |
+
this.msg = { url: url(path), size, footer };
|
| 228 |
+
this.seen = new Set([0]);
|
| 229 |
+
}
|
| 230 |
+
async open() {
|
| 231 |
+
Object.assign(this, await call(workers[0], { ...this.msg, op: 'open' }));
|
| 232 |
+
return this;
|
| 233 |
+
}
|
| 234 |
+
read(i, cols) {
|
| 235 |
+
const k = i % workers.length;
|
| 236 |
+
const first = !this.seen.has(k);
|
| 237 |
+
this.seen.add(k);
|
| 238 |
+
return call(workers[k], { ...this.msg, op: 'read', i, cols, tail: first ? this.tail : undefined });
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
class VectorSource {
|
| 242 |
constructor(def, info) {
|
| 243 |
this.def = def;
|
|
|
|
| 251 |
}
|
| 252 |
|
| 253 |
async ensureOpen() {
|
| 254 |
+
this.opening ??= new RemoteGeoParquet(this.info.path, this.info.size, this.info.footer).open().then((pq) => {
|
| 255 |
this.pq = pq;
|
| 256 |
scheduleUpdate(true);
|
| 257 |
}).catch((e) => { this.status = `error: ${e.message}`; console.warn(e); });
|
geoparquet.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
| 6 |
// read(i): fetches and decodes only that row group's column chunks, and turns the WKB geometry into flat Float64
|
| 7 |
// arrays ready for deck.gl (no GeoJSON objects in between).
|
| 8 |
|
| 9 |
-
import { parquetMetadataAsync, parquetReadObjects } from 'https://cdn.jsdelivr.net/npm/hyparquet@1.33.0/+esm';
|
| 10 |
import { compressors } from 'https://cdn.jsdelivr.net/npm/hyparquet-compressors@1.1.2/+esm';
|
| 11 |
import { fetchRange } from './hf.js';
|
| 12 |
|
|
@@ -22,9 +22,16 @@ export class GeoParquet {
|
|
| 22 |
};
|
| 23 |
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
this.metadata = md;
|
| 29 |
const geo = JSON.parse(md.key_value_metadata.find((kv) => kv.key === 'geo').value);
|
| 30 |
this.geomCol = geo.primary_column;
|
|
|
|
| 6 |
// read(i): fetches and decodes only that row group's column chunks, and turns the WKB geometry into flat Float64
|
| 7 |
// arrays ready for deck.gl (no GeoJSON objects in between).
|
| 8 |
|
| 9 |
+
import { parquetMetadata, parquetMetadataAsync, parquetReadObjects } from 'https://cdn.jsdelivr.net/npm/hyparquet@1.33.0/+esm';
|
| 10 |
import { compressors } from 'https://cdn.jsdelivr.net/npm/hyparquet-compressors@1.1.2/+esm';
|
| 11 |
import { fetchRange } from './hf.js';
|
| 12 |
|
|
|
|
| 22 |
};
|
| 23 |
}
|
| 24 |
|
| 25 |
+
/** Parse the footer. `tail` (the last `footer` bytes of the file) can be passed in to avoid fetching it again. */
|
| 26 |
+
async open(tail) {
|
| 27 |
+
let md;
|
| 28 |
+
if (!tail && this.footer) tail = await fetchRange(this.url, this.size - this.footer, this.size - 1, this.format);
|
| 29 |
+
if (tail) {
|
| 30 |
+
this.tail = tail;
|
| 31 |
+
md = parquetMetadata(tail, { geoparquet: false });
|
| 32 |
+
} else {
|
| 33 |
+
md = await parquetMetadataAsync(this.file, { geoparquet: false });
|
| 34 |
+
}
|
| 35 |
this.metadata = md;
|
| 36 |
const geo = JSON.parse(md.key_value_metadata.find((kv) => kv.key === 'geo').value);
|
| 37 |
this.geomCol = geo.primary_column;
|
hf.js
CHANGED
|
@@ -34,6 +34,11 @@ function record(format, file, start, end, bytes, ms) {
|
|
| 34 |
notify();
|
| 35 |
}
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
async function slot(signal) {
|
| 38 |
while (inflight >= MAX_INFLIGHT) {
|
| 39 |
await new Promise((res) => waiting.push(res));
|
|
|
|
| 34 |
notify();
|
| 35 |
}
|
| 36 |
|
| 37 |
+
/** Record a request made in a Web Worker (the workers post their stats entries to the page). */
|
| 38 |
+
export function ingest(e) {
|
| 39 |
+
record(e.format, e.file, e.start, e.end, e.bytes, e.ms);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
async function slot(signal) {
|
| 43 |
while (inflight >= MAX_INFLIGHT) {
|
| 44 |
await new Promise((res) => waiting.push(res));
|
index.html
CHANGED
|
@@ -80,8 +80,9 @@ html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); f
|
|
| 80 |
#collapse { display: block; }
|
| 81 |
body.collapsed #side > :not(h1) { display: none; }
|
| 82 |
#hud { left: 12px; right: 12px; width: auto; bottom: 26px; padding: 8px 12px; }
|
| 83 |
-
#hud #log, #hud .hl { display: none; }
|
| 84 |
-
#
|
|
|
|
| 85 |
.maplibregl-ctrl-top-right { top: auto; bottom: 150px; }
|
| 86 |
}
|
| 87 |
</style>
|
|
@@ -110,6 +111,7 @@ html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); f
|
|
| 110 |
<script src="https://cdn.jsdelivr.net/npm/pmtiles@4.5.0/dist/pmtiles.js"></script>
|
| 111 |
<script src="https://cdn.jsdelivr.net/npm/@protomaps/basemaps@5.7.2/dist/basemaps.js"></script>
|
| 112 |
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.4.1/dist.min.js"></script>
|
|
|
|
| 113 |
<script type="module" src="./app.js"></script>
|
| 114 |
</body>
|
| 115 |
</html>
|
|
|
|
| 80 |
#collapse { display: block; }
|
| 81 |
body.collapsed #side > :not(h1) { display: none; }
|
| 82 |
#hud { left: 12px; right: 12px; width: auto; bottom: 26px; padding: 8px 12px; }
|
| 83 |
+
#hud #log, #hud .hl, #hud #byfmt { display: none; }
|
| 84 |
+
#hud .big b { font-size: 18px; }
|
| 85 |
+
#side h1 .tag { display: none; }
|
| 86 |
.maplibregl-ctrl-top-right { top: auto; bottom: 150px; }
|
| 87 |
}
|
| 88 |
</style>
|
|
|
|
| 111 |
<script src="https://cdn.jsdelivr.net/npm/pmtiles@4.5.0/dist/pmtiles.js"></script>
|
| 112 |
<script src="https://cdn.jsdelivr.net/npm/@protomaps/basemaps@5.7.2/dist/basemaps.js"></script>
|
| 113 |
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.4.1/dist.min.js"></script>
|
| 114 |
+
<script>if (matchMedia('(max-width: 760px)').matches) document.body.classList.add('collapsed');</script>
|
| 115 |
<script type="module" src="./app.js"></script>
|
| 116 |
</body>
|
| 117 |
</html>
|
pqworker.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Web Worker: reads and decodes GeoParquet row groups off the main thread (range requests, zstd, WKB), and sends
|
| 2 |
+
// back flat Float64Arrays (transferred, not copied) plus attribute rows. Its byte counts are posted to the page.
|
| 3 |
+
import { GeoParquet } from './geoparquet.js';
|
| 4 |
+
import { stats } from './hf.js';
|
| 5 |
+
|
| 6 |
+
const files = new Map();
|
| 7 |
+
stats.listeners.add((s) => postMessage({ type: 'stat', entry: s.log[0] }));
|
| 8 |
+
|
| 9 |
+
function transferables(batch) {
|
| 10 |
+
const out = new Set();
|
| 11 |
+
for (const p of batch.polys ?? []) {
|
| 12 |
+
out.add((p.polygon.positions ?? p.polygon).buffer);
|
| 13 |
+
if (p.outer) out.add(p.outer.buffer);
|
| 14 |
+
}
|
| 15 |
+
for (const p of batch.paths ?? []) out.add(p.path.buffer);
|
| 16 |
+
if (batch.points) { out.add(batch.points.buffer); out.add(batch.pointFeature.buffer); }
|
| 17 |
+
return [...out];
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
onmessage = async ({ data: m }) => {
|
| 21 |
+
try {
|
| 22 |
+
let f = files.get(m.url);
|
| 23 |
+
if (!f) {
|
| 24 |
+
f = new GeoParquet(m.url, m.size, m.footer).open(m.tail); // tail: footer bytes another worker already read
|
| 25 |
+
files.set(m.url, f);
|
| 26 |
+
}
|
| 27 |
+
if (m.op === 'open') {
|
| 28 |
+
const pq = await f;
|
| 29 |
+
postMessage({ type: 'reply', id: m.id, result: { rowGroups: pq.rowGroups, rows: pq.rows, tail: pq.tail.slice(0) } });
|
| 30 |
+
} else if (m.op === 'read') {
|
| 31 |
+
const batch = await (await f).read(m.i, m.cols);
|
| 32 |
+
postMessage({ type: 'reply', id: m.id, result: batch }, transferables(batch));
|
| 33 |
+
}
|
| 34 |
+
} catch (e) {
|
| 35 |
+
postMessage({ type: 'reply', id: m.id, error: String(e?.message ?? e) });
|
| 36 |
+
}
|
| 37 |
+
};
|