From 9f6941e9691ef6a2580ed12746631356ac2a3bc7 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Fri, 14 Oct 2022 00:08:08 +0800 Subject: [PATCH] JS MapLibre Adapter: autodetect minzoom/maxzoom, clean up hacky loading syntax [#48] --- js/adapters.ts | 94 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 32 deletions(-) diff --git a/js/adapters.ts b/js/adapters.ts index a2b7855..edcc464 100644 --- a/js/adapters.ts +++ b/js/adapters.ts @@ -62,38 +62,68 @@ export class Protocol { } tile = (params: any, callback: any) => { - const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/); - const result = params.url.match(re); - const pmtiles_url = result[1]; + if (params.type == "json") { + const pmtiles_url = params.url.substr(10); + let instance = this.tiles.get(pmtiles_url); + if (!instance) { + instance = new PMTiles(pmtiles_url); + this.tiles.set(pmtiles_url, instance); + } - let instance = this.tiles.get(pmtiles_url); - if (!instance) { - instance = new PMTiles(pmtiles_url); - this.tiles.set(pmtiles_url, instance); + instance.getHeader().then((h) => { + const tilejson = { + tiles: [params.url + "/{z}/{x}/{y}"], + minzoom: h.minZoom, + maxzoom: h.maxZoom, + }; + callback(null, tilejson, null, null); + }); + + return { + cancel: () => {}, + }; + } else { + const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/); + const result = params.url.match(re); + const pmtiles_url = result[1]; + + let instance = this.tiles.get(pmtiles_url); + if (!instance) { + instance = new PMTiles(pmtiles_url); + this.tiles.set(pmtiles_url, instance); + } + const z = result[2]; + const x = result[3]; + const y = result[4]; + + const controller = new AbortController(); + const signal = controller.signal; + let cancel = () => { + controller.abort(); + }; + + instance + .getZxy(+z, +x, +y, signal) + .then((resp) => { + if (resp) { + callback( + null, + new Uint8Array(resp.data), + resp.cacheControl, + resp.expires + ); + } else { + callback(null, new Uint8Array(), null, null); + } + }) + .catch((e) => { + if (e.name !== "AbortError") { + throw e; + } + }); + return { + cancel: cancel, + }; } - const z = result[2]; - const x = result[3]; - const y = result[4]; - - const controller = new AbortController(); - const signal = controller.signal; - let cancel = () => { - controller.abort(); - }; - - instance.getZxy(+z, +x, +y, signal).then((resp) => { - if (resp) { - callback(null, new Uint8Array(resp.data), resp.cacheControl, resp.expires); - } else { - callback(null, new Uint8Array(), null, null); - } - }).catch((e) => { - if (e.name !== "AbortError") { - throw e; - } - }); - return { - cancel: cancel - }; }; -} \ No newline at end of file +}