-
Notifications
You must be signed in to change notification settings - Fork 13
/
XyHexLayer.tsx
137 lines (125 loc) · 3.43 KB
/
XyHexLayer.tsx
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
import React, { useState, useMemo, useEffect } from 'react'
import DeckGL from '@deck.gl/react'
import { StaticMap, InteractiveMap } from 'react-map-gl'
import { ArcLayer } from '@deck.gl/layers'
import HexagonLayer from './SelectableHexLayer'
import colormap from 'colormap'
import { MAPBOX_TOKEN, MAP_STYLES } from '@/Globals'
import { pointToHexbin } from './HexagonAggregator'
import globalStore from '@/store'
const material = {
ambient: 0.64,
diffuse: 0.6,
shininess: 32,
specularColor: [51, 51, 51],
}
const INITIAL_VIEW = {
zoom: 10,
longitude: 13.45,
latitude: 52.5,
pitch: 0,
bearing: 0,
}
export default function Layer({
data = [],
highlights = [],
dark = false,
radius = 100,
upperPercentile = 100,
coverage = 0.65,
extrude = true,
maxHeight = 200,
onClick = {} as any,
colorRamp = 'chlorophyll',
metric = 'Count',
selectedHexStats = { rows: 0, numHexagons: 0, selectedHexagonIds: [] },
}) {
// draw begins here
const initialViewState = Object.assign({}, INITIAL_VIEW)
const colors = colormap({
colormap: colorRamp,
nshades: 10,
format: 'rba',
alpha: 1,
}).map((c: number[]) => [c[0], c[1], c[2]])
function getTooltip({ object }: any) {
if (!object || !object.position || !object.position.length) {
return null
}
const lat = object.position[1]
const lng = object.position[0]
const count = object.points.length
return {
html: `\
<b>${highlights.length ? 'Count' : metric}: ${count} </b><br/>
${Number.isFinite(lat) ? lat.toFixed(4) : ''} / ${
Number.isFinite(lng) ? lng.toFixed(4) : ''
}
`,
style: dark
? { color: '#ccc', backgroundColor: '#2a3c4f' }
: { color: '#223', backgroundColor: 'white' },
}
}
function handleClick(target: any, event: any) {
onClick(target, event)
}
const layers = [
new ArcLayer({
id: 'arc-layer',
data: highlights,
getSourcePosition: (d: any) => d[0],
getTargetPosition: (d: any) => d[1],
pickable: false,
opacity: 0.4,
getHeight: 0,
getWidth: 1,
getSourceColor: dark ? [144, 96, 128] : [192, 192, 240],
getTargetColor: dark ? [144, 96, 128] : [192, 192, 240],
}),
new HexagonLayer({
id: 'hexlayer',
colorRange: dark ? colors.slice(1) : colors.reverse().slice(1),
coverage,
data,
autoHighlight: true,
elevationRange: [0, maxHeight],
elevationScale: data && data.length ? 50 : 0,
extruded: extrude,
selectedHexStats,
getPosition: (d: any) => d,
hexagonAggregator: pointToHexbin,
center: [initialViewState.longitude, initialViewState.latitude],
pickable: true,
opacity: 0.75, // dark && highlights.length ? 0.6 : 0.8,
radius,
upperPercentile,
material,
transitions: {
elevationScale: { type: 'interpolation', duration: 1000 },
opacity: { type: 'interpolation', duration: 200 },
},
}),
]
return (
<DeckGL
layers={layers}
initialViewState={initialViewState}
controller={true}
getTooltip={getTooltip}
onClick={handleClick}
onViewStateChange={(e: any) => {
globalStore.commit('setMapCamera', e.viewState)
}}
>
{
/*
// @ts-ignore */
<StaticMap
mapStyle={dark ? MAP_STYLES.dark : MAP_STYLES.light}
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
}
</DeckGL>
)
}