@csi-foxbyte/mesh-dem-to-terrain
Version:
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.
119 lines (89 loc) β’ 4.5 kB
Markdown
# mesh-dem-to-terrain ππ₯Ύ
A Node.js library for converting DEM meshes (DXF, GeoJSON, etc.) into Cesium Terrain Tiles (quantized-mesh or heightmap), with flexible zoom-level control.
## Table of Contents
* [Features](#features)
* [Installation](#installation)
* [Usage](#usage)
* [API](#api)
* [Options Overview](#options-overview)
* [CLI Wrapper Example](#cli-wrapper-example)
* [Contributing](#contributing)
## π Features
* **π Preprocess Mesh Archive**: Unpacks and prepares DXF/GeoJSON mesh archives into a working directory, reprojects coordinates using Proj4 strings. πΊοΈ
* **π» Terrain Tile Generation**: Generates Cesium-compatible terrain tiles (quantized-mesh or heightmap) across specified zoom levels. π
* **π Projection Support**: Accept any Proj4-formatted CRS string for flexible coordinate transformations. β¨
* **π Progress Callbacks**: Receive real-time progress updates (percentage or log messages). π’
* **π§΅ Multi-threading**: Customize worker threads for CPU-bound tasks. βοΈ
## π₯ Installation
```bash
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
### `preprocess(inputArchive, workDir, progressCallback, proj4String)`
* **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. β
### `generate(workDir, progressCallback, options)`
* **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
}
);
})();
```
## π€ Contributing
Contributions are welcome! Please open issues or pull requests on the GitHub repository. π