-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkernelsmoothing.html
143 lines (133 loc) Β· 6.13 KB
/
kernelsmoothing.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gridviz smoothing example</title>
<style>
html,
body {
height: 100%; /* Ensure the parent elements have a height so that the map can take up 100% correctly */
margin: 0; /* Remove default margins to prevent scrolling */
overflow: hidden; /* Prevent potential scrollbars */
}
</style>
</head>
<body>
<div>
Style<br /><select id="style">
<option value="color" selected>Color - continuous</option>
<option value="color_discrete">Color - discrete</option>
<option value="size">Circle size - continuous</option>
<option value="size_discrete">Circle size - discrete</option>
</select>
<br />Smoothing<br />
<input type="range" min="3" max="30" value="10" class="slider" id="sigma" />
</div>
<div id="map" style="height: 100%; width: 100%"></div>
<script src="../../dist/gridviz.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gridviz-smoothing@2.0.2"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-array@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-color@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-interpolate@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-scale-chromatic@3"></script>
<script>
//define map with initial view
const map = new gridviz.Map(document.getElementById('map'), {
x: 4500000,
y: 2900000,
z: 3000,
}).addZoomButtons()
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(
map,
'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/population2/' +
resolution +
'm/'
)
)
//define style
const smoothingStyle = new gridviz_smoothing.KernelSmoothingStyle({
value: (cell) => +cell.TOT_P_2021,
filterSmoothed: (value) => value > 0.001,
})
//add layer to map
map.layers = [new gridviz.GridLayer(dataset, [smoothingStyle])]
//
const update = () => {
//set sigma
const sig = +document.querySelector('#sigma').value
smoothingStyle.sigma = (resolution, z) => (resolution * sig) / 10
//set selected style
const sty = document.querySelector('#style').value
if (sty == 'color') {
smoothingStyle.factor = 3
const scale = gridviz.exponentialScale(-20)
smoothingStyle.styles = [
new gridviz.SquareColorWebGLStyle({
color: (t) => d3.interpolateYlOrRd(t), //d3.interpolateCividis, interpolateSpectral
viewScale: (cells) => d3.max(cells, (cell) => +cell.ksmval),
tFun: (cell, resolution, z, max) => {
const t = scale(cell.ksmval / max)
return t
},
}),
]
} else if (sty == 'color_discrete') {
smoothingStyle.factor = 2
smoothingStyle.styles = [
new gridviz.ShapeColorSizeStyle({
color: (c, r, z, viewScale) => viewScale(c.ksmval),
viewScale: gridviz.viewScaleColor({
valueFunction: (c) => +c.ksmval,
colors: d3.schemeYlOrRd[7],
stretching: gridviz.logarithmicScale(-7),
}),
}),
]
} else if (sty == 'size') {
smoothingStyle.factor = 8
smoothingStyle.styles = [
new gridviz.ShapeColorSizeStyle({
size: (c, r, z, viewScale) => viewScale(c.ksmval),
viewScale: gridviz.viewScale({
valueFunction: (c) => +c.ksmval,
maxSizeFactor: 1.3,
minSizePix: 1,
stretching: gridviz.logarithmicScale(-7),
}),
shape: () => 'circle',
color: () => 'black',
}),
]
} else if (sty == 'size_discrete') {
smoothingStyle.factor = 8
smoothingStyle.styles = [
new gridviz.ShapeColorSizeStyle({
size: (c, r, z, viewScale) => viewScale(c.ksmval),
viewScale: gridviz.viewScale({
valueFunction: (c) => +c.ksmval,
maxSizeFactor: 1.3,
minSizePix: 1,
stretching: gridviz.logarithmicScale(-7),
classNumber: 7,
}),
shape: () => 'circle',
color: () => 'black',
}),
]
}
//redraw
map.redraw()
}
//style selection
document.getElementById('style').addEventListener('change', update)
//sigma selection
document.getElementById('sigma').oninput = update
update()
</script>
</body>
</html>