-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-layeroptions.js
145 lines (122 loc) · 4.66 KB
/
index-layeroptions.js
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
const START_ZOOM = 5.0;
const START_CENTER = [ 39.828175, -98.5795 ];
let DEFAULT_RANGE = ['1800-01-01', '2022-01-01'];
let DEFAULT_DATE = '2022-01-01';
let MAP; // the L.Map
let SELECTEDVIZ; // string, currently-selected map
let OHMLAYER; // the L.MBGL layer
let TIMESLIDER; // the TimeSlider
document.addEventListener('DOMContentLoaded', function () {
const urlparams = new URLSearchParams(document.location.hash.replace(/^#/, ''));
// override DEFAULT_RANGE and DEFAULT_DATEif given in URL params
if (urlparams.get('date')) {
DEFAULT_DATE = urlparams.get('date');
}
if (urlparams.get('range')) {
DEFAULT_RANGE = urlparams.get('range').split(',');
}
// start the map
// starting view overridden by x/y/z params
MAP = L.map('map', {
zoomSnap: 0.1,
});
if (urlparams.get('z') && urlparams.get('x') && urlparams.get('y')) {
const z = parseFloat(urlparams.get('z'));
const x = parseFloat(urlparams.get('x'));
const y = parseFloat(urlparams.get('y'));
MAP.setView([y, x], z);
} else {
MAP.setView(START_CENTER, START_ZOOM);
}
L.control.scale().addTo(MAP);
// set up the radiobox behavior to pick a layer & add the timeslider to it
// check one of the boxes, either from URL param or default
// and trigger it now
const $checkboxes = document.querySelectorAll('input[type="radio"][name="layerchoice"]');
[...$checkboxes].forEach(($checkbox) => {
$checkbox.addEventListener('change', function () {
const which = document.querySelector('input[type="radio"][name="layerchoice"]:checked').getAttribute('value');
selectLayer(which);
});
});
if (urlparams.get('layer')) {
const selected = urlparams.get('layer');
document.querySelector(`input[type="radio"][name="layerchoice"][value="${selected}"]`).checked = true;
} else {
document.querySelector('input[type="radio"][name="layerchoice"][value="fullcolor"]').checked = true;
}
selectLayer( document.querySelector('input[type="radio"][name="layerchoice"]:checked').getAttribute('value') );
// start updating the URL hash every few seconds
setInterval(function () {
updateUrlHash();
}, 1 * 1000);
});
function selectLayer (which) {
// remove the layers & timeslider, but note the settings from the slider
let oldrange, olddate, vectorlayer;
if (OHMLAYER) {
MAP.removeLayer(OHMLAYER);
OHMLAYER = undefined;
}
if (TIMESLIDER) {
MAP.removeControl(TIMESLIDER);
olddate = TIMESLIDER.getDate();
oldrange = TIMESLIDER.getRange();
TIMESLIDER = undefined;
}
// create the new vector layer, reloading the style from scratch
// then apply the 'slider to it
SELECTEDVIZ = which;
switch (which) {
case 'lightest':
OHMLAYER = new L.MapboxGL({
attribution: "<a href='http://wiki.openstreetmap.org/wiki/OHM'>OHM</a>",
style: MAPSTYLE_LIGHTEST,
accessToken: "no-token",
});
break;
case 'light':
OHMLAYER = new L.MapboxGL({
attribution: "<a href='http://wiki.openstreetmap.org/wiki/OHM'>OHM</a>",
style: MAPSTYLE_LIGHT,
accessToken: "no-token",
});
break;
case 'fullcolor':
OHMLAYER = new L.MapboxGL({
attribution: "<a href='http://wiki.openstreetmap.org/wiki/OHM'>OHM</a>",
style: MAPSTYLE_FULLCOLOR,
accessToken: "no-token",
});
break;
default:
throw new Error("Unknown layer choice");
break;
}
OHMLAYER.addTo(MAP);
setTimeout(() => {
const tsoptions = {
vectorLayer: OHMLAYER,
vectorSourceName: 'osm',
range: DEFAULT_RANGE,
date: DEFAULT_DATE,
stepInterval: 1,
stepAmount: '10year',
};
if (olddate) tsoptions.date = olddate;
if (oldrange) tsoptions.range = oldrange;
TIMESLIDER = new L.Control.OHMTimeSlider(tsoptions).addTo(MAP);
}, 1 * 1000);
}
function updateUrlHash () {
if (! TIMESLIDER) return;
const params = {};
params.z = MAP.getZoom().toFixed(2);
params.y = MAP.getCenter().lat.toFixed(6);
params.x = MAP.getCenter().lng.toFixed(6);
params.date = TIMESLIDER.getDate();
params.range = TIMESLIDER.getRange().join(',');
params.layer = SELECTEDVIZ;
const urlhash = `x=${params.x}&y=${params.y}&z=${params.z}&date=${params.date}&range=${params.range}&layer=${params.layer}`;
document.location.hash = urlhash;
}