-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path4-makeVectorTiles.js
executable file
·129 lines (116 loc) · 3.86 KB
/
4-makeVectorTiles.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
#!/usr/bin/env node
require('make-dir').sync('mbtiles');
const tippecanoe = require('tippecanoe');
const fs = require('fs');
const child_process = require('child_process');
const optionList = [
{ name: 'separate-mbtiles', type: Boolean, defaultValue: false, description: 'Generate individual mbtilesfiles.' },
{ name: 'single-source', type: Boolean, defaultValue: false, description: 'Use a combined allout.geojson file' },
{ name: 'just-join', type: Boolean, defaultValue: false, description: 'Skip generation, just join existing mbtiles.' },
{ name: 'help', type: Boolean, alias: 'h' },
];
require('colors');
let options;
function help() {
console.log(require('command-line-usage')([
{
header: require('path').basename(process.argv[1]),
content: 'Convert geojson files to vector tiles.',
}, {
header: 'Usage',
optionList
}
]));
}
try {
options = require('command-line-args')(optionList);
if (options.help) {
help();
process.exit(0);
}
} catch (e) {
console.error(`Error: ${e.name.red} ${e.optionName || ''}` );
help();
process.exit(1);
}
const fields = {
captured: 15,
class: 15,
common: 10,
crown: 13,
dbh: 13,
description: 13,
family: 10,
genus: 1,
health: 9,
height: 13,
id: 13,
location: 15,
maturity: 12,
planted: 15,
ref: 13,
scientific: 10,
source: 8,
species: 1,
species_count: 10,
structure: 15,
subclass: 15,
tree_type: 15,
ule: 15,
ule_max: 15,
ule_min: 15,
updated: 15,
variety: 13
}
function makeTiles(input, output) {
tippecanoe(input, {
// tippecanoe(['tmp/allout.json'], {
layer:'trees',
force:true,
output,
minimumZoom: 1,
maximumZoom: 15,
baseZoom: 10,
minimumDetail:7,
forceFeatureLimit: true,
rf: 200000 ,
readParallel: true,
featureFilter: { 'trees': ['all',
...Object.keys(fields).map(f => [
'attribute-filter',
f,
['>=', '$zoom', fields[f]]
])]
}
}, { echo: true });
}
const files = fs.readdirSync('out').filter(f => f.match(/\.geojson$/));
if (options['separate-mbtiles'] || options['just-join']) {
if (options['separate-mbtiles']) {
console.log('Generating separate mbtiles for each source.');
for (let file of files) {
const mbtilesFile = `mbtiles/out-${file.replace(/\..*/, '')}.mbtiles`;
if (!fs.existsSync(mbtilesFile) || fs.existsSync(`${mbtilesFile}-journal`)) {
makeTiles(`out/${file}`, mbtilesFile)
}
}
}
const tilesFiles = fs.readdirSync('mbtiles').filter(f => f.match(/out-.*\.mbtiles$/)).map(f => `mbtiles/${f}`);
console.log(`Combining ${tilesFiles.length} mbtiles files.`);
const cmd = `tile-join -f -o mbtiles/trees-wip.mbtiles ${tilesFiles.join(' ')}`;
console.log(cmd.green);
child_process.execSync(cmd);
} else if (options['single-source']) {
console.log('Generating a single mbtiles file from allout.json.');
makeTiles(['tmp/allout.json'], 'mbtiles/trees-wip.mbtiles');
child_process.execSync('mv mbtiles/trees.mbtiles mbtiles/trees-old.mbtiles');
child_process.execSync('mv mbtiles/trees-wip.mbtiles mbtiles/trees.mbtiles');
console.log('Tiles now in mbtiles/trees.mbtiles');
} else {
console.log('Generating a single mbtiles file from many sources.');
makeTiles(files.map(f => `out/${f}`), 'mbtiles/trees-wip.mbtiles');
child_process.execSync('mv mbtiles/trees.mbtiles mbtiles/trees-old.mbtiles');
child_process.execSync('mv mbtiles/trees-wip.mbtiles mbtiles/trees.mbtiles');
console.log('Tiles now in mbtiles/trees.mbtiles');
}
//