Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pmtiles://s3:// #61

Merged
merged 3 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions localdata/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"s3://tiles/{z}/{x}/{y}.pbf"
],
"maxzoom": 6
},
"pmtiles-s3": {
"type": "vector",
"tiles": [
"pmtiles://s3://tiles/school.pmtiles/{z}/{x}/{y}"
],
"maxzoom": 10
}
},
"layers": [
Expand All @@ -37,7 +44,7 @@
"source-layer": "P2921",
"type": "circle",
"paint": {
"circle-radius": 10,
"circle-radius": 20,
"circle-color": "red"
}
},
Expand All @@ -47,7 +54,7 @@
"source-layer": "P2921",
"type": "circle",
"paint": {
"circle-radius": 7,
"circle-radius": 15,
"circle-color": "blue"
}
},
Expand All @@ -57,7 +64,7 @@
"source-layer": "P2921",
"type": "circle",
"paint": {
"circle-radius": 5,
"circle-radius": 10,
"circle-color": "yellow"
}
},
Expand All @@ -67,9 +74,19 @@
"source-layer": "P2921",
"type": "circle",
"paint": {
"circle-radius": 3,
"circle-radius": 5,
"circle-color": "green"
}
},
{
"id": "pmtiles-s3",
"source": "pmtiles-s3",
"source-layer": "P2921",
"type": "circle",
"paint": {
"circle-radius": 3,
"circle-color": "purple"
}
}
]
}
Binary file added localdata/tiles/school.pmtiles
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "chiitiler",
"version": "1.8.3",
"version": "1.9.0",
"description": "Tiny map rendering server for MapLibre Style Spec",
"main": "dist/main.js",
"scripts": {
Expand Down
82 changes: 59 additions & 23 deletions src/source/pmtiles.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as fs from 'fs';

import { PMTiles, Source, RangeResponse } from 'pmtiles';
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';

import { getS3Client } from '../s3.js';
import { type Cache, noneCache } from '../cache/index.js';

const pmtilesCache: { [key: string]: PMTiles } = {};

class PmtilesNodejsFileSource implements Source {
class FilesystemSource implements Source {
filepath: string;
fileHandle: Promise<fs.promises.FileHandle>;

Expand All @@ -26,6 +28,42 @@ class PmtilesNodejsFileSource implements Source {
}
}

class S3Source implements Source {
bucket: string;
key: string;
s3Client: S3Client;

constructor(bucket: string, key: string) {
this.bucket = bucket;
this.key = key;
this.s3Client = getS3Client({
region: process.env.CHIITILER_S3_REGION ?? 'us-east1',
endpoint: process.env.CHIITILER_S3_ENDPOINT ?? null,
});
}

getKey() {
return `s3://${this.bucket}/${this.key}`;
}

async getBytes(offset: number, length: number): Promise<RangeResponse> {
const cmd = new GetObjectCommand({
Bucket: this.bucket,
Key: this.key,
Range: `bytes=${offset}-${offset + length - 1}`,
});
try {
const obj = await this.s3Client.send(cmd);
if (obj.Body === undefined) return { data: Buffer.alloc(0).buffer };
const buf = Buffer.from(await obj.Body.transformToByteArray());
return { data: buf.buffer };
} catch (e: any) {
if (e.name !== 'NoSuchKey') console.log(e);
return { data: Buffer.alloc(0).buffer };
}
}
}

/**
* uri = pmtiles://path/to/file.pmtiles/{z}/{x}/{y}
* uri = pmtiles://http://url/to/file.pmtiles/{z}/{x}/{y}
Expand All @@ -35,43 +73,41 @@ async function getPmtilesSoruce(
uri: string,
cache: Cache = noneCache(),
): Promise<Buffer | null> {
// uri = pmtiles://path/to/file.pmtiles/{z}/{x}/{y}
const pmtilesFilepath = uri
const pmtilesUri = uri
.replace('pmtiles://', '')
.replace(/\/\d+\/\d+\/\d+$/, '');

const isRemoteData =
pmtilesFilepath.startsWith('http://') ||
pmtilesFilepath.startsWith('https://');
if (isRemoteData) {
// use cache only for http(s) sources
const isHttpSource =
pmtilesUri.startsWith('http://') || pmtilesUri.startsWith('https://');
if (isHttpSource) {
const val = await cache.get(uri);
if (val !== undefined) return val; // hit
}

if (!pmtilesCache[pmtilesFilepath]) {
if (isRemoteData) {
// remote file
pmtilesCache[pmtilesFilepath] = new PMTiles(pmtilesFilepath);
} else {
// local file
const fileSource = new PmtilesNodejsFileSource(pmtilesFilepath);
pmtilesCache[pmtilesFilepath] = new PMTiles(fileSource);
if (!pmtilesCache[pmtilesUri])
pmtilesCache[pmtilesUri] = new PMTiles(pmtilesUri);
} else if (pmtilesUri.startsWith('s3://')) {
if (!pmtilesCache[pmtilesUri]) {
const bucket = pmtilesUri.replace('s3://', '').split('/')[0];
const key = pmtilesUri.replace(`s3://${bucket}/`, '');
const s3Source = new S3Source(bucket, key);
pmtilesCache[pmtilesUri] = new PMTiles(s3Source);
}
} else {
// local filesystem
const fileSource = new FilesystemSource(pmtilesUri);
pmtilesCache[pmtilesUri] = new PMTiles(fileSource);
}

const [z, x, y] = uri
.replace(`pmtiles://${pmtilesFilepath}/`, '')
.split('/');
const tile = await pmtilesCache[pmtilesFilepath].getZxy(
const [z, x, y] = uri.replace(`pmtiles://${pmtilesUri}/`, '').split('/');
const tile = await pmtilesCache[pmtilesUri].getZxy(
Number(z),
Number(x),
Number(y),
);

if (!tile) return null;

const buf = Buffer.from(tile.data);
if (isRemoteData) cache.set(uri, buf);
if (isHttpSource) cache.set(uri, buf);
return buf;
}

Expand Down