@loaders.gl/schema
Version:
Table format APIs for JSON, CSV, etc...
4 lines • 111 kB
Source Map (JSON)
{
"version": 3,
"sources": ["index.js", "lib/table/simple-table/data-type.js", "lib/table/batches/base-table-batch-aggregator.js", "lib/table/simple-table/row-utils.js", "lib/table/batches/row-table-batch-aggregator.js", "lib/table/batches/columnar-table-batch-aggregator.js", "lib/table/batches/table-batch-builder.js", "lib/table/simple-table/table-accessors.js", "lib/table/arrow-api/arrow-like-field.js", "lib/table/arrow-api/arrow-like-schema.js", "lib/table/simple-table/table-schema.js", "lib/table/arrow-api/arrow-like-table.js", "lib/table/simple-table/make-table.js", "lib/table/simple-table/make-table-from-batches.js", "lib/table/simple-table/table-column.js", "lib/table/simple-table/convert-table.js", "lib/mesh/mesh-utils.js", "lib/mesh/deduce-mesh-schema.js", "lib/table/arrow-api/enum.js", "lib/table/arrow-api/arrow-like-type.js", "lib/table/arrow-api/get-type-info.js", "lib/utils/async-queue.js"],
"sourcesContent": ["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nexport { getArrayTypeFromDataType } from \"./lib/table/simple-table/data-type.js\";\n// TABLE CATEGORY UTILS\nexport { TableBatchBuilder } from \"./lib/table/batches/table-batch-builder.js\";\nexport { RowTableBatchAggregator } from \"./lib/table/batches/row-table-batch-aggregator.js\";\nexport { ColumnarTableBatchAggregator } from \"./lib/table/batches/columnar-table-batch-aggregator.js\";\nexport { isTable, getTableLength, getTableNumCols, getTableCell, getTableCellAt, getTableRowShape, getTableColumnIndex, getTableColumnName, getTableRowAsObject, getTableRowAsArray, makeRowIterator, makeArrayRowIterator, makeObjectRowIterator } from \"./lib/table/simple-table/table-accessors.js\";\nexport { ArrowLikeTable } from \"./lib/table/arrow-api/arrow-like-table.js\";\nexport { makeTableFromData } from \"./lib/table/simple-table/make-table.js\";\nexport { makeTableFromBatches, makeBatchFromTable } from \"./lib/table/simple-table/make-table-from-batches.js\";\nexport { convertTable } from \"./lib/table/simple-table/convert-table.js\";\nexport { deduceTableSchema } from \"./lib/table/simple-table/table-schema.js\";\nexport { convertToObjectRow, convertToArrayRow } from \"./lib/table/simple-table/row-utils.js\";\nexport { getDataTypeFromArray } from \"./lib/table/simple-table/data-type.js\";\nexport { getMeshSize, getMeshBoundingBox } from \"./lib/mesh/mesh-utils.js\";\n// Commented out due to https://github.com/visgl/deck.gl/issues/6906 and https://github.com/visgl/loaders.gl/issues/2177\n// export {convertMesh} from './category/mesh/convert-mesh';\nexport { deduceMeshSchema, deduceMeshField, makeMeshAttributeMetadata } from \"./lib/mesh/deduce-mesh-schema.js\";\n// SCHEMA\nexport { Schema as ArrowLikeSchema, Field as ArrowLikeField, DataType as ArrowLikeDataType, Null, Binary, Bool, Int, Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64, Float, Float16, Float32, Float64, Utf8, Date, DateDay, DateMillisecond, Time, TimeMillisecond, TimeSecond, Timestamp, TimestampSecond, TimestampMillisecond, TimestampMicrosecond, TimestampNanosecond, Interval, IntervalDayTime, IntervalYearMonth, FixedSizeList, Struct } from \"./lib/table/arrow-api/index.js\";\n// EXPERIMENTAL APIs\n// SCHEMA UTILS\nexport { getTypeInfo } from \"./lib/table/arrow-api/get-type-info.js\";\nexport { default as AsyncQueue } from \"./lib/utils/async-queue.js\";\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n/** Deduce column types from values */\nexport function getDataTypeFromValue(value, defaultNumberType = 'float32') {\n if (value instanceof Date) {\n return 'date-millisecond';\n }\n if (value instanceof Number) {\n return defaultNumberType;\n }\n if (typeof value === 'string') {\n return 'utf8';\n }\n if (value === null || value === 'undefined') {\n return 'null';\n }\n return 'null';\n}\n/**\n * Deduces a simple data type \"descriptor from a typed array instance\n */\nexport function getDataTypeFromArray(array) {\n let type = getDataTypeFromTypedArray(array);\n if (type !== 'null') {\n return { type, nullable: false };\n }\n if (array.length > 0) {\n type = getDataTypeFromValue(array[0]);\n return { type, nullable: true };\n }\n return { type: 'null', nullable: true };\n}\n/**\n * Deduces a simple data type \"descriptor from a typed array instance\n */\nexport function getDataTypeFromTypedArray(array) {\n switch (array.constructor) {\n case Int8Array:\n return 'int8';\n case Uint8Array:\n case Uint8ClampedArray:\n return 'uint8';\n case Int16Array:\n return 'int16';\n case Uint16Array:\n return 'uint16';\n case Int32Array:\n return 'int32';\n case Uint32Array:\n return 'uint32';\n case Float32Array:\n return 'float32';\n case Float64Array:\n return 'float64';\n default:\n return 'null';\n }\n}\nexport function getArrayTypeFromDataType(type, nullable) {\n if (!nullable) {\n switch (type) {\n case 'int8':\n return Int8Array;\n case 'uint8':\n return Uint8Array;\n case 'int16':\n return Int16Array;\n case 'uint16':\n return Uint16Array;\n case 'int32':\n return Int32Array;\n case 'uint32':\n return Uint32Array;\n case 'float32':\n return Float32Array;\n case 'float64':\n return Float64Array;\n default:\n break;\n }\n }\n // if (typeof BigInt64Array !== 'undefined') {\n // TYPED_ARRAY_TO_TYPE.BigInt64Array = new Int64();\n // TYPED_ARRAY_TO_TYPE.BigUint64Array = new Uint64();\n // }\n return Array;\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nconst DEFAULT_ROW_COUNT = 100;\nexport class BaseTableBatchAggregator {\n schema;\n options;\n shape;\n length = 0;\n rows = null;\n cursor = 0;\n _headers = [];\n constructor(schema, options) {\n this.options = options;\n this.schema = schema;\n // schema is an array if there're no headers\n // object if there are headers\n if (!Array.isArray(schema)) {\n this._headers = [];\n for (const key in schema) {\n this._headers[schema[key].index] = schema[key].name;\n }\n }\n }\n rowCount() {\n return this.length;\n }\n addArrayRow(row, cursor) {\n if (Number.isFinite(cursor)) {\n this.cursor = cursor;\n }\n this.shape = 'array-row-table';\n this.rows = this.rows || new Array(DEFAULT_ROW_COUNT);\n this.rows[this.length] = row;\n this.length++;\n }\n addObjectRow(row, cursor) {\n if (Number.isFinite(cursor)) {\n this.cursor = cursor;\n }\n this.shape = 'object-row-table';\n this.rows = this.rows || new Array(DEFAULT_ROW_COUNT);\n this.rows[this.length] = row;\n this.length++;\n }\n getBatch() {\n let rows = this.rows;\n if (!rows) {\n return null;\n }\n rows = rows.slice(0, this.length);\n this.rows = null;\n const batch = {\n shape: this.shape || 'array-row-table',\n batchType: 'data',\n data: rows,\n length: this.length,\n schema: this.schema,\n cursor: this.cursor\n };\n return batch;\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n/** Convert an object row to an array row */\nexport function convertToObjectRow(arrayRow, headers) {\n if (!arrayRow) {\n throw new Error('null row');\n }\n const objectRow = {};\n if (headers) {\n for (let i = 0; i < headers.length; i++) {\n objectRow[headers[i]] = arrayRow[i];\n }\n }\n else {\n for (let i = 0; i < arrayRow.length; i++) {\n const columnName = `column-${i}`;\n objectRow[columnName] = arrayRow[i];\n }\n }\n return objectRow;\n}\n/** Convert an object row to an array row */\nexport function convertToArrayRow(objectRow, headers) {\n if (!objectRow) {\n throw new Error('null row');\n }\n if (headers) {\n const arrayRow = new Array(headers.length);\n for (let i = 0; i < headers.length; i++) {\n arrayRow[i] = objectRow[headers[i]];\n }\n return arrayRow;\n }\n return Object.values(objectRow);\n}\n/** Get headers from a sample array row */\nexport function inferHeadersFromArrayRow(arrayRow) {\n const headers = [];\n for (let i = 0; i < arrayRow.length; i++) {\n const columnName = `column-${i}`;\n headers.push(columnName);\n }\n return headers;\n}\n/** Get headers from a smaple object row */\nexport function inferHeadersFromObjectRow(row) {\n return Object.keys(row);\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { convertToArrayRow, convertToObjectRow, inferHeadersFromArrayRow, inferHeadersFromObjectRow } from \"../simple-table/row-utils.js\";\nconst DEFAULT_ROW_COUNT = 100;\nexport class RowTableBatchAggregator {\n schema;\n options;\n length = 0;\n objectRows = null;\n arrayRows = null;\n cursor = 0;\n _headers = null;\n constructor(schema, options) {\n this.options = options;\n this.schema = schema;\n // schema is an array if there're no headers\n // object if there are headers\n if (schema) {\n this._headers = [];\n for (const key in schema) {\n this._headers[schema[key].index] = schema[key].name;\n }\n }\n }\n rowCount() {\n return this.length;\n }\n addArrayRow(row, cursor) {\n if (Number.isFinite(cursor)) {\n this.cursor = cursor;\n }\n // TODO - infer schema at a higher level, instead of hacking headers here?\n this._headers ||= inferHeadersFromArrayRow(row);\n // eslint-disable-next-line default-case\n switch (this.options.shape) {\n case 'object-row-table':\n const rowObject = convertToObjectRow(row, this._headers);\n this.addObjectRow(rowObject, cursor);\n break;\n case 'array-row-table':\n this.arrayRows = this.arrayRows || new Array(DEFAULT_ROW_COUNT);\n this.arrayRows[this.length] = row;\n this.length++;\n break;\n }\n }\n addObjectRow(row, cursor) {\n if (Number.isFinite(cursor)) {\n this.cursor = cursor;\n }\n // TODO - infer schema at a higher level, instead of hacking headers here?\n this._headers ||= inferHeadersFromObjectRow(row);\n // eslint-disable-next-line default-case\n switch (this.options.shape) {\n case 'array-row-table':\n const rowArray = convertToArrayRow(row, this._headers);\n this.addArrayRow(rowArray, cursor);\n break;\n case 'object-row-table':\n this.objectRows = this.objectRows || new Array(DEFAULT_ROW_COUNT);\n this.objectRows[this.length] = row;\n this.length++;\n break;\n }\n }\n getBatch() {\n let rows = this.arrayRows || this.objectRows;\n if (!rows) {\n return null;\n }\n rows = rows.slice(0, this.length);\n this.arrayRows = null;\n this.objectRows = null;\n return {\n shape: this.options.shape,\n batchType: 'data',\n data: rows,\n length: this.length,\n // @ts-expect-error we should infer a schema\n schema: this.schema,\n cursor: this.cursor\n };\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nconst DEFAULT_ROW_COUNT = 100;\nexport class ColumnarTableBatchAggregator {\n schema;\n length = 0;\n allocated = 0;\n columns = {};\n constructor(schema, options) {\n this.schema = schema;\n this._reallocateColumns();\n }\n rowCount() {\n return this.length;\n }\n addArrayRow(row) {\n // If user keeps pushing rows beyond batch size, reallocate\n this._reallocateColumns();\n let i = 0;\n // TODO what if no csv header, columns not populated?\n for (const fieldName in this.columns) {\n this.columns[fieldName][this.length] = row[i++];\n }\n this.length++;\n }\n addObjectRow(row) {\n // If user keeps pushing rows beyond batch size, reallocate\n this._reallocateColumns();\n for (const fieldName in row) {\n this.columns[fieldName][this.length] = row[fieldName];\n }\n this.length++;\n }\n getBatch() {\n this._pruneColumns();\n const columns = Array.isArray(this.schema) ? this.columns : {};\n // schema is an array if there're no headers\n // object if there are headers\n // columns should match schema format\n if (!Array.isArray(this.schema)) {\n for (const fieldName in this.schema) {\n const field = this.schema[fieldName];\n columns[field.name] = this.columns[field.index];\n }\n }\n this.columns = {};\n const batch = {\n shape: 'columnar-table',\n batchType: 'data',\n data: columns,\n schema: this.schema,\n length: this.length\n };\n return batch;\n }\n // HELPERS\n _reallocateColumns() {\n if (this.length < this.allocated) {\n return;\n }\n // @ts-ignore TODO\n this.allocated = this.allocated > 0 ? (this.allocated *= 2) : DEFAULT_ROW_COUNT;\n this.columns = {};\n for (const fieldName in this.schema) {\n const field = this.schema[fieldName];\n const ArrayType = field.type || Float32Array;\n const oldColumn = this.columns[field.index];\n if (oldColumn && ArrayBuffer.isView(oldColumn)) {\n // Copy the old data to the new array\n const typedArray = new ArrayType(this.allocated);\n typedArray.set(oldColumn);\n this.columns[field.index] = typedArray;\n }\n else if (oldColumn) {\n // Plain array\n oldColumn.length = this.allocated;\n this.columns[field.index] = oldColumn;\n }\n else {\n // Create new\n this.columns[field.index] = new ArrayType(this.allocated);\n }\n }\n }\n _pruneColumns() {\n for (const [columnName, column] of Object.entries(this.columns)) {\n this.columns[columnName] = column.slice(0, this.length);\n }\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { BaseTableBatchAggregator } from \"./base-table-batch-aggregator.js\";\nimport { RowTableBatchAggregator } from \"./row-table-batch-aggregator.js\";\nimport { ColumnarTableBatchAggregator } from \"./columnar-table-batch-aggregator.js\";\nconst DEFAULT_OPTIONS = {\n shape: undefined,\n batchSize: 'auto',\n batchDebounceMs: 0,\n limit: 0,\n _limitMB: 0\n};\nconst ERR_MESSAGE = 'TableBatchBuilder';\n/** Incrementally builds batches from a stream of rows */\nexport class TableBatchBuilder {\n schema;\n options;\n aggregator = null;\n batchCount = 0;\n bytesUsed = 0;\n isChunkComplete = false;\n lastBatchEmittedMs = Date.now();\n totalLength = 0;\n totalBytes = 0;\n rowBytes = 0;\n static ArrowBatch;\n constructor(schema, options) {\n this.schema = schema;\n this.options = { ...DEFAULT_OPTIONS, ...options };\n }\n limitReached() {\n if (Boolean(this.options?.limit) && this.totalLength >= this.options.limit) {\n return true;\n }\n if (Boolean(this.options?._limitMB) && this.totalBytes / 1e6 >= this.options._limitMB) {\n return true;\n }\n return false;\n }\n /** @deprecated Use addArrayRow or addObjectRow */\n addRow(row) {\n if (this.limitReached()) {\n return;\n }\n this.totalLength++;\n this.rowBytes = this.rowBytes || this._estimateRowMB(row);\n this.totalBytes += this.rowBytes;\n if (Array.isArray(row)) {\n this.addArrayRow(row);\n }\n else {\n this.addObjectRow(row);\n }\n }\n /** Add one row to the batch */\n addArrayRow(row) {\n if (!this.aggregator) {\n const TableBatchType = this._getTableBatchType();\n this.aggregator = new TableBatchType(this.schema, this.options);\n }\n this.aggregator.addArrayRow(row);\n }\n /** Add one row to the batch */\n addObjectRow(row) {\n if (!this.aggregator) {\n const TableBatchType = this._getTableBatchType();\n this.aggregator = new TableBatchType(this.schema, this.options);\n }\n this.aggregator.addObjectRow(row);\n }\n /** Mark an incoming raw memory chunk has completed */\n chunkComplete(chunk) {\n if (chunk instanceof ArrayBuffer) {\n this.bytesUsed += chunk.byteLength;\n }\n if (typeof chunk === 'string') {\n this.bytesUsed += chunk.length;\n }\n this.isChunkComplete = true;\n }\n getFullBatch(options) {\n return this._isFull() ? this._getBatch(options) : null;\n }\n getFinalBatch(options) {\n return this._getBatch(options);\n }\n // INTERNAL\n _estimateRowMB(row) {\n return Array.isArray(row) ? row.length * 8 : Object.keys(row).length * 8;\n }\n _isFull() {\n // No batch, not ready\n if (!this.aggregator || this.aggregator.rowCount() === 0) {\n return false;\n }\n // if batchSize === 'auto' we wait for chunk to complete\n // if batchSize === number, ensure we have enough rows\n if (this.options.batchSize === 'auto') {\n if (!this.isChunkComplete) {\n return false;\n }\n }\n else if (this.options.batchSize > this.aggregator.rowCount()) {\n return false;\n }\n // Debounce batches\n if (this.options.batchDebounceMs > Date.now() - this.lastBatchEmittedMs) {\n return false;\n }\n // Emit batch\n this.isChunkComplete = false;\n this.lastBatchEmittedMs = Date.now();\n return true;\n }\n /**\n * bytesUsed can be set via chunkComplete or via getBatch*\n */\n _getBatch(options) {\n if (!this.aggregator) {\n return null;\n }\n // TODO - this can overly increment bytes used?\n if (options?.bytesUsed) {\n this.bytesUsed = options.bytesUsed;\n }\n const normalizedBatch = this.aggregator.getBatch();\n normalizedBatch.count = this.batchCount;\n normalizedBatch.bytesUsed = this.bytesUsed;\n Object.assign(normalizedBatch, options);\n this.batchCount++;\n this.aggregator = null;\n return normalizedBatch;\n }\n _getTableBatchType() {\n switch (this.options.shape) {\n case 'array-row-table':\n case 'object-row-table':\n return RowTableBatchAggregator;\n case 'columnar-table':\n return ColumnarTableBatchAggregator;\n case 'arrow-table':\n if (!TableBatchBuilder.ArrowBatch) {\n throw new Error(ERR_MESSAGE);\n }\n return TableBatchBuilder.ArrowBatch;\n default:\n return BaseTableBatchAggregator;\n }\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nexport function isTable(table) {\n const shape = typeof table === 'object' && table?.shape;\n switch (shape) {\n case 'array-row-table':\n case 'object-row-table':\n return Array.isArray(table.data);\n case 'geojson-table':\n return Array.isArray(table.features);\n case 'columnar-table':\n return table.data && typeof table.data === 'object';\n case 'arrow-table':\n return Boolean(table?.data?.numRows !== undefined);\n default:\n return false;\n }\n}\n/**\n * Returns the length of the table (i.e. the number of rows)\n */\nexport function getTableLength(table) {\n switch (table.shape) {\n case 'array-row-table':\n case 'object-row-table':\n return table.data.length;\n case 'geojson-table':\n return table.features.length;\n case 'arrow-table':\n const arrowTable = table.data;\n return arrowTable.numRows;\n case 'columnar-table':\n for (const column of Object.values(table.data)) {\n return column.length || 0;\n }\n return 0;\n default:\n throw new Error('table');\n }\n}\n/**\n * Returns the number of columns in the table\n * @throws Fails to deduce number of columns if the table has no schema and is empty\n */\nexport function getTableNumCols(table) {\n if (table.schema) {\n return table.schema.fields.length;\n }\n if (getTableLength(table) === 0) {\n throw new Error('empty table');\n }\n switch (table.shape) {\n case 'array-row-table':\n return table.data[0].length;\n case 'object-row-table':\n return Object.keys(table.data[0]).length;\n case 'geojson-table':\n return Object.keys(table.features[0]).length;\n case 'columnar-table':\n return Object.keys(table.data).length;\n case 'arrow-table':\n const arrowTable = table.data;\n return arrowTable.numCols;\n default:\n throw new Error('table');\n }\n}\n/** Get a table cell value at row index and column name */\nexport function getTableCell(table, rowIndex, columnName) {\n switch (table.shape) {\n case 'array-row-table':\n const columnIndex = getTableColumnIndex(table, columnName);\n return table.data[rowIndex][columnIndex];\n case 'object-row-table':\n return table.data[rowIndex][columnName];\n case 'geojson-table':\n return table.features[rowIndex][columnName];\n case 'columnar-table':\n const column = table.data[columnName];\n return column[rowIndex];\n case 'arrow-table':\n const arrowTable = table.data;\n const arrowColumnIndex = arrowTable.schema.fields.findIndex((field) => field.name === columnName);\n return arrowTable.getChildAt(arrowColumnIndex)?.get(rowIndex);\n default:\n throw new Error('todo');\n }\n}\n/** Get a table cell value at row index and column name */\nexport function getTableCellAt(table, rowIndex, columnIndex) {\n switch (table.shape) {\n case 'array-row-table':\n return table.data[rowIndex][columnIndex];\n case 'object-row-table':\n const columnName1 = getTableColumnName(table, columnIndex);\n return table.data[rowIndex][columnName1];\n case 'geojson-table':\n const columnName2 = getTableColumnName(table, columnIndex);\n return table.features[rowIndex][columnName2];\n case 'columnar-table':\n const columnName3 = getTableColumnName(table, columnIndex);\n const column = table.data[columnName3];\n return column[rowIndex];\n case 'arrow-table':\n const arrowTable = table.data;\n return arrowTable.getChildAt(columnIndex)?.get(rowIndex);\n default:\n throw new Error('todo');\n }\n}\n/** Deduce the table row shape */\nexport function getTableRowShape(table) {\n switch (table.shape) {\n case 'array-row-table':\n case 'object-row-table':\n return table.shape;\n case 'geojson-table':\n // TODO - this is not correct, geojson-table is not a row table\n return 'object-row-table';\n case 'columnar-table':\n default:\n throw new Error('Not a row table');\n }\n}\n/** Get the index of a named table column. Requires the table to have a schema */\nexport function getTableColumnIndex(table, columnName) {\n const columnIndex = table.schema?.fields.findIndex((field) => field.name === columnName);\n if (columnIndex === undefined) {\n throw new Error(columnName);\n }\n return columnIndex;\n}\n/** Get the name of a table column by index. Requires the table to have a schema */\nexport function getTableColumnName(table, columnIndex) {\n const columnName = table.schema?.fields[columnIndex]?.name;\n if (!columnName) {\n throw new Error(`${columnIndex}`);\n }\n return columnName;\n}\n/**\n * Returns one row of the table in object format.\n * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance\n * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter\n */\n// eslint-disable-next-line complexity\nexport function getTableRowAsObject(table, rowIndex, target, copy) {\n switch (table.shape) {\n case 'object-row-table':\n return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];\n case 'array-row-table':\n if (table.schema) {\n const objectRow = target || {};\n for (let i = 0; i < table.schema.fields.length; i++) {\n objectRow[table.schema.fields[i].name] = table.data[rowIndex][i];\n }\n return objectRow;\n }\n throw new Error('no schema');\n case 'geojson-table':\n if (table.schema) {\n const objectRow = target || {};\n // TODO - should lift properties to top level\n for (let i = 0; i < table.schema.fields.length; i++) {\n objectRow[table.schema.fields[i].name] = table.features[rowIndex][i];\n }\n return objectRow;\n }\n throw new Error('no schema');\n case 'columnar-table':\n if (table.schema) {\n const objectRow = target || {};\n for (let i = 0; i < table.schema.fields.length; i++) {\n objectRow[table.schema.fields[i].name] =\n table.data[table.schema.fields[i].name][rowIndex];\n }\n return objectRow;\n }\n else {\n // eslint-disable-line no-else-return\n const objectRow = target || {};\n for (const [name, column] of Object.entries(table.data)) {\n objectRow[name] = column[rowIndex];\n }\n return objectRow;\n }\n case 'arrow-table':\n const arrowTable = table.data;\n const objectRow = target || {};\n const row = arrowTable.get(rowIndex);\n const schema = arrowTable.schema;\n for (let i = 0; i < schema.fields.length; i++) {\n objectRow[schema.fields[i].name] = row?.[schema.fields[i].name];\n }\n return objectRow;\n default:\n throw new Error('shape');\n }\n}\n/**\n * Returns one row of the table in array format.\n * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance.\n * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter\n */\n// eslint-disable-next-line complexity\nexport function getTableRowAsArray(table, rowIndex, target, copy) {\n switch (table.shape) {\n case 'array-row-table':\n return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];\n case 'object-row-table':\n if (table.schema) {\n const arrayRow = target || [];\n for (let i = 0; i < table.schema.fields.length; i++) {\n arrayRow[i] = table.data[rowIndex][table.schema.fields[i].name];\n }\n return arrayRow;\n }\n // Warning: just slap on the values, this risks mismatches between rows\n return Object.values(table.data[rowIndex]);\n case 'geojson-table':\n if (table.schema) {\n const arrayRow = target || [];\n // TODO - should lift properties to top level\n for (let i = 0; i < table.schema.fields.length; i++) {\n arrayRow[i] = table.features[rowIndex][table.schema.fields[i].name];\n }\n return arrayRow;\n }\n // Warning: just slap on the values, this risks mismatches between rows\n return Object.values(table.features[rowIndex]);\n case 'columnar-table':\n if (table.schema) {\n const arrayRow = target || [];\n for (let i = 0; i < table.schema.fields.length; i++) {\n arrayRow[i] = table.data[table.schema.fields[i].name][rowIndex];\n }\n return arrayRow;\n }\n else {\n // eslint-disable-line no-else-return\n const arrayRow = target || [];\n let i = 0;\n for (const column of Object.values(table.data)) {\n arrayRow[i] = column[rowIndex];\n i++;\n }\n return arrayRow;\n }\n case 'arrow-table':\n const arrowTable = table.data;\n const arrayRow = target || [];\n const row = arrowTable.get(rowIndex);\n const schema = arrowTable.schema;\n for (let i = 0; i < schema.fields.length; i++) {\n arrayRow[i] = row?.[schema.fields[i].name];\n }\n return arrayRow;\n default:\n throw new Error('shape');\n }\n}\n/** Convert any table into array row format */\nexport function makeArrayRowTable(table) {\n if (table.shape === 'array-row-table') {\n return table;\n }\n const length = getTableLength(table);\n const data = new Array(length);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n data[rowIndex] = getTableRowAsArray(table, rowIndex);\n }\n return {\n shape: 'array-row-table',\n schema: table.schema,\n data\n };\n}\n/** Convert any table into object row format */\nexport function makeObjectRowTable(table) {\n if (table.shape === 'object-row-table') {\n return table;\n }\n const length = getTableLength(table);\n const data = new Array(length);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n data[rowIndex] = getTableRowAsObject(table, rowIndex);\n }\n return {\n shape: 'object-row-table',\n schema: table.schema,\n data\n };\n}\n/** Convert any table into object row format */\nexport function makeColumnarTable(table) {\n if (table.shape === 'object-row-table') {\n return table;\n }\n const length = getTableLength(table);\n const data = new Array(length);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n data[rowIndex] = getTableRowAsObject(table, rowIndex);\n }\n return {\n shape: 'object-row-table',\n schema: table.schema,\n data\n };\n}\n// Row Iterators\n/**\n * Iterate over table rows\n * @param table\n * @param shape\n */\nexport function* makeRowIterator(table, shape) {\n switch (shape) {\n case 'array-row-table':\n yield* makeArrayRowIterator(table);\n break;\n case 'object-row-table':\n yield* makeObjectRowIterator(table);\n break;\n default:\n throw new Error(`Unknown row type ${shape}`);\n }\n}\n/**\n * Streaming processing: Iterate over table, yielding array rows\n * @param table\n * @param shape\n */\nexport function* makeArrayRowIterator(table, target = []) {\n const length = getTableLength(table);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n yield getTableRowAsArray(table, rowIndex, target);\n }\n}\n/**\n * Streaming processing: Iterate over table, yielding object rows\n * @param table\n * @param shape\n */\nexport function* makeObjectRowIterator(table, target = {}) {\n const length = getTableLength(table);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n yield getTableRowAsObject(table, rowIndex, target);\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n/**\n * ArrowJS `Field` API-compatible class for row-based tables\n * https://loaders.gl/arrowjs/docs/api-reference/field\n * A field holds name, nullable, and metadata information about a table \"column\"\n * A Schema is essentially a list of fields\n */\nexport class ArrowLikeField {\n name;\n type;\n nullable;\n metadata;\n constructor(name, type, nullable = false, metadata = new Map()) {\n this.name = name;\n this.type = type;\n this.nullable = nullable;\n this.metadata = metadata;\n }\n get typeId() {\n return this.type && this.type.typeId;\n }\n clone() {\n return new ArrowLikeField(this.name, this.type, this.nullable, this.metadata);\n }\n compareTo(other) {\n return (this.name === other.name &&\n this.type === other.type &&\n this.nullable === other.nullable &&\n this.metadata === other.metadata);\n }\n toString() {\n return `${JSON.stringify(this.type)}${this.nullable ? ', nullable' : ''}${this.metadata ? `, metadata: ${JSON.stringify(this.metadata)}` : ''}`;\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { ArrowLikeField } from \"./arrow-like-field.js\";\nexport class ArrowLikeSchema {\n fields;\n metadata;\n constructor(fields, metadata = new Map()) {\n // checkNames(fields);\n // For kepler fields, create arrow compatible `Fields` that have kepler fields as `metadata`\n this.fields = fields.map((field) => new ArrowLikeField(field.name, field.type, field.nullable, field.metadata));\n this.metadata =\n metadata instanceof Map ? metadata : new Map(Object.entries(metadata));\n }\n // TODO - arrow only seems to compare fields, not metadata\n compareTo(other) {\n if (this.metadata !== other.metadata) {\n return false;\n }\n if (this.fields.length !== other.fields.length) {\n return false;\n }\n for (let i = 0; i < this.fields.length; ++i) {\n if (!this.fields[i].compareTo(other.fields[i])) {\n return false;\n }\n }\n return true;\n }\n select(...columnNames) {\n // Ensure column names reference valid fields\n const nameMap = Object.create(null);\n for (const name of columnNames) {\n nameMap[name] = true;\n }\n const selectedFields = this.fields.filter((field) => nameMap[field.name]);\n return new ArrowLikeSchema(selectedFields, this.metadata);\n }\n selectAt(...columnIndices) {\n // Ensure column indices reference valid fields\n const selectedFields = columnIndices.map((index) => this.fields[index]).filter(Boolean);\n return new ArrowLikeSchema(selectedFields, this.metadata);\n }\n assign(schemaOrFields) {\n let fields;\n let metadata = this.metadata;\n if (schemaOrFields instanceof ArrowLikeSchema) {\n const otherArrowLikeSchema = schemaOrFields;\n fields = otherArrowLikeSchema.fields;\n metadata = mergeMaps(mergeMaps(new Map(), this.metadata), otherArrowLikeSchema.metadata);\n }\n else {\n fields = schemaOrFields;\n }\n // Create a merged list of fields, overwrite fields in place, new fields at end\n const fieldMap = Object.create(null);\n for (const field of this.fields) {\n fieldMap[field.name] = field;\n }\n for (const field of fields) {\n fieldMap[field.name] = field;\n }\n const mergedFields = Object.values(fieldMap);\n return new ArrowLikeSchema(mergedFields, metadata);\n }\n}\n// Warn if any duplicated field names\n// function checkNames(fields: Field[]): void {\n// const usedNames: Record<string, boolean> = {};\n// for (const field of fields) {\n// if (usedNames[field.name]) {\n// // eslint-disable-next-line\n// console.warn('ArrowLikeSchema: duplicated field name', field.name, field);\n// }\n// usedNames[field.name] = true;\n// }\n// }\nfunction mergeMaps(m1, m2) {\n // @ts-ignore\n return new Map([...(m1 || new Map()), ...(m2 || new Map())]);\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { getDataTypeFromArray, getDataTypeFromValue } from \"./data-type.js\";\n/**\n * SCHEMA SUPPORT - AUTODEDUCTION\n * @param {*} table\n * @param {*} schema\n * @returns\n */\nexport function deduceTableSchema(table) {\n switch (table.shape) {\n case 'array-row-table':\n case 'object-row-table':\n return deduceSchemaFromRows(table.data);\n case 'geojson-table':\n return deduceSchemaFromGeoJSON(table.features);\n case 'columnar-table':\n return deduceSchemaFromColumns(table.data);\n case 'arrow-table':\n default:\n throw new Error('Deduce schema');\n }\n}\nexport function deduceSchema(data) {\n return Array.isArray(data) ? deduceSchemaFromRows(data) : deduceSchemaFromColumns(data);\n}\n/** Given an object with columnar arrays, try to deduce a schema */\nfunction deduceSchemaFromColumns(columnarTable) {\n const fields = [];\n for (const [columnName, column] of Object.entries(columnarTable)) {\n const field = deduceFieldFromColumn(column, columnName);\n fields.push(field);\n }\n return { fields, metadata: {} };\n}\n/** Given an array of rows, try to deduce a schema */\nfunction deduceSchemaFromRows(rowTable) {\n if (!rowTable.length) {\n throw new Error('deduce from empty table');\n }\n const fields = [];\n const row0 = rowTable[0];\n // TODO - fields can be nullable, false detection...\n // Could look at additional rows if nulls in first row\n // TODO - if array, column names will be numbers\n for (const [columnName, value] of Object.entries(row0)) {\n fields.push(deduceFieldFromValue(value, columnName));\n }\n return { fields, metadata: {} };\n}\n/** Given a GeoJSON, try to deduce a schema */\nfunction deduceSchemaFromGeoJSON(features) {\n if (!features.length) {\n throw new Error('deduce from empty table');\n }\n const fields = [];\n const row0 = features[0].properties || {};\n // TODO - fields can be nullable, false detection...\n // Could look at additional rows if nulls in first row\n // TODO - if array, column names will be numbers\n for (const [columnName, value] of Object.entries(row0)) {\n fields.push(deduceFieldFromValue(value, columnName));\n }\n return { fields, metadata: {} };\n}\n/** Given a column (i.e. array), attempt to deduce an appropriate `Field` */\nfunction deduceFieldFromColumn(column, name) {\n if (ArrayBuffer.isView(column)) {\n const type = getDataTypeFromArray(column);\n return {\n name,\n type: type.type || 'null',\n nullable: type.nullable\n // metadata: {}\n };\n }\n if (Array.isArray(column) && column.length > 0) {\n const value = column[0];\n const type = getDataTypeFromValue(value);\n // TODO - support nested schemas?\n return {\n name,\n type,\n nullable: true\n // metadata: {},\n };\n }\n throw new Error('empty table');\n}\n/** Given a value, attempt to deduce an appropriate `Field` */\nfunction deduceFieldFromValue(value, name) {\n const type = getDataTypeFromValue(value);\n return {\n name,\n type,\n nullable: true\n // metadata: {}\n };\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { ArrowLikeSchema } from \"./arrow-like-schema.js\";\nimport { deduceTableSchema } from \"../simple-table/table-schema.js\";\nimport { getTableCell, getTableLength, getTableNumCols\n// getTableCell,\n// getTableRowShape,\n// getTableColumnIndex,\n// getTableColumnName,\n// getTableRowAsObject,\n// getTableRowAsArray\n } from \"../simple-table/table-accessors.js\";\nclass ArrowLikeVector {\n table;\n columnName;\n constructor(table, columnName) {\n this.table = table;\n this.columnName = columnName;\n }\n get(rowIndex) {\n return getTableCell(this.table, rowIndex, this.columnName);\n }\n toArray() {\n switch (this.table.shape) {\n case 'arrow-table':\n const arrowTable = this.table.data;\n return arrowTable.getChild(this.columnName)?.toArray();\n case 'columnar-table':\n return this.table.data[this.columnName];\n default:\n throw new Error(this.table.shape);\n }\n }\n}\n/**\n * Class that provides an API similar to Apache Arrow Table class\n * Forwards methods directly if the underlying table is Arrow, otherwise calls accessor functions\n */\nexport class ArrowLikeTable {\n schema;\n table;\n constructor(table) {\n const schema = table.schema || deduceTableSchema(table);\n this.schema = new ArrowLikeSchema(schema.fields, schema.metadata);\n this.table = { ...table, schema };\n }\n // get schema() {\n // return this.table.schema;\n // }\n get data() {\n return this.table.shape === 'geojson-table' ? this.table.features : this.table.data;\n }\n get numCols() {\n return getTableNumCols(this.table);\n }\n get length() {\n return getTableLength(this.table);\n }\n getChild(columnName) {\n return new ArrowLikeVector(this.table, columnName);\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { deduceTableSchema } from \"./table-schema.js\";\nexport function makeTableFromData(data) {\n let table;\n switch (getTableShapeFromData(data)) {\n case 'array-row-table':\n table = { shape: 'array-row-table', data: data };\n break;\n case 'object-row-table':\n table = { shape: 'object-row-table', data: data };\n break;\n case 'columnar-table':\n table = { shape: 'columnar-table', data: data };\n break;\n default:\n throw new Error('table');\n }\n const schema = deduceTableSchema(table);\n return { ...table, schema };\n}\n/** Helper function to get shape of data */\nfunction getTableShapeFromData(data) {\n if (Array.isArray(data)) {\n if (data.length === 0) {\n throw new Error('cannot deduce type of empty table');\n }\n // Deduce the table shape from the first row\n const firstRow = data[0];\n if (Array.isArray(firstRow)) {\n return 'array-row-table';\n }\n if (firstRow && typeof firstRow === 'object') {\n return 'object-row-table';\n }\n }\n if (data && typeof data === 'object') {\n return 'columnar-table';\n }\n throw new Error('invalid table');\n}\n/** Convert any table into object row format *\nexport function makeColumnarTable(table: Table): ColumnarTable {\n if (table.shape === 'columnar-table') {\n return table;\n }\n const length = getTableLength(table);\n const data = new Array<{[key: string]: unknown}>(length);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n data[rowIndex] = getTableRowAsObject(table, rowIndex);\n }\n return {\n shape: 'columnar-table',\n schema: table.schema,\n data\n };\n}\n\n\n/** Convert any table into array row format *\nexport function makeArrayRowTable(table: TableLike): ArrayRowTable {\n if (table.shape === 'array-row-table') {\n return table;\n }\n const length = getTableLength(table);\n const data = new Array<unknown[]>(length);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n data[rowIndex] = getTableRowAsArray(table, rowIndex);\n }\n return {\n shape: 'array-row-table',\n schema: table.schema,\n data\n };\n}\n\n/** Convert any table into object row format *\nexport function makeObjectRowTable(table: Table): ObjectRowTable {\n if (table.shape === 'object-row-table') {\n return table;\n }\n const length = getTableLength(table);\n const data = new Array<{[key: string]: unknown}>(length);\n for (let rowIndex = 0; rowIndex < length; rowIndex++) {\n data[rowIndex] = getTableRowAsObject(table, rowIndex);\n }\n return {\n shape: 'object-row-table',\n schema: table.schema,\n data\n };\n}\n*/\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { getTableLength } from \"./table-accessors.js\";\n/**\n * Returns an iterator that yields a single table as a sequence of batches.\n * @note Currently only a single batch is yielded.\n * @note All batches will have the same shape and schema as the original table.\n * @returns\n */\nexport function* makeBatchesFromTable(table) {\n yield makeBatchFromTable(table);\n}\n/**\n * Returns a table packaged as a single table batch\n * @note The batch will have the same shape and schema as the original table.\n * @returns `null` if no batches are yielded by the async iterator\n */\nexport function makeBatchFromTable(table) {\n return { ...table, length: getTableLength(table), batchType: 'data' };\n}\n/**\n * Assembles all batches from an async iterator into a single table.\n * @note All batches must have the same shape and schema\n * @param batchIterator\n * @returns `null` if no batches are yielded by the async iterator\n */\n// eslint-disable-next-line complexity\nexport async function makeTableFromBatches(batchIterator) {\n let arrayRows;\n let objectRows;\n let features;\n let shape = null;\n let schema;\n for await (const batch of batchIterator) {\n shape = shape || batch.shape;\n schema = schema || batch.schema;\n switch (batch.shape) {\n case 'array-row-table':\n arrayRows = arrayRows || [];\n for (let rowIndex = 0; rowIndex < getTableLength(batch); rowIndex++) {\n const row = batch.data[rowIndex];\n arrayRows.push(row);\n }\n break;\n case 'object-row-table':\n objectRows = objectRows || [];\n for (let rowIndex = 0; rowIndex < getTableLength(batch); rowIndex++) {\n const row = batch.data[rowIndex];\n objectRows.push(row);\n }\n break;\n case 'geojson-table':\n features = features || [];\n for (let rowIndex = 0; rowIndex < getTableLength(batch); rowIndex++) {\n const row = batch.features[rowIndex];\n features.push(row);\n }\n break;\n case 'columnar-table':\n case 'arrow-table':\n default:\n throw new Error('shape');\n }\n }\n if (!shape) {\n return null;\n }\n switch (shape) {\n case 'array-row-table':\n return { shape: 'array-row-table', data: arrayRows, schema };\n case 'object-row-table':\n return { shape: 'object-row-table', data: objectRows, schema };\n case 'geojson-table':\n return { shape: 'geojson-table', type: 'FeatureCollection', features: features, schema };\n default:\n return null;\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nimport { getArrayTypeFromDataType } from \"./data-type.js\";\nexport function makeColumnFromField(field, length) {\n const ArrayType = getArrayTypeFromDataType(field.type, field.nullable);\n return new ArrayType(length);\n}\n/*\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nfunction deduceSchema(rows) {\n const row = rows[0];\n\n const schema = {};\n let i = 0;\n for (const columnName in row) {\n const value = row[columnName];\n switch (typeof value) {\n case 'number':\n case 'boolean':\n // TODO - booleans could be handled differently...\n schema[columnName] = {name: