| 170 | * MapLibre GL JS protocol. Must be added once globally. |
| 171 | */ |
| 172 | export class Protocol { |
| 173 | /** @hidden */ |
| 174 | tiles: Map<string, PMTiles>; |
| 175 | metadata: boolean; |
| 176 | errorOnMissingTile: boolean; |
| 177 | |
| 178 | /** |
| 179 | * Initialize the MapLibre PMTiles protocol. |
| 180 | * |
| 181 | * * metadata: also load the metadata section of the PMTiles. required for some "inspect" functionality |
| 182 | * and to automatically populate the map attribution. Requires an extra HTTP request. |
| 183 | * * errorOnMissingTile: When a vector MVT tile is missing from the archive, raise an error instead of |
| 184 | * returning the empty array. Not recommended. This is only to reproduce the behavior of ZXY tile APIs |
| 185 | * which some applications depend on when overzooming. |
| 186 | */ |
| 187 | constructor(options?: { metadata?: boolean; errorOnMissingTile?: boolean }) { |
| 188 | this.tiles = new Map<string, PMTiles>(); |
| 189 | this.metadata = options?.metadata || false; |
| 190 | this.errorOnMissingTile = options?.errorOnMissingTile || false; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add a {@link PMTiles} instance to the global protocol instance. |
| 195 | * |
| 196 | * For remote fetch sources, references in MapLibre styles like pmtiles://http://... |
| 197 | * will resolve to the same instance if the URLs match. |
| 198 | */ |
| 199 | add(p: PMTiles) { |
| 200 | this.tiles.set(p.source.getKey(), p); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Fetch a {@link PMTiles} instance by URL, for remote PMTiles instances. |
| 205 | */ |
| 206 | get(url: string) { |
| 207 | return this.tiles.get(url); |
| 208 | } |
| 209 | |
| 210 | /** @hidden */ |
| 211 | tilev4 = async ( |
| 212 | params: RequestParameters, |
| 213 | abortController: AbortController |
| 214 | ) => { |
| 215 | if (params.type === "json") { |
| 216 | const pmtilesUrl = params.url.substr(10); |
| 217 | let instance = this.tiles.get(pmtilesUrl); |
| 218 | if (!instance) { |
| 219 | instance = new PMTiles(pmtilesUrl); |
| 220 | this.tiles.set(pmtilesUrl, instance); |
| 221 | } |
| 222 | |
| 223 | if (this.metadata) { |
| 224 | const data = await instance.getTileJson(params.url); |
| 225 | abortController.signal.throwIfAborted(); |
| 226 | return { data }; |
| 227 | } |
| 228 | |
| 229 | const h = await instance.getHeader(); |
nothing calls this directly
no test coverage detected
searching dependent graphs…