Skip to content

Commit

Permalink
固定瓦片尺寸256+2
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeGIS committed May 18, 2024
1 parent 40ffbfa commit 6c1f413
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 22 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
特点:

- 支持 `mapbox``terrarium` 两种地形瓦片编码格式供mapboxgl使用,其中terrarium格式是[tangram](https://www.mapzen.com/products/tangram/)引擎的官方地形格式,tangram是另外一款开源的webgl二三维一体化的引擎;
- 支持自定义瓦片级别和瓦片尺寸设置,瓦片周围会有 1px 的裙边。例如指定生成 512px 的瓦片,实际输出的瓦片文件分辨率为 `514 × 514`,与 Mapbox 官方一致;
- 固定瓦片尺寸256,瓦片周围有1cell的buffer,即实际瓦片是258*258.
- 自动读取数据源的坐标系统,重编码输入的 DEM 栅格文件,并重投影至指定的坐标系4490、4326、3857,默认3857,然后生成瓦片;
- 支持适用于3857、4490、4326的地形切片生产;
- 内置了影像金字塔索引和多进程实现(暂未使用多线程),加速瓦片生成速度;
Expand Down Expand Up @@ -83,7 +83,6 @@ Options:
-f, --configFile <File> <可选> 通过配置文件执行任务,输入绝对路径,可参考配置模板
-g, --epsg <number> <可选> Tile适用坐标系,3857 | 4490 | 4326 (default: 3857)
-c, --clean <number> <可选> 是否清空输出目录,0 | 1 (default: 0)
-s, --size <number> <可选> 指定生成瓦片的尺寸,256 | 512 (default: 512)
-z, --zoom <number-number> <可选> 指定瓦片的等级生成范围。例如,想生成 7 ~ 12 级的瓦片,则输入 -z 7-12 (default: "5-14")
-e, --encoding <string> <可选> 指定瓦片的数据编码规则(mapbox 或 terrarium) (default: "mapbox")
-h, --help 帮助
Expand All @@ -94,7 +93,6 @@ Options:
- `-o`: 输出目录,支持相对路径;
- `-g`: 指定地形Tile适用坐标系,默认是适用3857坐标系;
- `-z`: 由于地形栅格数据通常是 90m、30m 的空间分辨率,等级太大意义不大,等级太低时起伏辨识也不高,所以默认生成中间的 `5-14` 级;
- `-s`: 指定输出瓦片的尺寸,默认是 512 像素;
- `-c`: 指定是否预先清理输出瓦片的存储目录,默认0,不清理;
- `-e`: 指定切片编码规则,默认 mapbox,用户可指定 terrarium 规则输出。
- `-f`: 以上参数可以都放到一个配置json文件里,使用-f执行切片任务,简化操作;
Expand All @@ -105,7 +103,7 @@ Options:

* 方式1:通过命令行参数执行任务
```bash
dem2terrain -z 4-15 -s 256 -e terrarium -i ./ZONE.tiff -o ./output -c 1 -g 3857
dem2terrain -z 4-15 -e terrarium -i ./ZONE.tiff -o ./output -c 1 -g 3857
```

* 方式2:通过配置文件执行任务
Expand All @@ -115,7 +113,6 @@ dem2terrain -z 4-15 -s 256 -e terrarium -i ./ZONE.tiff -o ./output -c 1 -g 3857
{
"zoom":"5-13",
"epsg": 3857,
"size": 512,
"encoding": "mapbox",
"input": "./data/xxx.tif",
"output": "./data/tile",
Expand All @@ -134,14 +131,14 @@ dem2terrain -f d://config.json

-o参数为文件目录,则以文件形式存储:
```bash
dem2terrain -z 4-15 -s 256 -e terrarium -i ./ZONE.tiff -o ./output -c 1 -g 3857
dem2terrain -z 4-15 -e terrarium -i ./ZONE.tiff -o ./output -c 1 -g 3857
```

* 以mbtiles存储

-o参数带.mbtiles扩展名,则以mbtiles形式存储:
```bash
dem2terrain -z 4-15 -s 256 -e terrarium -i ./ZONE.tiff -o ./output/tile.mbtiles -c 1 -g 3857
dem2terrain -z 4-15 -e terrarium -i ./ZONE.tiff -o ./output/tile.mbtiles -c 1 -g 3857
```


Expand All @@ -155,7 +152,6 @@ dem2terrain -z 4-15 -s 256 -e terrarium -i ./ZONE.tiff -o ./output/tile.mbtiles
```javascript
// 数据编码,'mapbox'或'terrarium'
const encoding = 'mapbox';
const tileSize = 512;
const maxZoom = 14;
map.addSource('my-custom-terrain', {
type: 'raster-dem',
Expand All @@ -164,7 +160,7 @@ map.addSource('my-custom-terrain', {
tiles: ['./mapbox/{z}/{x}/{y}.png'],
// 注释掉官方的服务url,替换自己的
//'url': 'mapbox://mapbox.mapbox-terrain-dem-v1',
tileSize: tileSize,
tileSize: 256,
maxzoom: maxZoom,
})
```
Expand Down
6 changes: 2 additions & 4 deletions bin/dem2terrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ program
.option('-f, --configFile <File>', '<可选> 通过配置文件执行任务,输入绝对路径,可参考配置模板')
.option('-g, --epsg <number>', '<可选> Tile适用坐标系,3857 | 4490 | 4326', 3857)
.option('-c, --clean <number>', '<可选> 是否清空输出目录,0 | 1', 0)
.option('-s, --size <number>', '<可选> 指定生成瓦片的尺寸,256 | 512', 512)
.option('-z, --zoom <number-number>', '<可选> 指定瓦片的等级生成范围。例如,想生成 7 ~ 12 级的瓦片,则输入 -z 7-12', '5-14')
.option('-e, --encoding <string>', '<可选> 指定瓦片的数据编码规则(mapbox 或 terrarium)', 'mapbox');

Expand All @@ -42,7 +41,7 @@ if(inputDem===undefined||outputDir===undefined){
console.log('参数缺失: 输入文件路径或输出目录必填');
process.exit();
}
const tileSize = Number(params['size']);

const encoding = params['encoding'];
const epsg = Number(params['epsg']);
const isClean = Number(params['clean']);
Expand All @@ -68,7 +67,7 @@ const logMsg = `\n>> 开始转换...
- 输出路径: ${outFileAbsolutePath}
- Tile适用坐标系: EPSG:${epsg}
- 瓦片编码: ${encoding === 'mapbox' ? 'mapbox(raster-dem)' : encoding}
- 瓦片尺寸: ${tileSize} px
- 瓦片尺寸: 256 px
- 瓦片等级: ${minZoom}${maxZoom}
`;
console.log(logMsg);
Expand All @@ -77,7 +76,6 @@ main(inputDem, outputDir, {
minZoom,
maxZoom,
epsg,
tileSize,
encoding,
isClean
});
1 change: 0 additions & 1 deletion config.template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"zoom":"5-14",
"epsg": 3857,
"size": 512,
"encoding": "mapbox",
"input": "./xxx.tif",
"output": "./xxx",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dem2terrain",
"version": "2.1.1",
"version": "2.1.2",
"description": "使用 GDAL 制作 DEM 栅格的地形瓦片",
"main": "index.js",
"preferGlobal": true,
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,16 @@ let tileBoundTool;
* minZoom: number;
* maxZoom: number;
* epsg: number;
* tileSize: 256 | 512;
* encoding: 'mapbox' | 'terrarium';
* }} options 可选配置
*/
async function main(input, output, options) {
// 计时开始
const startTime = global.performance.now();
// 结构可选参数
// 结构可选参数
const { minZoom, maxZoom, epsg, tileSize, encoding, isClean } = options;
const { minZoom, maxZoom, epsg, encoding, isClean } = options;
// 固定瓦片尺寸
const tileSize = 256;
tileBoundTool = tileBoundMap.get(epsg);
// 判断是否以mbtiles转储
const isSavaMbtiles = (path.extname(output) === '.mbtiles');
Expand Down
9 changes: 5 additions & 4 deletions src/tile-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tileBoundMap.set(4326, {
});
// 根据xyz计算对应地理坐标系的地理边界
function ST_TileEnvelope(z, x, y, offset = 0, bbox = tileBoundMap.get(3857)) {
const tile_size = 256.0;
const boundsWidth = bbox.xmax - bbox.xmin;
const boundsHeight = bbox.ymax - bbox.ymin;
if (boundsWidth <= 0 || boundsHeight <= 0)
Expand All @@ -43,11 +44,11 @@ function ST_TileEnvelope(z, x, y, offset = 0, bbox = tileBoundMap.get(3857)) {
const tileGeoSizeX = boundsWidth * 1.0 / worldTileSize;
const tileGeoSizeY = boundsHeight * 1.0 / worldTileSize;

let x1 = bbox.xmin + tileGeoSizeX * x - tileGeoSizeX / 256.0 * offset;
let x2 = bbox.xmin + tileGeoSizeX * (x + 1) + tileGeoSizeX / 256.0 * offset;
let x1 = bbox.xmin + tileGeoSizeX * x - tileGeoSizeX / tile_size * offset;
let x2 = bbox.xmin + tileGeoSizeX * (x + 1) + tileGeoSizeX / tile_size * offset;

let y1 = bbox.ymax - tileGeoSizeY * (y + 1) - tileGeoSizeY / 256.0 * offset;
let y2 = bbox.ymax - tileGeoSizeY * (y) + tileGeoSizeY / 256.0 * offset;
let y1 = bbox.ymax - tileGeoSizeY * (y + 1) - tileGeoSizeY / tile_size * offset;
let y2 = bbox.ymax - tileGeoSizeY * (y) + tileGeoSizeY / tile_size * offset;

return [x1, y1, x2, y2];

Expand Down

0 comments on commit 6c1f413

Please sign in to comment.