-
Notifications
You must be signed in to change notification settings - Fork 4
/
cbsgrid.html
231 lines (214 loc) · 8 KB
/
cbsgrid.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- mapbox -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.13.2/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.13.2/mapbox-gl.css' rel='stylesheet' />
<!--handsontable -->
<link href="node_modules/handsontable/dist/handsontable.full.css" rel='stylesheet' />
<script src='node_modules/handsontable/dist/handsontable.full.js'></script>
<!-- papa parser -->
<script src="node_modules/papaparse/papaparse.min.js"></script>
<!-- page styles -->
<style>
body { margin:0; padding:0; font-family: Arial, Helvetica, sans-serif;}
#map { position:absolute; top:0; bottom:20%; width:100%; }
#metadata { position: absolute; top: 10px; left: 10px; background: white; border: 1 px solid darkgray;padding: 2px;min-width: 200px;font-size:xx-large;}
#table { position:absolute; top:80%; height:20%; width: 100%; outline: 1px solid black; background: white; overflow: auto;}
</style>
</head>
<body>
<div id='map'></div>
<div id="metadata"></div>
<div id='table'></div>
<script>
var map = new mapboxgl.Map({
container: 'map',
style: 'styles/freetilehosting/positron.json',
zoom: 7,
center: [4.913, 52.18]
});
// First value is the default, used where there is no data
var stops = [["0", "rgba(0,0,0,0)"]];
var uniqueDataId = 'gwb_code_10'; // unique id data elements
var uniqueMapElementId = 'gm_code'; // unique id map elements
var selectedDataFieldName = 'p_1gezw';
var data = null;
var metadata = null;
var colNames = null;
var dataIdColumn = 0;
var grid = null;
function classifyData(dataIdColumn, valueColumn, data)
{
var stops = [["0", "rgba(0,0,0,0)"]];
var minmax = data.reduce(function(minmax, currentRow) {
var value = parseFloat(currentRow[valueColumn]);
if (value < minmax[0]) {
minmax[0] = value;
}
if (value > minmax[1]) {
minmax[1] = value;
}
return minmax;
},[parseFloat(data[0][valueColumn]), parseFloat(data[0][valueColumn])]);
// http://colorbrewer2.org/#type=sequential&scheme=Greens&n=8
for (i = 0; i < data.length; i++) {
var ratio = (data[i][valueColumn] - minmax[0]) / (minmax[1] - minmax[0]);
var red = 247 - Math.floor(247 * ratio);
var green = 252 - Math.floor((252 - 90) * ratio);
var blue = 245 - Math.floor((245 - 50) * ratio);
var color = "rgba(" + red + "," + green + "," + blue + ",1)";
stops.push([data[i][dataIdColumn], color]);
}
return stops;
}
function updateMetaData(fieldName)
{
var metadataElement =document.getElementById('metadata');
if (metadata) {
var result = metadata.filter(function(element) {
return element[0] == fieldName;
});
if (result.length) {
metadataElement.innerText = result[0][1] + " (" + result[0][0] + ")"
} else {
metadataElement.innerText = fieldName;
}
}
}
function columnSortClicked(currentSortConfig, destinationSortConfigs){
const colName = grid.colHeaders[currentSortConfig[0].column];
console.log('colName');
}
Papa.parse("data/cbsgemeenten2016.csv", {
"download": true,
"complete": function(results) {
data = results.data;
colNames = data.splice(0, 1)[0]; // remove colNames from array
grid = new Handsontable(document.getElementById('table'), {
data: data,
minSpareCols: 0,
minSpareRows: 0,
rowHeaders: true,
colHeaders: colNames,
contextMenu: true,
columnSorting: true,
sortIndicator: true,
manualColumnResize: true,
fixedColumnsLeft: 1,
//afterColumnSort: columnSortClicked,
afterSelection: function(r,c, r2, c2, preventScrolling) {
if (selectedDataFieldName !=colNames[c]) {
// column changed, redraw map
selectedDataFieldName =colNames[c];
updateMetaData(selectedDataFieldName);
stops = classifyData(dataIdColumn, c, data);
map.setPaintProperty('gemeenten', 'fill-color', {
"property": uniqueMapElementId,
"type": "categorical",
"stops": stops
});
}
if (r == r2 && c == c2) {
// single cell selected
// visualize gemeente on map
map.setFilter('gemeenteselect', ['==', uniqueMapElementId, grid.getDataAtCell(r, colNames.indexOf(uniqueDataId))]);
}
}
});
var valueColumn = colNames.indexOf(selectedDataFieldName);
dataIdColumn = colNames.indexOf(uniqueDataId);
stops = classifyData(dataIdColumn, valueColumn, data);
}
});
Papa.parse("data/cbsgemeenten2016meta.csv", {
"download": true,
"complete": function(results) {
metadata = results.data;
updateMetaData(selectedDataFieldName);
}
});
map.on('load', function () {
//map.setLayoutProperty('country-label-lg', 'text-field', '{name_' + language + '}');
map.setLayoutProperty('place_city', 'text-field', '{name}');
map.setLayoutProperty('place_country_other', 'text-field', '{name}');
map.addSource("gemeenten", {
type: 'vector',
tiles:["https://saturnus.geodan.nl/mvt/gemeenten2016/{z}/{x}/{y}.mvt"],
"bounds": [3.38,50.73,7.2432,53.5455]
});
map.addLayer({
"id": "gemeenten",
"minzoom": 5,
"type": "fill",
"source": "gemeenten",
"source-layer": "gemeenten2016",
"filter": [ '==', 'water', 'NEE'],
"paint": {
"fill-color": {
"property": uniqueMapElementId,
"type": "categorical",
"stops": stops
},
"fill-opacity": 0.8
}
}, 'place_other');
map.addLayer({
'id': "gemeentewater",
"minzoom": 5,
"type": "fill",
"source": "gemeenten",
"source-layer": "gemeenten2016",
"filter": [ '==', 'water', 'JA'],
"paint": {
"fill-color": "hsl(205,56%, 73%)",
"fill-opacity": 0.5
}
}, 'place_other');
map.addLayer({
"id": "gemeentenline",
"minzoom": 5,
"type": "line",
"source": "gemeenten",
"source-layer": "gemeenten2016",
"paint": {
"line-color": 'gray',
"line-width": 0.5,
"line-opacity": 0.5
}
}, 'place_other');
map.addLayer ({
"id": "gemeenteselect",
"type" : "line",
"minzoom": 5,
"source": "gemeenten",
"source-layer": "gemeenten2016",
"paint": {
"line-color": "red",
"line-width": 1,
"line-opacity": 0.8
},
"filter": ["==", uniqueMapElementId, ""]
}, "place_other");
});
map.on("click", function (e) {
var features = map.queryRenderedFeatures(e.point).map(function(feature){ return {layer: {id: feature.layer.id, type: feature.layer.type}, properties:(feature.properties)};});
var layer = features.filter(function(feature) {return(feature.layer && feature.layer.id && feature.layer.id=='gemeenten')});
if (layer.length) {
var keyValue = layer[0].properties[uniqueMapElementId];
var row = grid.getDataAtCol(colNames.indexOf(uniqueDataId)).indexOf(keyValue);
if (row > -1) {
var valueColumn = colNames.indexOf(selectedDataFieldName);
grid.selectCell(row, valueColumn, row, valueColumn, true, false);
}
} else {
// gemeente layer not selected
map.setFilter('gemeenteselect', ['==', uniqueMapElementId, '']);
}
});
</script>
</body>
</html>