feat: add --file option to replace mbtiles

Get mbtiles load working again

Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
Andrew Calcutt 2023-10-07 19:42:01 -04:00
parent 9e76cc4a5e
commit c295ae4b6a

View file

@ -25,9 +25,15 @@ import { program } from 'commander';
program program
.description('tileserver-gl startup options') .description('tileserver-gl startup options')
.usage('tileserver-gl [mbtiles] [options]') .usage('tileserver-gl [mbtiles] [options]')
.option(
'--file <file>',
'MBTiles or PMTiles file\n' +
'\t ignored if the configuration file is also specified',
)
.option( .option(
'--mbtiles <file>', '--mbtiles <file>',
'MBTiles file (uses demo configuration);\n' + '(DEPRECIATED) MBTiles file (uses demo configuration);\n' +
'\t ignored if file is also specified'+
'\t ignored if the configuration file is also specified', '\t ignored if the configuration file is also specified',
) )
.option( .option(
@ -266,35 +272,51 @@ const startWithMBTiles = (mbtilesFile) => {
fs.stat(path.resolve(opts.config), (err, stats) => { fs.stat(path.resolve(opts.config), (err, stats) => {
if (err || !stats.isFile() || stats.size === 0) { if (err || !stats.isFile() || stats.size === 0) {
let mbtiles = opts.mbtiles; let inputfile
if (!mbtiles) { if(opts.file) {
inputfile = opts.file;
} else if (opts.mbtiles){
inputfile = opts.mbtiles;
}
if (!inputfile) {
// 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) {
if (filename.endsWith('.mbtiles')) { if (filename.endsWith('.mbtiles') || filename.endsWith('.pmtiles')) {
const mbTilesStats = fs.statSync(filename); const InputFilesStats = fs.statSync(filename);
if (mbTilesStats.isFile() && mbTilesStats.size > 0) { if (InputFilesStats.isFile() && InputFilesStats.size > 0) {
mbtiles = filename; inputfile = filename;
break; break;
} }
} }
} }
if (mbtiles) { if (inputfile) {
console.log(`No MBTiles specified, using ${mbtiles}`); console.log(`No input file specified, using ${inputfile}`);
return startWithPMTiles(mbtiles); const extension = inputfile.split('.').pop().toLowerCase();
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';
const filename = 'zurich_switzerland.mbtiles'; const filename = 'zurich_switzerland.mbtiles';
const stream = fs.createWriteStream(filename); const stream = fs.createWriteStream(filename);
console.log(`No MBTiles 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', () => startWithMBTiles(filename));
return request.get(url).pipe(stream); return request.get(url).pipe(stream);
} }
} }
if (mbtiles) { if (inputfile) {
return startWithPMTiles(mbtiles); 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}`);