| 21 | * For vector tiles see https://github.com/protomaps/protomaps-leaflet |
| 22 | */ |
| 23 | export const leafletRasterLayer = (source: PMTiles, options: unknown) => { |
| 24 | let loaded = false; |
| 25 | let mimeType = ""; |
| 26 | const cls = L.GridLayer.extend({ |
| 27 | createTile: (coord: Coords, done: DoneCallback) => { |
| 28 | const el = document.createElement("img"); |
| 29 | const controller = new AbortController(); |
| 30 | const signal = controller.signal; |
| 31 | el.cancel = () => { |
| 32 | controller.abort(); |
| 33 | }; |
| 34 | if (!loaded) { |
| 35 | source.getHeader().then((header) => { |
| 36 | if ( |
| 37 | header.tileType === TileType.Mvt || |
| 38 | header.tileType === TileType.Mlt |
| 39 | ) { |
| 40 | console.error( |
| 41 | "Error: archive contains vector tiles, but leafletRasterLayer is for displaying raster tiles. See https://github.com/protomaps/PMTiles/tree/main/js for details." |
| 42 | ); |
| 43 | } else if (header.tileType === 2) { |
| 44 | mimeType = "image/png"; |
| 45 | } else if (header.tileType === 3) { |
| 46 | mimeType = "image/jpeg"; |
| 47 | } else if (header.tileType === 4) { |
| 48 | mimeType = "image/webp"; |
| 49 | } else if (header.tileType === 5) { |
| 50 | mimeType = "image/avif"; |
| 51 | } |
| 52 | }); |
| 53 | loaded = true; |
| 54 | } |
| 55 | source |
| 56 | .getZxy(coord.z, coord.x, coord.y, signal) |
| 57 | .then((arr) => { |
| 58 | if (arr) { |
| 59 | const blob = new Blob([arr.data], { type: mimeType }); |
| 60 | const imageUrl = window.URL.createObjectURL(blob); |
| 61 | el.src = imageUrl; |
| 62 | } else { |
| 63 | el.style.display = "none"; |
| 64 | } |
| 65 | el.cancel = undefined; |
| 66 | done(undefined, el); |
| 67 | }) |
| 68 | .catch((e) => { |
| 69 | if (e.name !== "AbortError") { |
| 70 | throw e; |
| 71 | } |
| 72 | }); |
| 73 | return el; |
| 74 | }, |
| 75 | |
| 76 | _removeTile: function (key: string) { |
| 77 | const tile = this._tiles[key]; |
| 78 | if (!tile) { |
| 79 | return; |
| 80 | } |