-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
236 lines (211 loc) · 6.85 KB
/
index.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
232
233
234
235
236
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Territory Control in Switzerland 1000-today</title>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v2.5.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.5.0/mapbox-gl.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
width: 100%;
height: 95%;
}
#year-level-selector {
position: absolute;
bottom: 0;
width: 100%;
/*min-height: 40px;*/
background-color: white;
z-index: 1000;
}
</style>
</head>
<body>
<div id='map'></div>
<form id='year-level-selector' onsubmit="return false" >
<fieldset>
<label>Level:</label>
<select id="level-selector" name="level-selector">
<option value="gde">Communes</option>
<option value="bez">Districts</option>
<option value="kt" selected>Cantons</option>
</select>
<label for="year">Year:</label> <input id="year-selector" name="year" type="text" value="2012">
</fieldset>
</form>
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoic2NhbnZhbi1kaGxhYiIsImEiOiJja2lwd29qam8wYTE2MnFwandrODQ3NmcxIn0.aeqc4_YClf5MIp2x3dH8hg';
const map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/light-v10', // stylesheet location
center: [8.2,46.8], // starting position [lng, lat]
zoom: 7 // starting zoom
});
map.on('load', () => {
map.addSource('territories', {
type: 'geojson',
// Use a URL for the value for the `data` property.
data: './web/epsg4326_g0g1848_no_lakes.geojson',//'./web/epsg4326_K4_1850_gde.geojson' //
//maxzoom: 12
});
map.addLayer({
'id': 'territories-fill',
'type': 'fill',
'source': 'territories',
layout:{},
'paint': {
"fill-color": "rgb(123,40,30)",
"fill-opacity": 1
}
});
// Add a black outline around the polygon.
/*map.addLayer({
'id': 'territories-outline',
'type': 'line',
'source': 'territories',
'layout': {},
'paint': {
'line-color': '#000',
'line-width': 0.5
}
});*/
/*map.addLayer({
id: "territories-hid",
type: "symbol",
source: "territories",
minzoom: 8,
layout: {
"text-field": ["concat", ["get", "GdeName"], "\n",["get", "histid"]],
"text-font": [
"case",
["has", "histid"],
["literal", ["Open Sans Bold"]],
["literal", ["Open Sans Regular"]],
],
//"text-variable-anchor": ["top", "bottom", "left", "right"],
//"text-radial-offset": 0.5,
"text-justify": "center",
"text-size": [
"interpolate",
["exponential", 0.5],
["zoom"],
10,
12,
22,
18,
],
}
})*/
setControlsAtDateAndLevel(yearSelector.value, levelSelector.value)
console.log("done")
});
let GDE = "gde"
let BEZ = "bez"
let KT = "kt"
let levels=[GDE, BEZ, KT]
let gdes=false
let bez=false
let kt=false
let levelsControllers = {}
levelsControllers[GDE] = gdes
levelsControllers[BEZ] = bez
levelsControllers[KT] = kt
/** Loads control relationships at level for all dates, from static jsons
*/
async function loadControlsAtLevel(level){
if(!levels.includes(level)) {
throw new Error("loadControlsAtLevel(), Invalid level", level);
}
if(!levelsControllers[level]){
let resp = await fetch("./web/controls_at_dates_"+level+".json")
let levelControllers = await resp.json()
levelsControllers[level] = levelControllers
gdes = level==GDE? levelControllers : gdes
bez = level==BEZ? levelControllers : bez
kt = level==KT? levelControllers : kt
}
return levelsControllers[level]
}
/** Loads control relationships at given level and date
*
* level: one of "gde", "bez", "kt"
*/
async function loadControlsAtDateAndLevel(date, level){
date = ensureDateFormat(date)
let controllers = await loadControlsAtLevel(level)
return controllers.map(c=>{
let controlsBefore = c.controlledAtDates.filter(cad=>cad.date<date)
let controlBefore = controlsBefore.length>0? controlsBefore[controlsBefore.length-1] : false
return ({
controller: c.controller,
controlledAtDate: controlBefore
})
}).filter(c=>c.controlledAtDate!==false)
}
loadControlsAtLevel(GDE)
let controlsAtDateAndLevel = false
async function setControlsAtDateAndLevel(date, level){
controlsAtDateAndLevel = await loadControlsAtDateAndLevel(date,level)
let fillColorMatch = controlsAtDateAndLevel.filter(c=>c.controlledAtDate.controlledEntities.length>0)
.map( c=> {
color = "rgb("+randomInt(0,256)+","+randomInt(0,256)+","+randomInt(0,256)+")"
if(level=="kt"){
color = c.controller.color
console.log("setControlsAtDateAndLevel() level KT, c:",c, ", c.controller.color:", c.controller.color)
}
return [
c.controlledAtDate.controlledEntities,
color
]
}).flat()
console.log("fillColorMatch:", fillColorMatch)
map.setPaintProperty(
'territories-fill',
'fill-color',
['match', ['get', 'histid'], ...fillColorMatch, "transparent"]
);
}
let levelSelector = document.getElementById("level-selector")
levelSelector.addEventListener("change",()=>{
console.log("level changed:", levelSelector.value)
setControlsAtDateAndLevel(yearSelector.value, levelSelector.value)
})
let yearSelector = document.getElementById("year-selector")
yearSelector.addEventListener("change",()=>{
console.log("year changed:", yearSelector.value)
setControlsAtDateAndLevel(yearSelector.value, levelSelector.value)
})
// ===================================================================================
function ensureDateFormat(date){
return date>=1000 && date<10000? date*10000 : (date>=10*8&& date<2030*10**4? date : 18500000)
}
function range(start, stop, step=1, length=Math.ceil((stop-start)/step)){
return Array.from({length}, (x,i)=> start+i*step)
}
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function color_cuboid_subdivision(nbColors, rmin, rmax, gmin, gmax, bmin, bmax){
nbPerColor = Math.ceil(nbColors **(1./3.))
rstep = Math.floor((rmax-rmin)/nbPerColor)
gstep = Math.floor((gmax-gmin)/nbPerColor)
bstep = Math.floor((bmax-bmin)/nbPerColor)
rrange = range(rmin+Math.round(rstep/2), rmax, rstep)
grange = range(gmin+Math.round(gstep/2), gmax, gstep)
brange = range(bmin+Math.round(bstep/2), bmax, bstep)
colors = []
rrange.forEach(r=> grange.forEach(g=> forEach(brange.forEach(b=> colors.push(r,g,b)))))
return colors
}
</script>
</body>
</html>