Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
331 lines (300 loc) · 9.2 KB
/
Copy pathscript.js
File metadata and controls
331 lines (300 loc) · 9.2 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
let map;
// add the PMTiles plugin to the maplibregl global.
const protocol = new pmtiles.Protocol();
maplibregl.addProtocol('pmtiles', (request) => {
return new Promise((resolve, reject) => {
const callback = (err, data) => {
if (err) {
reject(err);
} else {
resolve({data});
}
};
protocol.tile(request, callback);
});
});
const PMTILES_URL_DEMO = 'https://vector-tiles-testing.s3.us-east-2.amazonaws.com/brmpo-demographics.pmtiles';
const PMTILES_URL_ROAD = 'https://vector-tiles-testing.s3.us-east-2.amazonaws.com/brmpo_roads.pmtiles';
const PMTILES_URL_MCFRM = 'https://vector-tiles-testing.s3.us-east-2.amazonaws.com/brmpo_mcfrm.pmtiles';
const p_demo = new pmtiles.PMTiles(PMTILES_URL_DEMO);
const p_road = new pmtiles.PMTiles(PMTILES_URL_ROAD);
const p_mcfrm= new pmtiles.PMTiles(PMTILES_URL_MCFRM);
protocol.add(p_demo);
protocol.add(p_road);
protocol.add(p_mcfrm);
document.addEventListener('DOMContentLoaded', () => {
// Initialize the map with a plain background
map = new maplibregl.Map({
container: 'map',
style: {
version: 8,
// No raster sources; just an empty object
// sourcing pmtiles from S3 storage
sources: {
'brmpo_demo_tiles': {
type: 'vector',
url: `pmtiles://${PMTILES_URL_DEMO}`,
attribution: '<a href="https://www.census.gov/">US Census</a>'
},
'brmpo_road_tiles': {
type: 'vector',
url: `pmtiles://${PMTILES_URL_ROAD}`,
attribution: '<a href="https://gis.data.mass.gov/datasets/4ad165863b9c4dffa399a1eef89c0cf2/about">MassGIS</a>'
},
'brmpo_mcfrm_tiles': {
type: 'vector',
url: `pmtiles://${PMTILES_URL_MCFRM}`,
attribution: '<a href="https://www.woodsholegroup.com/innovation/massachusetts-coast-flood-risk-model/">MCFRM</a>'
}
},
layers: [
{
// A simple background layer so we have a neutral backdrop
id: 'background',
type: 'background',
paint: {
'background-color': '#eeeeee' // light grey
},
},
{
// 2022 demographic data
'id': 'demo',
'source': 'brmpo_demo_tiles',
'source-layer': 'brmpo_tract',
'type': 'fill',
'paint': {
'fill-color': '#f2f2f2',
'fill-outline-color': 'white',
'fill-opacity': 1
}
},
{
// MCFRM polygons to show coastal flood risk, 2018
'id': 'mcfrm_prob_2018',
'source': 'brmpo_mcfrm_tiles',
'source-layer': 'mcfrm_prob_2018',
'type': 'fill',
'paint': {
'fill-color': '#6dd1fc',
'fill-outline-color': '#6dd1fc',
'fill-opacity': .6
}
},
{
// MCFRM polygons to show coastal flood risk, 2030
'id': 'mcfrm_prob_2030',
'source': 'brmpo_mcfrm_tiles',
'source-layer': 'mcfrm_prob_2030',
'type': 'fill',
'paint': {
'fill-color': '#6dd1fc',
'fill-outline-color': '#6dd1fc',
'fill-opacity': .4
}
},
{
// MCFRM polygons to show coastal flood risk, 2050
'id': 'mcfrm_prob_2050',
'source': 'brmpo_mcfrm_tiles',
'source-layer': 'mcfrm_prob_2050',
'type': 'fill',
'paint': {
'fill-color': '#6dd1fc',
'fill-outline-color':'#6dd1fc',
'fill-opacity': .2
}
},
{
// MCFRM polygons to show coastal flood risk, 2070
'id': 'mcfrm_prob_2070',
'source': 'brmpo_mcfrm_tiles',
'source-layer': 'mcfrm_prob_2070',
'type': 'fill',
'paint': {
'fill-color': '#6dd1fc',
'fill-outline-color':'#6dd1fc',
'fill-opacity': .1
}
},
{
// Road Inventory major roads
'id': 'roads_major_casing',
'source': 'brmpo_road_tiles',
'source-layer': 'roads_major',
'type': 'line',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#fff',
'line-gap-width': 1.2
}
},
{
// Road Inventory all roads
'id': 'roads_mpo',
'source': 'brmpo_road_tiles',
'source-layer': 'roads_brmpo',
'type': 'line',
'minzoom': 11,
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#b0b0b0',
'line-width': .25
}
},
{
// Road Inventory major roads
'id': 'roads_major',
'source': 'brmpo_road_tiles',
'source-layer': 'roads_major',
'type': 'line',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#bfbfbf',
'line-width': 1.1
}
}
]
},
// A default center/zoom in case we can't fit bounds
center: [-95, 39],
zoom: 4
});
// Add standard navigation control
map.addControl(new maplibregl.NavigationControl(), 'top-right');
// When the map finishes loading, fetch & add our GeoJSON
map.on('load', () => {
fetch('data/in_progress_taz_jan21.geojson')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
console.log('GeoJSON fetch response:', response);
return response.json();
})
.then(geojsonData => {
console.log('Loaded GeoJSON data:', geojsonData);
// Add the GeoJSON as a source
map.addSource('tazData', {
type: 'geojson',
data: geojsonData
});
// Population layer (persns19)
map.addLayer({
id: 'populationLayer',
type: 'fill',
source: 'tazData',
paint: {
// Distinct color so we can see it easily
'fill-color': '#1f78b4', // a blue-ish color
'fill-opacity': 0.6
}
});
// Employment layer (workrs19)
map.addLayer({
id: 'employmentLayer',
type: 'fill',
source: 'tazData',
paint: {
'fill-color': '#33a02c', // green-ish
'fill-opacity': 0.6
}
});
// Optional outline for polygons
map.addLayer({
id: 'tazOutline',
type: 'line',
source: 'tazData',
paint: {
'line-color': '#333',
'line-width': 1
}
});
// Fit the map to the bounding box of the data
const bounds = turf.bbox(geojsonData);
console.log('Computed bounds:', bounds);
if (bounds && Number.isFinite(bounds[0])) {
map.fitBounds(bounds, { padding: 30 });
} else {
console.warn('Could not compute valid bounds for GeoJSON data.');
}
// Setup UI controls (checkboxes, sliders)
setupControls();
})
.catch(err => {
console.error('Error loading or parsing GeoJSON:', err);
});
});
});
/**
* Attaches event listeners to our checkboxes and sliders.
*/
function setupControls() {
// --- Checkboxes ---
const persCheck = document.getElementById('persCheckbox');
const workrsCheck = document.getElementById('workrsCheckbox');
// Toggling population layer
persCheck.addEventListener('change', () => {
toggleLayerVisibility('populationLayer', persCheck.checked);
});
// Toggling employment layer
workrsCheck.addEventListener('change', () => {
toggleLayerVisibility('employmentLayer', workrsCheck.checked);
});
// --- Sliders ---
const persSlider = document.getElementById('persnsSlider');
const persValue = document.getElementById('persnsValue');
const workrsSlider = document.getElementById('workrsSlider');
const workrsValue = document.getElementById('workrsValue');
// Show initial slider values
persValue.textContent = persSlider.value;
workrsValue.textContent = workrsSlider.value;
// Population slider
persSlider.addEventListener('input', () => {
persValue.textContent = persSlider.value;
applyPopulationFilter();
});
// Employment slider
workrsSlider.addEventListener('input', () => {
workrsValue.textContent = workrsSlider.value;
applyEmploymentFilter();
});
}
/**
* Show/hide a layer by changing the layout property.
*/
function toggleLayerVisibility(layerId, isVisible) {
map.setLayoutProperty(layerId, 'visibility', isVisible ? 'visible' : 'none');
}
/**
* Filter polygons in populationLayer to those with persns19 >= slider value.
*/
function applyPopulationFilter() {
const sliderValue = parseInt(document.getElementById('persnsSlider').value, 10);
console.log('[Population Filter] persns19 >=', sliderValue);
map.setFilter('populationLayer', [
'>=',
['get', 'persns19'],
sliderValue
]);
}
/**
* Filter polygons in employmentLayer to those with workrs19 >= slider value.
*/
function applyEmploymentFilter() {
const sliderValue = parseInt(document.getElementById('workrsSlider').value, 10);
console.log('[Employment Filter] workrs19 >=', sliderValue);
map.setFilter('employmentLayer', [
'>=',
['get', 'workrs19'],
sliderValue
]);
}