Protocol Buffers-based raster elevation tiles.
3-tier zoom LOD ยท GEBCO + ALOS (+ GSI DEM10B in Japan) ยท Int16 delta + deflateRaw ยท Worker zero-copy
altpbf is a binary raster elevation tile format built on Protocol Buffers. Where GeoPBF handles vector geometry (coastlines, roads, polygons), altpbf handles raster elevation grids โ the digital elevation model (DEM) and bathymetry data needed for terrain rendering, shadow calculation, and 3D visualization.
Each tile is a rectangular grid of Int16 elevation values (meters),
delta-encoded and compressed with deflateRaw.
Three tile sizes cover the globe at different resolutions,
and the ortho-earth engine selects the appropriate tier automatically based on zoom level.
The altpbf npm package (1.2.0) is the format only:
encode, decode, encodeName / decodeName and altpbf2png.
It is written in TypeScript (strict) and ships type definitions
(AltpbfTile, AltpbfDecoded, AltpbfSource, Altpbf2PngOptions, โฆ).
Its only runtime dependency is geopbf; it needs CompressionStream,
and altpbf2png also needs OffscreenCanvas.
Fetching, caching, worker decoding and height lookup (createGetHeight, createTileLoader)
live in the engine, @ortho-earth/core/elevation.
Earth's elevation range is roughly โ11,000 m (Mariana Trench) to +8,849 m (Everest). A signed 16-bit integer covers โ32,768 to +32,767 m โ exactly enough, with no wasted bits. Compared to Float32, Int16 halves the data size before any compression is applied.
| Source | Coverage | Resolution | Used for |
|---|---|---|---|
| GEBCO General Bathymetric Chart of the Oceans |
Global (land + sea) | ~450 m (15 arc-sec) | HGT90 ยท HGT10 |
| ALOS AW3D30 JAXA ALOS World 3D |
Land only | ~30 m (1 arc-sec) | HGT01 |
| GSI DEM10B Geospatial Information Authority of Japan |
Japan only (bare earth) | ~10 m, baked to a 1โณ grid | HGT01 (Japan) ยท HGT10 (Japan, merged with GEBCO) |
GEBCO provides global coverage including ocean bathymetry, making it the baseline for all zoom levels. ALOS AW3D30 is a high-precision land-only DEM published by JAXA, used for close-up terrain rendering where its 30 m resolution delivers visually meaningful detail. Since ALOS tiles only exist for land areas, the engine keeps an index of available tiles and falls back gracefully to GEBCO HGT10 for ocean queries. In Japan, HGT01 tiles are re-baked from GSI DEM10B (bare-earth DTM, where AW3D30 is a surface model) and stored in the bucket; the engine reads the bucket first and only falls back to JAXA when no baked tile exists.
altpbf defines three tile sizes, each covering a different angular extent of the globe.
createGetHeight (@ortho-earth/core/elevation) switches between tiers based on the current zoom level,
defaulting to level1 = 7 and level2 = 12.
Each tier is a strict fallback chain: HGT01 โ HGT10 โ HGT90. If a high-resolution tile is unavailable (ocean, no ALOS data) or returns no elevation, the next coarser tier answers the query transparently. The caller always receives a valid elevation value.
Within each tile, calcHeight(x, y, v) performs bilinear interpolation
across the four nearest grid cells โ no staircase artifacts.
Normalized coordinates (0โ1) within the tile are computed from the raw lat/lng
and passed directly to the interpolation function.
Each .altpbf tile is a deflateRaw-compressed Protocol Buffers message.
deflateRaw / inflateRawTerrain elevation changes gradually โ neighboring cells in a DEM rarely differ by more than a few tens of metres. Delta encoding exploits this locality: storing differences rather than absolute values keeps numbers small, and small numbers encode to fewer Varint bytes.
After delta encoding, most values in a smooth terrain tile fit in a single Varint byte. Combined with deflateRaw on top, altpbf tiles are typically 5โ15ร smaller than an equivalent raw Int16 array.
decode reads the packed SVarint body straight into the output Int16Array โ
no intermediate JS arrays โ using a small embedded WebAssembly kernel (SIMD128 where available, scalar wasm otherwise)
with a plain-JS fallback when wasm is unavailable (e.g. blocked by CSP).
A 5.7M-point HGT10 tile decodes in ~21 ms / +12 MB instead of ~99 ms / +230 MB with the previous implementation.
altpbf2png renders a tile as a PNG image using altitude/depth step colors:
Each tile is uniquely identified by a compact name string encoding its range, latitude, and longitude:
encodeName(lng, lat, range)
// range > 0 โ "R{02d(range)}{lat_prefix}{03d(|lat|)}{lng_prefix}{03d(|lng|)}"
// range omitted / 0 โ "{lat_prefix}{03d(|lat|)}{lng_prefix}{03d(|lng|)}" (key of the JAXA AW3D30 index)
// (lng, lat) is the tile's bottom-left (south-west) corner; decodeName(name) โ [lng, lat, range]
// Examples:
encodeName(0, -90, 90) // โ "R90S090E000" (HGT90 quadrant 0โ90ยฐE ยท 0โ90ยฐS)
encodeName(130, 30, 10) // โ "R10N030E130" (HGT10 Japan region)
encodeName(135, 35, 1) // โ "R01N035E135" (HGT01, Kinki region)
encodeName(135, 35) // โ "N035E135" (AW3D30 index key)
decodeName("R01N035E135") // โ [135, 35, 1]
ALOS AW3D30 tiles exist only over land. Before requesting an HGT01 tile,
the engine checks an in-memory index loaded from the JAXA tile list
(index_alos() in @ortho-earth/core/elevation, cached in IndexedDB).
Ocean queries immediately fall back to HGT10 without a network round-trip.
If JAXA is unreachable, the engine continues with an empty index and refetches it in the background.
// packages/ortho-core/src/elevation/worker.ts โ zero-copy Int16Array transfer to main thread
import { load, setApiUrl } from "./loader.ts";
onmessage = async e => {
const { name, apiUrl } = e.data;
if (apiUrl) setApiUrl(apiUrl);
try { const obj = await load(name);
obj ? postMessage(obj, [obj.data.buffer]) // โ Transferable: zero-copy
: postMessage(null);
} catch (e) { postMessage(null); }
};
The worker inflates and decodes the tile off-main-thread,
then transfers the Int16Array buffer back via the Transferable interface โ
no copying, no serialization.
The worker runs under the engine's "ortho:height" worker role.
createGetHeight uses a single worker that handles requests one at a time;
while a load is in flight, render-side queries return 0 until it arrives, and { wait: true } callers wait for it.
createTileLoader (used by the terrain atlas) runs a pool of up to 3 workers, merges concurrent requests for the same tile,
and decodes in-thread when nested workers are unavailable.
Once decoded, tiles are persisted to IndexedDB for instant re-use across sessions.
The raw GEBCO source is a set of GeoTIFFs at 21,600 ร 21,600 pixels per 90ยฐ quadrant
(eight quadrants, 86,400 ร 43,200 for the globe).
The uploader app's GEBCO() function (apps/uploader/src/terrain/gebco.ts, not part of the altpbf package)
downloads and tiles it into altpbf format:
// Step 1 โ HGT90: 8ร average-pool downsampling
// 21600 ร 21600 โ 2700 ร 2700 (one 90ยฐ tile per quadrant)
// Each output pixel = mean of 8ร8 source pixels
// Step 2 โ HGT10: extract 9ร9 = 81 sub-tiles per quadrant (2400 px tall)
// Apply polar width shrink so tiles remain square on-screen
// shrink(lat): fraction of full-width actually used at that latitude
Meridians converge toward the poles, so a 10ยฐ longitude span represents far fewer kilometres at high latitudes than at the equator. GEBCO HGT10 tile generation compensates by reducing the pixel width of polar tiles:
Tile pixel width relative to equatorial baseline. Polar tiles are narrower, matching the reduced real-world extent per degree of longitude.
// โโ altpbf (npm) โ the format โโโโโโโโโโโโโโโโโโโโโโโโโโโ
import { encode, decode, encodeName, decodeName, altpbf2png } from "altpbf";
import type { AltpbfTile, AltpbfDecoded, AltpbfSource, AltpbfColorMap, Altpbf2PngOptions } from "altpbf";
// โโ createGetHeight โ engine entry point (@ortho-earth/core/elevation) โโ
import { createGetHeight } from "@ortho-earth/core/elevation";
const getHeight = await createGetHeight({
apiUrl: 'https://your-api.example.com', // nativeBucket base URL
level1: 7, // zoom threshold HGT90 โ HGT10
level2: 12, // zoom threshold HGT10 โ HGT01
onstart: name => console.log(`loading ${name}...`),
onend: name => console.log(`done ${name}`),
});
// Query elevation at any (lng, lat, zoom)
const h = await getHeight(139.77, 35.68, 14); // โ height in metres
// Zoom omitted โ always use highest resolution (HGT01)
const h = await getHeight(139.77, 35.68);
// โโ altpbf2png โ render tile as PNG โโโโโโโโโโโโโโโโโโโโ
const blob = await altpbf2png(pbfBlob, {
size: 256, // output PNG size (square)
colorMap: h => [r, g, b] // custom color function (optional)
});
// โโ Low-level encode / decode โโโโโโโโโโโโโโโโโโโโโโโโโโโ
const buf = await encode({ name, source, lng, lat, range, width, height, data }); // โ Uint8Array
const obj = await decode(blob); // โ { name, source, lng, lat, range, width, height, data: Int16Array }
// โโ GEBCO bulk tile generation (apps/uploader) โโโโโโโโโ
await GEBCO({ year: 2026, noIce: false, log: console });
// Downloads ~12 GB source, generates HGT90 + HGT10 altpbf tiles, uploads to bucket
// โโ ALOS tile index (engine-internal, elevation/loader.ts) โโ
const index = await index_alos();
// โ { "N035E135": "v2404", "N036E135": "v2404", ... }