-
Notifications
You must be signed in to change notification settings - Fork 13
/
ClusterIndex.ts
261 lines (241 loc) · 7.33 KB
/
ClusterIndex.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
/*
* Copyright (c) Flowmap.gl contributors
* Copyright (c) 2018-2020 Teralytics
* SPDX-License-Identifier: Apache-2.0
*/
import {
AggregateFlow,
Cluster,
ClusterLevels,
ClusterNode,
FlowAccessors,
FlowCountsMapReduce,
isCluster,
} from './../types';
import {ascending, bisectLeft, extent} from 'd3-array';
export type LocationWeightGetter = (id: string | number) => number;
/**
* A data structure representing the cluster levels for efficient flow aggregation.
*/
export interface ClusterIndex<F> {
availableZoomLevels: number[];
getClusterById: (clusterId: string | number) => Cluster | undefined;
/**
* List the nodes on the given zoom level.
*/
getClusterNodesFor: (zoom: number | undefined) => ClusterNode[] | undefined;
/**
* Get the min zoom level on which the location is not clustered.
*/
getMinZoomForLocation: (locationId: string | number) => number;
/**
* List the IDs of all locations in the cluster (leaves of the subtree starting in the cluster).
*/
expandCluster: (cluster: Cluster, targetZoom?: number) => string[];
/**
* Find the cluster the given location is residing in on the specified zoom level.
*/
findClusterFor: (
locationId: string | number,
zoom: number,
) => string | number | undefined;
/**
* Aggregate flows for the specified zoom level.
*/
aggregateFlows: (
flows: F[],
zoom: number,
{getFlowOriginId, getFlowDestId, getFlowMagnitude}: FlowAccessors<F>,
options?: {
flowCountsMapReduce?: FlowCountsMapReduce<F>;
},
) => (F | AggregateFlow)[];
}
/**
* Build ClusterIndex from the given cluster hierarchy
*/
export function buildIndex<F>(clusterLevels: ClusterLevels): ClusterIndex<F> {
const nodesByZoom = new Map<number, ClusterNode[]>();
const clustersById = new Map<string | number, Cluster>();
const minZoomByLocationId = new Map<string | number, number>();
for (const {zoom, nodes} of clusterLevels) {
nodesByZoom.set(zoom, nodes);
for (const node of nodes) {
if (isCluster(node)) {
clustersById.set(node.id, node);
} else {
const {id} = node;
const mz = minZoomByLocationId.get(id);
if (mz == null || mz > zoom) {
minZoomByLocationId.set(id, zoom);
}
}
}
}
const [minZoom, maxZoom] = extent(clusterLevels, (cl) => cl.zoom);
if (minZoom == null || maxZoom == null) {
throw new Error('Could not determine minZoom or maxZoom');
}
const leavesToClustersByZoom = new Map<
number,
Map<string | number, Cluster>
>();
for (const cluster of clustersById.values()) {
const {zoom} = cluster;
let leavesToClusters = leavesToClustersByZoom.get(zoom);
if (!leavesToClusters) {
leavesToClusters = new Map<string, Cluster>();
leavesToClustersByZoom.set(zoom, leavesToClusters);
}
visitClusterLeaves(cluster, (leafId) => {
leavesToClusters?.set(leafId, cluster);
});
}
function visitClusterLeaves(cluster: Cluster, visit: (id: string) => void) {
for (const childId of cluster.children) {
const child = clustersById.get(childId);
if (child) {
visitClusterLeaves(child, visit);
} else {
visit(childId);
}
}
}
const expandCluster = (cluster: Cluster, targetZoom: number = maxZoom) => {
const ids: string[] = [];
const visit = (c: Cluster, expandedIds: (string | number)[]) => {
if (targetZoom > c.zoom) {
for (const childId of c.children) {
const child = clustersById.get(childId);
if (child) {
visit(child, expandedIds);
} else {
expandedIds.push(childId);
}
}
} else {
expandedIds.push(c.id);
}
};
visit(cluster, ids);
return ids;
};
function findClusterFor(locationId: string | number, zoom: number) {
const leavesToClusters = leavesToClustersByZoom.get(zoom);
if (!leavesToClusters) {
return undefined;
}
const cluster = leavesToClusters.get(locationId);
return cluster ? cluster.id : undefined;
}
const availableZoomLevels = clusterLevels
.map((cl) => +cl.zoom)
.sort((a, b) => ascending(a, b));
return {
availableZoomLevels,
getClusterNodesFor: (zoom) => {
if (zoom === undefined) {
return undefined;
}
return nodesByZoom.get(zoom);
},
getClusterById: (clusterId) => clustersById.get(clusterId),
getMinZoomForLocation: (locationId) =>
minZoomByLocationId.get(locationId) || minZoom,
expandCluster,
findClusterFor,
aggregateFlows: (
flows,
zoom,
{getFlowOriginId, getFlowDestId, getFlowMagnitude},
options = {},
) => {
if (zoom > maxZoom) {
return flows;
}
const result: (F | AggregateFlow)[] = [];
const aggFlowsByKey = new Map<string, AggregateFlow>();
const makeKey = (origin: string | number, dest: string | number) =>
`${origin}:${dest}`;
const {
flowCountsMapReduce = {
map: getFlowMagnitude,
reduce: (acc: any, count: number) => (acc || 0) + count,
},
} = options;
for (const flow of flows) {
const origin = getFlowOriginId(flow);
const dest = getFlowDestId(flow);
const originCluster = findClusterFor(origin, zoom) || origin;
const destCluster = findClusterFor(dest, zoom) || dest;
const key = makeKey(originCluster, destCluster);
if (originCluster === origin && destCluster === dest) {
result.push(flow);
} else {
let aggregateFlow = aggFlowsByKey.get(key);
if (!aggregateFlow) {
aggregateFlow = {
origin: originCluster,
dest: destCluster,
count: flowCountsMapReduce.map(flow),
aggregate: true,
};
result.push(aggregateFlow);
aggFlowsByKey.set(key, aggregateFlow);
} else {
aggregateFlow.count = flowCountsMapReduce.reduce(
aggregateFlow.count,
flowCountsMapReduce.map(flow),
);
}
}
}
return result;
},
};
}
export function makeLocationWeightGetter<F>(
flows: F[],
{getFlowOriginId, getFlowDestId, getFlowMagnitude}: FlowAccessors<F>,
): LocationWeightGetter {
const locationTotals = {
incoming: new Map<string | number, number>(),
outgoing: new Map<string | number, number>(),
};
for (const flow of flows) {
const origin = getFlowOriginId(flow);
const dest = getFlowDestId(flow);
const count = getFlowMagnitude(flow);
locationTotals.incoming.set(
dest,
(locationTotals.incoming.get(dest) || 0) + count,
);
locationTotals.outgoing.set(
origin,
(locationTotals.outgoing.get(origin) || 0) + count,
);
}
return (id: string | number) =>
Math.max(
Math.abs(locationTotals.incoming.get(id) || 0),
Math.abs(locationTotals.outgoing.get(id) || 0),
);
}
/**
* @param availableZoomLevels Must be sorted in ascending order
* @param targetZoom
*/
export function findAppropriateZoomLevel(
availableZoomLevels: number[],
targetZoom: number,
) {
if (!availableZoomLevels.length) {
throw new Error('No available zoom levels');
}
return availableZoomLevels[
Math.min(
bisectLeft(availableZoomLevels, Math.floor(targetZoom)),
availableZoomLevels.length - 1,
)
];
}