Expand description
Reproject web-mercator (XYZ) DEM tiles onto a geodetic (EPSG:4326) grid.
Elevation tiles are almost always served in the web-mercator XYZ
tiling (Terrarium, Mapbox Terrain-RGB, …), while Cesium quantized-mesh
terrain is served in the geodetic TMS scheme (EPSG:4326). Those are
different projections, not just different tilings — web-mercator’s
latitude axis is non-linear — so producing a geodetic terrain tile means
resampling (warping) the mercator DEM, not merely cropping and
stitching it.
MercatorDem holds a contiguous block of decoded web-mercator DEM
tiles stitched into one grid and lets you sample it by longitude /
latitude (bilinear). From there:
MercatorDem::geodetic_gridproduces the2^n+1elevation grid thatcrate::terrain::encode_terrainexpects, andMercatorDem::buffered_geodeticproduces the halo-extendedBufferedElevationsthatcrate::terrain::NormalMode::BufferedGradientexpects for seam-free normals.
Fetching the source tiles is left to the caller (HTTP, disk, cache, …) so
this module stays free of any IO/async assumptions — supply an already
decoded tile via MercatorDem::new / MercatorDem::from_tiles.
§Example
use terrain_codec::quantized_mesh::TileBounds;
use terrain_codec::mercator::MercatorDem;
use terrain_codec::terrain::{encode_terrain, TerrainOptions};
use terrain_codec::tile_coords::geodetic_tms;
// Target geodetic TMS tile we want to emit.
let (w, s, e, n) = geodetic_tms::tile_to_bounds(12, 7252, 2852);
let bounds = TileBounds::new(w, s, e, n);
let grid_size = 257; // 2^8 + 1
// Source web-mercator DEM: decide a source zoom, find the covering XYZ
// tiles, and assemble them (the closure does your fetch + heightmap decode).
let src_zoom = 13;
let tile_size = 512;
let (x0, y0, tx, ty) = MercatorDem::tiles_covering(src_zoom, w, s, e, n);
let dem = MercatorDem::from_tiles(src_zoom, x0, y0, tx, ty, tile_size, |z, x, y| {
// fetch z/x/y.png, decode to elevations (tile_size², row-major N→S)
});
let grid = dem.geodetic_grid(&bounds, grid_size);
let terrain = encode_terrain(&grid, grid_size, &bounds, &TerrainOptions::default());Structs§
- Mercator
Dem - A contiguous rectangular block of decoded web-mercator (XYZ) DEM tiles, stitched into a single elevation grid and sampleable by longitude / latitude.