-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pmtiles): Experimental PMTilesLoader (#3023)
- Loading branch information
Showing
16 changed files
with
273 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
import type {Schema, Field, DataType, SchemaMetadata, FieldMetadata} from '@loaders.gl/schema'; | ||
import type {TileJSONLayer, TileJSONField} from './parse-tilejson'; | ||
|
||
// LAYERS | ||
|
||
export function getSchemaFromTileJSONLayer(layer: TileJSONLayer): Schema { | ||
const fields: Field[] = []; | ||
if (layer.fields) { | ||
for (const field of layer.fields) { | ||
fields.push({ | ||
name: field.name, | ||
type: getDataTypeFromTileJSONField(field), | ||
metadata: getMetadataFromTileJSONField(field) | ||
}); | ||
} | ||
} | ||
return { | ||
metadata: getMetadataFromTileJSONLayer(layer), | ||
fields | ||
}; | ||
} | ||
|
||
function getMetadataFromTileJSONLayer(layer: TileJSONLayer): SchemaMetadata { | ||
const metadata: Record<string, string> = {}; | ||
for (const [key, value] of Object.entries(layer)) { | ||
if (key !== 'fields' && value) { | ||
metadata[key] = JSON.stringify(value); | ||
} | ||
} | ||
return metadata; | ||
} | ||
|
||
// FIELDS | ||
|
||
function getDataTypeFromTileJSONField(field: TileJSONField): DataType { | ||
switch (field.type.toLowerCase()) { | ||
case 'float32': | ||
return 'float32'; | ||
case 'number': | ||
case 'float64': | ||
return 'float64'; | ||
case 'string': | ||
case 'utf8': | ||
return 'utf8'; | ||
case 'boolean': | ||
return 'bool'; | ||
default: | ||
return 'null'; | ||
} | ||
} | ||
|
||
function getMetadataFromTileJSONField(field: TileJSONField): FieldMetadata { | ||
const metadata: Record<string, string> = {}; | ||
for (const [key, value] of Object.entries(field)) { | ||
if (key !== 'name' && value) { | ||
metadata[key] = JSON.stringify(value); | ||
} | ||
} | ||
return metadata; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
// Version constant cannot be imported, it needs to correspond to the build version of **this** module. | ||
// __VERSION__ is injected by babel-plugin-version-inline | ||
// @ts-ignore TS2304: Cannot find name '__VERSION__'. | ||
export const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
import type {LoaderWithParser, LoaderOptions, ReadableFile} from '@loaders.gl/loader-utils'; | ||
import {BlobFile} from '@loaders.gl/loader-utils'; | ||
import {VERSION} from './lib/version'; | ||
|
||
import {VectorSourceInfo, ImageSourceInfo} from './source-info'; | ||
import {PMTilesTileSource, PMTilesTileSourceProps} from './pmtiles-source'; | ||
|
||
export type PMTilesLoaderOptions = LoaderOptions & { | ||
pmtiles?: PMTilesTileSourceProps['pmtiles']; | ||
}; | ||
|
||
/** | ||
* 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: ArrayBuffer, options?: PMTilesLoaderOptions) => | ||
parseFileAsPMTiles(new BlobFile(new Blob([arrayBuffer])), options), | ||
parseFile: parseFileAsPMTiles | ||
} as const satisfies LoaderWithParser< | ||
VectorSourceInfo | ImageSourceInfo, | ||
never, | ||
PMTilesLoaderOptions | ||
>; | ||
|
||
async function parseFileAsPMTiles( | ||
file: ReadableFile, | ||
options?: PMTilesLoaderOptions | ||
): Promise<VectorSourceInfo | ImageSourceInfo> { | ||
const source = new PMTilesTileSource(file.handle as string | Blob, { | ||
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 | ||
} as VectorSourceInfo; | ||
case 'image/png': | ||
case 'image/jpeg': | ||
return {shape: 'image-source', formatSpecificMetadata} as ImageSourceInfo; | ||
default: | ||
throw new Error(`PMTilesLoader: Unsupported tile MIME type ${tileMIMEType}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// loaders.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
import {Schema} from '@loaders.gl/schema'; | ||
|
||
export type SourceInfo = VectorSourceInfo | ImageSourceInfo; | ||
|
||
/** Information about a vector source */ | ||
export type VectorSourceInfo = { | ||
shape: 'vector-source'; | ||
/** List of geospatial tables */ | ||
layers: {name: string; schema: Schema}[]; | ||
/** List of nongeospatial tables */ | ||
tables: {name: string; schema: Schema}[]; | ||
/** Format specific metadata */ | ||
formatSpecificMetadata?: Record<string, any>; | ||
}; | ||
|
||
/** Information about an image source */ | ||
export type ImageSourceInfo = { | ||
shape: 'image-source'; | ||
/** Format specific metadata */ | ||
formatSpecificMetadata?: Record<string, any>; | ||
}; |
Binary file added
BIN
+3.78 MB
modules/pmtiles/test/data/pmtiles-v3/protomaps(vector)ODbL_firenze.pmtiles
Binary file not shown.
Binary file added
BIN
+1.87 MB
modules/pmtiles/test/data/pmtiles-v3/usgs-mt-whitney-8-15-webp-512.pmtiles
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
// Copyright (c) vis.gl contributors | ||
|
||
import './pmtiles-source.spec'; | ||
|
||
import './pmtiles-loader.spec'; |
Oops, something went wrong.