-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.ts
379 lines (333 loc) · 11.4 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { assert } from "@deck.gl/core/typed";
import * as arrow from "apache-arrow";
import * as ga from "@geoarrow/geoarrow-js";
import {
AccessorContext,
AccessorFunction,
_InternalAccessorContext,
} from "./types";
export type TypedArray =
| Uint8Array
| Uint8ClampedArray
| Uint16Array
| Uint32Array
| Int8Array
| Int16Array
| Int32Array
| Float32Array
| Float64Array;
export function findGeometryColumnIndex(
schema: arrow.Schema,
extensionName: string,
geometryColumnName?: string | null,
): number | null {
const index = schema.fields.findIndex(
(field) =>
field.name === geometryColumnName ||
field.metadata.get("ARROW:extension:name") === extensionName,
);
return index !== -1 ? index : null;
}
/**
* Returns `true` if the input is a reference to a column in the table
*/
export function isColumnReference(input: any): input is string {
return typeof input === "string";
}
function isDataInterleavedCoords(
data: arrow.Data,
): data is arrow.Data<arrow.FixedSizeList<arrow.Float64>> {
// TODO: also check 2 or 3d? Float64?
return data.type instanceof arrow.FixedSizeList;
}
function isDataSeparatedCoords(
data: arrow.Data,
): data is arrow.Data<arrow.Struct<{ x: arrow.Float64; y: arrow.Float64 }>> {
// TODO: also check child names? Float64?
return data.type instanceof arrow.Struct;
}
/**
* Convert geoarrow Struct coordinates to FixedSizeList coords
*
* The GeoArrow spec allows for either separated or interleaved coords, but at
* this time deck.gl only supports interleaved.
*/
// TODO: this hasn't been tested yet
function convertStructToFixedSizeList(
coords:
| arrow.Data<arrow.FixedSizeList<arrow.Float64>>
| arrow.Data<arrow.Struct<{ x: arrow.Float64; y: arrow.Float64 }>>,
): arrow.Data<arrow.FixedSizeList<arrow.Float64>> {
if (isDataInterleavedCoords(coords)) {
return coords;
} else if (isDataSeparatedCoords(coords)) {
// TODO: support 3d
const interleavedCoords = new Float64Array(coords.length * 2);
const [xChild, yChild] = coords.children;
for (let i = 0; i < coords.length; i++) {
interleavedCoords[i * 2] = xChild.values[i];
interleavedCoords[i * 2 + 1] = yChild.values[i];
}
const childDataType = new arrow.Float64();
const dataType = new arrow.FixedSizeList(
2,
new arrow.Field("coords", childDataType),
);
const interleavedCoordsData = arrow.makeData({
type: childDataType,
length: interleavedCoords.length,
});
const data = arrow.makeData({
type: dataType,
length: coords.length,
nullCount: coords.nullCount,
nullBitmap: coords.nullBitmap,
child: interleavedCoordsData,
});
return data;
}
assert(false);
}
type AssignAccessorProps = {
/** The object on which to assign the resolved accesor */
props: Record<string, any>;
/** The name of the prop to set */
propName: string;
/** The user-supplied input to the layer. Must either be a scalar value or a reference to a column in the table. */
propInput: any;
/** Numeric index in the table */
chunkIdx: number;
/** a map from the geometry index to the coord offsets for that geometry. */
geomCoordOffsets?: Int32Array | null;
};
/**
* A wrapper around a user-provided accessor function
*
* For layers like Scatterplot, Path, and Polygon, we automatically handle
* "exploding" the table when multi-geometry input are provided. This means that
* the upstream `index` value passed to the user will be the correct row index
* _only_ for non-exploded data.
*
* With this function, we simplify the user usage by automatically converting
* back from "exploded" index back to the original row index.
*/
function wrapAccessorFunction<In, Out>(
objectInfo: _InternalAccessorContext<In>,
userAccessorFunction: AccessorFunction<In, Out>,
): Out {
const { index, data } = objectInfo;
let newIndex = index;
if (data.invertedGeomOffsets !== undefined) {
newIndex = data.invertedGeomOffsets[index];
}
const newObjectData = {
data: data.data,
length: data.length,
attributes: data.attributes,
};
const newObjectInfo = {
index: newIndex,
data: newObjectData,
target: objectInfo.target,
};
return userAccessorFunction(newObjectInfo);
}
/**
* Resolve accessor and assign to props object
*
* This is useful as a helper function because a scalar prop is set at the top
* level while a vectorized prop is set inside data.attributes
*
*/
export function assignAccessor(args: AssignAccessorProps) {
const { props, propName, propInput, chunkIdx, geomCoordOffsets } = args;
if (propInput === undefined) {
return;
}
if (propInput instanceof arrow.Vector) {
const columnData = propInput.data[chunkIdx];
if (arrow.DataType.isFixedSizeList(columnData)) {
assert(columnData.children.length === 1);
let values = columnData.children[0].values;
if (geomCoordOffsets) {
values = expandArrayToCoords(
values,
columnData.type.listSize,
geomCoordOffsets,
);
}
props.data.attributes[propName] = {
value: values,
size: columnData.type.listSize,
// Set to `true` to signify that colors are already 0-255, and deck/luma
// does not need to rescale
// https://github.com/visgl/deck.gl/blob/401d624c0529faaa62125714c376b3ba3b8f379f/docs/api-reference/core/attribute-manager.md?plain=1#L66
normalized: true,
};
} else if (arrow.DataType.isFloat(columnData)) {
let values = columnData.values;
if (geomCoordOffsets) {
values = expandArrayToCoords(values, 1, geomCoordOffsets);
}
props.data.attributes[propName] = {
value: values,
size: 1,
};
}
} else if (typeof propInput === "function") {
props[propName] = <In>(object: any, objectInfo: AccessorContext<In>) => {
// Special case that doesn't have the same parameters
if (propName === "getPolygonOffset") {
return propInput(object, objectInfo);
}
return wrapAccessorFunction(objectInfo, propInput);
};
} else {
props[propName] = propInput;
}
}
/**
* Expand an array from "one element per geometry" to "one element per coordinate"
*
* @param input: the input array to expand
* @param size : the number of nested elements in the input array per geometry. So for example, for RGB data this would be 3, for RGBA this would be 4. For radius, this would be 1.
* @param geomOffsets : an offsets array mapping from the geometry to the coordinate indexes. So in the case of a LineStringArray, this is retrieved directly from the GeoArrow storage. In the case of a PolygonArray, this comes from the resolved indexes that need to be given to the SolidPolygonLayer anyways.
*
* @return {TypedArray} values expanded to be per-coordinate
*/
export function expandArrayToCoords<T extends TypedArray>(
input: T,
size: number,
geomOffsets: Int32Array,
): T {
const numCoords = geomOffsets[geomOffsets.length - 1];
// @ts-expect-error
const outputArray: T = new input.constructor(numCoords * size);
// geomIdx is an index into the geomOffsets array
// geomIdx is also the geometry/table index
for (let geomIdx = 0; geomIdx < geomOffsets.length - 1; geomIdx++) {
// geomOffsets maps from the geometry index to the coord index
// So here we get the range of coords that this geometry covers
const lastCoordIdx = geomOffsets[geomIdx];
const nextCoordIdx = geomOffsets[geomIdx + 1];
// Iterate over this range of coord indices
for (let coordIdx = lastCoordIdx; coordIdx < nextCoordIdx; coordIdx++) {
// Iterate over size
for (let i = 0; i < size; i++) {
// Copy from the geometry index in `input` to the coord index in
// `output`
outputArray[coordIdx * size + i] = input[geomIdx * size + i];
}
}
}
return outputArray;
}
/**
* Get a geometry vector with the specified extension type name from the table.
*/
export function getGeometryVector(
table: arrow.Table,
geoarrowTypeName: string,
): arrow.Vector | null {
const geometryColumnIdx = findGeometryColumnIndex(
table.schema,
geoarrowTypeName,
);
if (geometryColumnIdx === null) {
return null;
// throw new Error(`No column found with extension type ${geoarrowTypeName}`);
}
return table.getChildAt(geometryColumnIdx);
}
export function getListNestingLevels(data: arrow.Data): number {
let nestingLevels = 0;
if (arrow.DataType.isList(data.type)) {
nestingLevels += 1;
data = data.children[0];
}
return nestingLevels;
}
export function getMultiLineStringResolvedOffsets(
data: ga.data.MultiLineStringData,
): Int32Array {
const geomOffsets = data.valueOffsets;
const lineStringData = ga.child.getMultiLineStringChild(data);
const ringOffsets = lineStringData.valueOffsets;
const resolvedRingOffsets = new Int32Array(geomOffsets.length);
for (let i = 0; i < resolvedRingOffsets.length; ++i) {
// Perform the lookup into the ringIndices array using the geomOffsets
// array
resolvedRingOffsets[i] = ringOffsets[geomOffsets[i]];
}
return resolvedRingOffsets;
}
export function getPolygonResolvedOffsets(
data: ga.data.PolygonData,
): Int32Array {
const geomOffsets = data.valueOffsets;
const ringData = ga.child.getPolygonChild(data);
const ringOffsets = ringData.valueOffsets;
const resolvedRingOffsets = new Int32Array(geomOffsets.length);
for (let i = 0; i < resolvedRingOffsets.length; ++i) {
// Perform the lookup into the ringIndices array using the geomOffsets
// array
resolvedRingOffsets[i] = ringOffsets[geomOffsets[i]];
}
return resolvedRingOffsets;
}
export function getMultiPolygonResolvedOffsets(
data: ga.data.MultiPolygonData,
): Int32Array {
const polygonData = ga.child.getMultiPolygonChild(data);
const ringData = ga.child.getPolygonChild(polygonData);
const geomOffsets = data.valueOffsets;
const polygonOffsets = polygonData.valueOffsets;
const ringOffsets = ringData.valueOffsets;
const resolvedRingOffsets = new Int32Array(geomOffsets.length);
for (let i = 0; i < resolvedRingOffsets.length; ++i) {
resolvedRingOffsets[i] = ringOffsets[polygonOffsets[geomOffsets[i]]];
}
return resolvedRingOffsets;
}
/**
* Invert offsets so that lookup can go in the opposite direction
*/
export function invertOffsets(
offsets: Int32Array,
): Uint8Array | Uint16Array | Uint32Array {
const largestOffset = offsets[offsets.length - 1];
const arrayConstructor =
offsets.length < Math.pow(2, 8)
? Uint8Array
: offsets.length < Math.pow(2, 16)
? Uint16Array
: Uint32Array;
const invertedOffsets = new arrayConstructor(largestOffset);
for (let arrayIdx = 0; arrayIdx < offsets.length - 1; arrayIdx++) {
const thisOffset = offsets[arrayIdx];
const nextOffset = offsets[arrayIdx + 1];
for (let offset = thisOffset; offset < nextOffset; offset++) {
invertedOffsets[offset] = arrayIdx;
}
}
return invertedOffsets;
}
// TODO: better typing
export function extractAccessorsFromProps(
props: Record<string, any>,
excludeKeys: string[],
): [Record<string, any>, Record<string, any>] {
const accessors: Record<string, any> = {};
const otherProps: Record<string, any> = {};
for (const [key, value] of Object.entries(props)) {
if (excludeKeys.includes(key)) {
continue;
}
if (key.startsWith("get")) {
accessors[key] = value;
} else {
otherProps[key] = value;
}
}
return [accessors, otherProps];
}