-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon-cluster-layer.js
80 lines (71 loc) · 2.06 KB
/
icon-cluster-layer.js
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
import {CompositeLayer} from '@deck.gl/core';
import {IconLayer} from '@deck.gl/layers';
import Supercluster from 'supercluster';
function getIconName(size) {
if (size === 0) {
return '';
}
if (size < 10) {
return `marker-${size}`;
}
if (size < 100) {
return `marker-${Math.floor(size / 10)}0`;
}
return 'marker-100';
}
function getIconSize(size) {
return Math.min(100, size) / 100 + 1;
}
export default class IconClusterLayer extends CompositeLayer {
shouldUpdateState({changeFlags}) {
return changeFlags.somethingChanged;
}
updateState({props, oldProps, changeFlags}) {
const rebuildIndex = changeFlags.dataChanged || props.sizeScale !== oldProps.sizeScale;
if (rebuildIndex) {
const index = new Supercluster({maxZoom: 16, radius: props.sizeScale * Math.sqrt(2)});
index.load(
props.data.map(d => ({
geometry: {coordinates: props.getPosition(d)},
properties: d
}))
);
this.setState({index});
}
const z = Math.floor(this.context.viewport.zoom);
if (rebuildIndex || z !== this.state.z) {
this.setState({
data: this.state.index.getClusters([-180, -85, 180, 85], z),
z
});
}
}
getPickingInfo({info, mode}) {
const pickedObject = info.object && info.object.properties;
if (pickedObject) {
if (pickedObject.cluster && mode !== 'hover') {
info.objects = this.state.index
.getLeaves(pickedObject.cluster_id, 25)
.map(f => f.properties);
}
info.object = pickedObject;
}
return info;
}
renderLayers() {
const {data} = this.state;
const {iconAtlas, iconMapping, sizeScale} = this.props;
return new IconLayer(
this.getSubLayerProps({
id: 'icon',
data,
iconAtlas,
iconMapping,
sizeScale,
getPosition: d => d.geometry.coordinates,
getIcon: d => getIconName(d.properties.cluster ? d.properties.point_count : 1),
getSize: d => getIconSize(d.properties.cluster ? d.properties.point_count : 1)
})
);
}
}