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 { VectorTile } from '@mapbox/vector-tile';
import { getTileUrls, fixTileJSONCenter } from './utils.js';
import { getTileUrls, fixTileJSONCenter, GetPMtilesInfo } from './utils.js';
export const serve_data = {
init: (options, repo) => {
@ -137,7 +137,7 @@ export const serve_data = {
return app;
},
add: (options, repo, params, id, publicUrl) => {
add: async (options, repo, params, id, publicUrl) => {
const mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles);
let tileJSON = {
tiles: params.domains || options.domains,
@ -147,7 +147,32 @@ export const serve_data = {
if (!mbtilesFileStats.isFile() || mbtilesFileStats.size === 0) {
throw Error(`Not valid MBTiles file: ${mbtilesFile}`);
}
const extension = mbtilesFile.split('.').pop().toLowerCase();
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) => {
source = new MBTiles(mbtilesFile + '?mode=ro', (err) => {
if (err) {
@ -180,12 +205,15 @@ export const serve_data = {
});
});
return sourceInfoPromise.then(() => {
await sourceInfoPromise;
}
console.log(tileJSON);
repo[id] = {
tileJSON,
publicUrl,
source,
};
});
},
};

View file

@ -191,5 +191,6 @@ export const GetPMtilesInfo = async (pmtilesFile) => {
const header = await pmtiles.getHeader();
const metadata = await pmtiles.getMetadata();
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 };
}