feat: move pmtiles reading to utils

Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
Andrew Calcutt 2023-10-06 18:28:29 -04:00
parent ccd1dbf343
commit 8031f94ca4
2 changed files with 37 additions and 18 deletions

View file

@ -7,9 +7,9 @@ import path from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import request from 'request'; import request from 'request';
import { server } from './server.js'; import { server } from './server.js';
import { GetPMtilesInfo } from './utils.js';
import MBTiles from '@mapbox/mbtiles'; import MBTiles from '@mapbox/mbtiles';
import PMTiles from 'pmtiles';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
@ -102,22 +102,11 @@ const startWithPMTiles = async (pmtilesFile) => {
console.log(`ERROR: Not valid pmtiles file: ${pmtilesFile}`); console.log(`ERROR: Not valid pmtiles file: ${pmtilesFile}`);
process.exit(1); process.exit(1);
} }
const buffer = fs.readFileSync(pmtilesFile)
const arrayBuffer = new ArrayBuffer(buffer.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
let source = new PMTilesLocalSource(arrayBuffer); const info = await GetPMtilesInfo(pmtilesFile);
let pmtiles = new PMTiles.PMTiles(source); const metadata = info.metadata
const header = await pmtiles.getHeader();
const info = await pmtiles.getMetadata();
const bounds = [header.minLat, header.minLon, header.maxLat, header.maxLon]
console.log(header);
console.log(info); console.log(info);
console.log(bounds);
const styleDir = path.resolve( const styleDir = path.resolve(
__dirname, __dirname,
@ -138,8 +127,8 @@ const startWithPMTiles = async (pmtilesFile) => {
}; };
if ( if (
info.format === 'pbf' && metadata.format === 'pbf' &&
info.name.toLowerCase().indexOf('openmaptiles') > -1 metadata.name.toLowerCase().indexOf('openmaptiles') > -1
) { ) {
config['data'][`v3`] = { config['data'][`v3`] = {
mbtiles: path.basename(pmtilesFile), mbtiles: path.basename(pmtilesFile),
@ -153,7 +142,7 @@ const startWithPMTiles = async (pmtilesFile) => {
config['styles'][styleName] = { config['styles'][styleName] = {
style: styleFileRel, style: styleFileRel,
tilejson: { tilejson: {
bounds: bounds, bounds: info.bounds,
}, },
}; };
} }
@ -163,7 +152,7 @@ const startWithPMTiles = async (pmtilesFile) => {
`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`, `WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`,
); );
config['data'][ config['data'][
(info.id || 'mbtiles') (metadata.id || 'mbtiles')
.replace(/\//g, '_') .replace(/\//g, '_')
.replace(/:/g, '_') .replace(/:/g, '_')
.replace(/\?/g, '_') .replace(/\?/g, '_')

View file

@ -5,6 +5,7 @@ import fs from 'node:fs';
import clone from 'clone'; import clone from 'clone';
import glyphCompose from '@mapbox/glyph-pbf-composite'; import glyphCompose from '@mapbox/glyph-pbf-composite';
import PMTiles from 'pmtiles';
/** /**
* Generate new URL object * Generate new URL object
@ -163,3 +164,32 @@ export const getFontsPbf = (
return Promise.all(queue).then((values) => glyphCompose.combine(values)); return Promise.all(queue).then((values) => glyphCompose.combine(values));
}; };
const PMTilesLocalSource = class {
constructor(file) {
this.file = file;
}
getKey() {
return this.file.name;
}
async getBytes(offset, length) {
const blob = this.file.slice(offset, offset + length);
return { data: blob };
}
};
export const GetPMtilesInfo = async (pmtilesFile) => {
const buffer = fs.readFileSync(pmtilesFile)
const arrayBuffer = new ArrayBuffer(buffer.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
let source = new PMTilesLocalSource(arrayBuffer);
let pmtiles = new PMTiles.PMTiles(source);
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 };
}