@loaders.gl/schema
Version:
Table format APIs for JSON, CSV, etc...
52 lines (45 loc) • 1.26 kB
text/typescript
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import type {Mesh} from '../../types/category-mesh';
import type {ColumnarTable, ArrowTable} from '../../types/category-table';
// import {convertMeshToArrowTable} from './mesh-to-arrow-table';
type TargetShape = 'mesh' | 'columnar-table' | 'arrow-table';
/**
* Convert a mesh to a specific shape
*/
export function convertMesh(
mesh: Mesh,
shape: TargetShape,
options?: any
): Mesh | ColumnarTable | ArrowTable {
switch (shape || 'mesh') {
case 'mesh':
return mesh;
case 'columnar-table':
return convertMeshToColumnarTable(mesh);
// case 'arrow-table':
// return {
// shape: 'arrow-table',
// data: convertMeshToArrowTable(mesh)
// };
default:
throw new Error(`Unsupported shape ${options?.shape}`);
}
}
/**
* Convert a loaders.gl Mesh to a Columnar Table
* @param mesh
* @returns
*/
export function convertMeshToColumnarTable(mesh: Mesh): ColumnarTable {
const columns = {};
for (const [columnName, attribute] of Object.entries(mesh.attributes)) {
columns[columnName] = attribute.value;
}
return {
shape: 'columnar-table',
schema: mesh.schema,
data: columns
};
}