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 request from 'request';
|
||||||
import { server } from './server.js';
|
import { server } from './server.js';
|
||||||
import MBTiles from '@mapbox/mbtiles';
|
import MBTiles from '@mapbox/mbtiles';
|
||||||
import {
|
import { PMtilesOpen, PMtilesClose, GetPMtilesInfo } from './pmtiles_adapter.js';
|
||||||
PMtilesOpen,
|
|
||||||
PMtilesClose,
|
|
||||||
GetPMtilesInfo,
|
|
||||||
} from './pmtiles_adapter.js';
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
@ -120,10 +116,11 @@ const startWithinputFile = async (inputFile) => {
|
||||||
|
|
||||||
const extension = inputFile.split('.').pop().toLowerCase();
|
const extension = inputFile.split('.').pop().toLowerCase();
|
||||||
if (extension === 'pmtiles') {
|
if (extension === 'pmtiles') {
|
||||||
const FileDescriptor = PMtilesOpen(inputFile);
|
let FileOpenInfo = PMtilesOpen(inputFile);
|
||||||
const info = await GetPMtilesInfo(FileDescriptor);
|
const info = await GetPMtilesInfo(FileOpenInfo.pmtiles);
|
||||||
const metadata = info.metadata;
|
const metadata = info.metadata;
|
||||||
PMtilesClose(FileDescriptor);
|
if (FileOpenInfo.fd !== undefined) {PMtilesClose(FileOpenInfo.fd);}
|
||||||
|
FileOpenInfo = null;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
metadata.format === 'pbf' &&
|
metadata.format === 'pbf' &&
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,25 @@ import fs from 'node:fs';
|
||||||
import PMTiles from 'pmtiles';
|
import PMTiles from 'pmtiles';
|
||||||
|
|
||||||
export const PMtilesOpen = (FilePath) => {
|
export const PMtilesOpen = (FilePath) => {
|
||||||
const fd = fs.openSync(FilePath, 'r');
|
let pmtiles = undefined;
|
||||||
return fd;
|
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) => {
|
export const PMtilesClose = (fd) => {
|
||||||
fs.closeSync(fd);
|
fs.closeSync(fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
const PMTilesFileDescriptorSource = class {
|
const PMTilesFileSource = class {
|
||||||
constructor(fd) {
|
constructor(fd) {
|
||||||
this.fd = fd;
|
this.fd = fd;
|
||||||
}
|
}
|
||||||
|
|
@ -35,9 +45,7 @@ const ReadBytes = async (fd, buffer, offset) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const GetPMtilesInfo = async (fd) => {
|
export const GetPMtilesInfo = async (pmtiles) => {
|
||||||
const source = new PMTilesFileDescriptorSource(fd);
|
|
||||||
const pmtiles = new PMTiles.PMTiles(source);
|
|
||||||
const header = await pmtiles.getHeader();
|
const header = await pmtiles.getHeader();
|
||||||
const metadata = await pmtiles.getMetadata();
|
const metadata = await pmtiles.getMetadata();
|
||||||
|
|
||||||
|
|
@ -54,9 +62,7 @@ export const GetPMtilesInfo = async (fd) => {
|
||||||
return { header: header, metadata: metadata };
|
return { header: header, metadata: metadata };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const GetPMtilesTile = async (fd, z, x, y) => {
|
export const GetPMtilesTile = async (pmtiles, z, x, y) => {
|
||||||
const source = new PMTilesFileDescriptorSource(fd);
|
|
||||||
const pmtiles = new PMTiles.PMTiles(source);
|
|
||||||
const header = await pmtiles.getHeader();
|
const header = await pmtiles.getHeader();
|
||||||
const TileType = GetPmtilesTileType(header.tileType);
|
const TileType = GetPmtilesTileType(header.tileType);
|
||||||
let zxyTile = await pmtiles.getZxy(z, x, y);
|
let zxyTile = await pmtiles.getZxy(z, x, y);
|
||||||
|
|
@ -116,3 +122,15 @@ const ArrayBufferToBuffer = (array_buffer) => {
|
||||||
}
|
}
|
||||||
return 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 { VectorTile } from '@mapbox/vector-tile';
|
||||||
|
|
||||||
import { getTileUrls, fixTileJSONCenter } from './utils.js';
|
import { getTileUrls, fixTileJSONCenter } from './utils.js';
|
||||||
import {
|
import { PMtilesOpen, GetPMtilesInfo, GetPMtilesTile } from './pmtiles_adapter.js';
|
||||||
PMtilesOpen,
|
|
||||||
PMtilesClose,
|
|
||||||
GetPMtilesInfo,
|
|
||||||
GetPMtilesTile,
|
|
||||||
} from './pmtiles_adapter.js';
|
|
||||||
|
|
||||||
export const serve_data = {
|
export const serve_data = {
|
||||||
init: (options, repo) => {
|
init: (options, repo) => {
|
||||||
|
|
@ -214,10 +209,10 @@ export const serve_data = {
|
||||||
let source;
|
let source;
|
||||||
let source_type;
|
let source_type;
|
||||||
if (inputType === 'pmtiles') {
|
if (inputType === 'pmtiles') {
|
||||||
const FileDescriptor = PMtilesOpen(inputFile);
|
let FileOpenInfo = PMtilesOpen(inputFile);
|
||||||
const info = await GetPMtilesInfo(FileDescriptor);
|
const info = await GetPMtilesInfo(FileOpenInfo.pmtiles);
|
||||||
const metadata = info.metadata;
|
const metadata = info.metadata;
|
||||||
source = FileDescriptor;
|
source = FileOpenInfo.pmtiles;
|
||||||
source_type = 'pmtiles';
|
source_type = 'pmtiles';
|
||||||
|
|
||||||
tileJSON['name'] = id;
|
tileJSON['name'] = id;
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,7 @@ import polyline from '@mapbox/polyline';
|
||||||
import proj4 from 'proj4';
|
import proj4 from 'proj4';
|
||||||
import request from 'request';
|
import request from 'request';
|
||||||
import { getFontsPbf, getTileUrls, fixTileJSONCenter } from './utils.js';
|
import { getFontsPbf, getTileUrls, fixTileJSONCenter } from './utils.js';
|
||||||
import {
|
import { PMtilesOpen, GetPMtilesInfo, GetPMtilesTile } from './pmtiles_adapter.js';
|
||||||
PMtilesOpen,
|
|
||||||
PMtilesClose,
|
|
||||||
GetPMtilesInfo,
|
|
||||||
GetPMtilesTile,
|
|
||||||
} from './pmtiles_adapter.js';
|
|
||||||
|
|
||||||
const FLOAT_PATTERN = '[+-]?(?:\\d+|\\d+.?\\d+)';
|
const FLOAT_PATTERN = '[+-]?(?:\\d+|\\d+.?\\d+)';
|
||||||
const PATH_PATTERN =
|
const PATH_PATTERN =
|
||||||
|
|
@ -1496,10 +1491,10 @@ export const serve_rendered = {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source_type === 'pmtiles') {
|
if (source_type === 'pmtiles') {
|
||||||
let FileDescriptor = PMtilesOpen(inputFile);
|
let FileOpenInfo = PMtilesOpen(inputFile);
|
||||||
const info = await GetPMtilesInfo(FileDescriptor);
|
const info = await GetPMtilesInfo(FileOpenInfo.pmtiles);
|
||||||
const metadata = info.metadata;
|
const metadata = info.metadata;
|
||||||
map.sources[metadata.name.toLowerCase()] = FileDescriptor;
|
map.sources[metadata.name.toLowerCase()] = FileOpenInfo.pmtiles;
|
||||||
map.source_types[metadata.name.toLowerCase()] = 'pmtiles';
|
map.source_types[metadata.name.toLowerCase()] = 'pmtiles';
|
||||||
|
|
||||||
if (!repoobj.dataProjWGStoInternalWGS && metadata.proj4) {
|
if (!repoobj.dataProjWGStoInternalWGS && metadata.proj4) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue