-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·202 lines (158 loc) · 5.36 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#! /usr/bin/env node
// TODO: Allow CLI options to be defined and passed through
var Promise = require('bluebird');
var async = require('async');
var program = require('commander');
var path = require('path');
var fs = Promise.promisifyAll(require('fs'));
var chalk = require('chalk');
var childProcess = require('child_process');
var shell = require('shelljs');
var turf = require('turf');
// Check for CLI binary
if (!shell.which("polygon-city")) {
console.error(chalk.bgRed.gray('Error: Polygon City CLI not found'));
process.exit();
}
var processFiles = function(inputDirectory, options) {
console.log('EPSG: %j', options.epsg);
console.log('Mapzen elevation key: %j', options.elevationKey);
if (options.prefix) {
console.log('Prefix: %j', options.prefix);
}
console.log('Output directory: %j', program.output);
console.log('Input directory: %j', inputDirectory);
// Check input file path is defined
if (!inputDirectory) {
console.error(chalk.red('Exiting: Input file path not specified'));
process.exit(1);
}
// Check output file path is defined
if (!options.output) {
console.error(chalk.red('Exiting: Output file path not specified'));
process.exit(1);
}
// Check EPSG code
if (!options.epsg) {
console.error(chalk.red('Exiting: EPSG code not specified'));
process.exit(1);
}
// Check Mapzen key
if (!options.elevationKey) {
console.error(chalk.red('Exiting: Mapzen elevation key not specified'));
process.exit(1);
}
fs.readdirAsync(inputDirectory).then((files) => {
var ext, args, child;
// Spawn a new CLI instance for each file, but only after the previous
// one has completed
files.forEach((file) => {
ext = path.extname(file);
bareName = file.split(ext)[0];
if (ext !== '.gml') {
return;
}
args = [];
args.push('-c');
args.push(options.epsg);
args.push('-E');
args.push(options.elevationKey);
if (options.prefix) {
args.push('-p');
args.push(options.prefix);
}
if (options.elevation) {
args.push('-e');
args.push(options.elevation);
}
if (options.wof) {
args.push('-w');
args.push(options.wof);
}
if (options.wofKey) {
args.push('-W');
args.push(options.wofKey);
}
if (options.attribution) {
args.push('-a');
args.push(options.attribution);
}
if (options.license) {
args.push('-l');
args.push(options.license);
}
args.push('-o');
var output = (options.prefix) ? options.prefix + bareName : bareName;
args.push(path.join(options.output, output));
args.push(path.join(inputDirectory, file));
console.log(chalk.bgGreen.gray('Spawning polygon-city process...'));
console.log(args);
child = childProcess.spawnSync('polygon-city', args, {
killSignal: 'SIGINT'
});
});
var outputDirs = fs.readdirSync(options.output).filter(function(file) {
return fs.statSync(path.join(options.output, file)).isDirectory();
});
// Gather GeoJSON files and create a combined index that has a polygon for
// each directory showing the area it covers
var geojsonIndexPaths = outputDirs.map(dir => {
var geojsonPath = path.join(options.output, dir, 'index.geojson');
try {
fs.statSync(geojsonPath);
return geojsonPath;
} catch(err) {
return;
}
});
// Remove empties
geojsonIndexPaths = geojsonIndexPaths.filter(function(n) {
return n !== undefined;
});
// Get GeoJSON indexes
async.map(geojsonIndexPaths, fs.readFile, (err, results) => {
var envelopes = [];
if (err) {
throw err;
}
var relativePath;
results.forEach((result, index) => {
relativePath = path.relative(options.output, geojsonIndexPaths[index]);
envelope = turf.envelope(JSON.parse(result.toString()));
envelope.properties.id = relativePath.split('/index.geojson')[0];
envelope.properties.path = path.relative(options.output, geojsonIndexPaths[index]);
envelopes.push(envelope);
});
var fc = turf.featurecollection(envelopes);
fs.writeFileAsync(path.join(options.output, 'index.geojson'), JSON.stringify(fc)).then(result => {
console.log(chalk.bgBlue.gray('All files processed. Exiting...'));
});
});
}).catch((err) => {
console.error(chalk.bgRed.gray(err));
process.exit();
});
};
var resumeJobs = function() {
var child = childProcess.spawnSync('polygon-city', ['resume'], {
killSignal: 'SIGINT'
});
};
program
.version('0.0.1')
.usage('[options] <input directory>')
.option('-c, --epsg [code]', 'EPSG code for input data')
.option('-p, --prefix [prefix]', 'Prefix for building IDs')
.option('-e, --elevation [url]', 'Elevation endpoint')
.option('-E, --elevationKey [key]', 'Mapzen Elevation API key')
.option('-w, --wof [url]', 'Who\'s On First endpoint')
.option('-W, --wofKey [key]', 'Mapzen Who\'s on First API key')
.option('-a, --attribution [attribution]', 'Attribution text')
.option('-l, --license [license]', 'License text')
.option('-o, --output [directory]', 'Output directory')
.action(processFiles);
program
.command('resume')
.description('Resume processing of existing jobs')
.action(resumeJobs);
program.parse(process.argv);