@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,350 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ export function isTable(table) {
5
+ const shape = typeof table === 'object' && table?.shape;
6
+ switch (shape) {
7
+ case 'array-row-table':
8
+ case 'object-row-table':
9
+ return Array.isArray(table.data);
10
+ case 'geojson-table':
11
+ return Array.isArray(table.features);
12
+ case 'columnar-table':
13
+ return table.data && typeof table.data === 'object';
14
+ case 'arrow-table':
15
+ return Boolean(table?.data?.numRows !== undefined);
16
+ default:
17
+ return false;
18
+ }
19
+ }
20
+ /**
21
+ * Returns the length of the table (i.e. the number of rows)
22
+ */
23
+ export function getTableLength(table) {
24
+ switch (table.shape) {
25
+ case 'array-row-table':
26
+ case 'object-row-table':
27
+ return table.data.length;
28
+ case 'geojson-table':
29
+ return table.features.length;
30
+ case 'arrow-table':
31
+ const arrowTable = table.data;
32
+ return arrowTable.numRows;
33
+ case 'columnar-table':
34
+ for (const column of Object.values(table.data)) {
35
+ return column.length || 0;
36
+ }
37
+ return 0;
38
+ default:
39
+ throw new Error('table');
40
+ }
41
+ }
42
+ /**
43
+ * Returns the number of columns in the table
44
+ * @throws Fails to deduce number of columns if the table has no schema and is empty
45
+ */
46
+ export function getTableNumCols(table) {
47
+ if (table.schema) {
48
+ return table.schema.fields.length;
49
+ }
50
+ if (getTableLength(table) === 0) {
51
+ throw new Error('empty table');
52
+ }
53
+ switch (table.shape) {
54
+ case 'array-row-table':
55
+ return table.data[0].length;
56
+ case 'object-row-table':
57
+ return Object.keys(table.data[0]).length;
58
+ case 'geojson-table':
59
+ return Object.keys(table.features[0]).length;
60
+ case 'columnar-table':
61
+ return Object.keys(table.data).length;
62
+ case 'arrow-table':
63
+ const arrowTable = table.data;
64
+ return arrowTable.numCols;
65
+ default:
66
+ throw new Error('table');
67
+ }
68
+ }
69
+ /** Get a table cell value at row index and column name */
70
+ export function getTableCell(table, rowIndex, columnName) {
71
+ switch (table.shape) {
72
+ case 'array-row-table':
73
+ const columnIndex = getTableColumnIndex(table, columnName);
74
+ return table.data[rowIndex][columnIndex];
75
+ case 'object-row-table':
76
+ return table.data[rowIndex][columnName];
77
+ case 'geojson-table':
78
+ return table.features[rowIndex][columnName];
79
+ case 'columnar-table':
80
+ const column = table.data[columnName];
81
+ return column[rowIndex];
82
+ case 'arrow-table':
83
+ const arrowTable = table.data;
84
+ const arrowColumnIndex = arrowTable.schema.fields.findIndex((field) => field.name === columnName);
85
+ return arrowTable.getChildAt(arrowColumnIndex)?.get(rowIndex);
86
+ default:
87
+ throw new Error('todo');
88
+ }
89
+ }
90
+ /** Get a table cell value at row index and column name */
91
+ export function getTableCellAt(table, rowIndex, columnIndex) {
92
+ switch (table.shape) {
93
+ case 'array-row-table':
94
+ return table.data[rowIndex][columnIndex];
95
+ case 'object-row-table':
96
+ const columnName1 = getTableColumnName(table, columnIndex);
97
+ return table.data[rowIndex][columnName1];
98
+ case 'geojson-table':
99
+ const columnName2 = getTableColumnName(table, columnIndex);
100
+ return table.features[rowIndex][columnName2];
101
+ case 'columnar-table':
102
+ const columnName3 = getTableColumnName(table, columnIndex);
103
+ const column = table.data[columnName3];
104
+ return column[rowIndex];
105
+ case 'arrow-table':
106
+ const arrowTable = table.data;
107
+ return arrowTable.getChildAt(columnIndex)?.get(rowIndex);
108
+ default:
109
+ throw new Error('todo');
110
+ }
111
+ }
112
+ /** Deduce the table row shape */
113
+ export function getTableRowShape(table) {
114
+ switch (table.shape) {
115
+ case 'array-row-table':
116
+ case 'object-row-table':
117
+ return table.shape;
118
+ case 'geojson-table':
119
+ // TODO - this is not correct, geojson-table is not a row table
120
+ return 'object-row-table';
121
+ case 'columnar-table':
122
+ default:
123
+ throw new Error('Not a row table');
124
+ }
125
+ }
126
+ /** Get the index of a named table column. Requires the table to have a schema */
127
+ export function getTableColumnIndex(table, columnName) {
128
+ const columnIndex = table.schema?.fields.findIndex((field) => field.name === columnName);
129
+ if (columnIndex === undefined) {
130
+ throw new Error(columnName);
131
+ }
132
+ return columnIndex;
133
+ }
134
+ /** Get the name of a table column by index. Requires the table to have a schema */
135
+ export function getTableColumnName(table, columnIndex) {
136
+ const columnName = table.schema?.fields[columnIndex]?.name;
137
+ if (!columnName) {
138
+ throw new Error(`${columnIndex}`);
139
+ }
140
+ return columnName;
141
+ }
142
+ /**
143
+ * Returns one row of the table in object format.
144
+ * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance
145
+ * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter
146
+ */
147
+ // eslint-disable-next-line complexity
148
+ export function getTableRowAsObject(table, rowIndex, target, copy) {
149
+ switch (table.shape) {
150
+ case 'object-row-table':
151
+ return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
152
+ case 'array-row-table':
153
+ if (table.schema) {
154
+ const objectRow = target || {};
155
+ for (let i = 0; i < table.schema.fields.length; i++) {
156
+ objectRow[table.schema.fields[i].name] = table.data[rowIndex][i];
157
+ }
158
+ return objectRow;
159
+ }
160
+ throw new Error('no schema');
161
+ case 'geojson-table':
162
+ if (table.schema) {
163
+ const objectRow = target || {};
164
+ // TODO - should lift properties to top level
165
+ for (let i = 0; i < table.schema.fields.length; i++) {
166
+ objectRow[table.schema.fields[i].name] = table.features[rowIndex][i];
167
+ }
168
+ return objectRow;
169
+ }
170
+ throw new Error('no schema');
171
+ case 'columnar-table':
172
+ if (table.schema) {
173
+ const objectRow = target || {};
174
+ for (let i = 0; i < table.schema.fields.length; i++) {
175
+ objectRow[table.schema.fields[i].name] =
176
+ table.data[table.schema.fields[i].name][rowIndex];
177
+ }
178
+ return objectRow;
179
+ }
180
+ else {
181
+ // eslint-disable-line no-else-return
182
+ const objectRow = target || {};
183
+ for (const [name, column] of Object.entries(table.data)) {
184
+ objectRow[name] = column[rowIndex];
185
+ }
186
+ return objectRow;
187
+ }
188
+ case 'arrow-table':
189
+ const arrowTable = table.data;
190
+ const objectRow = target || {};
191
+ const row = arrowTable.get(rowIndex);
192
+ const schema = arrowTable.schema;
193
+ for (let i = 0; i < schema.fields.length; i++) {
194
+ objectRow[schema.fields[i].name] = row?.[schema.fields[i].name];
195
+ }
196
+ return objectRow;
197
+ default:
198
+ throw new Error('shape');
199
+ }
200
+ }
201
+ /**
202
+ * Returns one row of the table in array format.
203
+ * @param target Optional parameter will be used if needed to store the row. Can be reused between calls to improve performance.
204
+ * @returns an array representing the row. May be the original array in the row, a new object, or the target parameter
205
+ */
206
+ // eslint-disable-next-line complexity
207
+ export function getTableRowAsArray(table, rowIndex, target, copy) {
208
+ switch (table.shape) {
209
+ case 'array-row-table':
210
+ return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
211
+ case 'object-row-table':
212
+ if (table.schema) {
213
+ const arrayRow = target || [];
214
+ for (let i = 0; i < table.schema.fields.length; i++) {
215
+ arrayRow[i] = table.data[rowIndex][table.schema.fields[i].name];
216
+ }
217
+ return arrayRow;
218
+ }
219
+ // Warning: just slap on the values, this risks mismatches between rows
220
+ return Object.values(table.data[rowIndex]);
221
+ case 'geojson-table':
222
+ if (table.schema) {
223
+ const arrayRow = target || [];
224
+ // TODO - should lift properties to top level
225
+ for (let i = 0; i < table.schema.fields.length; i++) {
226
+ arrayRow[i] = table.features[rowIndex][table.schema.fields[i].name];
227
+ }
228
+ return arrayRow;
229
+ }
230
+ // Warning: just slap on the values, this risks mismatches between rows
231
+ return Object.values(table.features[rowIndex]);
232
+ case 'columnar-table':
233
+ if (table.schema) {
234
+ const arrayRow = target || [];
235
+ for (let i = 0; i < table.schema.fields.length; i++) {
236
+ arrayRow[i] = table.data[table.schema.fields[i].name][rowIndex];
237
+ }
238
+ return arrayRow;
239
+ }
240
+ else {
241
+ // eslint-disable-line no-else-return
242
+ const arrayRow = target || [];
243
+ let i = 0;
244
+ for (const column of Object.values(table.data)) {
245
+ arrayRow[i] = column[rowIndex];
246
+ i++;
247
+ }
248
+ return arrayRow;
249
+ }
250
+ case 'arrow-table':
251
+ const arrowTable = table.data;
252
+ const arrayRow = target || [];
253
+ const row = arrowTable.get(rowIndex);
254
+ const schema = arrowTable.schema;
255
+ for (let i = 0; i < schema.fields.length; i++) {
256
+ arrayRow[i] = row?.[schema.fields[i].name];
257
+ }
258
+ return arrayRow;
259
+ default:
260
+ throw new Error('shape');
261
+ }
262
+ }
263
+ /** Convert any table into array row format */
264
+ export function makeArrayRowTable(table) {
265
+ if (table.shape === 'array-row-table') {
266
+ return table;
267
+ }
268
+ const length = getTableLength(table);
269
+ const data = new Array(length);
270
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
271
+ data[rowIndex] = getTableRowAsArray(table, rowIndex);
272
+ }
273
+ return {
274
+ shape: 'array-row-table',
275
+ schema: table.schema,
276
+ data
277
+ };
278
+ }
279
+ /** Convert any table into object row format */
280
+ export function makeObjectRowTable(table) {
281
+ if (table.shape === 'object-row-table') {
282
+ return table;
283
+ }
284
+ const length = getTableLength(table);
285
+ const data = new Array(length);
286
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
287
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
288
+ }
289
+ return {
290
+ shape: 'object-row-table',
291
+ schema: table.schema,
292
+ data
293
+ };
294
+ }
295
+ /** Convert any table into object row format */
296
+ export function makeColumnarTable(table) {
297
+ if (table.shape === 'object-row-table') {
298
+ return table;
299
+ }
300
+ const length = getTableLength(table);
301
+ const data = new Array(length);
302
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
303
+ data[rowIndex] = getTableRowAsObject(table, rowIndex);
304
+ }
305
+ return {
306
+ shape: 'object-row-table',
307
+ schema: table.schema,
308
+ data
309
+ };
310
+ }
311
+ // Row Iterators
312
+ /**
313
+ * Iterate over table rows
314
+ * @param table
315
+ * @param shape
316
+ */
317
+ export function* makeRowIterator(table, shape) {
318
+ switch (shape) {
319
+ case 'array-row-table':
320
+ yield* makeArrayRowIterator(table);
321
+ break;
322
+ case 'object-row-table':
323
+ yield* makeObjectRowIterator(table);
324
+ break;
325
+ default:
326
+ throw new Error(`Unknown row type ${shape}`);
327
+ }
328
+ }
329
+ /**
330
+ * Streaming processing: Iterate over table, yielding array rows
331
+ * @param table
332
+ * @param shape
333
+ */
334
+ export function* makeArrayRowIterator(table, target = []) {
335
+ const length = getTableLength(table);
336
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
337
+ yield getTableRowAsArray(table, rowIndex, target);
338
+ }
339
+ }
340
+ /**
341
+ * Streaming processing: Iterate over table, yielding object rows
342
+ * @param table
343
+ * @param shape
344
+ */
345
+ export function* makeObjectRowIterator(table, target = {}) {
346
+ const length = getTableLength(table);
347
+ for (let rowIndex = 0; rowIndex < length; rowIndex++) {
348
+ yield getTableRowAsObject(table, rowIndex, target);
349
+ }
350
+ }
@@ -0,0 +1,7 @@
1
+ import type { Field } from '@loaders.gl/schema';
2
+ export interface ArrayType<T = unknown> {
3
+ readonly length: number;
4
+ [n: number]: T;
5
+ }
6
+ export declare function makeColumnFromField(field: Field, length: number): ArrayType;
7
+ //# sourceMappingURL=table-column.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table-column.d.ts","sourceRoot":"","sources":["../../../../src/lib/table/tables/table-column.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,oBAAoB,CAAC;AAG9C,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,OAAO;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC;CAChB;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAG3E"}
@@ -0,0 +1,39 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { getArrayTypeFromDataType } from "../../schema/data-type.js";
5
+ export function makeColumnFromField(field, length) {
6
+ const ArrayType = getArrayTypeFromDataType(field.type, field.nullable);
7
+ return new ArrayType(length);
8
+ }
9
+ /*
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
11
+ function deduceSchema(rows) {
12
+ const row = rows[0];
13
+
14
+ const schema = {};
15
+ let i = 0;
16
+ for (const columnName in row) {
17
+ const value = row[columnName];
18
+ switch (typeof value) {
19
+ case 'number':
20
+ case 'boolean':
21
+ // TODO - booleans could be handled differently...
22
+ schema[columnName] = {name: String(columnName), index: i, type: Float32Array};
23
+ break;
24
+
25
+ case 'object':
26
+ schema[columnName] = {name: String(columnName), index: i, type: Array};
27
+ break;
28
+
29
+ case 'string':
30
+ default:
31
+ schema[columnName] = {name: String(columnName), index: i, type: Array};
32
+ // We currently only handle numeric rows
33
+ // TODO we could offer a function to map strings to numbers?
34
+ }
35
+ i++;
36
+ }
37
+ return schema;
38
+ }
39
+ */
@@ -0,0 +1,12 @@
1
+ import type { Table, ObjectRowTable, ArrayRowTable, ColumnarTable, GeoJSONTable, ArrowTable } from '@loaders.gl/schema';
2
+ /** Checks if a table is of array row layout */
3
+ export declare function isArrayRowTable(table: Table): table is ArrayRowTable;
4
+ /** Checks if a table is of array row layout */
5
+ export declare function isObjectRowTable(table: Table): table is ObjectRowTable;
6
+ /** Checks if a table is of columnar layout */
7
+ export declare function isColumnarTable(table: Table): table is ColumnarTable;
8
+ /** Checks if a table is of GeoJSON format */
9
+ export declare function isGeoJSONTable(table: Table): table is GeoJSONTable;
10
+ /** Checks if table wraps an Apache Arrow table */
11
+ export declare function isArrowTable(table: Table): table is ArrowTable;
12
+ //# sourceMappingURL=table-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table-types.d.ts","sourceRoot":"","sources":["../../../../src/lib/table/tables/table-types.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,KAAK,EACL,cAAc,EACd,aAAa,EACb,aAAa,EACb,YAAY,EACZ,UAAU,EACX,MAAM,oBAAoB,CAAC;AAE5B,+CAA+C;AAC/C,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,aAAa,CAEpE;AAED,+CAA+C;AAC/C,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,cAAc,CAEtE;AAED,8CAA8C;AAC9C,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,aAAa,CAEpE;AAED,6CAA6C;AAC7C,wBAAgB,cAAc,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,YAAY,CAElE;AAED,kDAAkD;AAClD,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,UAAU,CAE9D"}
@@ -0,0 +1,23 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /** Checks if a table is of array row layout */
5
+ export function isArrayRowTable(table) {
6
+ return table.shape === 'array-row-table';
7
+ }
8
+ /** Checks if a table is of array row layout */
9
+ export function isObjectRowTable(table) {
10
+ return table.shape === 'object-row-table';
11
+ }
12
+ /** Checks if a table is of columnar layout */
13
+ export function isColumnarTable(table) {
14
+ return table.shape === 'columnar-table';
15
+ }
16
+ /** Checks if a table is of GeoJSON format */
17
+ export function isGeoJSONTable(table) {
18
+ return table.shape === 'geojson-table';
19
+ }
20
+ /** Checks if table wraps an Apache Arrow table */
21
+ export function isArrowTable(table) {
22
+ return table.shape === 'arrow-table';
23
+ }
@@ -0,0 +1,2 @@
1
+ export declare function assert(condition: boolean, message?: string): void;
2
+ //# sourceMappingURL=assert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/assert.ts"],"names":[],"mappings":"AAGA,wBAAgB,MAAM,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,QAI1D"}
@@ -0,0 +1,8 @@
1
+ // Replacement for the external assert method to reduce bundle size
2
+ // Note: We don't use the second "message" argument in calling code,
3
+ // so no need to support it here
4
+ export function assert(condition, message) {
5
+ if (!condition) {
6
+ throw new Error(message || 'loader assertion failed.');
7
+ }
8
+ }
@@ -0,0 +1,18 @@
1
+ export default class AsyncQueue<T> {
2
+ private _values;
3
+ private _settlers;
4
+ private _closed;
5
+ constructor();
6
+ close(): void;
7
+ [Symbol.asyncIterator](): AsyncIterator<T>;
8
+ enqueue(value: T | Error): void;
9
+ /**
10
+ * @returns a Promise for an IteratorResult
11
+ */
12
+ next(): Promise<any>;
13
+ }
14
+ /**
15
+ * @returns a Promise for an Array with the elements in `asyncIterable`
16
+ */
17
+ export declare function takeAsync(asyncIterable: AsyncIterable<any>, count?: number): Promise<any[]>;
18
+ //# sourceMappingURL=async-queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-queue.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/async-queue.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,OAAO,OAAO,UAAU,CAAC,CAAC;IAC/B,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,SAAS,CAA8E;IAC/F,OAAO,CAAC,OAAO,CAAU;;IAazB,KAAK,IAAI,IAAI;IAOb,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC;IAI1C,OAAO,CAAC,KAAK,EAAE,CAAC,GAAG,KAAK,GAAG,IAAI;IAoB/B;;OAEG;IACH,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC;CAoBrB;AAED;;GAEG;AACH,wBAAsB,SAAS,CAC7B,aAAa,EAAE,aAAa,CAAC,GAAG,CAAC,EACjC,KAAK,SAAW,GACf,OAAO,CAAC,GAAG,EAAE,CAAC,CAWhB"}
@@ -0,0 +1,90 @@
1
+ // From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license
2
+ // http://2ality.com/2016/10/asynchronous-iteration.html
3
+ class ArrayQueue extends Array {
4
+ enqueue(value) {
5
+ // Add at the end
6
+ return this.push(value);
7
+ }
8
+ dequeue() {
9
+ // Remove first element
10
+ return this.shift();
11
+ }
12
+ }
13
+ export default class AsyncQueue {
14
+ _values;
15
+ _settlers;
16
+ _closed;
17
+ constructor() {
18
+ // enqueues > dequeues
19
+ this._values = new ArrayQueue();
20
+ // dequeues > enqueues
21
+ this._settlers = new ArrayQueue();
22
+ this._closed = false;
23
+ }
24
+ close() {
25
+ while (this._settlers.length > 0) {
26
+ this._settlers.dequeue().resolve({ done: true });
27
+ }
28
+ this._closed = true;
29
+ }
30
+ [Symbol.asyncIterator]() {
31
+ return this;
32
+ }
33
+ enqueue(value) {
34
+ if (this._closed) {
35
+ throw new Error('Closed');
36
+ }
37
+ if (this._settlers.length > 0) {
38
+ if (this._values.length > 0) {
39
+ throw new Error('Illegal internal state');
40
+ }
41
+ const settler = this._settlers.dequeue();
42
+ if (value instanceof Error) {
43
+ settler.reject(value);
44
+ }
45
+ else {
46
+ settler.resolve({ value });
47
+ }
48
+ }
49
+ else {
50
+ this._values.enqueue(value);
51
+ }
52
+ }
53
+ /**
54
+ * @returns a Promise for an IteratorResult
55
+ */
56
+ next() {
57
+ if (this._values.length > 0) {
58
+ const value = this._values.dequeue();
59
+ if (value instanceof Error) {
60
+ return Promise.reject(value);
61
+ }
62
+ return Promise.resolve({ value });
63
+ }
64
+ if (this._closed) {
65
+ if (this._settlers.length > 0) {
66
+ throw new Error('Illegal internal state');
67
+ }
68
+ return Promise.resolve({ done: true });
69
+ }
70
+ // Wait for new values to be enqueued
71
+ return new Promise((resolve, reject) => {
72
+ this._settlers.enqueue({ resolve, reject });
73
+ });
74
+ }
75
+ }
76
+ /**
77
+ * @returns a Promise for an Array with the elements in `asyncIterable`
78
+ */
79
+ export async function takeAsync(asyncIterable, count = Infinity) {
80
+ const result = [];
81
+ const iterator = asyncIterable[Symbol.asyncIterator]();
82
+ while (result.length < count) {
83
+ const { value, done } = await iterator.next();
84
+ if (done) {
85
+ break;
86
+ }
87
+ result.push(value);
88
+ }
89
+ return result;
90
+ }
@@ -0,0 +1,15 @@
1
+ /** Any typed array */
2
+ export type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array;
3
+ export type BigTypedArray = TypedArray | BigInt64Array | BigUint64Array;
4
+ export type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor;
5
+ export type BigTypedArrayConstructor = TypedArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor;
6
+ /** Any numeric array: typed array or `number[]` */
7
+ export type NumberArray = number[] | TypedArray;
8
+ export type NumericArray = number[] | TypedArray;
9
+ export interface ArrayType<T = unknown> {
10
+ readonly length: number;
11
+ [n: number]: T;
12
+ }
13
+ /** Any array: typed array or js array (`any[]`) */
14
+ export type AnyArray = any[] | TypedArray;
15
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":"AAIA,sBAAsB;AACtB,MAAM,MAAM,UAAU,GAClB,SAAS,GACT,UAAU,GACV,UAAU,GACV,WAAW,GACX,UAAU,GACV,WAAW,GACX,iBAAiB,GACjB,YAAY,GACZ,YAAY,CAAC;AAEjB,MAAM,MAAM,aAAa,GAAG,UAAU,GAAG,aAAa,GAAG,cAAc,CAAC;AAExE,MAAM,MAAM,qBAAqB,GAC7B,oBAAoB,GACpB,qBAAqB,GACrB,qBAAqB,GACrB,sBAAsB,GACtB,qBAAqB,GACrB,sBAAsB,GACtB,uBAAuB,GACvB,uBAAuB,CAAC;AAE5B,MAAM,MAAM,wBAAwB,GAChC,qBAAqB,GACrB,wBAAwB,GACxB,yBAAyB,CAAC;AAE9B,mDAAmD;AACnD,MAAM,MAAM,WAAW,GAAG,MAAM,EAAE,GAAG,UAAU,CAAC;AAEhD,MAAM,MAAM,YAAY,GAAG,MAAM,EAAE,GAAG,UAAU,CAAC;AAEjD,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,OAAO;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC;CAChB;AAED,mDAAmD;AACnD,MAAM,MAAM,QAAQ,GAAG,GAAG,EAAE,GAAG,UAAU,CAAC"}
@@ -0,0 +1,4 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ export {};
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@loaders.gl/schema-utils",
3
+ "version": "4.4.0-alpha.1",
4
+ "description": "Schema-related utilities for loaders.gl...",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/visgl/loaders.gl"
13
+ },
14
+ "keywords": [
15
+ "webgl",
16
+ "loader",
17
+ "3d",
18
+ "mesh",
19
+ "point cloud",
20
+ "PLY"
21
+ ],
22
+ "types": "dist/index.d.ts",
23
+ "main": "dist/index.cjs",
24
+ "module": "dist/index.js",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.cjs"
30
+ }
31
+ },
32
+ "sideEffects": false,
33
+ "files": [
34
+ "src",
35
+ "dist",
36
+ "README.md"
37
+ ],
38
+ "scripts": {
39
+ "pre-build": "npm run build-bundle && npm run build-bundle-dev",
40
+ "build-bundle": "ocular-bundle ./bundle.ts --output=dist/dist.min.js",
41
+ "build-bundle-dev": "ocular-bundle ./bundle.ts --env=dev --output=dist/dist.dev.js"
42
+ },
43
+ "dependencies": {
44
+ "@loaders.gl/schema": "4.4.0-alpha.1",
45
+ "@types/geojson": "^7946.0.7",
46
+ "apache-arrow": ">= 15.0.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@loaders.gl/core": "4.4.0-alpha.0"
50
+ },
51
+ "gitHead": "f1732de45907bd500bf4eedb4803beca8bf4bfb0"
52
+ }