Skip to content

Commit

Permalink
refactor: rework so remote pmtiles may be possible
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Calcutt <[email protected]>
  • Loading branch information
acalcutt committed Oct 10, 2023
1 parent b84c20d commit 3297ebe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
13 changes: 5 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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' &&
Expand Down
36 changes: 27 additions & 9 deletions src/pmtiles_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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:";
}
13 changes: 4 additions & 9 deletions src/serve_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 4 additions & 9 deletions src/serve_rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -1470,10 +1465,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) {
Expand Down

0 comments on commit 3297ebe

Please sign in to comment.