-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimage_chernoff.html
183 lines (169 loc) Β· 8.41 KB
/
image_chernoff.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gridviz chernoff faces 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 id="map" style="height: 600px; width: 1000px"></div>
<script src="../../dist/gridviz.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gridviz-parquet@1.0.2"></script>
<script src="https://cdn.jsdelivr.net/npm/gridviz-eurostat@2.0.2"></script>
<script>
//define map with initial view
const map = new gridviz.Map(document.getElementById('map'), {
x: 4500000,
y: 2900000,
z: 200,
}).addZoomButtons()
//define layers
const boundaryLayer = new gridviz.GeoJSONLayer(gridviz_eurostat.getEurostatBoundariesLayer())
const labelLayer = new gridviz.LabelLayer(gridviz_eurostat.getEuronymeLabelLayer())
const backgroundLayer1 = new gridviz.BackgroundLayer({
url: 'https://gisco-services.ec.europa.eu/maps/tiles/OSMPositronBackground/EPSG3035/',
resolutions: [
156543.03392804097, 78271.51696402048, 39135.75848201024, 19567.87924100512,
9783.93962050256, 4891.96981025128, 2445.98490512564, 1222.99245256282, 611.49622628141,
305.748113140705, 152.8740565703525, 76.43702828517625, 38.21851414258813,
19.109257071294063, 9.554628535647032, 4.777314267823516, 2.388657133911758,
1.19432856695, 0.597164283477939,
],
origin: [0, 6000000],
visible: (z) => z < 50,
})
const backgroundLayer2 = new gridviz.BackgroundLayer({
url: 'https://raw.githubusercontent.com/jgaffuri/mbxyz/main/pub/elevation_shading/',
resolutions: Array.from({ length: 9 }, (_, i) => 28.00132289714475 * Math.pow(2, 10 - i)),
origin: [0, 6000000],
filterColor: (zf) => '#ffffff60',
visible: (z) => z >= 50,
})
//function to compute the percentage of a cell value
const computePercentage = (c, col, totalFunction) => {
const total = totalFunction(c)
if (total == 0) {
c['s' + col] = undefined
return
}
c['s' + col] = (+c[col] / total) * 100
//if (c["s" + col] < 0 || c["s" + col] > 100 || isNaN(total)) console.log("Unexpected rate for " + col + ": " + c["s" + col], c[col], total)
c['s' + col] = c['s' + col] > 100 ? 100 : c['s' + col] < 0 ? 0 : c['s' + col]
}
//define dataset
const dataset = new gridviz.MultiResolutionDataset(
//the resolutions
[1000, 2000, 5000, 10000, 20000, 50000, 100000],
//the dataset for each resolution
(resolution) =>
new gviz_par.TiledParquetGrid(
map,
'https://raw.githubusercontent.com/jgaffuri/tiled-census2021/main/pub/v2/parquet/' +
resolution +
'/',
{
//process the grid cells
preprocess: (c) => {
if (!c.T || +c.T == 0) return false
//male/female index
//if (+c.F + +c.M != c.T) console.error("Error found in sex total")
c.indMF = +c.M - +c.F
c.indMF = (100 * c.indMF) / (+c.M + +c.F)
if (isNaN(c.indMF)) c.indMF = 0
//compute percentages
computePercentage(c, 'F', (c) => +c.F + +c.M)
computePercentage(c, 'M', (c) => +c.F + +c.M)
computePercentage(c, 'Y_LT15', (c) => +c.Y_LT15 + +c.Y_1564 + +c.Y_GE65)
computePercentage(c, 'Y_1564', (c) => +c.Y_LT15 + +c.Y_1564 + +c.Y_GE65)
computePercentage(c, 'Y_GE65', (c) => +c.Y_LT15 + +c.Y_1564 + +c.Y_GE65)
computePercentage(c, 'EMP', (c) => c.T) //TODO sure?
computePercentage(c, 'NAT', (c) => +c.NAT + +c.EU_OTH + +c.OTH)
computePercentage(c, 'EU_OTH', (c) => +c.NAT + +c.EU_OTH + +c.OTH)
computePercentage(c, 'OTH', (c) => +c.NAT + +c.EU_OTH + +c.OTH)
computePercentage(c, 'SAME', (c) => +c.SAME + +c.CHG_IN + +c.CHG_OUT)
computePercentage(c, 'CHG_IN', (c) => +c.SAME + +c.CHG_IN + +c.CHG_OUT)
computePercentage(c, 'CHG_OUT', (c) => +c.SAME + +c.CHG_IN + +c.CHG_OUT)
},
}
)
)
//age classifier - 3/4 classes
const ageClassifier = gridviz.ternaryClassifier(['sY_LT15', 'sY_1564', 'sY_GE65'], (c) => 100, {
center: [0.15, 0.64, 0.21],
withMixedClasses: false,
})
//sex classifier
const sexClassifier = (c) => (c.indMF <= 0 ? 'f' : 'm')
//employment classifier
const breaks = [45, 55]
const empClassifier = gridviz.classifier(breaks)
//classificatio for size / total population
const classNumberSize = 3
//define image style
const style = new gridviz.ImageStyle({
//return image URL from cell, using https://github.com/jgaffuri/chernoff-faces
image: (c) => {
//age
const a = +ageClassifier(c)
const ageCode = a == 0 ? 'y' : a == 1 ? 'm' : 'o'
//sex
const sexCode = sexClassifier(c)
//employment
const empCode = empClassifier(c.sEMP)
return (
'https://jgaffuri.github.io/chernoff-faces/out/v1/' +
sexCode +
ageCode +
empCode +
'.png'
)
},
//return image size, based on total population
size: (c, r, z, viewScale) => viewScale(c.T),
viewScale: gridviz.viewScaleQuantile({
valueFunction: (c) => +c.T,
classNumber: classNumberSize,
minSizePix: 30,
maxSizeFactor: 0.9,
}),
blendOperation: (z) => (z < 50 ? 'multiply' : 'source-over'),
})
const gridLayer = new gridviz.GridLayer(dataset, [style])
gridLayer.minPixelsPerCell = 40
gridLayer.cellInfoHTML = (c) => {
const a = ageClassifier(c)
const s = sexClassifier(c)
const e = empClassifier(c.sEMP)
console.log(c.EMP)
return (
'' +
c.T +
' person' +
(c.T ? 's' : '') +
'<br>Over representation of <b>' +
(s == 'm' ? 'men' : 'women') +
'</b>' +
'<br>' +
(a == '0'
? 'younger than <b>15</b>'
: a == '1'
? 'aged between <b>15 and 64</b>'
: 'aged <b>65</b> and older') +
'<br>with <b>' +
(e == 0 ? 'low' : e == 1 ? 'average' : 'high') +
'</b> employment'
)
}
//add layers to map
map.layers = [backgroundLayer1, backgroundLayer2, gridLayer, boundaryLayer, labelLayer]
</script>
</body>
</html>