feat: generate tilejson from pmtiles

Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
Andrew Calcutt 2023-10-06 19:58:25 -04:00
parent 8031f94ca4
commit 3df423e55f
2 changed files with 60 additions and 31 deletions

View file

@ -10,7 +10,7 @@ import MBTiles from '@mapbox/mbtiles';
import Pbf from 'pbf'; import Pbf from 'pbf';
import { VectorTile } from '@mapbox/vector-tile'; import { VectorTile } from '@mapbox/vector-tile';
import { getTileUrls, fixTileJSONCenter } from './utils.js'; import { getTileUrls, fixTileJSONCenter, GetPMtilesInfo } from './utils.js';
export const serve_data = { export const serve_data = {
init: (options, repo) => { init: (options, repo) => {
@ -137,7 +137,7 @@ export const serve_data = {
return app; return app;
}, },
add: (options, repo, params, id, publicUrl) => { add: async (options, repo, params, id, publicUrl) => {
const mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles); const mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles);
let tileJSON = { let tileJSON = {
tiles: params.domains || options.domains, tiles: params.domains || options.domains,
@ -147,45 +147,73 @@ export const serve_data = {
if (!mbtilesFileStats.isFile() || mbtilesFileStats.size === 0) { if (!mbtilesFileStats.isFile() || mbtilesFileStats.size === 0) {
throw Error(`Not valid MBTiles file: ${mbtilesFile}`); throw Error(`Not valid MBTiles file: ${mbtilesFile}`);
} }
const extension = mbtilesFile.split('.').pop().toLowerCase();
let source; let source;
const sourceInfoPromise = new Promise((resolve, reject) => { if (extension === 'pmtiles') {
source = new MBTiles(mbtilesFile + '?mode=ro', (err) => { const info = await GetPMtilesInfo(mbtilesFile);
if (err) { const metadata = info.metadata
reject(err); source = info.source
return;
} tileJSON['name'] = id;
source.getInfo((err, info) => { tileJSON['format'] = 'pbf';
tileJSON['bounds'] = info.bounds;
tileJSON['center'] = info.center;
Object.assign(tileJSON, metadata);
tileJSON['tilejson'] = '2.0.0';
delete tileJSON['filesize'];
delete tileJSON['mtime'];
delete tileJSON['scheme'];
Object.assign(tileJSON, params.tilejson || {});
fixTileJSONCenter(tileJSON);
if (options.dataDecoratorFunc) {
tileJSON = options.dataDecoratorFunc(id, 'tilejson', tileJSON);
}
} else {
const sourceInfoPromise = new Promise((resolve, reject) => {
source = new MBTiles(mbtilesFile + '?mode=ro', (err) => {
if (err) { if (err) {
reject(err); reject(err);
return; return;
} }
tileJSON['name'] = id; source.getInfo((err, info) => {
tileJSON['format'] = 'pbf'; if (err) {
reject(err);
return;
}
tileJSON['name'] = id;
tileJSON['format'] = 'pbf';
Object.assign(tileJSON, info); Object.assign(tileJSON, info);
tileJSON['tilejson'] = '2.0.0'; tileJSON['tilejson'] = '2.0.0';
delete tileJSON['filesize']; delete tileJSON['filesize'];
delete tileJSON['mtime']; delete tileJSON['mtime'];
delete tileJSON['scheme']; delete tileJSON['scheme'];
Object.assign(tileJSON, params.tilejson || {}); Object.assign(tileJSON, params.tilejson || {});
fixTileJSONCenter(tileJSON); fixTileJSONCenter(tileJSON);
if (options.dataDecoratorFunc) { if (options.dataDecoratorFunc) {
tileJSON = options.dataDecoratorFunc(id, 'tilejson', tileJSON); tileJSON = options.dataDecoratorFunc(id, 'tilejson', tileJSON);
} }
resolve(); resolve();
});
}); });
}); });
});
return sourceInfoPromise.then(() => { await sourceInfoPromise;
repo[id] = { }
tileJSON,
publicUrl, console.log(tileJSON);
source,
}; repo[id] = {
}); tileJSON,
publicUrl,
source,
};
}, },
}; };

View file

@ -191,5 +191,6 @@ export const GetPMtilesInfo = async (pmtilesFile) => {
const header = await pmtiles.getHeader(); const header = await pmtiles.getHeader();
const metadata = await pmtiles.getMetadata(); const metadata = await pmtiles.getMetadata();
const bounds = [header.minLat, header.minLon, header.maxLat, header.maxLon] const bounds = [header.minLat, header.minLon, header.maxLat, header.maxLon]
return { header: header, metadata: metadata, bounds: bounds }; const center = [header.centerLon, header.centerLat, header.centerZoom]
return { source: pmtiles, header: header, metadata: metadata, bounds: bounds, center: center };
} }