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),
};
const styles = fs.readdirSync(path.resolve(styleDir, 'styles')); if (
for (const styleName of styles) { metadata.format === 'pbf' &&
const styleFileRel = styleName + '/style.json'; metadata.name.toLowerCase().indexOf('openmaptiles') > -1
const styleFile = path.resolve(styleDir, 'styles', styleFileRel); ) {
if (fs.existsSync(styleFile)) { config['data'][`v3`] = {
config['styles'][styleName] = { mbtiles: path.basename(inputfile),
style: styleFileRel,
tilejson: {
bounds: metadata.bounds,
},
};
}
}
} else {
console.log(
`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`,
);
config['data'][
(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) => {
if (err || !info) {
console.log('ERROR: Metadata missing in the MBTiles.');
console.log(
`Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`,
);
process.exit(1);
}
const bounds = info.bounds;
const styleDir = path.resolve(
__dirname,
'../node_modules/tileserver-gl-styles/',
);
const config = {
options: {
paths: {
root: styleDir,
fonts: 'fonts',
styles: 'styles',
mbtiles: path.dirname(mbtilesFile),
},
},
styles: {},
data: {},
}; };
if ( const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
info.format === 'pbf' && for (const styleName of styles) {
info.name.toLowerCase().indexOf('openmaptiles') > -1 const styleFileRel = styleName + '/style.json';
) { const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
config['data'][`v3`] = { if (fs.existsSync(styleFile)) {
mbtiles: path.basename(mbtilesFile), config['styles'][styleName] = {
}; style: styleFileRel,
tilejson: {
const styles = fs.readdirSync(path.resolve(styleDir, 'styles')); bounds: metadata.bounds,
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 { }
console.log( } else {
`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`, console.log(
); `WARN: PMTiles not in "openmaptiles" format. Serving raw data only...`,
config['data'][ );
(info.id || 'mbtiles') config['data'][
.replace(/\//g, '_') (metadata.id || 'mbtiles')
.replace(/:/g, '_') .replace(/\//g, '_')
.replace(/\?/g, '_') .replace(/:/g, '_')
] = { .replace(/\?/g, '_')
mbtiles: path.basename(mbtilesFile), ] = {
}; 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);
} else {
const instance = new MBTiles(inputfile + '?mode=ro', (err) => {
if (err) {
console.log('ERROR: Unable to open MBTiles.');
console.log(`Make sure ${path.basename(inputfile)} is valid MBTiles.`);
process.exit(1);
} }
if (opts.verbose) { instance.getInfo((err, info) => {
console.log(JSON.stringify(config, undefined, 2)); if (err || !info) {
} else { console.log('ERROR: Metadata missing in the MBTiles.');
console.log('Run with --verbose to see the config file here.'); console.log(
} `Make sure ${path.basename(inputfile)} is valid MBTiles.`,
);
process.exit(1);
}
const bounds = info.bounds;
return startServer(null, config); if (
info.format === 'pbf' &&
info.name.toLowerCase().indexOf('openmaptiles') > -1
) {
config['data'][`v3`] = {
mbtiles: path.basename(inputfile),
};
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 {
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);