fix: simplify input file loading

Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
Andrew Calcutt 2023-10-08 12:13:41 -04:00
parent 9cebc4459c
commit 18b261c4ad

View file

@ -80,26 +80,21 @@ const startServer = (configPath, config) => {
}); });
}; };
const startWithPMTiles = async (pmtilesFile) => { const startWithInputFile = async (inputfile) => {
console.log(`[INFO] Automatically creating config file for ${pmtilesFile}`); console.log(`[INFO] Automatically creating config file for ${inputfile}`);
console.log(`[INFO] Only a basic preview style will be used.`); console.log(`[INFO] Only a basic preview style will be used.`);
console.log( console.log(
`[INFO] See documentation to learn how to create config.json file.`, `[INFO] See documentation to learn how to create config.json file.`,
); );
pmtilesFile = path.resolve(process.cwd(), pmtilesFile); inputfile = path.resolve(process.cwd(), inputfile);
const pmtilesStats = fs.statSync(pmtilesFile); const tilesStats = fs.statSync(inputfile);
if (!pmtilesStats.isFile() || pmtilesStats.size === 0) { if (!tilesStats.isFile() || tilesStats.size === 0) {
console.log(`ERROR: Not valid pmtiles file: ${pmtilesFile}`); console.log(`ERROR: Not valid MBTiles or PMTiles file: ${inputfile}`);
process.exit(1); process.exit(1);
} }
const info = await GetPMtilesInfo(pmtilesFile);
const metadata = info.metadata;
console.log(info);
const styleDir = path.resolve( const styleDir = path.resolve(
__dirname, __dirname,
'../node_modules/tileserver-gl-styles/', '../node_modules/tileserver-gl-styles/',
@ -111,150 +106,123 @@ const startWithPMTiles = async (pmtilesFile) => {
root: styleDir, root: styleDir,
fonts: 'fonts', fonts: 'fonts',
styles: 'styles', styles: 'styles',
mbtiles: path.dirname(pmtilesFile), mbtiles: path.dirname(inputfile),
}, },
}, },
styles: {}, styles: {},
data: {}, data: {},
}; };
if ( const extension = inputfile.split('.').pop().toLowerCase();
metadata.format === 'pbf' && if (extension === 'pmtiles') {
metadata.name.toLowerCase().indexOf('openmaptiles') > -1 const info = await GetPMtilesInfo(inputfile);
) { const metadata = info.metadata;
config['data'][`v3`] = {
mbtiles: path.basename(pmtilesFile), if (
}; metadata.format === 'pbf' &&
metadata.name.toLowerCase().indexOf('openmaptiles') > -1
const styles = fs.readdirSync(path.resolve(styleDir, 'styles')); ) {
for (const styleName of styles) { config['data'][`v3`] = {
const styleFileRel = styleName + '/style.json'; mbtiles: path.basename(inputfile),
const styleFile = path.resolve(styleDir, 'styles', styleFileRel); };
if (fs.existsSync(styleFile)) {
config['styles'][styleName] = { const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
style: styleFileRel, for (const styleName of styles) {
tilejson: { const styleFileRel = styleName + '/style.json';
bounds: metadata.bounds, const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
}, if (fs.existsSync(styleFile)) {
}; config['styles'][styleName] = {
style: styleFileRel,
tilejson: {
bounds: metadata.bounds,
},
};
}
} }
} else {
console.log(
`WARN: PMTiles not in "openmaptiles" format. Serving raw data only...`,
);
config['data'][
(metadata.id || 'mbtiles')
.replace(/\//g, '_')
.replace(/:/g, '_')
.replace(/\?/g, '_')
] = {
mbtiles: path.basename(inputfile),
};
} }
} else {
console.log( if (opts.verbose) {
`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`, console.log(JSON.stringify(config, undefined, 2));
); } else {
config['data'][ console.log('Run with --verbose to see the config file here.');
(metadata.id || 'mbtiles')
.replace(/\//g, '_')
.replace(/:/g, '_')
.replace(/\?/g, '_')
] = {
mbtiles: path.basename(pmtilesFile),
};
}
if (opts.verbose) {
console.log(JSON.stringify(config, undefined, 2));
} else {
console.log('Run with --verbose to see the config file here.');
}
return startServer(null, config);
};
const startWithMBTiles = (mbtilesFile) => {
console.log(`[INFO] Automatically creating config file for ${mbtilesFile}`);
console.log(`[INFO] Only a basic preview style will be used.`);
console.log(
`[INFO] See documentation to learn how to create config.json file.`,
);
mbtilesFile = path.resolve(process.cwd(), mbtilesFile);
const mbtilesStats = fs.statSync(mbtilesFile);
if (!mbtilesStats.isFile() || mbtilesStats.size === 0) {
console.log(`ERROR: Not valid MBTiles file: ${mbtilesFile}`);
process.exit(1);
}
const instance = new MBTiles(mbtilesFile + '?mode=ro', (err) => {
if (err) {
console.log('ERROR: Unable to open MBTiles.');
console.log(`Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`);
process.exit(1);
} }
instance.getInfo((err, info) => { return startServer(null, config);
if (err || !info) { } else {
console.log('ERROR: Metadata missing in the MBTiles.'); const instance = new MBTiles(inputfile + '?mode=ro', (err) => {
console.log( if (err) {
`Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`, console.log('ERROR: Unable to open MBTiles.');
); console.log(`Make sure ${path.basename(inputfile)} is valid MBTiles.`);
process.exit(1); process.exit(1);
} }
const bounds = info.bounds;
instance.getInfo((err, info) => {
const styleDir = path.resolve( if (err || !info) {
__dirname, console.log('ERROR: Metadata missing in the MBTiles.');
'../node_modules/tileserver-gl-styles/', console.log(
); `Make sure ${path.basename(inputfile)} is valid MBTiles.`,
);
const config = { process.exit(1);
options: {
paths: {
root: styleDir,
fonts: 'fonts',
styles: 'styles',
mbtiles: path.dirname(mbtilesFile),
},
},
styles: {},
data: {},
};
if (
info.format === 'pbf' &&
info.name.toLowerCase().indexOf('openmaptiles') > -1
) {
config['data'][`v3`] = {
mbtiles: path.basename(mbtilesFile),
};
const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
for (const styleName of styles) {
const styleFileRel = styleName + '/style.json';
const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
if (fs.existsSync(styleFile)) {
config['styles'][styleName] = {
style: styleFileRel,
tilejson: {
bounds: bounds,
},
};
}
} }
} else { const bounds = info.bounds;
console.log(
`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`, if (
); info.format === 'pbf' &&
config['data'][ info.name.toLowerCase().indexOf('openmaptiles') > -1
(info.id || 'mbtiles') ) {
.replace(/\//g, '_') config['data'][`v3`] = {
.replace(/:/g, '_') mbtiles: path.basename(inputfile),
.replace(/\?/g, '_') };
] = {
mbtiles: path.basename(mbtilesFile), const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
}; for (const styleName of styles) {
} const styleFileRel = styleName + '/style.json';
const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
if (opts.verbose) { if (fs.existsSync(styleFile)) {
console.log(JSON.stringify(config, undefined, 2)); config['styles'][styleName] = {
} else { style: styleFileRel,
console.log('Run with --verbose to see the config file here.'); tilejson: {
} bounds: bounds,
},
return startServer(null, config); };
}
}
} else {
console.log(
`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`,
);
config['data'][
(info.id || 'mbtiles')
.replace(/\//g, '_')
.replace(/:/g, '_')
.replace(/\?/g, '_')
] = {
mbtiles: path.basename(inputfile),
};
}
if (opts.verbose) {
console.log(JSON.stringify(config, undefined, 2));
} else {
console.log('Run with --verbose to see the config file here.');
}
return startServer(null, config);
});
}); });
}); }
}; };
fs.stat(path.resolve(opts.config), (err, stats) => { fs.stat(path.resolve(opts.config), (err, stats) => {
@ -266,7 +234,9 @@ fs.stat(path.resolve(opts.config), (err, stats) => {
inputfile = opts.mbtiles; inputfile = opts.mbtiles;
} }
if (!inputfile) { if (inputfile) {
return startWithInputFile(inputfile);
} else {
// try to find in the cwd // try to find in the cwd
const files = fs.readdirSync(process.cwd()); const files = fs.readdirSync(process.cwd());
for (const filename of files) { for (const filename of files) {
@ -280,12 +250,7 @@ fs.stat(path.resolve(opts.config), (err, stats) => {
} }
if (inputfile) { if (inputfile) {
console.log(`No input file specified, using ${inputfile}`); console.log(`No input file specified, using ${inputfile}`);
const extension = inputfile.split('.').pop().toLowerCase(); return startWithInputFile(inputfile);
if (extension === 'pmtiles') {
return startWithPMTiles(inputfile);
} else {
return startWithMBTiles(inputfile);
}
} else { } else {
const url = const url =
'https://github.com/maptiler/tileserver-gl/releases/download/v1.3.0/zurich_switzerland.mbtiles'; 'https://github.com/maptiler/tileserver-gl/releases/download/v1.3.0/zurich_switzerland.mbtiles';
@ -293,18 +258,10 @@ fs.stat(path.resolve(opts.config), (err, stats) => {
const stream = fs.createWriteStream(filename); const stream = fs.createWriteStream(filename);
console.log(`No input file found`); console.log(`No input file found`);
console.log(`[DEMO] Downloading sample data (${filename}) from ${url}`); console.log(`[DEMO] Downloading sample data (${filename}) from ${url}`);
stream.on('finish', () => startWithMBTiles(filename)); stream.on('finish', () => startWithInputFile(filename));
return request.get(url).pipe(stream); return request.get(url).pipe(stream);
} }
} }
if (inputfile) {
const extension = inputfile.split('.').pop().toLowerCase();
if (extension === 'pmtiles') {
return startWithPMTiles(inputfile);
} else {
return startWithMBTiles(inputfile);
}
}
} else { } else {
console.log(`Using specified config file from ${opts.config}`); console.log(`Using specified config file from ${opts.config}`);
return startServer(opts.config, null); return startServer(opts.config, null);