refactor: rework so remote pmtiles may be possible
Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
parent
ac87447f29
commit
f13d6e52d9
4 changed files with 40 additions and 35 deletions
13
src/main.js
13
src/main.js
|
|
@ -8,11 +8,7 @@ import { fileURLToPath } from 'url';
|
|||
import request from 'request';
|
||||
import { server } from './server.js';
|
||||
import MBTiles from '@mapbox/mbtiles';
|
||||
import {
|
||||
PMtilesOpen,
|
||||
PMtilesClose,
|
||||
GetPMtilesInfo,
|
||||
} from './pmtiles_adapter.js';
|
||||
import { PMtilesOpen, PMtilesClose, GetPMtilesInfo } from './pmtiles_adapter.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
|
@ -120,10 +116,11 @@ const startWithinputFile = async (inputFile) => {
|
|||
|
||||
const extension = inputFile.split('.').pop().toLowerCase();
|
||||
if (extension === 'pmtiles') {
|
||||
const FileDescriptor = PMtilesOpen(inputFile);
|
||||
const info = await GetPMtilesInfo(FileDescriptor);
|
||||
let FileOpenInfo = PMtilesOpen(inputFile);
|
||||
const info = await GetPMtilesInfo(FileOpenInfo.pmtiles);
|
||||
const metadata = info.metadata;
|
||||
PMtilesClose(FileDescriptor);
|
||||
if (FileOpenInfo.fd !== undefined) {PMtilesClose(FileOpenInfo.fd);}
|
||||
FileOpenInfo = null;
|
||||
|
||||
if (
|
||||
metadata.format === 'pbf' &&
|
||||
|
|
|
|||
|
|
@ -2,15 +2,25 @@ import fs from 'node:fs';
|
|||
import PMTiles from 'pmtiles';
|
||||
|
||||
export const PMtilesOpen = (FilePath) => {
|
||||
const fd = fs.openSync(FilePath, 'r');
|
||||
return fd;
|
||||
let pmtiles = undefined;
|
||||
let fd = undefined;
|
||||
|
||||
if(isValidHttpUrl(FilePath)) {
|
||||
const source = new PMTiles.FetchSource(FilePath)
|
||||
pmtiles = new PMTiles.PMTiles(source);
|
||||
} else {
|
||||
fd = fs.openSync(FilePath, 'r');
|
||||
const source = new PMTilesFileSource(fd);
|
||||
pmtiles = new PMTiles.PMTiles(source);
|
||||
}
|
||||
return { pmtiles: pmtiles, fd: fd };
|
||||
};
|
||||
|
||||
export const PMtilesClose = (fd) => {
|
||||
fs.closeSync(fd);
|
||||
};
|
||||
|
||||
const PMTilesFileDescriptorSource = class {
|
||||
const PMTilesFileSource = class {
|
||||
constructor(fd) {
|
||||
this.fd = fd;
|
||||
}
|
||||
|
|
@ -35,9 +45,7 @@ const ReadBytes = async (fd, buffer, offset) => {
|
|||
});
|
||||
};
|
||||
|
||||
export const GetPMtilesInfo = async (fd) => {
|
||||
const source = new PMTilesFileDescriptorSource(fd);
|
||||
const pmtiles = new PMTiles.PMTiles(source);
|
||||
export const GetPMtilesInfo = async (pmtiles) => {
|
||||
const header = await pmtiles.getHeader();
|
||||
const metadata = await pmtiles.getMetadata();
|
||||
|
||||
|
|
@ -54,9 +62,7 @@ export const GetPMtilesInfo = async (fd) => {
|
|||
return { header: header, metadata: metadata };
|
||||
};
|
||||
|
||||
export const GetPMtilesTile = async (fd, z, x, y) => {
|
||||
const source = new PMTilesFileDescriptorSource(fd);
|
||||
const pmtiles = new PMTiles.PMTiles(source);
|
||||
export const GetPMtilesTile = async (pmtiles, z, x, y) => {
|
||||
const header = await pmtiles.getHeader();
|
||||
const TileType = GetPmtilesTileType(header.tileType);
|
||||
let zxyTile = await pmtiles.getZxy(z, x, y);
|
||||
|
|
@ -116,3 +122,15 @@ const ArrayBufferToBuffer = (array_buffer) => {
|
|||
}
|
||||
return buffer;
|
||||
};
|
||||
|
||||
function isValidHttpUrl(string) {
|
||||
let url;
|
||||
|
||||
try {
|
||||
url = new URL(string);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return url.protocol === "http:" || url.protocol === "https:";
|
||||
}
|
||||
|
|
@ -11,12 +11,7 @@ import Pbf from 'pbf';
|
|||
import { VectorTile } from '@mapbox/vector-tile';
|
||||
|
||||
import { getTileUrls, fixTileJSONCenter } from './utils.js';
|
||||
import {
|
||||
PMtilesOpen,
|
||||
PMtilesClose,
|
||||
GetPMtilesInfo,
|
||||
GetPMtilesTile,
|
||||
} from './pmtiles_adapter.js';
|
||||
import { PMtilesOpen, GetPMtilesInfo, GetPMtilesTile } from './pmtiles_adapter.js';
|
||||
|
||||
export const serve_data = {
|
||||
init: (options, repo) => {
|
||||
|
|
@ -214,10 +209,10 @@ export const serve_data = {
|
|||
let source;
|
||||
let source_type;
|
||||
if (inputType === 'pmtiles') {
|
||||
const FileDescriptor = PMtilesOpen(inputFile);
|
||||
const info = await GetPMtilesInfo(FileDescriptor);
|
||||
let FileOpenInfo = PMtilesOpen(inputFile);
|
||||
const info = await GetPMtilesInfo(FileOpenInfo.pmtiles);
|
||||
const metadata = info.metadata;
|
||||
source = FileDescriptor;
|
||||
source = FileOpenInfo.pmtiles;
|
||||
source_type = 'pmtiles';
|
||||
|
||||
tileJSON['name'] = id;
|
||||
|
|
|
|||
|
|
@ -19,12 +19,7 @@ import polyline from '@mapbox/polyline';
|
|||
import proj4 from 'proj4';
|
||||
import request from 'request';
|
||||
import { getFontsPbf, getTileUrls, fixTileJSONCenter } from './utils.js';
|
||||
import {
|
||||
PMtilesOpen,
|
||||
PMtilesClose,
|
||||
GetPMtilesInfo,
|
||||
GetPMtilesTile,
|
||||
} from './pmtiles_adapter.js';
|
||||
import { PMtilesOpen, GetPMtilesInfo, GetPMtilesTile } from './pmtiles_adapter.js';
|
||||
|
||||
const FLOAT_PATTERN = '[+-]?(?:\\d+|\\d+.?\\d+)';
|
||||
const PATH_PATTERN =
|
||||
|
|
@ -1496,10 +1491,10 @@ export const serve_rendered = {
|
|||
}
|
||||
|
||||
if (source_type === 'pmtiles') {
|
||||
let FileDescriptor = PMtilesOpen(inputFile);
|
||||
const info = await GetPMtilesInfo(FileDescriptor);
|
||||
let FileOpenInfo = PMtilesOpen(inputFile);
|
||||
const info = await GetPMtilesInfo(FileOpenInfo.pmtiles);
|
||||
const metadata = info.metadata;
|
||||
map.sources[metadata.name.toLowerCase()] = FileDescriptor;
|
||||
map.sources[metadata.name.toLowerCase()] = FileOpenInfo.pmtiles;
|
||||
map.source_types[metadata.name.toLowerCase()] = 'pmtiles';
|
||||
|
||||
if (!repoobj.dataProjWGStoInternalWGS && metadata.proj4) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue