@loaders.gl/schema-utils 4.4.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/LICENSE +41 -0
  2. package/README.md +9 -0
  3. package/dist/dist.dev.js +14269 -0
  4. package/dist/dist.min.js +18 -0
  5. package/dist/index.cjs +2436 -0
  6. package/dist/index.cjs.map +7 -0
  7. package/dist/index.d.ts +28 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +73 -0
  10. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts +11 -0
  11. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts.map +1 -0
  12. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.js +40 -0
  13. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts +11 -0
  14. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts.map +1 -0
  15. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.js +32 -0
  16. package/dist/lib/mesh/convert-mesh-to-table.d.ts +18 -0
  17. package/dist/lib/mesh/convert-mesh-to-table.d.ts.map +1 -0
  18. package/dist/lib/mesh/convert-mesh-to-table.js +60 -0
  19. package/dist/lib/mesh/convert-table-to-mesh.d.ts +7 -0
  20. package/dist/lib/mesh/convert-table-to-mesh.d.ts.map +1 -0
  21. package/dist/lib/mesh/convert-table-to-mesh.js +40 -0
  22. package/dist/lib/mesh/deduce-mesh-schema.d.ts +23 -0
  23. package/dist/lib/mesh/deduce-mesh-schema.d.ts.map +1 -0
  24. package/dist/lib/mesh/deduce-mesh-schema.js +62 -0
  25. package/dist/lib/mesh/mesh-utils.d.ts +23 -0
  26. package/dist/lib/mesh/mesh-utils.d.ts.map +1 -0
  27. package/dist/lib/mesh/mesh-utils.js +46 -0
  28. package/dist/lib/schema/convert-arrow-schema.d.ts +23 -0
  29. package/dist/lib/schema/convert-arrow-schema.d.ts.map +1 -0
  30. package/dist/lib/schema/convert-arrow-schema.js +280 -0
  31. package/dist/lib/schema/data-type.d.ts +16 -0
  32. package/dist/lib/schema/data-type.d.ts.map +1 -0
  33. package/dist/lib/schema/data-type.js +88 -0
  34. package/dist/lib/schema/deduce-table-schema.d.ts +14 -0
  35. package/dist/lib/schema/deduce-table-schema.d.ts.map +1 -0
  36. package/dist/lib/schema/deduce-table-schema.js +100 -0
  37. package/dist/lib/table/arrow-api/arrow-like-field.d.ts +19 -0
  38. package/dist/lib/table/arrow-api/arrow-like-field.d.ts.map +1 -0
  39. package/dist/lib/table/arrow-api/arrow-like-field.js +36 -0
  40. package/dist/lib/table/arrow-api/arrow-like-interface.d.ts +43 -0
  41. package/dist/lib/table/arrow-api/arrow-like-interface.d.ts.map +1 -0
  42. package/dist/lib/table/arrow-api/arrow-like-interface.js +4 -0
  43. package/dist/lib/table/arrow-api/arrow-like-schema.d.ts +12 -0
  44. package/dist/lib/table/arrow-api/arrow-like-schema.d.ts.map +1 -0
  45. package/dist/lib/table/arrow-api/arrow-like-schema.js +81 -0
  46. package/dist/lib/table/arrow-api/arrow-like-table.d.ts +28 -0
  47. package/dist/lib/table/arrow-api/arrow-like-table.d.ts.map +1 -0
  48. package/dist/lib/table/arrow-api/arrow-like-table.js +63 -0
  49. package/dist/lib/table/arrow-api/arrow-like-type.d.ts +177 -0
  50. package/dist/lib/table/arrow-api/arrow-like-type.d.ts.map +1 -0
  51. package/dist/lib/table/arrow-api/arrow-like-type.js +442 -0
  52. package/dist/lib/table/arrow-api/enum.d.ts +92 -0
  53. package/dist/lib/table/arrow-api/enum.d.ts.map +1 -0
  54. package/dist/lib/table/arrow-api/enum.js +97 -0
  55. package/dist/lib/table/arrow-api/get-type-info.d.ts +13 -0
  56. package/dist/lib/table/arrow-api/get-type-info.d.ts.map +1 -0
  57. package/dist/lib/table/arrow-api/get-type-info.js +27 -0
  58. package/dist/lib/table/arrow-api/index.d.ts +5 -0
  59. package/dist/lib/table/arrow-api/index.d.ts.map +1 -0
  60. package/dist/lib/table/arrow-api/index.js +7 -0
  61. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts +10 -0
  62. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts.map +1 -0
  63. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.js +67 -0
  64. package/dist/lib/table/batch-builder/arrow-table-builder.d.ts +25 -0
  65. package/dist/lib/table/batch-builder/arrow-table-builder.d.ts.map +1 -0
  66. package/dist/lib/table/batch-builder/arrow-table-builder.js +99 -0
  67. package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts +19 -0
  68. package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts.map +1 -0
  69. package/dist/lib/table/batch-builder/base-table-batch-aggregator.js +63 -0
  70. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts +20 -0
  71. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts.map +1 -0
  72. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.js +83 -0
  73. package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts +21 -0
  74. package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts.map +1 -0
  75. package/dist/lib/table/batch-builder/row-table-batch-aggregator.js +85 -0
  76. package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts +24 -0
  77. package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts.map +1 -0
  78. package/dist/lib/table/batch-builder/table-batch-aggregator.js +4 -0
  79. package/dist/lib/table/batch-builder/table-batch-builder.d.ts +52 -0
  80. package/dist/lib/table/batch-builder/table-batch-builder.d.ts.map +1 -0
  81. package/dist/lib/table/batch-builder/table-batch-builder.js +148 -0
  82. package/dist/lib/table/batches/convert-batches.d.ts +10 -0
  83. package/dist/lib/table/batches/convert-batches.d.ts.map +1 -0
  84. package/dist/lib/table/batches/convert-batches.js +45 -0
  85. package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts +17 -0
  86. package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts.map +1 -0
  87. package/dist/lib/table/batches/make-arrow-batch-iterator.js +57 -0
  88. package/dist/lib/table/batches/make-table-batch-iterator.d.ts +15 -0
  89. package/dist/lib/table/batches/make-table-batch-iterator.d.ts.map +1 -0
  90. package/dist/lib/table/batches/make-table-batch-iterator.js +21 -0
  91. package/dist/lib/table/batches/make-table-from-batches.d.ts +9 -0
  92. package/dist/lib/table/batches/make-table-from-batches.d.ts.map +1 -0
  93. package/dist/lib/table/batches/make-table-from-batches.js +62 -0
  94. package/dist/lib/table/tables/convert-arrow-table.d.ts +24 -0
  95. package/dist/lib/table/tables/convert-arrow-table.d.ts.map +1 -0
  96. package/dist/lib/table/tables/convert-arrow-table.js +150 -0
  97. package/dist/lib/table/tables/convert-table.d.ts +43 -0
  98. package/dist/lib/table/tables/convert-table.d.ts.map +1 -0
  99. package/dist/lib/table/tables/convert-table.js +120 -0
  100. package/dist/lib/table/tables/make-table.d.ts +67 -0
  101. package/dist/lib/table/tables/make-table.d.ts.map +1 -0
  102. package/dist/lib/table/tables/make-table.js +101 -0
  103. package/dist/lib/table/tables/row-utils.d.ts +15 -0
  104. package/dist/lib/table/tables/row-utils.d.ts.map +1 -0
  105. package/dist/lib/table/tables/row-utils.js +49 -0
  106. package/dist/lib/table/tables/table-accessors.d.ts +68 -0
  107. package/dist/lib/table/tables/table-accessors.d.ts.map +1 -0
  108. package/dist/lib/table/tables/table-accessors.js +350 -0
  109. package/dist/lib/table/tables/table-column.d.ts +7 -0
  110. package/dist/lib/table/tables/table-column.d.ts.map +1 -0
  111. package/dist/lib/table/tables/table-column.js +39 -0
  112. package/dist/lib/table/tables/table-types.d.ts +12 -0
  113. package/dist/lib/table/tables/table-types.d.ts.map +1 -0
  114. package/dist/lib/table/tables/table-types.js +23 -0
  115. package/dist/lib/utils/assert.d.ts +2 -0
  116. package/dist/lib/utils/assert.d.ts.map +1 -0
  117. package/dist/lib/utils/assert.js +8 -0
  118. package/dist/lib/utils/async-queue.d.ts +18 -0
  119. package/dist/lib/utils/async-queue.d.ts.map +1 -0
  120. package/dist/lib/utils/async-queue.js +90 -0
  121. package/dist/types/types.d.ts +15 -0
  122. package/dist/types/types.d.ts.map +1 -0
  123. package/dist/types/types.js +4 -0
  124. package/package.json +52 -0
  125. package/src/index.ts +134 -0
  126. package/src/lib/arrow-utils/arrow-fixed-size-list-utils.ts +63 -0
  127. package/src/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.ts +48 -0
  128. package/src/lib/mesh/convert-mesh-to-table.ts +77 -0
  129. package/src/lib/mesh/convert-table-to-mesh.ts +49 -0
  130. package/src/lib/mesh/deduce-mesh-schema.ts +75 -0
  131. package/src/lib/mesh/mesh-utils.ts +64 -0
  132. package/src/lib/schema/convert-arrow-schema.ts +301 -0
  133. package/src/lib/schema/data-type.ts +103 -0
  134. package/src/lib/schema/deduce-table-schema.ts +119 -0
  135. package/src/lib/table/arrow-api/arrow-like-field.ts +53 -0
  136. package/src/lib/table/arrow-api/arrow-like-interface.ts +84 -0
  137. package/src/lib/table/arrow-api/arrow-like-schema.ts +101 -0
  138. package/src/lib/table/arrow-api/arrow-like-table.ts +85 -0
  139. package/src/lib/table/arrow-api/arrow-like-type.ts +502 -0
  140. package/src/lib/table/arrow-api/enum.ts +99 -0
  141. package/src/lib/table/arrow-api/get-type-info.ts +38 -0
  142. package/src/lib/table/arrow-api/index.ts +8 -0
  143. package/src/lib/table/batch-builder/arrow-table-batch-aggregator-builders.ts.disabled +190 -0
  144. package/src/lib/table/batch-builder/arrow-table-batch-aggregator.ts +82 -0
  145. package/src/lib/table/batch-builder/arrow-table-builder.ts +112 -0
  146. package/src/lib/table/batch-builder/base-table-batch-aggregator.ts +82 -0
  147. package/src/lib/table/batch-builder/columnar-table-batch-aggregator.ts +99 -0
  148. package/src/lib/table/batch-builder/row-table-batch-aggregator.ts +108 -0
  149. package/src/lib/table/batch-builder/table-batch-aggregator.ts +28 -0
  150. package/src/lib/table/batch-builder/table-batch-builder.ts +185 -0
  151. package/src/lib/table/batches/convert-batches.ts +83 -0
  152. package/src/lib/table/batches/make-arrow-batch-iterator.ts +75 -0
  153. package/src/lib/table/batches/make-table-batch-iterator.ts +25 -0
  154. package/src/lib/table/batches/make-table-from-batches.ts +84 -0
  155. package/src/lib/table/tables/convert-arrow-table.ts +198 -0
  156. package/src/lib/table/tables/convert-table.ts +151 -0
  157. package/src/lib/table/tables/make-table.ts +127 -0
  158. package/src/lib/table/tables/row-utils.ts +59 -0
  159. package/src/lib/table/tables/table-accessors.ts +417 -0
  160. package/src/lib/table/tables/table-column.ts +49 -0
  161. package/src/lib/table/tables/table-types.ts +37 -0
  162. package/src/lib/utils/assert.ts +8 -0
  163. package/src/lib/utils/async-queue.ts +104 -0
  164. package/src/types/types.ts +45 -0
package/src/index.ts ADDED
@@ -0,0 +1,134 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ // Schema
6
+ export {getArrayTypeFromDataType} from './lib/schema/data-type';
7
+
8
+ // Schema utils
9
+ export {
10
+ convertArrowToSchema,
11
+ convertSchemaToArrow,
12
+ // DETAILED FUNCTIONS
13
+ serializeArrowSchema,
14
+ deserializeArrowSchema,
15
+ serializeArrowMetadata,
16
+ deserializeArrowMetadata,
17
+ serializeArrowField,
18
+ deserializeArrowField,
19
+ serializeArrowType,
20
+ deserializeArrowType
21
+ } from './lib/schema/convert-arrow-schema';
22
+ export {getDataTypeFromArray} from './lib/schema/data-type';
23
+
24
+ // TABLE CATEGORY UTILS
25
+
26
+ export {deduceTableSchema} from './lib/schema/deduce-table-schema';
27
+ export {makeTableFromData} from './lib/table/tables/make-table';
28
+ export {makeTableFromBatches} from './lib/table/batches/make-table-from-batches';
29
+
30
+ export {convertTable} from './lib/table/tables/convert-table';
31
+ export {convertToObjectRow, convertToArrayRow} from './lib/table/tables/row-utils';
32
+ export {convertArrowToTable, convertTableToArrow} from './lib/table/tables/convert-arrow-table';
33
+
34
+ export {
35
+ makeTableBatchIterator,
36
+ makeBatchFromTable
37
+ } from './lib/table/batches/make-table-batch-iterator';
38
+ export {
39
+ makeArrowTableBatchIterator,
40
+ makeArrowRecordBatchIterator
41
+ } from './lib/table/batches/make-arrow-batch-iterator';
42
+ export {convertBatch, convertBatches} from './lib/table/batches/convert-batches';
43
+
44
+ export {
45
+ isArrayRowTable,
46
+ isObjectRowTable,
47
+ isColumnarTable,
48
+ isGeoJSONTable,
49
+ isArrowTable
50
+ } from './lib/table/tables/table-types';
51
+
52
+ export {
53
+ isTable,
54
+ getTableLength,
55
+ getTableNumCols,
56
+ getTableCell,
57
+ getTableCellAt,
58
+ getTableRowShape,
59
+ getTableColumnIndex,
60
+ getTableColumnName,
61
+ getTableRowAsObject,
62
+ getTableRowAsArray,
63
+ makeRowIterator,
64
+ makeArrayRowIterator,
65
+ makeObjectRowIterator
66
+ } from './lib/table/tables/table-accessors';
67
+
68
+ // Table batch builders
69
+
70
+ export {TableBatchBuilder} from './lib/table/batch-builder/table-batch-builder';
71
+ export type {TableBatchAggregator} from './lib/table/batch-builder/table-batch-aggregator';
72
+ export {RowTableBatchAggregator} from './lib/table/batch-builder/row-table-batch-aggregator';
73
+ export {ColumnarTableBatchAggregator} from './lib/table/batch-builder/columnar-table-batch-aggregator';
74
+
75
+ export {ArrowLikeTable} from './lib/table/arrow-api/arrow-like-table';
76
+
77
+ // MESH CATEGORY
78
+
79
+ export {getMeshSize, getMeshBoundingBox} from './lib/mesh/mesh-utils';
80
+ export {convertMeshToTable} from './lib/mesh/convert-mesh-to-table';
81
+ export {convertTableToMesh} from './lib/mesh/convert-table-to-mesh';
82
+ export {
83
+ deduceMeshSchema,
84
+ deduceMeshField,
85
+ makeMeshAttributeMetadata
86
+ } from './lib/mesh/deduce-mesh-schema';
87
+
88
+ // SCHEMA
89
+ export {
90
+ Schema as ArrowLikeSchema,
91
+ Field as ArrowLikeField,
92
+ DataType as ArrowLikeDataType
93
+ // Null,
94
+ // Binary,
95
+ // Bool,
96
+ // Int,
97
+ // Int8,
98
+ // Int16,
99
+ // Int32,
100
+ // Int64,
101
+ // Uint8,
102
+ // Uint16,
103
+ // Uint32,
104
+ // Uint64,
105
+ // Float,
106
+ // Float16,
107
+ // Float32,
108
+ // Float64,
109
+ // Utf8,
110
+ // Date,
111
+ // DateDay,
112
+ // DateMillisecond,
113
+ // Time,
114
+ // TimeMillisecond,
115
+ // TimeSecond,
116
+ // Timestamp,
117
+ // TimestampSecond,
118
+ // TimestampMillisecond,
119
+ // TimestampMicrosecond,
120
+ // TimestampNanosecond,
121
+ // Interval,
122
+ // IntervalDayTime,
123
+ // IntervalYearMonth,
124
+ // FixedSizeList,
125
+ // Struct
126
+ } from './lib/table/arrow-api/index';
127
+
128
+ // EXPERIMENTAL APIs
129
+ export {ArrowTableBuilder} from './lib/table/batch-builder/arrow-table-builder';
130
+
131
+ // Schema utils
132
+ export {getTypeInfo} from './lib/table/arrow-api/get-type-info';
133
+
134
+ export {default as AsyncQueue} from './lib/utils/async-queue';
@@ -0,0 +1,63 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {TypedArray} from '@math.gl/types';
6
+ import * as arrow from 'apache-arrow';
7
+ import {getDataTypeFromArray} from '../schema/data-type';
8
+ import {deserializeArrowType} from '../schema/convert-arrow-schema';
9
+
10
+ export function isFixedSizeList(vector: arrow.Vector): vector is arrow.Vector<arrow.FixedSizeList> {
11
+ return vector.type instanceof arrow.FixedSizeList;
12
+ }
13
+
14
+ export function getFixedSizeListSize(vector: arrow.Vector): number {
15
+ return vector.type instanceof arrow.FixedSizeList ? vector.type.listSize : 1;
16
+ }
17
+
18
+ /** Get Arrow FixedSizeList vector from a typed array */
19
+ export function getFixedSizeListVector(
20
+ typedArray: TypedArray,
21
+ stride: number
22
+ ): arrow.Vector<arrow.FixedSizeList> {
23
+ const data = getFixedSizeListData(typedArray, stride);
24
+ return new arrow.Vector<arrow.FixedSizeList>([data]);
25
+ }
26
+
27
+ /** Get Arrow FixedSizeList vector from a typed array */
28
+ export function getFixedSizeListData(
29
+ typedArray: TypedArray,
30
+ stride: number
31
+ ): arrow.Data<arrow.FixedSizeList> {
32
+ const listType = getFixedSizeListType(typedArray, stride);
33
+ const nestedType = listType.children[0].type;
34
+ const buffers: Partial<Record<arrow.BufferType, any>> = {
35
+ // valueOffsets: undefined,
36
+ [arrow.BufferType.DATA]: typedArray // values
37
+ // nullBitmap: undefined,
38
+ // typeIds: undefined
39
+ };
40
+
41
+ // Note: The contiguous array of data is held by the nested "primitive type" column
42
+ const nestedData = new arrow.Data(nestedType, 0, typedArray.length, 0, buffers);
43
+
44
+ // Wrapped in a FixedSizeList column that provides a "strided" view of the data
45
+ const data = new arrow.Data<arrow.FixedSizeList>(
46
+ listType,
47
+ 0,
48
+ typedArray.length / stride,
49
+ 0,
50
+ undefined,
51
+ [nestedData]
52
+ );
53
+
54
+ return data;
55
+ }
56
+
57
+ /** Get Arrow FixedSizeList vector from a typed array */
58
+ export function getFixedSizeListType(typedArray: TypedArray, stride: number): arrow.FixedSizeList {
59
+ const {type} = getDataTypeFromArray(typedArray);
60
+ const arrowType = deserializeArrowType(type);
61
+ const listType = new arrow.FixedSizeList(stride, new arrow.Field('value', arrowType));
62
+ return listType;
63
+ }
@@ -0,0 +1,48 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {TypedArray} from '@math.gl/types';
6
+ import * as arrow from 'apache-arrow';
7
+ import {getDataTypeFromArray} from '../schema/data-type';
8
+ import {deserializeArrowType} from '../schema/convert-arrow-schema';
9
+
10
+ export function isListFixedSizeList(
11
+ vector: arrow.Vector
12
+ ): vector is arrow.Vector<arrow.FixedSizeList> {
13
+ return vector.type instanceof arrow.FixedSizeList;
14
+ }
15
+
16
+ export function getListFixedSizeListSize(vector: arrow.Vector): number {
17
+ return vector.type instanceof arrow.FixedSizeList ? vector.type.listSize : 1;
18
+ }
19
+
20
+ /** Get Arrow FixedSizeList vector from a typed array */
21
+ export function getListFixedSizeListVector(
22
+ indexes: Uint32Array,
23
+ typedArray: TypedArray,
24
+ stride: number
25
+ ): arrow.Vector<arrow.FixedSizeList> {
26
+ const data = getFixedSizeListData(typedArray, stride);
27
+ return new arrow.Vector<arrow.FixedSizeList>([data]);
28
+ }
29
+
30
+ /** Get Arrow FixedSizeList vector from a typed array */
31
+ export function getFixedSizeListData(
32
+ typedArray: TypedArray,
33
+ stride: number
34
+ ): arrow.Data<arrow.FixedSizeList> {
35
+ const listType = getFixedSizeListType(typedArray, stride);
36
+ const data = new arrow.Data<arrow.FixedSizeList>(listType, 0, typedArray.length / stride, 0, [
37
+ typedArray
38
+ ]);
39
+ return data;
40
+ }
41
+
42
+ /** Get Arrow FixedSizeList vector from a typed array */
43
+ export function getFixedSizeListType(typedArray: TypedArray, stride: number): arrow.FixedSizeList {
44
+ const {type} = getDataTypeFromArray(typedArray);
45
+ const arrowType = deserializeArrowType(type);
46
+ const listType = new arrow.FixedSizeList(stride, new arrow.Field('value', arrowType));
47
+ return listType;
48
+ }
@@ -0,0 +1,77 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Mesh, ArrowTable, ColumnarTable} from '@loaders.gl/schema';
6
+ import * as arrow from 'apache-arrow';
7
+ import {getFixedSizeListData} from '../arrow-utils/arrow-fixed-size-list-utils';
8
+ import {deserializeArrowSchema} from '../schema/convert-arrow-schema';
9
+
10
+ export function convertMeshToTable(mesh: Mesh, shape: 'columnar-table'): ColumnarTable;
11
+ export function convertMeshToTable(mesh: Mesh, shape: 'arrow-table'): ArrowTable;
12
+
13
+ /**
14
+ * Convert a mesh to a specific shape
15
+ */
16
+ export function convertMeshToTable(
17
+ mesh: Mesh,
18
+ shape: 'columnar-table' | 'arrow-table'
19
+ ): Mesh | ColumnarTable | ArrowTable {
20
+ switch (shape) {
21
+ case 'columnar-table':
22
+ return convertMeshToColumnarTable(mesh);
23
+ case 'arrow-table':
24
+ return convertMeshToArrowTable(mesh);
25
+ default:
26
+ throw new Error(shape);
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Convert a loaders.gl Mesh to a Columnar Table
32
+ * @param mesh
33
+ * @returns
34
+ */
35
+ export function convertMeshToColumnarTable(mesh: Mesh): ColumnarTable {
36
+ const columns = {};
37
+
38
+ for (const [columnName, attribute] of Object.entries(mesh.attributes)) {
39
+ columns[columnName] = attribute.value;
40
+ }
41
+
42
+ return {
43
+ shape: 'columnar-table',
44
+ schema: mesh.schema,
45
+ data: columns
46
+ };
47
+ }
48
+
49
+ /**
50
+ * * Convert a loaders.gl Mesh to an Apache Arrow Table
51
+ * @param mesh
52
+ * @param metadata
53
+ * @param batchSize
54
+ * @returns
55
+ */
56
+ export function convertMeshToArrowTable(mesh: Mesh, batchSize?: number): ArrowTable {
57
+ const {schema} = mesh;
58
+ const arrowSchema = deserializeArrowSchema(schema);
59
+
60
+ const arrowDatas: arrow.Data[] = [];
61
+ for (const attributeKey in mesh.attributes) {
62
+ const attribute = mesh.attributes[attributeKey];
63
+ const {value, size = 1} = attribute;
64
+
65
+ const listData = getFixedSizeListData(value, size);
66
+ arrowDatas.push(listData);
67
+ // fields.push(field);
68
+ }
69
+
70
+ const structField = new arrow.Struct(arrowSchema.fields);
71
+ const length = arrowDatas[0].length;
72
+ const structData = new arrow.Data(structField, 0, length, 0, undefined, arrowDatas);
73
+ const recordBatch = new arrow.RecordBatch(arrowSchema, structData);
74
+ const table = new arrow.Table([recordBatch]);
75
+
76
+ return {shape: 'arrow-table', schema, data: table};
77
+ }
@@ -0,0 +1,49 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Mesh, ColumnarTable, ArrowTable, Schema} from '@loaders.gl/schema';
6
+ import {getFixedSizeListSize} from '../arrow-utils/arrow-fixed-size-list-utils';
7
+ import {serializeArrowSchema} from '../schema/convert-arrow-schema';
8
+ // import {makeMeshAttributeMetadata} from './deduce-mesh-schema';
9
+
10
+ /**
11
+ * Convert a mesh to a specific shape
12
+ */
13
+ export function convertTableToMesh(table: ColumnarTable | ArrowTable): Mesh {
14
+ switch (table.shape) {
15
+ // case 'columnar-table':
16
+ // return convertColumnarTableToMesh(table);
17
+ case 'arrow-table':
18
+ return convertArrowTableToMesh(table);
19
+ default:
20
+ throw new Error(table.shape);
21
+ }
22
+ }
23
+
24
+ export function convertArrowTableToMesh(table: ArrowTable): Mesh {
25
+ const arrowTable = table.data;
26
+
27
+ const schema = serializeArrowSchema(arrowTable.schema);
28
+ const fields = schema.fields;
29
+
30
+ const attributes: Mesh['attributes'] = {};
31
+ for (const field of fields) {
32
+ const {name} = field;
33
+ const attributeData = arrowTable.getChild(name)!;
34
+ const size = getFixedSizeListSize(attributeData);
35
+ const typedArray = attributeData?.toArray();
36
+ attributes[name] = {value: typedArray, size};
37
+ }
38
+
39
+ fixMetadata(schema);
40
+ const topology = schema.metadata.topology as any;
41
+
42
+ return {schema, attributes, topology, mode: 0};
43
+ }
44
+
45
+ function fixMetadata(schema: Schema) {
46
+ schema.metadata ||= {};
47
+ schema.metadata.topology ||= 'point-list';
48
+ schema.metadata.mode ||= '0';
49
+ }
@@ -0,0 +1,75 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {MeshAttribute, MeshAttributes, Schema, Field} from '@loaders.gl/schema';
6
+ import {getDataTypeFromTypedArray} from '../schema/data-type';
7
+
8
+ /**
9
+ * Create a schema for mesh attributes data
10
+ * @param attributes
11
+ * @param metadata
12
+ * @returns
13
+ */
14
+ export function deduceMeshSchema(
15
+ attributes: MeshAttributes,
16
+ metadata: Record<string, string> = {}
17
+ ): Schema {
18
+ const fields = deduceMeshFields(attributes);
19
+ return {fields, metadata};
20
+ }
21
+
22
+ /**
23
+ * Create arrow-like schema field for mesh attribute
24
+ * @param attributeName
25
+ * @param attribute
26
+ * @param optionalMetadata
27
+ * @returns
28
+ */
29
+ export function deduceMeshField(
30
+ name: string,
31
+ attribute: MeshAttribute,
32
+ optionalMetadata?: Record<string, string>
33
+ ): Field {
34
+ const type = getDataTypeFromTypedArray(attribute.value);
35
+ const metadata = optionalMetadata ? optionalMetadata : makeMeshAttributeMetadata(attribute);
36
+ return {
37
+ name,
38
+ type: {type: 'fixed-size-list', listSize: attribute.size, children: [{name: 'value', type}]},
39
+ nullable: false,
40
+ metadata
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Create fields array for mesh attributes
46
+ * @param attributes
47
+ * @returns
48
+ */
49
+ function deduceMeshFields(attributes: MeshAttributes): Field[] {
50
+ const fields: Field[] = [];
51
+ for (const attributeName in attributes) {
52
+ const attribute: MeshAttribute = attributes[attributeName];
53
+ fields.push(deduceMeshField(attributeName, attribute));
54
+ }
55
+ return fields;
56
+ }
57
+
58
+ /**
59
+ * Make metadata by mesh attribute properties
60
+ * @param attribute
61
+ * @returns
62
+ */
63
+ export function makeMeshAttributeMetadata(attribute: MeshAttribute): Record<string, string> {
64
+ const result: Record<string, string> = {};
65
+ if ('byteOffset' in attribute) {
66
+ result.byteOffset = attribute.byteOffset!.toString(10);
67
+ }
68
+ if ('byteStride' in attribute) {
69
+ result.byteStride = attribute.byteStride!.toString(10);
70
+ }
71
+ if ('normalized' in attribute) {
72
+ result.normalized = attribute.normalized!.toString();
73
+ }
74
+ return result;
75
+ }
@@ -0,0 +1,64 @@
1
+ // Mesh category utilities
2
+ // TODO - move to mesh category module, or to math.gl/geometry module
3
+ import {MeshAttributes} from '@loaders.gl/schema';
4
+ import {TypedArray} from '../../types/types';
5
+
6
+ type TypedArrays = {[key: string]: TypedArray};
7
+
8
+ /**
9
+ * Holds an axis aligned bounding box
10
+ * TODO - make sure AxisAlignedBoundingBox in math.gl/culling understands this format (or change this format)
11
+ */
12
+ type BoundingBox = [[number, number, number], [number, number, number]];
13
+
14
+ /**
15
+ * Get number of vertices in mesh
16
+ * @param attributes
17
+ */
18
+ export function getMeshSize(attributes: TypedArrays): number {
19
+ let size = 0;
20
+ for (const attributeName in attributes) {
21
+ const attribute = attributes[attributeName];
22
+ if (ArrayBuffer.isView(attribute)) {
23
+ // @ts-ignore DataView doesn't have BYTES_PER_ELEMENT
24
+ size += attribute.byteLength * attribute.BYTES_PER_ELEMENT;
25
+ }
26
+ }
27
+ return size;
28
+ }
29
+
30
+ /**
31
+ * Get the (axis aligned) bounding box of a mesh
32
+ * @param attributes
33
+ * @returns array of two vectors representing the axis aligned bounding box
34
+ */
35
+ // eslint-disable-next-line complexity
36
+ export function getMeshBoundingBox(attributes: MeshAttributes): BoundingBox {
37
+ let minX = Infinity;
38
+ let minY = Infinity;
39
+ let minZ = Infinity;
40
+ let maxX = -Infinity;
41
+ let maxY = -Infinity;
42
+ let maxZ = -Infinity;
43
+
44
+ const positions = attributes.POSITION ? attributes.POSITION.value : [];
45
+ const len = positions && positions.length;
46
+
47
+ for (let i = 0; i < len; i += 3) {
48
+ const x = positions[i];
49
+ const y = positions[i + 1];
50
+ const z = positions[i + 2];
51
+
52
+ minX = x < minX ? x : minX;
53
+ minY = y < minY ? y : minY;
54
+ minZ = z < minZ ? z : minZ;
55
+
56
+ maxX = x > maxX ? x : maxX;
57
+ maxY = y > maxY ? y : maxY;
58
+ maxZ = z > maxZ ? z : maxZ;
59
+ }
60
+ return [
61
+ [minX, minY, minZ],
62
+ [maxX, maxY, maxZ]
63
+ ];
64
+ }