A Node.js library that converts DEM meshes (DXF, GeoJSON, etc.) into Cesium Terrain Tiles (quantized-mesh or heightmap) with flexible zoom-level control and multi-threading support.
npm install @csi-foxbyte/mesh-dem-to-terrainbash
npm install mesh-dem-to-terrain
`
π» Usage
`js
import { preprocess, generate } from "mesh-dem-to-terrain/index.js";
(async () => {
// Step 1: Unpack and reproject your mesh archive π οΈ
await preprocess(
"D:\\dxf.zip", // Path to DXF/GeoJSON archive
"D:\\dxf_test", // Working directory for intermediate files
console.log, // Progress callback (log messages)
"+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs" // Proj4 CRS string
);
// Step 2: Generate Cesium Terrain Tiles π
await generate(
"D:\\dxf_test", // Working directory from preprocess
(progress) => console.log((progress * 100).toFixed(2), "%"), // Progress callback (percentage)
{ endZoom: 16, startZoom: 0, threadCount: 2 } // Options: zoom range & threads
);
})();
`
βοΈ API
$3
* inputArchive (string) β Path to a ZIP archive containing DXF, GeoJSON, or other mesh files. π
* workDir (string) β Directory to extract and preprocess mesh data. π
* progressCallback (function) β Called with log messages or progress updates. π’
* proj4String (string) β Proj4-formatted CRS string for coordinate transformation. π
Returns: A promise that resolves when preprocessing is complete. β
$3
* workDir (string) β Directory prepared by preprocess(). π
* progressCallback (function) β Called with a number in \[0,1] indicating progress. π
* options (object):
* startZoom (number) β Minimum zoom level to generate (default: 0). βοΈ
* endZoom (number) β Maximum zoom level to generate (required). βοΈ
* threadCount (number) β Number of worker threads (default: number of CPU cores). π§΅
Returns: A promise that resolves when terrain tile generation is complete. β
π οΈ Options Overview
| Option | Default | Description |
| ------------- | ------------------ | ------------------------------------------ |
| startZoom | 0 | Minimum zoom level for generated tiles. βοΈ |
| endZoom | required | Maximum zoom level for generated tiles. βοΈ |
| threadCount | os.cpus().length | Number of parallel worker threads. π§΅ |
π CLI Wrapper Example
Wrap the library in a convenient CLI script:
`js
#!/usr/bin/env node
import path from "path";
import { preprocess, generate } from "mesh-dem-to-terrain/index.js";
const [,, archive, outDir, proj, startZ, endZ, threads] = process.argv;
(async () => {
// Preprocess mesh archive π οΈ
await preprocess(
path.resolve(archive),
path.resolve(outDir),
console.log,
proj
);
// Generate terrain tiles π
await generate(
path.resolve(outDir),
(p) => console.log(${(p*100).toFixed(2)} %),
{
startZoom: Number(startZ) || 0,
endZoom: Number(endZ) || 16,
threadCount: Number(threads) || 4
}
);
})();
``