-
Notifications
You must be signed in to change notification settings - Fork 8
/
rasterize-vector-tiles.js
133 lines (122 loc) · 3.78 KB
/
rasterize-vector-tiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
"use strict";
const meow = require("meow");
const log = require("./log");
const target = require("./target");
const render = require("./render");
const { decodePbf } = require("./mbtiles/pbf/pbf_dump");
const { readTile, readMetadata, listFiles } = require("./mbtiles/mbtileReader");
const printProgress = require("./printprogress");
const cli = meow(
`
Usage
$ rasterize-vector-tiles -i <input> -o <output> [options]
Options
--source, -i Vector tile set in .mbtiles format
--target, -o Raster tile set to be created (.mbtiles)
--colorprop Feature property to use for gray level (0-255), default='value'"
--color Single color to use on all pixels (overrides colorprop), default=''"
--nodata Colorprop value to interpret as nodata (will not be rendered), default=255"
--antialias Control anti-aliasing (none/default/gray/subpixel), default='default'"
--zoomlevel Upper limit on zoom level to rasterize (0-99), default=highest in input file"
--bitmapsize Width/Height of the created rasters, default = 256
--png Export as individual png files rather than .mbtiles archive"
Examples
$ rasterize-vector-tiles -i vector.mbtiles -o raster.mbtiles -prop value
`,
{
flags: {
source: {
type: "string",
alias: "i"
},
target: {
type: "string",
alias: "o"
},
colorprop: {
type: "string",
alias: "prop",
default: "value"
},
color: {
type: "string"
},
nodata: {
type: "number",
default: -1
},
antialias: {
type: "string",
default: "default"
},
zoomlevel: {
type: "number",
alias: "zoom",
default: 99
},
bitmapsize: {
type: "number",
default: 256
},
png: {
type: "boolean",
default: false
}
}
}
);
log.debug(cli);
const option = cli.flags;
log.info(
"Feature property controlling color: " + option.colorprop || "<empty>"
);
log.info("Nodata value (ignored features): " + option.nodata);
log.info("Constant color for all geometries: " + option.color);
log.info("Antialiasing mode: " + option.antialias);
log.info("Source file: " + option.source);
log.info("Target file: " + option.target);
log.info(
"Target bitmap size: " +
option.bitmapsize +
" x " +
option.bitmapsize
);
if (!option.source) error("Error: Source file argument (-i) is required.");
if (!option.target) error("Error: Target file argument (-o) is required.");
function error(msg) {
console.error(msg);
process.exitCode = 1;
process.exit();
}
async function readSource() {
log.info(`Reading '${option.source}'`);
const source = await listFiles(option.source, option.zoomlevel);
source.metadata = await readMetadata(option.source);
log.info("Source tile count: " + source.files.length);
return source;
}
async function rasterize(files, targetdb) {
const start = new Date();
for (let i = 0; i < files.length; ) {
await rasterizeTile(files[i], targetdb);
i++;
printProgress(start, i, files.length);
}
targetdb.close();
}
async function rasterizeTile(vtile, targetdb) {
const pbfjson = decodePbf(vtile.tile_data);
const imageBuffer = render(pbfjson, option);
const { zoom_level, tile_column, tile_row } = vtile;
await targetdb.createTile(zoom_level, tile_column, tile_row, imageBuffer);
}
async function convert() {
const source = await readSource();
log.info(`Creating '${option.target}'`);
const targetdb = await target.create(source, option.png, option.target);
await rasterize(source.files, targetdb);
}
convert().then((reject, resolve) => {
if (reject) throw new Error(reject);
});