add getTileJson method to PMTiles class [#239, #247] (#453)

* add getTileJson method to PMTiles class [#239, #247]
* update docs related to FetchSource and headers [#397]
This commit is contained in:
Brandon Liu
2024-09-18 15:57:14 +08:00
committed by GitHub
parent 6ed85a28fb
commit 6f5479439b
7 changed files with 121 additions and 56 deletions

View File

@@ -12,7 +12,7 @@ import {
Source,
TileType,
} from "../../../js/index";
import { pmtiles_path, tileJSON, tile_path } from "../../shared/index";
import { pmtiles_path, tile_path } from "../../shared/index";
import { createHash } from "crypto";
import zlib from "zlib";
@@ -177,15 +177,13 @@ export const handlerRaw = async (
}
headers["Content-Type"] = "application/json";
const t = tileJSON(
header,
await p.getMetadata(),
process.env.PUBLIC_HOSTNAME ||
const t = await p.getTileJson(
`https://${
process.env.PUBLIC_HOSTNAME ||
event.headers["x-distribution-domain-name"] ||
"",
name
""
}/${name}`
);
return apiResp(200, JSON.stringify(t), false, headers);
}

View File

@@ -14,7 +14,7 @@
"deploy": "wrangler deploy",
"test": "tsx ../shared/index.test.ts",
"tsc": "tsc --watch",
"build": "wrangler publish --outdir dist --dry-run",
"build": "wrangler deploy --outdir dist --dry-run",
"biome": "biome check --config-path=../../js/ src/index.ts --apply",
"biome-check": "biome check --config-path=../../js src/index.ts"
}

View File

@@ -7,7 +7,7 @@ import {
Source,
TileType,
} from "../../../js/index";
import { pmtiles_path, tileJSON, tile_path } from "../../shared/index";
import { pmtiles_path, tile_path } from "../../shared/index";
interface Env {
// biome-ignore lint: config name
@@ -159,14 +159,9 @@ export default {
if (!tile) {
cacheableHeaders.set("Content-Type", "application/json");
const t = tileJSON(
pHeader,
await p.getMetadata(),
env.PUBLIC_HOSTNAME || url.hostname,
name
const t = await p.getTileJson(
`https://${env.PUBLIC_HOSTNAME || url.hostname}/${name}`
);
return cacheableResponse(JSON.stringify(t), cacheableHeaders, 200);
}

View File

@@ -1,5 +1,3 @@
import { Header, TileType } from "../../js/index";
export const pmtiles_path = (name: string, setting?: string): string => {
if (setting) {
return setting.replaceAll("{name}", name);
@@ -35,39 +33,4 @@ export const tile_path = (
}
return { ok: false, name: "", tile: [0, 0, 0], ext: "" };
};
export const tileJSON = (
header: Header,
metadata: any,
hostname: string,
tileset_name: string
) => {
let ext = "";
if (header.tileType === TileType.Mvt) {
ext = ".mvt";
} else if (header.tileType === TileType.Png) {
ext = ".png";
} else if (header.tileType === TileType.Jpeg) {
ext = ".jpg";
} else if (header.tileType === TileType.Webp) {
ext = ".webp";
} else if (header.tileType === TileType.Avif) {
ext = ".avif";
}
return {
tilejson: "3.0.0",
scheme: "xyz",
tiles: ["https://" + hostname + "/" + tileset_name + "/{z}/{x}/{y}" + ext],
vector_layers: metadata.vector_layers,
attribution: metadata.attribution,
description: metadata.description,
name: metadata.name,
version: metadata.version,
bounds: [header.minLon, header.minLat, header.maxLon, header.maxLat],
center: [header.centerLon, header.centerLat, header.centerZoom],
minzoom: header.minZoom,
maxzoom: header.maxZoom,
};
};
};