Skip to content

Commit

Permalink
撤回对进程池的修改
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeGIS committed Aug 27, 2022
1 parent bb4c921 commit d0b9b87
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 757 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

本版本无功能更新。特性如下:

- 重构源码至 ESModule
- 重构源码,拆分各个子模块任务区域
- 代码添加了 JSDoc 注释,在 VSCode 开发时有较好的类型提示
- 优化控制台进度提示文字的样式
- 禁用 package 依赖锁文件的生成
Expand Down
31 changes: 13 additions & 18 deletions bin/dem2terrain.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
#!/usr/bin/env node --experimental-modules
import { resolve, isAbsolute, relative } from 'node:path'
import { Command } from 'commander';
import {
main
} from '../index.js';

const program = new Command();
const version = '1.0.4'
#!/usr/bin/env node
const program = require('commander');
const main = require('../index');
const path = require('path');
const version = '1.0.4';

program.name('dem2terrain')
.description('使用 GDAL 制作地形瓦片,支持 raster-dem(mapboxgl) 和 terrarium 两种编码输出格式,当前仅输出 PNG 容器格式。')
.argument('<input-tiff-file>', '输入 tif 格式的 DEM 文件路径,支持相对路径')
.argument('<output-directory>', '输出目录,支持相对路径')
.version(version, '-v, --version', '当前版本')
.helpOption('-h, --help', '帮助')
.helpOption('-h, --help', '帮助');

// --- 配置可选参数
program
.option('-s, --size <number>', '指定生成瓦片的尺寸(256 或 512)| 默认 512 像素', '512')
.option('-z, --zoom <number-number>', '指定瓦片的等级生成范围。例如,想生成 7 ~ 12 级的瓦片,则输入 -z 7-12 | 默认值是 -z 5-14', '5-14')
.option('-e, --encoding <string>', '指定瓦片的数据编码规则(mapbox 或 terrarium)| 默认 -e mapbox', 'mapbox')
.option('-e, --encoding <string>', '指定瓦片的数据编码规则(mapbox 或 terrarium)| 默认 -e mapbox', 'mapbox');

// --- 解析参数
program.parse();
Expand All @@ -37,7 +33,7 @@ const outputDir = args[1];
const options = program.opts();

const tileSize = Number(options['size']);
const encoding = options['encoding']
const encoding = options['encoding'];
let zoom = options['zoom'];
zoom = zoom.split('-');
const minZoom = Number(zoom[0]);
Expand All @@ -51,22 +47,21 @@ if (minZoom >= maxZoom) {
process.exit();
}

const inputAbsolutePath = isAbsolute(inputDem) ? inputDem : resolve(process.cwd(), inputDem)
const outFileAbsolutePath = isAbsolute(outputDir) ? outputDir : resolve(process.cwd(), outputDir)
const inputAbsolutePath = path.isAbsolute(inputDem) ? inputDem : path.resolve(process.cwd(), inputDem);
const outFileAbsolutePath = path.isAbsolute(outputDir) ? outputDir : path.resolve(process.cwd(), outputDir);

const logMsg = `\n>> 开始转换...
- 输入文件: ${inputAbsolutePath}
- 输出路径: ${outFileAbsolutePath}
- 瓦片编码: ${encoding === 'mapbox' ? 'mapbox(raster-dem)' : encoding}
- 瓦片尺寸: ${tileSize} px
- 瓦片等级: ${minZoom}${maxZoom}
`
console.log(logMsg)
`;
console.log(logMsg);

main(inputDem, outputDir, {
minZoom,
maxZoom,
tileSize,
encoding
});
});
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { default as main } from './src/index.js'
const main = require('./src/index');

module.exports = main;

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"engines": {
"node": ">= 14"
},
"type": "module",
"bin": {
"dem2terrain": "./bin/dem2terrain.js"
},
"scripts": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/FreeGIS/dem2terrain.git"
Expand All @@ -34,4 +35,4 @@
"gdal": "^0.11.1",
"single-line-log": "^1.1.2"
}
}
}
122 changes: 52 additions & 70 deletions src/createtile.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,59 @@
import { join } from 'path';
import gdal from 'gdal';
import { createDirs } from './util.js';
import { getDriverByName } from './gdal-util.js';
const gdal = require('gdal');
const { createDirs } = require('./util');
const { getDriverByName } = require('./gdal-util');
const path = require('path');

const {
GDT_Byte,
open,
} = gdal

function write_terrain_tile(overviewInfo, readinfo, writeinfo, bandnums) {
bandnums.forEach(i => {
let readband;
if (overviewInfo.index === undefined)
readband = readinfo.ds.bands.get(i);
else // 从影像金字塔里读取band信息
readband = readinfo.ds.bands.get(i).overviews.get(overviewInfo.index);
let writeband = writeinfo.ds.bands.get(i);
let bandBuffData = new Uint8Array(writeinfo.wxsize * writeinfo.wysize);
// 从数据集band读取对应的像素出来写入bandBuffData
readband.pixels.read(readinfo.rx, readinfo.ry, readinfo.rxsize, readinfo.rysize, bandBuffData, {
buffer_width: writeinfo.wxsize,
buffer_height: writeinfo.wysize,
data_type: GDT_Byte
});
// 写入
writeband.pixels.write(writeinfo.wx, writeinfo.wy, writeinfo.wxsize, writeinfo.wysize, bandBuffData);
})
function writeTerrainTile(overviewInfo, readinfo, writeinfo, bandnums) {
bandnums.forEach(i => {
let readband;
if (overviewInfo.index === undefined)
readband = readinfo.ds.bands.get(i);
else // 从影像金字塔里读取band信息
readband = readinfo.ds.bands.get(i).overviews.get(overviewInfo.index);
let writeband = writeinfo.ds.bands.get(i);
let bandBuffData = new Uint8Array(writeinfo.wxsize * writeinfo.wysize);
// 从数据集band读取对应的像素出来写入bandBuffData
readband.pixels.read(readinfo.rx, readinfo.ry, readinfo.rxsize, readinfo.rysize, bandBuffData, {
buffer_width: writeinfo.wxsize,
buffer_height: writeinfo.wysize,
data_type: gdal.GDT_Byte
});
// 写入
writeband.pixels.write(writeinfo.wx, writeinfo.wy, writeinfo.wxsize, writeinfo.wysize, bandBuffData);
})
}

let dataset = null;
let memDriver = null;

export function createTile(createInfo, callback) {
const {
outTileSize,
overviewInfo,
rb,
wb,
dsPath,
x,
y,
z,
outputTile,
} = createInfo;
if (dataset === null) {
dataset = open(dsPath, 'r');
}
// 创建一个 mem 驱动,将读取的像素暂存至内存
if (memDriver === null) {
memDriver = getDriverByName('mem');
}
const msmDS = memDriver.create("", outTileSize, outTileSize, 3);
rb.ds = dataset;
wb.ds = msmDS;
write_terrain_tile(overviewInfo, rb, wb, [1, 2, 3]);
const pngPath = join(outputTile, '/' + z + '/' + x + '/' + y + '.png');
//递归创建文件目录
createDirs(pngPath);
const pngDriver = getDriverByName('png');
const pngDs = pngDriver.createCopy(pngPath, msmDS);
let dataset = null, memDriver = null;
function createTile(createInfo, callback) {
const { outTileSize, overviewInfo, rb, wb, dsPath, x, y, z, outputTile } = createInfo;
if (dataset === null)
dataset = gdal.open(dsPath, 'r');
// 创建一个mem内存,将读取的像素写入mem
if (memDriver === null)
memDriver = getDriverByName('mem');
const msmDS = memDriver.create("", outTileSize, outTileSize, 3);
rb.ds = dataset;
wb.ds = msmDS;
writeTerrainTile(overviewInfo, rb, wb, [1, 2, 3]);
const pngPath = path.join(outputTile, '/' + z + '/' + x + '/' + y + '.png');
//递归创建文件目录
createDirs(pngPath);
const pngDriver = getDriverByName('png');
const pngDs = pngDriver.createCopy(pngPath, msmDS);

// 释放内存
msmDS.flush();
msmDS.close();
pngDs.close();
callback(null, process.pid);
// 释放内存
msmDS.flush();
msmDS.close();
pngDs.close();
callback(null, process.pid);
}
function closeDataset(callback) {
if (dataset) {
dataset.close();
dataset = null;
callback(null, process.pid);
}
callback(null, null);

export function closeDataset(callback) {
if (dataset) {
dataset.close();
dataset = null;
callback(null, process.pid);
}
callback(null, null);
}
module.exports = { createTile, closeDataset }
36 changes: 20 additions & 16 deletions src/dem-encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
* @param {number} height 高程值
* @returns {[number, number, number]}
*/
export function mapboxEncode(height) {
const value = Math.floor((height + 10000) * 10);
const r = value >> 16;
const g = value >> 8 & 0x0000FF;
const b = value & 0x0000FF;
return [r, g, b];
function mapboxEncode(height) {
const value = Math.floor((height + 10000) * 10);
const r = value >> 16;
const g = value >> 8 & 0x0000FF;
const b = value & 0x0000FF;
return [r, g, b];
}

/**
* MapboxGL raster-dem 解码
* @param {[number, number, number]} color
* @returns {number} 高程值
*/
export function mapboxDecode(color) {
return -10000 + ((color[0] * 256 * 256 + color[1] * 256 + color[2]) * 0.1);
function mapboxDecode(color) {
return -10000 + ((color[0] * 256 * 256 + color[1] * 256 + color[2]) * 0.1);
}


Expand All @@ -26,19 +26,23 @@ export function mapboxDecode(color) {
* @param {number} height 高程值
* @returns {[number, number, number]}
*/
export function terrariumEncode(height) {
height += 32768;
const r = Math.floor(height / 256.0);
const g = Math.floor(height % 256);
const b = Math.floor((height - Math.floor(height)) * 256.0);
return [r, g, b];
function terrariumEncode(height) {
height += 32768;
const r = Math.floor(height / 256.0);
const g = Math.floor(height % 256);
const b = Math.floor((height - Math.floor(height)) * 256.0);
return [r, g, b];
}

/**
* Terrarium 解码
* @param {[number, number, number]} color
* @returns {number} 高程值
*/
export function terrariumDecode(color) {
return (color[0] * 256 + color[1] + color[2] / 256.0) - 32768;
function terrariumDecode(color) {
return (color[0] * 256 + color[1] + color[2] / 256.0) - 32768;
}

module.exports = {
mapboxEncode, terrariumEncode, mapboxDecode, terrariumDecode
}
Loading

0 comments on commit d0b9b87

Please sign in to comment.