-
Notifications
You must be signed in to change notification settings - Fork 195
/
converter-cli.ts
409 lines (385 loc) · 14.8 KB
/
converter-cli.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* eslint-disable no-console */
import '@loaders.gl/polyfills';
import {join} from 'path';
import inquirer from 'inquirer';
import {I3SConverter, Tiles3DConverter} from '@loaders.gl/tile-converter';
import {DepsInstaller} from './deps-installer/deps-installer';
import {
getBooleanValue,
getIntegerValue,
getStringValue,
getURLValue,
validateOptionsWithEqual
} from './lib/utils/cli-utils';
import {addOneFile, composeHashFile, makeZipCDHeaderIterator} from '@loaders.gl/zip';
import {FileHandleFile} from '@loaders.gl/loader-utils';
// @ts-ignore
import {copyFile} from 'node:fs/promises';
type TileConversionOptions = {
/** "I3S" - for I3S to 3DTiles conversion, "3DTILES" for 3DTiles to I3S conversion */
inputType?: string;
/** "tileset.json" file (3DTiles) / "http://..../SceneServer/layers/0" resource (I3S) */
tileset?: string;
/** Tileset name. This option is used for naming in resulting json resouces and for resulting path/*.slpk file naming */
name?: string;
/** Output folder. This folder will be created by converter if doesn't exist. It is relative to the converter path.
* Default: "data" folder */
output: string;
/** 3DTile version.
* Default: version "1.1" */
outputVersion?: string;
/** Keep created 3DNodeIndexDocument files on disk instead of memory. This option reduce memory usage but decelerates conversion speed */
instantNodeWriting: boolean;
/** Try to merge similar materials to be able to merge meshes into one node (I3S to 3DTiles conversion only) */
mergeMaterials: boolean;
/** location of the Earth Gravity Model (*.pgm) file to convert heights from ellipsoidal to gravity-related format,
* "None" for not using Earth Gravity Model (*.pgm)
* default: "./deps/egm2008-5.pgm". A model file can be loaded from GeographicLib
* https://geographiclib.sourceforge.io/html/geoid.html */
egm: string;
/** 3DTile->I3S only. Token for Cesium ION tileset authentication. */
token?: string;
/** 3DTiles->I3S only. Enable draco compression for geometry. Default: true */
draco: boolean;
/** Run the script for installing dependencies. Run this options separate from others. Now "*.pgm" file installation is
* implemented */
installDependencies: boolean;
/** 3DTile->I3S only. Enable KTX2 textures generation if only one of (JPG, PNG) texture is provided or generate JPG texture
* if only KTX2 is provided */
generateTextures: boolean;
/** 3DTile->I3S only. Will generate obb and mbs bounding volumes from geometry */
generateBoundingVolumes: boolean;
/** Validate the dataset during conversion. Validation messages will be posted in the console output */
validate: boolean;
/** Maximal depth of the hierarchical tiles tree traversal, default: infinite */
maxDepth?: number;
/** adds hash file to the slpk if there's no one */
addHash: boolean;
/** Feature metadata class from EXT_FEATURE_METADATA or EXT_STRUCTURAL_METADATA extensions */
metadataClass?: string;
/** With this options the tileset content will be analyzed without conversion */
analyze?: boolean;
/** Skip all prompts that stop conversion and wait for a user input */
quiet?: boolean;
};
/* During validation we check that particular options are defined so they can't be undefined */
type ValidatedTileConversionOptions = TileConversionOptions & {
/** "I3S" - for I3S to 3DTiles conversion, "3DTILES" for 3DTiles to I3S conversion */
inputType: string;
/** "tileset.json" file (3DTiles) / "http://..../SceneServer/layers/0" resource (I3S) */
tileset: string;
/** Tileset name. This option is used for naming in resulting json resouces and for resulting path/*.slpk file naming */
name: string;
};
const TILESET_TYPE = {
I3S: 'I3S',
_3DTILES: '3DTILES'
};
/**
* CLI entry
* @returns
*/
// eslint-disable-next-line max-statements
async function main() {
const [, , ...args] = process.argv;
if (args.length === 0) {
printHelp();
}
const validatedOptionsArr = validateOptionsWithEqual(args);
const options: TileConversionOptions = parseOptions(validatedOptionsArr);
if (options.installDependencies) {
const depthInstaller = new DepsInstaller();
depthInstaller.install('deps');
return;
}
if (options.addHash) {
const validatedOptions = validateOptions(options, true);
let finalPath = validatedOptions.tileset;
if (!options.quiet) {
if (validatedOptions.output === 'data') {
const nameWithoutExt = validatedOptions.tileset.substring(
0,
validatedOptions.tileset.length - 5
);
const result = await inquirer.prompt<{isNewFileRequired: boolean}>([
{
name: 'isNewFileRequired',
type: 'list',
message: 'What would you like to do?',
choices: [
{
name: 'Add hash file to the current SLPK file',
value: false
},
{
name: `Create a new file ${nameWithoutExt}-hash.slpk with hash file inside`,
value: true
}
]
}
]);
if (result.isNewFileRequired) {
finalPath = `${nameWithoutExt}-hash.slpk`;
}
} else {
finalPath = validatedOptions.output;
}
}
if (finalPath !== validatedOptions.tileset) {
await copyFile(validatedOptions.tileset, finalPath);
}
const hashTable = await composeHashFile(makeZipCDHeaderIterator(new FileHandleFile(finalPath)));
await addOneFile(finalPath, hashTable, '@specialIndexFileHASH128@');
return;
}
const validatedOptions: ValidatedTileConversionOptions = validateOptions(options);
await convert(validatedOptions);
}
main().catch((error) => {
console.log(error);
process.exit(1); // eslint-disable-line no-process-exit
});
/**
* Output for `npx tile-converter --help`
*/
function printHelp(): void {
console.log('cli: converter 3dTiles to I3S or I3S to 3dTiles...');
console.log(
'--install-dependencies [Run the script for installing dependencies. Run this options separate from others. Now "*.pgm" file installation is implemented]'
);
console.log(
'--max-depth [Maximal depth of hierarchical tiles tree traversal, default: infinite]'
);
console.log('--name [Tileset name]');
console.log('--output [Output folder, default: "data" folder]');
console.log(
'--instant-node-writing [Keep created 3DNodeIndexDocument files on disk instead of memory. This option reduce memory usage but decelerates conversion speed]'
);
console.log(
'--split-nodes [Prevent to merge similar materials that could lead to incorrect visualization (I3S to 3DTiles conversion only)]'
);
console.log(
'--tileset [tileset.json file (3DTiles) / http://..../SceneServer/layers/0 resource (I3S)]'
);
console.log('--input-type [tileset input type: I3S or 3DTILES]');
console.log(
'--output-version [3dtile version: 1.0 or 1.1, default: 1.1]. This option supports only 1.0/1.1 values for 3DTiles output. I3S output version setting is not supported yet.'
);
console.log(
'--egm [location of Earth Gravity Model *.pgm file to convert heights from ellipsoidal to gravity-related format or "None" to not use it. A model file can be loaded from GeographicLib https://geographiclib.sourceforge.io/html/geoid.html], default: "./deps/egm2008-5.zip"'
);
console.log('--token [Token for Cesium ION tilesets authentication]');
console.log('--no-draco [Disable draco compression for geometry]');
console.log(
'--generate-textures [Enable KTX2 textures generation if only one of (JPG, PNG) texture is provided or generate JPG texture if only KTX2 is provided]'
);
console.log('--generate-bounding-volumes [Generate obb and mbs bounding volumes from geometry]');
console.log('--analyze [Analyze the input tileset content without conversion, default: false]');
console.log(
'--metadata-class [One of the list of feature metadata classes, detected by converter on "analyze" stage, default: not set]'
);
console.log('--validate [Enable validation]');
console.log(
'--quiet [Skip all prompts that stop conversion and wait for a user input: default: false]'
);
process.exit(0); // eslint-disable-line
}
/**
* Run conversion process
* @param options validated tile-converter options
*/
async function convert(options: ValidatedTileConversionOptions) {
console.log(`------------------------------------------------`); // eslint-disable-line
console.log(`Starting conversion of ${options.inputType}`); // eslint-disable-line
console.log(`------------------------------------------------`); // eslint-disable-line
const inputType = options.inputType.toUpperCase();
switch (inputType) {
case TILESET_TYPE.I3S:
const tiles3DConverter = new Tiles3DConverter();
await tiles3DConverter.convert({
inputUrl: options.tileset,
outputPath: options.output,
outputVersion: options.outputVersion,
tilesetName: options.name,
maxDepth: options.maxDepth,
egmFilePath: options.egm,
analyze: options.analyze,
inquirer: options.quiet ? undefined : inquirer
});
break;
case TILESET_TYPE._3DTILES:
const converter = new I3SConverter();
await converter.convert({
inputUrl: options.tileset,
outputPath: options.output,
tilesetName: options.name,
maxDepth: options.maxDepth,
egmFilePath: options.egm,
token: options.token,
draco: options.draco,
mergeMaterials: options.mergeMaterials,
generateTextures: options.generateTextures,
generateBoundingVolumes: options.generateBoundingVolumes,
validate: options.validate,
instantNodeWriting: options.instantNodeWriting,
metadataClass: options.metadataClass,
analyze: options.analyze,
inquirer: options.quiet ? undefined : inquirer
});
break;
default:
printHelp();
}
}
// OPTIONS
/**
* Validate input options of the CLI command
* @param options - input options of the CLI command
* @returns validated options
*/
function validateOptions(
options: TileConversionOptions,
addHash?: boolean
): ValidatedTileConversionOptions {
const mandatoryOptionsWithExceptions: {
[key: string]: {
getMessage: () => void;
condition?: (optionValue: any) => boolean;
};
} = {
name: {
getMessage: () => console.log('Missed: --name [Tileset name]'),
condition: (value: any) => addHash || Boolean(value) || Boolean(options.analyze)
},
output: {getMessage: () => console.log('Missed: --output [Output path name]')},
egm: {getMessage: () => console.log('Missed: --egm [*.pgm earth gravity model file path]')},
tileset: {getMessage: () => console.log('Missed: --tileset [tileset.json file]')},
inputType: {
getMessage: () =>
console.log('Missed/Incorrect: --input-type [tileset input type: I3S or 3DTILES]'),
condition: (value) =>
addHash || (Boolean(value) && Object.values(TILESET_TYPE).includes(value.toUpperCase()))
},
outputVersion: {
getMessage: () =>
console.log('Incorrect: --output-version [1.0 or 1.1] is for --input-type "I3S" only'),
condition: (value) =>
addHash ||
(Boolean(value) &&
Object.values(['1.0', '1.1']).includes(value) &&
Boolean(options.inputType === 'I3S')) ||
Boolean(options.inputType !== 'I3S') ||
Boolean(options.analyze)
}
};
const exceptions: (() => void)[] = [];
for (const mandatoryOption in mandatoryOptionsWithExceptions) {
const optionValue = options[mandatoryOption];
const conditionFunc = mandatoryOptionsWithExceptions[mandatoryOption].condition;
const testValue = conditionFunc ? conditionFunc(optionValue) : optionValue;
if (!testValue) {
exceptions.push(mandatoryOptionsWithExceptions[mandatoryOption].getMessage);
}
}
if (exceptions.length) {
exceptions.forEach((exeption) => exeption());
process.exit(1); // eslint-disable-line no-process-exit
}
return <ValidatedTileConversionOptions>options;
}
/**
* Parse option from the cli arguments array
* @param args
* @returns
*/
function parseOptions(args: string[]): TileConversionOptions {
const opts: TileConversionOptions = {
output: 'data',
outputVersion: '1.1',
instantNodeWriting: false,
mergeMaterials: true,
egm: join(process.cwd(), 'deps', 'egm2008-5.pgm'),
draco: true,
installDependencies: false,
generateTextures: false,
generateBoundingVolumes: false,
validate: false,
addHash: false,
quiet: false
};
// eslint-disable-next-line complexity
args.forEach((arg, index) => {
if (arg.indexOf('--') === 0) {
switch (arg) {
case '--input-type':
opts.inputType = getStringValue(index, args);
break;
case '--tileset':
opts.tileset = getURLValue(index, args);
break;
case '--name':
opts.name = getStringValue(index, args);
break;
case '--output':
opts.output = getStringValue(index, args);
break;
case '--output-version':
opts.outputVersion = getStringValue(index, args);
break;
case '--instant-node-writing':
opts.instantNodeWriting = getBooleanValue(index, args);
break;
case '--split-nodes':
opts.mergeMaterials = getBooleanValue(index, args);
break;
case '--max-depth':
opts.maxDepth = getIntegerValue(index, args);
break;
case '--add-hash':
opts.addHash = getBooleanValue(index, args);
break;
case '--egm':
opts.egm = getStringValue(index, args);
break;
case '--token':
opts.token = getStringValue(index, args);
break;
case '--no-draco':
opts.draco = getBooleanValue(index, args);
break;
case '--validate':
opts.validate = getBooleanValue(index, args);
break;
case '--install-dependencies':
opts.installDependencies = getBooleanValue(index, args);
break;
case '--generate-textures':
opts.generateTextures = getBooleanValue(index, args);
break;
case '--generate-bounding-volumes':
opts.generateBoundingVolumes = getBooleanValue(index, args);
break;
case '--analyze':
opts.analyze = getBooleanValue(index, args);
break;
case '--quiet':
opts.quiet = getBooleanValue(index, args);
break;
case '--metadata-class':
opts.metadataClass = getStringValue(index, args);
break;
case '--help':
printHelp();
break;
// we need this option for backward compatibility
// do nothing but don't throw the error
case '--slpk':
break;
default:
console.warn(`Unknown option ${arg}`);
process.exit(0); // eslint-disable-line
}
}
});
return opts;
}