mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
JS MapLibre Adapter: autodetect minzoom/maxzoom, clean up hacky loading syntax [#48]
This commit is contained in:
@@ -62,38 +62,68 @@ export class Protocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tile = (params: any, callback: any) => {
|
tile = (params: any, callback: any) => {
|
||||||
const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/);
|
if (params.type == "json") {
|
||||||
const result = params.url.match(re);
|
const pmtiles_url = params.url.substr(10);
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
let instance = this.tiles.get(pmtiles_url);
|
instance.getHeader().then((h) => {
|
||||||
if (!instance) {
|
const tilejson = {
|
||||||
instance = new PMTiles(pmtiles_url);
|
tiles: [params.url + "/{z}/{x}/{y}"],
|
||||||
this.tiles.set(pmtiles_url, instance);
|
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
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user