UNPKG

@loaders.gl/pmtiles

Version:

Framework-independent loader for the pmtiles format

48 lines (47 loc) 1.75 kB
// loaders.gl // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors import { BlobFile } from '@loaders.gl/loader-utils'; import { VERSION } from "./lib/version.js"; import { PMTilesTileSource } from "./pmtiles-source.js"; /** * Loader for PMTiles metadata * @note This loader is intended to allow PMTiles to be treated like other file types in top-level loading logic. * @note For actual access to the tile data, use the PMTilesSource class. */ export const PMTilesLoader = { name: 'PMTiles', id: 'pmtiles', module: 'pmtiles', version: VERSION, extensions: ['pmtiles'], mimeTypes: ['application/octet-stream'], tests: ['PMTiles'], options: { pmtiles: {} }, parse: async (arrayBuffer, options) => parseFileAsPMTiles(new BlobFile(new Blob([arrayBuffer])), options), parseFile: parseFileAsPMTiles }; async function parseFileAsPMTiles(file, options) { const source = new PMTilesTileSource(file.handle, { pmtiles: options?.pmtiles || {} }); const formatSpecificMetadata = await source.getMetadata(); const { tileMIMEType, tilejson = {} } = formatSpecificMetadata; const { layers = [] } = tilejson; switch (tileMIMEType) { case 'application/vnd.mapbox-vector-tile': return { shape: 'vector-source', layers: layers.map((layer) => ({ name: layer.name, schema: layer.schema })), tables: [], formatSpecificMetadata }; case 'image/png': case 'image/jpeg': return { shape: 'image-source', formatSpecificMetadata }; default: throw new Error(`PMTilesLoader: Unsupported tile MIME type ${tileMIMEType}`); } }