-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
256 lines (229 loc) · 7.23 KB
/
Copy pathscript.js
File metadata and controls
256 lines (229 loc) · 7.23 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
const { MapboxOverlay, PolygonLayer, ScatterplotLayer, LineLayer } = deck;
async function loadGeoJSON(url) {
const response = await fetch(url);
return await response.json();
}
const baseline_spacing = 800;
let currentMode = "spatial"; // "spatial" or "semantic"
let blocks, buildings, nodes, spatial_edges, semantic_edges;
let map, deckOverlay;
let layer_spacing = baseline_spacing;
let n_layers = 8;
const layerToggles = {
showBlocks: true,
showBuildings: true,
showNodes: true,
showEdges: false,
invertLayers: false,
rotateMap: false,
};
let rotateInterval = null;
let inactivityTimeout = null;
const ROTATE_SPEED = 0.2; // degrees per frame
const ROTATE_INTERVAL_MS = 40; // ms between frames
const ROTATE_RESUME_DELAY = 20000; // 20 seconds
// --------------------- map rotation ---------------------
function startRotation() {
if (rotateInterval || !layerToggles.rotateMap) return;
rotateInterval = setInterval(() => {
if (!map || !layerToggles.rotateMap) return;
const bearing = map.getBearing();
map.setBearing(bearing + ROTATE_SPEED);
}, ROTATE_INTERVAL_MS);
}
function stopRotation() {
if (rotateInterval) {
clearInterval(rotateInterval);
rotateInterval = null;
}
}
function resetInactivityTimer() {
stopRotation();
if (inactivityTimeout) clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(() => {
startRotation();
}, ROTATE_RESUME_DELAY);
}
// Attach listeners after map is initialized
function setupRotationListeners() {
// Pause rotation on any mouse interaction
["mousedown", "touchstart", "touchmove"].forEach((evt) => {
map.on(evt, resetInactivityTimer);
});
}
// --------------------- map layers ---------------------
// Update elevation by user input slider for layer spacing
const get_elevation = (cluster) => {
if (layerToggles.invertLayers) {
return (cluster + 1) * layer_spacing + baseline_spacing;
}
return (n_layers - cluster) * layer_spacing + baseline_spacing;
};
const lightenColor = (color, amount = 0.5) => {
// color: [r, g, b] or [r, g, b, a]
return [
Math.round(color[0] + (255 - color[0]) * amount),
Math.round(color[1] + (255 - color[1]) * amount),
Math.round(color[2] + (255 - color[2]) * amount),
color.length > 3 ? color[3] : 255,
];
};
function buildingLayer(mode) {
const color = `${mode}_color`;
return new PolygonLayer({
id: `buildings-${mode}`,
data: [...buildings.features],
getPolygon: (f) => {
const coords = f.geometry.coordinates;
if (f.geometry.type === "Polygon") return coords;
if (f.geometry.type === "MultiPolygon") return coords[0];
return [];
},
getFillColor: (f) => [...f.properties[color], 200], // add alpha
extruded: true,
getElevation: (f) => (f.properties.num_floors || 1) * 14,
pickable: true,
stroked: false,
});
}
function blockLayer(mode) {
const color = `${mode}_color`;
return new PolygonLayer({
id: "blocks",
data: [...blocks.features],
getPolygon: (f) => {
const coords = f.geometry.coordinates;
if (f.geometry.type === "Polygon") return coords;
if (f.geometry.type === "MultiPolygon") return coords[0];
return [];
},
getFillColor: (f) => f.properties[color],
pickable: true,
extruded: false,
stroked: true,
getLineColor: [255, 255, 255, 200], // white block borders
lineWidthMinPixels: 1,
});
}
function nodeLayer(mode) {
const color = `${mode}_color`;
const cluster = `${mode}_cluster`;
return new ScatterplotLayer({
id: "nodes",
data: [...nodes.features],
getPosition: (f) => [
f.geometry.coordinates[0],
f.geometry.coordinates[1],
get_elevation(f.properties[cluster]),
],
getColor: (f) => f.properties[color],
getRadius: 30,
pickable: true,
});
}
function edgeLayer(mode) {
const color = `${mode}_color`;
const cluster = `${mode}_cluster`;
const edgesData =
mode === "spatial" ? spatial_edges.features : semantic_edges.features;
return new LineLayer({
id: "edges",
data: [...edgesData],
getSourcePosition: (f) => [
f.geometry.coordinates[0][0],
f.geometry.coordinates[0][1],
get_elevation(f.properties[cluster]),
],
getTargetPosition: (f) => [
f.geometry.coordinates[1][0],
f.geometry.coordinates[1][1],
get_elevation(f.properties[cluster]),
],
getColor: (f) => lightenColor(f.properties[color], 0.2),
getWidth: 0.5,
pickable: true,
});
}
function getLayers(mode) {
const layers = [];
if (layerToggles.showBlocks) layers.push(blockLayer(mode));
if (layerToggles.showBuildings) layers.push(buildingLayer(mode));
if (layerToggles.showNodes) layers.push(nodeLayer(mode));
if (layerToggles.showEdges) layers.push(edgeLayer(mode));
return layers;
}
// Checkbox event listeners
[
"showBlocks",
"showBuildings",
"showNodes",
"showEdges",
"invertLayers",
].forEach((id) => {
document.getElementById(id).addEventListener("change", (e) => {
layerToggles[id] = e.target.checked;
if (deckOverlay) {
deckOverlay.setProps({ layers: getLayers(currentMode) });
}
});
});
document.getElementById("rotateMap").addEventListener("change", (e) => {
layerToggles.rotateMap = e.target.checked;
if (layerToggles.rotateMap) {
startRotation();
} else {
stopRotation();
}
});
// --------------------- event listeners ---------------------
document.getElementById("spatialToggle").addEventListener("click", () => {
currentMode = "spatial";
document.getElementById("spatialToggle").classList.add("active");
document.getElementById("semanticToggle").classList.remove("active");
if (deckOverlay) deckOverlay.setProps({ layers: getLayers(currentMode) });
});
document.getElementById("semanticToggle").addEventListener("click", () => {
currentMode = "semantic";
document.getElementById("semanticToggle").classList.add("active");
document.getElementById("spatialToggle").classList.remove("active");
if (deckOverlay) deckOverlay.setProps({ layers: getLayers(currentMode) });
});
document.getElementById("layerSpacingSlider").addEventListener("input", (e) => {
layer_spacing = Number(e.target.value);
if (deckOverlay) deckOverlay.setProps({ layers: getLayers(currentMode) });
});
// Initialize the map and layers
async function initMap() {
blocks = await loadGeoJSON("webmap-geodata/blocks.geojson");
buildings = await loadGeoJSON("webmap-geodata/buildings.geojson");
nodes = await loadGeoJSON("webmap-geodata/network_nodes.geojson");
spatial_edges = await loadGeoJSON(
"webmap-geodata/spatial_network_edges.geojson"
);
semantic_edges = await loadGeoJSON(
"webmap-geodata/semantic_network_edges.geojson"
);
map = new maplibregl.Map({
container: "map",
style: "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
center: [-73.99, 40.76],
zoom: 11.7,
pitch: 90,
antialias: true,
});
map.on("style.load", () => {
map.getStyle().layers.forEach((layer) => {
if (
layer.type === "symbol" ||
(layer.layout && layer.layout["text-field"])
) {
map.setLayoutProperty(layer.id, "visibility", "none");
}
});
deckOverlay = new MapboxOverlay({
layers: getLayers(currentMode),
});
map.addControl(deckOverlay);
});
}
initMap().then(setupRotationListeners);