-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworker-grid.ts
143 lines (114 loc) · 3.37 KB
/
worker-grid.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
import { LegendLayer } from "@/stores";
import { load } from "@loaders.gl/core";
import { WKBLoader } from "@loaders.gl/wkt";
import { tableFromIPC, Vector } from "apache-arrow";
import { ColorConfig, getColorSchema } from "@/libs/colors";
/**
* input for this worker
*/
export type WorkerGridInput = {
table: Uint8Array;
color: ColorConfig;
};
/**
* value thet exported from this worker
*/
export type WorkerGridData = {
/**
* just for record how much time needed
*/
timer: number;
/**
* IPC buffer for apache arrow tables
*/
table: Uint8Array;
/**
* quantile of the data
*/
legend: LegendLayer;
/**
* color in INT8 value
*/
colors: Uint8Array;
/**
* coordinates buffer
*/
coordinates: Float64Array;
};
/**
* this woker handle data transformation and preparation before it
* can be usage in layer. we will generate coordinates, radiuses, and
* colors binary array from the data
*/
self.onmessage = async (event: MessageEvent<WorkerGridInput>) => {
const start = performance.now();
const table = tableFromIPC(event.data.table);
const WKBCoords = table.getChild("location") || [];
const values = table.getChild("value");
/**
* we count data length based on location column, if they
* don't exist so we don't have to return anything
*/
const length = WKBCoords.length;
/**
* flat coordinates [x0, y0, x1, y1, ...]
*/
const flatCoordinates = new Float64Array(length * 2);
/**
* we store color value as INT8 (max 256),
* [r0, g0, b0, alpha0, r1, g1, b1, alpha1, ...]
*/
const flatColors = new Uint8Array(length * 4);
/**
* sorted data to create a histogram max min and range
*/
const sorted = (values?.toArray() as Float64Array | undefined)?.toSorted();
const min = sorted?.[0]!;
const max = sorted?.[sorted?.length - 1]!;
const width = (max! - min!) / event.data.color.length;
const colorsSchema = getColorSchema(event.data.color);
/**
* create legend with cutoff
*/
const legend: LegendLayer = new Map(
Array.from({ length: event.data.color.length }).map((_, id) => {
return [
(min! + (id + 1) * width).toFixed(2).toString(),
{ total: 0, color: colorsSchema[id] },
];
})
);
const cutOff = [...legend.keys()];
/**
* legend will be used to show the mathing color, value, and
* it's total.
*/
for (let index = 0; index < length; index++) {
const coord = (WKBCoords as Vector).get(index);
type WKB = { positions: { value: Float64Array } };
/**
* parsing wellknown binary to FLoat64
*/
const point = await load(coord, WKBLoader, { worker: false }).catch(() => {
return { positions: { value: [0, 0] } };
});
const coordinates = (point as WKB).positions.value;
flatCoordinates[index * 2] = coordinates[0];
flatCoordinates[index * 2 + 1] = coordinates[1];
const value = values?.get(index) || 0;
const lastCutoff = cutOff.find((x) => value <= Number(x!));
flatColors.set(legend.get(lastCutoff)!.color, 4 * index);
legend.get(lastCutoff)!.total++;
}
const end = performance.now();
(self as unknown as Worker).postMessage(
{
timer: end - start,
table: event.data.table,
legend: legend,
colors: flatColors,
coordinates: flatCoordinates,
} as WorkerGridData,
[event.data.table.buffer, flatColors.buffer, flatCoordinates.buffer]
);
};