@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
@@ -0,0 +1,198 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import * as arrow from 'apache-arrow';
6
+ import type {
7
+ Table,
8
+ ArrayRowTable,
9
+ ColumnarTable,
10
+ ObjectRowTable,
11
+ GeoJSONTable,
12
+ ArrowTable,
13
+ Feature
14
+ } from '@loaders.gl/schema';
15
+
16
+ import {convertTable} from './convert-table';
17
+ import {convertArrowToSchema} from '../../schema/convert-arrow-schema';
18
+ import {makeArrowRecordBatchIterator} from '../batches/make-arrow-batch-iterator';
19
+
20
+ /**
21
+ * * Convert a loaders.gl Table to an Apache Arrow Table
22
+ * @param mesh
23
+ * @param metadata
24
+ * @param batchSize
25
+ * @returns
26
+ */
27
+ export function convertTableToArrow(table: Table, options?: {batchSize?: number}): arrow.Table {
28
+ switch (table.shape) {
29
+ case 'arrow-table':
30
+ return table.data;
31
+
32
+ case 'columnar-table':
33
+ // TODO - optimized implementation is possible
34
+ // return convertColumnarTableToArrow(table, options);
35
+
36
+ // fall through
37
+
38
+ default:
39
+ const arrowBatchIterator = makeArrowRecordBatchIterator(table, options);
40
+ return new arrow.Table(arrowBatchIterator);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Convert an Apache Arrow table to a loaders.gl Table
46
+ * @note Currently does not convert schema
47
+ */
48
+ export function convertArrowToTable(arrow: arrow.Table, shape: 'arrow-table'): ArrowTable;
49
+ export function convertArrowToTable(arrow: arrow.Table, shape: 'columnar-table'): ColumnarTable;
50
+ export function convertArrowToTable(arrow: arrow.Table, shape: 'object-row-table'): ObjectRowTable;
51
+ export function convertArrowToTable(arrow: arrow.Table, shape: 'array-row-table'): ArrayRowTable;
52
+ export function convertArrowToTable(arrow: arrow.Table, shape: 'geojson-table'): GeoJSONTable;
53
+ export function convertArrowToTable(arrow: arrow.Table, shape: 'columnar-table'): ColumnarTable;
54
+ export function convertArrowToTable(arrow: arrow.Table, shape: Table['shape']): Table;
55
+
56
+ /**
57
+ * Convert an Apache Arrow table to a loaders.gl Table
58
+ * @note Currently does not convert schema
59
+ */
60
+ export function convertArrowToTable(arrowTable: arrow.Table, shape: Table['shape']): Table {
61
+ switch (shape) {
62
+ case 'arrow-table':
63
+ return convertArrowToArrowTable(arrowTable);
64
+ case 'array-row-table':
65
+ return convertArrowToArrayRowTable(arrowTable);
66
+ case 'object-row-table':
67
+ return convertArrowToObjectRowTable(arrowTable);
68
+ case 'columnar-table':
69
+ return convertArrowToColumnarTable(arrowTable);
70
+ case 'geojson-table':
71
+ return convertArrowToGeoJSONTable(arrowTable);
72
+ default:
73
+ throw new Error(shape);
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Wrap an apache arrow table in a loaders.gl table wrapper.
79
+ * From this additional conversions are available.
80
+ * @param arrowTable
81
+ * @returns
82
+ */
83
+ function convertArrowToArrowTable(arrowTable: arrow.Table): ArrowTable {
84
+ return {
85
+ shape: 'arrow-table',
86
+ schema: convertArrowToSchema(arrowTable.schema),
87
+ data: arrowTable
88
+ };
89
+ }
90
+
91
+ function convertArrowToArrayRowTable(arrowTable: arrow.Table): Table {
92
+ const columnarTable = convertArrowToColumnarTable(arrowTable);
93
+ return convertTable(columnarTable, 'array-row-table');
94
+ }
95
+
96
+ function convertArrowToObjectRowTable(arrowTable: arrow.Table): Table {
97
+ const columnarTable = convertArrowToColumnarTable(arrowTable);
98
+ return convertTable(columnarTable, 'object-row-table');
99
+ }
100
+
101
+ /**
102
+ * Convert an Apache Arrow table to a ColumnarTable
103
+ * @note Currently does not convert schema
104
+ */
105
+ function convertArrowToColumnarTable(arrowTable: arrow.Table): ColumnarTable {
106
+ // TODO - avoid calling `getColumn` on columns we are not interested in?
107
+ // Add options object?
108
+
109
+ const columns: ColumnarTable['data'] = {};
110
+
111
+ for (const field of arrowTable.schema.fields) {
112
+ // This (is intended to) coalesce all record batches into a single typed array
113
+ const arrowColumn = arrowTable.getChild(field.name);
114
+ const values = arrowColumn?.toArray();
115
+ columns[field.name] = values;
116
+ }
117
+
118
+ const schema = convertArrowToSchema(arrowTable.schema);
119
+
120
+ return {
121
+ shape: 'columnar-table',
122
+ schema,
123
+ data: columns
124
+ };
125
+ }
126
+
127
+ /**
128
+ * Convert an Apache Arrow table to a GeoJSONTable
129
+ * @note Currently does not convert schema
130
+ */
131
+ function convertArrowToGeoJSONTable(arrowTable: arrow.Table): GeoJSONTable {
132
+ const schema = convertArrowToSchema(arrowTable.schema);
133
+
134
+ const features: Feature[] = [];
135
+
136
+ for (let row = 0; row < arrowTable.numRows; row++) {
137
+ // parse arrow geometry to geojson feature
138
+ const properties = arrowTable.get(row)?.toJSON() || {};
139
+ features.push({type: 'Feature', geometry: null!, properties});
140
+ }
141
+
142
+ return {
143
+ shape: 'geojson-table',
144
+ type: 'FeatureCollection',
145
+ schema,
146
+ features
147
+ };
148
+ }
149
+
150
+ // /**
151
+ // * Wrap an apache arrow table in a loaders.gl table wrapper.
152
+ // * From this additional conversions are available.
153
+ // * @param arrowTable
154
+ // * @returns
155
+ // */
156
+ // function convertArrowToArrowTable(arrowTable: arrow.Table): ArrowTable {
157
+ // return {
158
+ // shape: 'arrow-table',
159
+ // schema: convertArrowToSchema(arrowTable.schema),
160
+ // data: arrowTable
161
+ // };
162
+ // }
163
+
164
+ // function convertArrowToArrayRowTable(arrowTable: arrow.Table): Table {
165
+ // const columnarTable = convertArrowToColumnarTable(arrowTable);
166
+ // return convertTable(columnarTable, 'array-row-table');
167
+ // }
168
+
169
+ // function convertArrowToObjectRowTable(arrowTable: arrow.Table): Table {
170
+ // const columnarTable = convertArrowToColumnarTable(arrowTable);
171
+ // return convertTable(columnarTable, 'object-row-table');
172
+ // }
173
+
174
+ // /**
175
+ // * Convert an Apache Arrow table to a ColumnarTable
176
+ // * @note Currently does not convert schema
177
+ // */
178
+ // function convertArrowToColumnarTable(arrowTable: arrow.Table): ColumnarTable {
179
+ // // TODO - avoid calling `getColumn` on columns we are not interested in?
180
+ // // Add options object?
181
+
182
+ // const columns: ColumnarTable['data'] = {};
183
+
184
+ // for (const field of arrowTable.schema.fields) {
185
+ // // This (is intended to) coalesce all record batches into a single typed array
186
+ // const arrowColumn = arrowTable.getChild(field.name);
187
+ // const values = arrowColumn?.toArray();
188
+ // columns[field.name] = values;
189
+ // }
190
+
191
+ // const schema = convertArrowToSchema(arrowTable.schema);
192
+
193
+ // return {
194
+ // shape: 'columnar-table',
195
+ // schema,
196
+ // data: columns
197
+ // };
198
+ // }
@@ -0,0 +1,151 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {
6
+ Table,
7
+ ArrayRowTable,
8
+ ObjectRowTable,
9
+ ColumnarTable,
10
+ ArrowTable
11
+ } from '@loaders.gl/schema';
12
+ import {
13
+ getTableCell,
14
+ getTableLength,
15
+ getTableRowAsArray,
16
+ getTableRowAsObject
17
+ } from './table-accessors';
18
+ import {deduceTableSchema} from '../../schema/deduce-table-schema';
19
+ import {makeColumnFromField} from './table-column';
20
+ import {convertTableToArrow} from './convert-arrow-table';
21
+
22
+ export function convertTable(table: Table, shape: 'object-row-table'): ObjectRowTable;
23
+ export function convertTable(table: Table, shape: 'array-row-table'): ArrayRowTable;
24
+ export function convertTable(table: Table, shape: 'columnar-table'): ColumnarTable;
25
+ export function convertTable(table: Table, shape: 'arrow-table'): ArrowTable;
26
+
27
+ /**
28
+ * Convert a table to a different shape
29
+ * @param table
30
+ * @param shape
31
+ * @returns
32
+ */
33
+ export function convertTable(
34
+ table: Table,
35
+ shape: 'object-row-table' | 'array-row-table' | 'columnar-table' | 'arrow-table'
36
+ ) {
37
+ switch (shape) {
38
+ case 'object-row-table':
39
+ return convertToObjectRowTable(table);
40
+ case 'array-row-table':
41
+ return convertToArrayRowTable(table);
42
+ case 'columnar-table':
43
+ return convertToColumnarTable(table);
44
+ case 'arrow-table':
45
+ return convertToArrowTable(table);
46
+ default:
47
+ throw new Error(shape);
48
+ }
49
+ }
50
+
51
+ /** Convert any simple table into columnar format */
52
+ export function convertToColumnarTable(table: Table): ColumnarTable {
53
+ // TODO - should schema really be optional?
54
+ const schema = table.schema || deduceTableSchema(table);
55
+ const fields = table.schema?.fields || [];
56
+
57
+ if (table.shape === 'columnar-table') {
58
+ return {...table, schema};
59
+ }
60
+
61
+ const length = getTableLength(table);
62
+
63
+ const columns: {[column: string]: ArrayLike<unknown>} = {};
64
+ for (const field of fields) {
65
+ const column = makeColumnFromField(field, length);
66
+ columns[field.name] = column;
67
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
68
+ column[rowIndex] = getTableCell(table, rowIndex, field.name);
69
+ }
70
+ }
71
+
72
+ return {
73
+ shape: 'columnar-table',
74
+ schema,
75
+ data: columns
76
+ };
77
+ }
78
+
79
+ /** Convert any table into array row format */
80
+ export function convertToArrayRowTable(table: Table): ArrayRowTable {
81
+ if (table.shape === 'array-row-table') {
82
+ return table;
83
+ }
84
+ const length = getTableLength(table);
85
+ const data = new Array<unknown[]>(length);
86
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
87
+ data[rowIndex] = getTableRowAsArray(table, rowIndex);
88
+ }
89
+ return {
90
+ shape: 'array-row-table',
91
+ schema: table.schema,
92
+ data
93
+ };
94
+ }
95
+
96
+ /** Convert any table into object row format */
97
+ export function convertToObjectRowTable(table: Table): ObjectRowTable {
98
+ if (table.shape === 'object-row-table') {
99
+ return table;
100
+ }
101
+ const length = getTableLength(table);
102
+ const data = new Array<{[key: string]: unknown}>(length);
103
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
104
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
105
+ }
106
+ return {
107
+ shape: 'object-row-table',
108
+ schema: table.schema,
109
+ data
110
+ };
111
+ }
112
+
113
+ /**
114
+ * Convert a table to apache arrow format
115
+ * @note this depends on the `@loaders.gl/arrow module being imported
116
+ */
117
+ export function convertToArrowTable(table: Table): ArrowTable {
118
+ const arrowTable = convertTableToArrow(table);
119
+ return {
120
+ shape: 'arrow-table',
121
+ schema: table.schema,
122
+ data: arrowTable
123
+ };
124
+ }
125
+
126
+ /**
127
+ *
128
+ * @note - should be part of schema module
129
+ *
130
+ export function convertColumnarToRowFormatTable(columnarTable: ColumnarTable): ObjectRowTable {
131
+ const tableKeys = Object.keys(columnarTable);
132
+ const tableRowsCount = columnarTable[tableKeys[0]].length;
133
+
134
+ const rowFormatTable: {}[] = [];
135
+
136
+ for (let index = 0; index < tableRowsCount; index++) {
137
+ const tableItem = {};
138
+ for (let keyIndex = 0; keyIndex < tableKeys.length; keyIndex++) {
139
+ const fieldName = tableKeys[keyIndex];
140
+ tableItem[fieldName] = columnarTable[fieldName][index];
141
+ }
142
+ rowFormatTable.push(tableItem);
143
+ }
144
+
145
+ return {
146
+ shape: 'object-row-table',
147
+ schema: columnarTable.schema,
148
+ data: rowFormatTable
149
+ };
150
+ }
151
+ */
@@ -0,0 +1,127 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import * as arrow from 'apache-arrow';
6
+ import type {
7
+ Table,
8
+ ArrayRowTable,
9
+ ObjectRowTable,
10
+ ColumnarTable,
11
+ ArrowTable
12
+ } from '@loaders.gl/schema';
13
+ import {deduceTableSchema} from '../../schema/deduce-table-schema';
14
+
15
+ /**
16
+ * Makes a typed table from data.
17
+ * @throws Row tables must contain at least one row. Columnar tables must contain empty arrays
18
+ */
19
+ export function makeTableFromData(data: unknown[][]): ArrayRowTable;
20
+ export function makeTableFromData(data: {[column: string]: unknown}[]): ObjectRowTable;
21
+ export function makeTableFromData(data: {[column: string]: ArrayLike<unknown>}): ColumnarTable;
22
+ export function makeTableFromData(data: arrow.Table): ArrowTable;
23
+
24
+ export function makeTableFromData(data: unknown): Table {
25
+ let table: Table;
26
+ switch (getTableShapeFromData(data)) {
27
+ case 'array-row-table':
28
+ table = {shape: 'array-row-table', data: data as unknown[][]};
29
+ break;
30
+ case 'object-row-table':
31
+ table = {shape: 'object-row-table', data: data as {[key: string]: unknown}[]};
32
+ break;
33
+ case 'columnar-table':
34
+ table = {shape: 'columnar-table', data: data as {[column: string]: ArrayLike<unknown>}};
35
+ break;
36
+ case 'arrow-table':
37
+ table = {shape: 'arrow-table', data: data as arrow.Table};
38
+ break;
39
+ default:
40
+ throw new Error('table');
41
+ }
42
+ const schema = deduceTableSchema(table);
43
+ return {...table, schema};
44
+ }
45
+
46
+ /** Helper function to get shape of data */
47
+ function getTableShapeFromData(data) {
48
+ if (data instanceof arrow.Table) {
49
+ return 'arrow-table';
50
+ }
51
+
52
+ if (Array.isArray(data)) {
53
+ if (data.length === 0) {
54
+ throw new Error('cannot deduce type of empty table');
55
+ }
56
+
57
+ // Deduce the table shape from the first row
58
+ const firstRow = data[0];
59
+
60
+ if (Array.isArray(firstRow)) {
61
+ return 'array-row-table';
62
+ }
63
+
64
+ if (firstRow && typeof firstRow === 'object') {
65
+ return 'object-row-table';
66
+ }
67
+ }
68
+
69
+ if (data && typeof data === 'object') {
70
+ return 'columnar-table';
71
+ }
72
+
73
+ throw new Error('invalid table');
74
+ }
75
+
76
+ /** Convert any table into object row format *
77
+ export function makeColumnarTable(table: Table): ColumnarTable {
78
+ if (table.shape === 'columnar-table') {
79
+ return table;
80
+ }
81
+ const length = getTableLength(table);
82
+ const data = new Array<{[key: string]: unknown}>(length);
83
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
84
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
85
+ }
86
+ return {
87
+ shape: 'columnar-table',
88
+ schema: table.schema,
89
+ data
90
+ };
91
+ }
92
+
93
+
94
+ /** Convert any table into array row format *
95
+ export function makeArrayRowTable(table: TableLike): ArrayRowTable {
96
+ if (table.shape === 'array-row-table') {
97
+ return table;
98
+ }
99
+ const length = getTableLength(table);
100
+ const data = new Array<unknown[]>(length);
101
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
102
+ data[rowIndex] = getTableRowAsArray(table, rowIndex);
103
+ }
104
+ return {
105
+ shape: 'array-row-table',
106
+ schema: table.schema,
107
+ data
108
+ };
109
+ }
110
+
111
+ /** Convert any table into object row format *
112
+ export function makeObjectRowTable(table: Table): ObjectRowTable {
113
+ if (table.shape === 'object-row-table') {
114
+ return table;
115
+ }
116
+ const length = getTableLength(table);
117
+ const data = new Array<{[key: string]: unknown}>(length);
118
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
119
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
120
+ }
121
+ return {
122
+ shape: 'object-row-table',
123
+ schema: table.schema,
124
+ data
125
+ };
126
+ }
127
+ */
@@ -0,0 +1,59 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** Convert an object row to an array row */
6
+ export function convertToObjectRow(
7
+ arrayRow: unknown[],
8
+ headers: string[] | null
9
+ ): {[columnName: string]: unknown} {
10
+ if (!arrayRow) {
11
+ throw new Error('null row');
12
+ }
13
+ const objectRow: {[columnName: string]: unknown} = {};
14
+ if (headers) {
15
+ for (let i = 0; i < headers.length; i++) {
16
+ objectRow[headers[i]] = arrayRow[i];
17
+ }
18
+ } else {
19
+ for (let i = 0; i < arrayRow.length; i++) {
20
+ const columnName = `column-${i}`;
21
+ objectRow[columnName] = arrayRow[i];
22
+ }
23
+ }
24
+ return objectRow;
25
+ }
26
+
27
+ /** Convert an object row to an array row */
28
+ export function convertToArrayRow(
29
+ objectRow: {[columnName: string]: unknown},
30
+ headers: string[] | null
31
+ ): unknown[] {
32
+ if (!objectRow) {
33
+ throw new Error('null row');
34
+ }
35
+
36
+ if (headers) {
37
+ const arrayRow = new Array(headers.length);
38
+ for (let i = 0; i < headers.length; i++) {
39
+ arrayRow[i] = objectRow[headers[i]];
40
+ }
41
+ return arrayRow;
42
+ }
43
+ return Object.values(objectRow);
44
+ }
45
+
46
+ /** Get headers from a sample array row */
47
+ export function inferHeadersFromArrayRow(arrayRow: unknown[]) {
48
+ const headers: string[] = [];
49
+ for (let i = 0; i < arrayRow.length; i++) {
50
+ const columnName = `column-${i}`;
51
+ headers.push(columnName);
52
+ }
53
+ return headers;
54
+ }
55
+
56
+ /** Get headers from a smaple object row */
57
+ export function inferHeadersFromObjectRow(row: {[columnName: string]: unknown}) {
58
+ return Object.keys(row);
59
+ }