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,7 +147,32 @@ 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;
if (extension === 'pmtiles') {
const info = await GetPMtilesInfo(mbtilesFile);
const metadata = info.metadata
source = info.source
tileJSON['name'] = id;
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) => { const sourceInfoPromise = new Promise((resolve, reject) => {
source = new MBTiles(mbtilesFile + '?mode=ro', (err) => { source = new MBTiles(mbtilesFile + '?mode=ro', (err) => {
if (err) { if (err) {
@ -180,12 +205,15 @@ export const serve_data = {
}); });
}); });
return sourceInfoPromise.then(() => { await sourceInfoPromise;
}
console.log(tileJSON);
repo[id] = { repo[id] = {
tileJSON, tileJSON,
publicUrl, publicUrl,
source, 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 };
} }