@luma.gl/graph 10.0.0-alpha.5 → 10.0.0-alpha.6

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 (110) hide show
  1. package/LICENSE +38 -0
  2. package/README.md +7 -1
  3. package/dist/arrow/dggs/dggs-gpu-polygons.cjs +667 -0
  4. package/dist/arrow/dggs/dggs-gpu-polygons.d.ts +75 -0
  5. package/dist/arrow/dggs/dggs-gpu-polygons.d.ts.map +1 -0
  6. package/dist/arrow/dggs/dggs-gpu-polygons.js +651 -0
  7. package/dist/arrow/dggs/dggs-gpu-polygons.js.map +1 -0
  8. package/dist/arrow/geoarrow/arrow-geoarrow-adapter.cjs +411 -0
  9. package/dist/arrow/geoarrow/arrow-geoarrow-adapter.d.ts +31 -0
  10. package/dist/arrow/geoarrow/arrow-geoarrow-adapter.d.ts.map +1 -0
  11. package/dist/arrow/geoarrow/arrow-geoarrow-adapter.js +395 -0
  12. package/dist/arrow/geoarrow/arrow-geoarrow-adapter.js.map +1 -0
  13. package/dist/arrow/geoarrow/arrow-geoarrow-conversion.cjs +304 -0
  14. package/dist/arrow/geoarrow/arrow-geoarrow-conversion.d.ts +32 -0
  15. package/dist/arrow/geoarrow/arrow-geoarrow-conversion.d.ts.map +1 -0
  16. package/dist/arrow/geoarrow/arrow-geoarrow-conversion.js +293 -0
  17. package/dist/arrow/geoarrow/arrow-geoarrow-conversion.js.map +1 -0
  18. package/dist/arrow/geoarrow/arrow-polygon-tessellation.cjs +232 -0
  19. package/dist/arrow/geoarrow/arrow-polygon-tessellation.d.ts +54 -0
  20. package/dist/arrow/geoarrow/arrow-polygon-tessellation.d.ts.map +1 -0
  21. package/dist/arrow/geoarrow/arrow-polygon-tessellation.js +222 -0
  22. package/dist/arrow/geoarrow/arrow-polygon-tessellation.js.map +1 -0
  23. package/dist/arrow/gpu/arrow-gpu-analytics-adapters.cjs +314 -0
  24. package/dist/arrow/gpu/arrow-gpu-analytics-adapters.d.ts +42 -0
  25. package/dist/arrow/gpu/arrow-gpu-analytics-adapters.d.ts.map +1 -0
  26. package/dist/arrow/gpu/arrow-gpu-analytics-adapters.js +318 -0
  27. package/dist/arrow/gpu/arrow-gpu-analytics-adapters.js.map +1 -0
  28. package/dist/arrow/gpu/arrow-gpu-analytics-readback.cjs +204 -0
  29. package/dist/arrow/gpu/arrow-gpu-analytics-readback.d.ts +30 -0
  30. package/dist/arrow/gpu/arrow-gpu-analytics-readback.d.ts.map +1 -0
  31. package/dist/arrow/gpu/arrow-gpu-analytics-readback.js +195 -0
  32. package/dist/arrow/gpu/arrow-gpu-analytics-readback.js.map +1 -0
  33. package/dist/arrow/gpu/arrow-gpu-table-adapters.cjs +257 -0
  34. package/dist/arrow/gpu/arrow-gpu-table-adapters.d.ts +45 -0
  35. package/dist/arrow/gpu/arrow-gpu-table-adapters.d.ts.map +1 -0
  36. package/dist/arrow/gpu/arrow-gpu-table-adapters.js +257 -0
  37. package/dist/arrow/gpu/arrow-gpu-table-adapters.js.map +1 -0
  38. package/dist/arrow/gpu/arrow-gpu-table-conversion.cjs +230 -0
  39. package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts +71 -0
  40. package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts.map +1 -0
  41. package/dist/arrow/gpu/arrow-gpu-table-conversion.js +215 -0
  42. package/dist/arrow/gpu/arrow-gpu-table-conversion.js.map +1 -0
  43. package/dist/arrow/gpu/arrow-input-schema.cjs +36 -0
  44. package/dist/arrow/gpu/arrow-input-schema.d.ts +41 -0
  45. package/dist/arrow/gpu/arrow-input-schema.d.ts.map +1 -0
  46. package/dist/arrow/gpu/arrow-input-schema.js +17 -0
  47. package/dist/arrow/gpu/arrow-input-schema.js.map +1 -0
  48. package/dist/arrow/index.cjs +90 -0
  49. package/dist/arrow/index.cjs.map +6 -0
  50. package/dist/arrow/index.d.ts +9 -0
  51. package/dist/arrow/index.d.ts.map +1 -0
  52. package/dist/arrow/index.js +12 -0
  53. package/dist/arrow/index.js.map +1 -0
  54. package/dist/arrow/internal.cjs +30 -0
  55. package/dist/arrow/internal.cjs.map +6 -0
  56. package/dist/arrow/internal.d.ts +19 -0
  57. package/dist/arrow/internal.d.ts.map +1 -0
  58. package/dist/arrow/internal.js +7 -0
  59. package/dist/arrow/internal.js.map +1 -0
  60. package/dist/dist.dev.js +283 -12
  61. package/dist/dist.min.js +29 -29
  62. package/dist/geospatial/geospatial-utils.cjs +12 -11
  63. package/dist/geospatial/geospatial-utils.d.ts +1 -1
  64. package/dist/geospatial/geospatial-utils.d.ts.map +1 -1
  65. package/dist/geospatial/geospatial-utils.js +3 -2
  66. package/dist/geospatial/geospatial-utils.js.map +1 -1
  67. package/dist/geospatial/index.cjs.map +2 -2
  68. package/dist/gpu-a5/index.cjs.map +1 -1
  69. package/dist/gpu-dggs/benchmarks.cjs.map +1 -1
  70. package/dist/gpu-dggs/gpu-dggs-cell-projection.cjs +2 -2
  71. package/dist/gpu-dggs/gpu-dggs-cell-projection.js +1 -1
  72. package/dist/gpu-dggs/gpu-dggs-cell-projection.js.map +1 -1
  73. package/dist/gpu-dggs/index.cjs.map +1 -1
  74. package/dist/gpu-h3/index.cjs.map +1 -1
  75. package/dist/gpu-project/benchmarks.cjs.map +2 -2
  76. package/dist/gpu-project/crs.cjs.map +2 -2
  77. package/dist/gpu-project/index.cjs.map +2 -2
  78. package/dist/gpu-project/projection-program.cjs +2 -2
  79. package/dist/gpu-project/projection-program.d.ts +1 -1
  80. package/dist/gpu-project/projection-program.d.ts.map +1 -1
  81. package/dist/gpu-project/projection-program.js +1 -1
  82. package/dist/gpu-project/projection-program.js.map +1 -1
  83. package/dist/gpu-sql/lu-sql.d.ts +1 -1
  84. package/dist/gpu-sql/lu-sql.js +1 -1
  85. package/dist/gpu-tables/engine/gpu-table-shader-bindings.d.ts +1 -1
  86. package/dist/gpu-tables/engine/gpu-table-shader-bindings.d.ts.map +1 -1
  87. package/dist/gpu-vector-search/index.cjs.map +1 -1
  88. package/dist/utils/assemble-compute-kernel.cjs +2 -2
  89. package/dist/utils/assemble-compute-kernel.d.ts +2 -1
  90. package/dist/utils/assemble-compute-kernel.d.ts.map +1 -1
  91. package/dist/utils/assemble-compute-kernel.js +2 -2
  92. package/dist/utils/assemble-compute-kernel.js.map +1 -1
  93. package/package.json +19 -7
  94. package/src/arrow/dggs/dggs-gpu-polygons.ts +820 -0
  95. package/src/arrow/geoarrow/arrow-geoarrow-adapter.ts +519 -0
  96. package/src/arrow/geoarrow/arrow-geoarrow-conversion.ts +415 -0
  97. package/src/arrow/geoarrow/arrow-polygon-tessellation.ts +369 -0
  98. package/src/arrow/gpu/arrow-gpu-analytics-adapters.ts +474 -0
  99. package/src/arrow/gpu/arrow-gpu-analytics-readback.ts +294 -0
  100. package/src/arrow/gpu/arrow-gpu-table-adapters.ts +378 -0
  101. package/src/arrow/gpu/arrow-gpu-table-conversion.ts +324 -0
  102. package/src/arrow/gpu/arrow-input-schema.ts +98 -0
  103. package/src/arrow/index.ts +79 -0
  104. package/src/arrow/internal.ts +23 -0
  105. package/src/geospatial/geospatial-utils.ts +3 -7
  106. package/src/gpu-dggs/gpu-dggs-cell-projection.ts +1 -1
  107. package/src/gpu-project/projection-program.ts +1 -1
  108. package/src/gpu-sql/lu-sql.ts +1 -1
  109. package/src/gpu-tables/engine/gpu-table-shader-bindings.ts +1 -1
  110. package/src/utils/assemble-compute-kernel.ts +3 -7
@@ -0,0 +1,519 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {
6
+ GeoArrowBatchView,
7
+ GeoArrowRowView,
8
+ GeoArrowArray,
9
+ GeoArrowColumn,
10
+ GeoArrowCoordinateLayout,
11
+ GeoArrowDenseUnion,
12
+ GeoArrowDimension,
13
+ GeoArrowEncoding,
14
+ GeoArrowNumericArray,
15
+ GeoArrowOffsets,
16
+ GeoArrowValidity
17
+ } from '@math.gl/geospatial/geoarrow';
18
+ import {
19
+ getGeoArrowRowView,
20
+ iterateGeoArrowBatches,
21
+ mergeGeoArrowDimensions
22
+ } from '@math.gl/geospatial/geoarrow';
23
+ import {
24
+ Binary,
25
+ BufferType,
26
+ Data,
27
+ DataType,
28
+ DenseUnion,
29
+ Field,
30
+ FixedSizeList,
31
+ Float32,
32
+ Float64,
33
+ Int8,
34
+ Int16,
35
+ Int32,
36
+ Int64,
37
+ List,
38
+ Struct,
39
+ Uint8,
40
+ Uint16,
41
+ Uint32,
42
+ Uint64,
43
+ Utf8,
44
+ Vector,
45
+ vectorFromArray,
46
+ type DataType as ArrowDataType
47
+ } from 'apache-arrow';
48
+
49
+ /** Options for adapting one Apache Arrow vector to math.gl's runtime-neutral GeoArrow ABI. */
50
+ export type MakeGeoArrowColumnFromArrowVectorOptions = {
51
+ /** GeoArrow extension encoding. Inferred from the physical type when omitted. */
52
+ encoding?: GeoArrowEncoding;
53
+ /** Semantic coordinate dimension. Inferred from coordinate field names when omitted. */
54
+ dimension?: GeoArrowDimension;
55
+ /** Coordinate organization. Inferred from the physical type when omitted. */
56
+ coordinateLayout?: GeoArrowCoordinateLayout | null;
57
+ /** Opaque extension metadata retained on the descriptor envelope. */
58
+ metadata?: Readonly<Record<string, unknown>>;
59
+ };
60
+
61
+ /**
62
+ * Adapts Apache Arrow physical buffers to the borrowed descriptor ABI used by
63
+ * `@math.gl/geospatial/geoarrow`.
64
+ */
65
+ export function makeGeoArrowColumnFromArrowVector<T extends ArrowDataType>(
66
+ vector: Vector<T>,
67
+ options: MakeGeoArrowColumnFromArrowVectorOptions = {}
68
+ ): GeoArrowColumn {
69
+ const encoding = options.encoding ?? inferGeoArrowEncodingFromArrowType(vector.type);
70
+ const dimension = options.dimension ?? inferGeoArrowDimensionFromArrowType(vector.type);
71
+ const coordinateLayout =
72
+ options.coordinateLayout === undefined
73
+ ? inferGeoArrowCoordinateLayoutFromArrowType(vector.type)
74
+ : options.coordinateLayout;
75
+
76
+ return {
77
+ encoding,
78
+ dimension,
79
+ coordinateLayout,
80
+ chunks: vector.data.map(data => makeGeoArrowArrayFromArrowData(data)),
81
+ metadata: options.metadata
82
+ };
83
+ }
84
+
85
+ /** Iterates borrowed batch views while retaining Arrow chunk boundaries and buffers. */
86
+ export function iterateGeoArrowBatchesFromArrowVector<T extends ArrowDataType>(
87
+ vector: Vector<T>,
88
+ options: MakeGeoArrowColumnFromArrowVectorOptions = {}
89
+ ): IterableIterator<GeoArrowBatchView> {
90
+ return iterateGeoArrowBatches(makeGeoArrowColumnFromArrowVector(vector, options));
91
+ }
92
+
93
+ /** Gets a borrowed row view by logical index across Arrow chunks. */
94
+ export function getGeoArrowRowViewFromArrowVector<T extends ArrowDataType>(
95
+ vector: Vector<T>,
96
+ rowIndex: number,
97
+ options: MakeGeoArrowColumnFromArrowVectorOptions = {}
98
+ ): GeoArrowRowView {
99
+ return getGeoArrowRowView(makeGeoArrowColumnFromArrowVector(vector, options), rowIndex);
100
+ }
101
+
102
+ /** Converts a math.gl GeoArrow descriptor back to an Apache Arrow vector. */
103
+ export function makeArrowVectorFromGeoArrowColumn(column: GeoArrowColumn): Vector {
104
+ return new Vector(
105
+ column.chunks.map(chunk =>
106
+ makeArrowDataFromGeoArrowArray(chunk, column.dimension, column.coordinateLayout)
107
+ )
108
+ );
109
+ }
110
+
111
+ /** Infers a native GeoArrow encoding from an Apache Arrow physical type. */
112
+ export function inferGeoArrowEncodingFromArrowType(type: ArrowDataType): GeoArrowEncoding {
113
+ if (DataType.isDenseUnion(type)) return 'geoarrow.geometry';
114
+ if (DataType.isBinary(type)) return 'geoarrow.wkb';
115
+ if (DataType.isUtf8(type)) return 'geoarrow.wkt';
116
+
117
+ const nesting = getArrowListNesting(type);
118
+ switch (nesting) {
119
+ case 0:
120
+ return 'geoarrow.point';
121
+ case 1:
122
+ return 'geoarrow.linestring';
123
+ case 2:
124
+ return 'geoarrow.polygon';
125
+ case 3:
126
+ return 'geoarrow.multipolygon';
127
+ default:
128
+ throw new Error(`Cannot infer GeoArrow encoding from Arrow type ${type.toString()}`);
129
+ }
130
+ }
131
+
132
+ /** Infers the semantic GeoArrow coordinate dimension from Arrow coordinate field names. */
133
+ export function inferGeoArrowDimensionFromArrowType(type: ArrowDataType): GeoArrowDimension {
134
+ if (DataType.isFixedSizeList(type)) {
135
+ const dimensionName = type.children[0]?.name.toLowerCase();
136
+ if (isGeoArrowDimension(dimensionName)) return dimensionName;
137
+ switch (type.listSize) {
138
+ case 2:
139
+ return 'xy';
140
+ case 3:
141
+ return 'xyz';
142
+ case 4:
143
+ return 'xyzm';
144
+ default:
145
+ throw new Error(
146
+ `GeoArrow coordinates require 2, 3, or 4 values, received ${type.listSize}`
147
+ );
148
+ }
149
+ }
150
+ if (DataType.isStruct(type)) {
151
+ const names = type.children.map(field => field.name.toLowerCase());
152
+ if (names[0] === 'x' && names[1] === 'y') {
153
+ if (names[2] === 'm') return 'xym';
154
+ if (names[2] === 'z' && names[3] === 'm') return 'xyzm';
155
+ if (names[2] === 'z') return 'xyz';
156
+ return 'xy';
157
+ }
158
+ }
159
+ if (DataType.isList(type)) {
160
+ return inferGeoArrowDimensionFromArrowType(type.children[0].type);
161
+ }
162
+ if (DataType.isDenseUnion(type)) {
163
+ return type.children.reduce<GeoArrowDimension>(
164
+ (dimension, field) =>
165
+ mergeGeoArrowDimensions(dimension, inferGeoArrowDimensionFromArrowType(field.type)),
166
+ 'xy'
167
+ );
168
+ }
169
+ throw new Error(`Cannot infer GeoArrow dimension from Arrow type ${type.toString()}`);
170
+ }
171
+
172
+ /** Infers whether Arrow coordinate leaves are interleaved or separated. */
173
+ export function inferGeoArrowCoordinateLayoutFromArrowType(
174
+ type: ArrowDataType
175
+ ): GeoArrowCoordinateLayout | null {
176
+ if (DataType.isFixedSizeList(type)) return 'interleaved';
177
+ if (DataType.isStruct(type)) return 'separated';
178
+ if (DataType.isList(type)) {
179
+ return inferGeoArrowCoordinateLayoutFromArrowType(type.children[0].type);
180
+ }
181
+ if (DataType.isDenseUnion(type)) {
182
+ for (const field of type.children) {
183
+ const layout = inferGeoArrowCoordinateLayoutFromArrowType(field.type);
184
+ if (layout) return layout;
185
+ }
186
+ }
187
+ return null;
188
+ }
189
+
190
+ function makeGeoArrowArrayFromArrowData(data: Data): GeoArrowArray {
191
+ const validity = makeGeoArrowValidity(data);
192
+ if (DataType.isFixedSizeList(data.type)) {
193
+ return {
194
+ kind: 'fixed-size-list',
195
+ length: data.length,
196
+ size: data.type.listSize,
197
+ child: makeGeoArrowArrayFromArrowData(data.children[0]),
198
+ validity
199
+ };
200
+ }
201
+ if (DataType.isList(data.type)) {
202
+ return {
203
+ kind: 'list',
204
+ length: data.length,
205
+ offsets: data.valueOffsets as GeoArrowOffsets,
206
+ child: makeGeoArrowArrayFromArrowData(data.children[0]),
207
+ validity
208
+ };
209
+ }
210
+ if (DataType.isStruct(data.type)) {
211
+ const children: Record<string, GeoArrowArray> = {};
212
+ for (let childIndex = 0; childIndex < data.type.children.length; childIndex++) {
213
+ children[data.type.children[childIndex].name] = makeGeoArrowArrayFromArrowData(
214
+ data.children[childIndex]
215
+ );
216
+ }
217
+ return {kind: 'struct', length: data.length, children, validity};
218
+ }
219
+ if (DataType.isDenseUnion(data.type)) {
220
+ const unionType = data.type;
221
+ return {
222
+ kind: 'dense-union',
223
+ length: data.length,
224
+ typeIds: data.typeIds as Int8Array | Uint8Array,
225
+ valueOffsets: data.valueOffsets,
226
+ children: unionType.children.map((field, childIndex) => ({
227
+ name: field.name,
228
+ typeId: unionType.typeIds[childIndex],
229
+ encoding: inferGeoArrowEncodingFromUnionChild(field),
230
+ dimension: inferGeoArrowDimensionFromArrowType(field.type),
231
+ coordinateLayout: inferGeoArrowCoordinateLayoutFromArrowType(field.type),
232
+ data: makeGeoArrowArrayFromArrowData(data.children[childIndex])
233
+ })),
234
+ validity
235
+ };
236
+ }
237
+ if (DataType.isBinary(data.type) || DataType.isUtf8(data.type)) {
238
+ return {
239
+ kind: 'serialized',
240
+ encoding: DataType.isBinary(data.type) ? 'binary' : 'utf8',
241
+ length: data.length,
242
+ offsets: data.valueOffsets as GeoArrowOffsets,
243
+ values: data.values as Uint8Array,
244
+ validity
245
+ };
246
+ }
247
+ if (DataType.isInt(data.type) || DataType.isFloat(data.type)) {
248
+ return {
249
+ kind: 'primitive',
250
+ length: data.length,
251
+ values: data.values as GeoArrowNumericArray,
252
+ validity
253
+ };
254
+ }
255
+ throw new Error(`Unsupported GeoArrow physical Arrow type ${data.type.toString()}`);
256
+ }
257
+
258
+ function makeGeoArrowValidity(data: Data): GeoArrowValidity | undefined {
259
+ if (data.nullCount <= 0 || !data.nullBitmap || data.nullBitmap.length === 0) return undefined;
260
+ return {values: data.nullBitmap, bitOffset: data.offset};
261
+ }
262
+
263
+ function makeArrowDataFromGeoArrowArray(
264
+ array: GeoArrowArray,
265
+ dimension: GeoArrowDimension,
266
+ coordinateLayout: GeoArrowCoordinateLayout | null
267
+ ): Data {
268
+ const validity = makeCompactValidity(array.validity, array.length);
269
+ const nullCount = validity ? -1 : 0;
270
+ const validityBuffers = validity ? {[BufferType.VALIDITY]: validity} : {};
271
+
272
+ switch (array.kind) {
273
+ case 'primitive': {
274
+ const values = makePackedPrimitiveValues(array);
275
+ const type = getArrowTypeForValues(values);
276
+ return new Data(type, 0, array.length, nullCount, {
277
+ ...validityBuffers,
278
+ [BufferType.DATA]: values
279
+ });
280
+ }
281
+ case 'fixed-size-list': {
282
+ const child = makeArrowDataFromGeoArrowArray(array.child, dimension, coordinateLayout);
283
+ const childStart = (array.offset ?? 0) * array.size;
284
+ const slicedChild =
285
+ childStart === 0 && child.length === array.length * array.size
286
+ ? child
287
+ : child.slice(childStart, array.length * array.size);
288
+ const type = new FixedSizeList(array.size, new Field(dimension, slicedChild.type, false));
289
+ return new Data(type, 0, array.length, nullCount, validityBuffers, [slicedChild]);
290
+ }
291
+ case 'list': {
292
+ if (array.offsets instanceof BigInt64Array) {
293
+ throw new Error('Apache Arrow JS does not expose LargeList in this supported version');
294
+ }
295
+ const child = makeArrowDataFromGeoArrowArray(array.child, dimension, coordinateLayout);
296
+ const offsets = makeArrowOffsets(array.offsets, array.offset, array.length, array.offsetBase);
297
+ const type = new List(new Field('values', child.type, true));
298
+ return new Data(
299
+ type,
300
+ 0,
301
+ array.length,
302
+ nullCount,
303
+ {...validityBuffers, [BufferType.OFFSET]: offsets},
304
+ [child]
305
+ );
306
+ }
307
+ case 'struct': {
308
+ const fields: Field[] = [];
309
+ const children: Data[] = [];
310
+ for (const [name, childArray] of Object.entries(array.children)) {
311
+ let child = makeArrowDataFromGeoArrowArray(childArray, dimension, coordinateLayout);
312
+ const childOffset = array.offset ?? 0;
313
+ if (childOffset > 0 || child.length !== array.length) {
314
+ child = child.slice(childOffset, array.length);
315
+ }
316
+ fields.push(new Field(name, child.type, true));
317
+ children.push(child);
318
+ }
319
+ return new Data(new Struct(fields), 0, array.length, nullCount, validityBuffers, children);
320
+ }
321
+ case 'dense-union':
322
+ return makeArrowDenseUnionData(array, dimension, coordinateLayout);
323
+ case 'serialized': {
324
+ if (array.offsets instanceof BigInt64Array) {
325
+ throw new Error('Large GeoArrow serialized output is not supported by this adapter');
326
+ }
327
+ const offsets = makeArrowOffsets(array.offsets, array.offset, array.length, array.offsetBase);
328
+ const type = array.encoding === 'binary' ? new Binary() : new Utf8();
329
+ return new Data(type, 0, array.length, nullCount, {
330
+ ...validityBuffers,
331
+ [BufferType.OFFSET]: offsets,
332
+ [BufferType.DATA]: array.values
333
+ });
334
+ }
335
+ }
336
+ }
337
+
338
+ function makeArrowDenseUnionData(
339
+ array: GeoArrowDenseUnion,
340
+ dimension: GeoArrowDimension,
341
+ coordinateLayout: GeoArrowCoordinateLayout | null
342
+ ): Data {
343
+ const offset = array.offset ?? 0;
344
+ const typeIds = array.typeIds.slice(offset, offset + array.length);
345
+ const valueOffsets = array.valueOffsets.slice(offset, offset + array.length);
346
+ const children = array.children.map(child =>
347
+ makeArrowDataFromGeoArrowArray(
348
+ child.data,
349
+ child.dimension ?? dimension,
350
+ child.coordinateLayout === undefined ? coordinateLayout : child.coordinateLayout
351
+ )
352
+ );
353
+
354
+ if (array.validity && array.children.length > 0) {
355
+ const nullChildIndex = 0;
356
+ const nullChild = children[nullChildIndex];
357
+ const nullValueOffset = nullChild.length;
358
+ children[nullChildIndex] = appendNullToArrowData(nullChild);
359
+ for (let rowIndex = 0; rowIndex < array.length; rowIndex++) {
360
+ if (!isGeoArrowValueValid(array.validity, rowIndex)) {
361
+ typeIds[rowIndex] = array.children[nullChildIndex].typeId;
362
+ valueOffsets[rowIndex] = nullValueOffset;
363
+ }
364
+ }
365
+ }
366
+
367
+ const fields = array.children.map(
368
+ (child, childIndex) => new Field(child.name, children[childIndex].type, true)
369
+ );
370
+ const type = new DenseUnion(Int32Array.from(array.children.map(child => child.typeId)), fields);
371
+ return new Data(
372
+ type,
373
+ 0,
374
+ array.length,
375
+ 0,
376
+ {[BufferType.TYPE]: Int8Array.from(typeIds), [BufferType.OFFSET]: valueOffsets},
377
+ children
378
+ );
379
+ }
380
+
381
+ function appendNullToArrowData(data: Data): Data {
382
+ const vector = new Vector([data]);
383
+ const values: unknown[] = [];
384
+ for (let index = 0; index < vector.length; index++) {
385
+ values.push(materializeArrowValue(vector.get(index)));
386
+ }
387
+ values.push(null);
388
+ return vectorFromArray(values, data.type).data[0];
389
+ }
390
+
391
+ function materializeArrowValue(value: unknown): unknown {
392
+ if (value === null || value === undefined) return null;
393
+ if (ArrayBuffer.isView(value)) {
394
+ return Array.from(value as unknown as ArrayLike<number | bigint>);
395
+ }
396
+ if (Array.isArray(value)) return value.map(materializeArrowValue);
397
+ if (isArrowVectorLike(value)) {
398
+ const values: unknown[] = [];
399
+ for (let index = 0; index < value.length; index++) {
400
+ values.push(materializeArrowValue(value.get(index)));
401
+ }
402
+ return values;
403
+ }
404
+ return value;
405
+ }
406
+
407
+ function isArrowVectorLike(
408
+ value: unknown
409
+ ): value is {length: number; get: (index: number) => unknown} {
410
+ return Boolean(
411
+ value &&
412
+ typeof value === 'object' &&
413
+ 'length' in value &&
414
+ 'get' in value &&
415
+ typeof (value as {get?: unknown}).get === 'function'
416
+ );
417
+ }
418
+
419
+ function makePackedPrimitiveValues(
420
+ array: Extract<GeoArrowArray, {kind: 'primitive'}>
421
+ ): GeoArrowNumericArray {
422
+ const offset = array.offset ?? 0;
423
+ const stride = array.stride ?? 1;
424
+ if (stride === 1) {
425
+ return array.values.subarray(offset, offset + array.length) as GeoArrowNumericArray;
426
+ }
427
+ const ValuesConstructor = array.values.constructor as {
428
+ new (length: number): GeoArrowNumericArray;
429
+ };
430
+ const values = new ValuesConstructor(array.length);
431
+ for (let index = 0; index < array.length; index++) {
432
+ values[index] = array.values[offset + index * stride] as never;
433
+ }
434
+ return values;
435
+ }
436
+
437
+ function getArrowTypeForValues(values: GeoArrowNumericArray): ArrowDataType {
438
+ if (values instanceof Int8Array) return new Int8();
439
+ if (values instanceof Uint8Array || values instanceof Uint8ClampedArray) return new Uint8();
440
+ if (values instanceof Int16Array) return new Int16();
441
+ if (values instanceof Uint16Array) return new Uint16();
442
+ if (values instanceof Int32Array) return new Int32();
443
+ if (values instanceof Uint32Array) return new Uint32();
444
+ if (values instanceof BigInt64Array) return new Int64();
445
+ if (values instanceof BigUint64Array) return new Uint64();
446
+ if (values instanceof Float32Array) return new Float32();
447
+ if (values instanceof Float64Array) return new Float64();
448
+ throw new Error(`Unsupported GeoArrow primitive values ${(values as object).constructor.name}`);
449
+ }
450
+
451
+ function makeArrowOffsets(
452
+ offsets: GeoArrowOffsets,
453
+ offset = 0,
454
+ length: number,
455
+ offsetBase: number | bigint = 0
456
+ ): Int32Array {
457
+ const source = offsets.subarray(offset, offset + length + 1);
458
+ if (offsetBase === 0) return source as Int32Array;
459
+ const normalized = new Int32Array(source.length);
460
+ for (let index = 0; index < source.length; index++) {
461
+ normalized[index] = Number(source[index]) - Number(offsetBase);
462
+ }
463
+ return normalized;
464
+ }
465
+
466
+ function makeCompactValidity(
467
+ validity: GeoArrowValidity | undefined,
468
+ length: number
469
+ ): Uint8Array | undefined {
470
+ if (!validity) return undefined;
471
+ const bitOffset = validity.bitOffset ?? 0;
472
+ const values = new Uint8Array(Math.ceil(length / 8));
473
+ let nullCount = 0;
474
+ for (let index = 0; index < length; index++) {
475
+ if (isGeoArrowValueValid(validity, index)) {
476
+ values[index >> 3] |= 1 << (index & 7);
477
+ } else {
478
+ nullCount++;
479
+ }
480
+ }
481
+ return nullCount > 0 || bitOffset > 0 ? values : undefined;
482
+ }
483
+
484
+ function isGeoArrowValueValid(validity: GeoArrowValidity, index: number): boolean {
485
+ const bitIndex = (validity.bitOffset ?? 0) + index;
486
+ return Boolean(validity.values[bitIndex >> 3] & (1 << (bitIndex & 7)));
487
+ }
488
+
489
+ function inferGeoArrowEncodingFromUnionChild(field: Field): GeoArrowEncoding {
490
+ const normalizedName = field.name.replace(/[^a-z]/gi, '').toLowerCase();
491
+ const names: Record<string, GeoArrowEncoding> = {
492
+ point: 'geoarrow.point',
493
+ linestring: 'geoarrow.linestring',
494
+ polygon: 'geoarrow.polygon',
495
+ multipoint: 'geoarrow.multipoint',
496
+ multilinestring: 'geoarrow.multilinestring',
497
+ multipolygon: 'geoarrow.multipolygon',
498
+ geometrycollection: 'geoarrow.geometrycollection'
499
+ };
500
+ for (const [name, encoding] of Object.entries(names)) {
501
+ if (normalizedName.startsWith(name)) return encoding;
502
+ }
503
+ return inferGeoArrowEncodingFromArrowType(field.type);
504
+ }
505
+
506
+ function getArrowListNesting(type: ArrowDataType): number {
507
+ let nesting = 0;
508
+ let currentType = type;
509
+ while (DataType.isList(currentType)) {
510
+ nesting++;
511
+ currentType = currentType.children[0].type;
512
+ }
513
+ if (DataType.isFixedSizeList(currentType) || DataType.isStruct(currentType)) return nesting;
514
+ throw new Error(`GeoArrow type does not terminate in coordinates: ${type.toString()}`);
515
+ }
516
+
517
+ function isGeoArrowDimension(value: string | undefined): value is GeoArrowDimension {
518
+ return value === 'xy' || value === 'xyz' || value === 'xym' || value === 'xyzm';
519
+ }