-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
383 lines (302 loc) · 12.7 KB
/
map.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Set your Mapbox access token here
mapboxgl.accessToken = 'pk.eyJ1Ijoicm9zZXlyb3NlbyIsImEiOiJjbTdlMjN1NnowOWoyMnFwdXBkZmRib3h1In0.PcCswgFmZi5pYmeOrMO-ow';
// Step 5.2: Create a variable to hold the time filter value
let timeFilter = -1;
// Select slider and display elements
const timeSlider = document.getElementById('time-slider');
const selectedTime = document.getElementById('selected-time');
const anyTimeLabel = document.getElementById('any-time');
const tooltip = d3.select("#tooltip");
let trips = [];
let filteredTrips = [];
let filteredArrivals = new Map();
let filteredDepartures = new Map();
let filteredStations = [];
// Initialize the map
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v11',
center: [-71.09415, 42.36027],
zoom: 12,
minZoom: 5,
maxZoom: 18
});
// traffic scale
const stationFlow = d3.scaleQuantize()
.domain([0, 1]) // Range of departure ratio values
.range([0, 0.5, 1]); // Discrete color scale values
// Shared style object for bike lanes
const bikeLaneStyle = {
'line-color': '#32D400',
'line-width': 3,
'line-opacity': 0.4
};
// Helper function to convert coordinates
function getCoords(station) {
if (!station || isNaN(station.lon) || isNaN(station.lat)) {
console.warn("🚨 Invalid station coordinates:", station);
return { cx: 100, cy: 100 }; // Assign a visible fallback position
}
const point = new mapboxgl.LngLat(+station.lon, +station.lat);
const { x, y } = map.project(point);
//console.log(`Station: ${station.short_name}, X: ${x}, Y: ${y}`); // Debugging
return { cx: x, cy: y };
}
// Function to update circle positions dynamically
function updatePositions() {
//console.log("Updating positions of circles");
const circles = svg.selectAll('circle')
.data(filteredStations, d => d.short_name);
circles.attr('cx', d => getCoords(d).cx)
.attr('cy', d => getCoords(d).cy);
}
// Attach to Mapbox move/zoom events globally
map.on('move', updatePositions);
map.on('zoom', updatePositions);
map.on('resize', updatePositions);
map.on('moveend', updatePositions);
// minutes since midnight
function minutesSinceMidnight(date) {
if (!date) return null; // Prevent errors
return date.getHours() * 60 + date.getMinutes();
}
// update map vis function
function updateMapVisualization() {
if (!filteredStations.length) {
console.warn("⚠️ No filtered stations found, forcing circle rendering!");
filteredStations = stations.slice(0, 20); // Display at least some stations for debugging
}
//console.log("Rendering Circles for Stations:", filteredStations.length);
const radiusScale = d3.scaleSqrt()
.domain([0, d3.max(filteredStations, d => d.totalTraffic) || 1]) // Ensures correct scaling
.range(timeFilter === -1
? [3, 25] // Ensures 0-trip stations are the smallest
: [3, Math.max(5, Math.min(50, 150 * (d3.max(filteredStations, d => d.totalTraffic) / (d3.max(stations, d => d.totalTraffic) || 1))))]);
const circles = svg.selectAll('circle')
.data(filteredStations, d => d.short_name);
// Remove old circles
circles.exit().remove();
// Enter + Merge new circles
const mergedCircles = circles.enter()
.append('circle')
.merge(circles)
.attr('r', d => radiusScale(d.totalTraffic))
.style("--departure-ratio", d => {
if (d.totalTraffic === 0) return 0.5; // Default to neutral if no traffic
return stationFlow(d.departures / d.totalTraffic);
})
.attr('stroke', 'white')
.attr('stroke-width', 1)
.attr('opacity', 0.6);
//console.log("Circles updated:", mergedCircles.size());
// Update positions immediately after rendering
updatePositions();
// Apply event listeners AFTER elements exist
mergedCircles.on("mouseover", function(event, d) {
if (!d) return; // Prevent event errors
tooltip.style("visibility", "visible")
.html(`<strong>${d.totalTraffic} trips</strong> <br> (${d.departures} departures, ${d.arrivals} arrivals)`);
})
.on("mousemove", function(event) {
tooltip.style("top", (event.pageY + 10) + "px")
.style("left", (event.pageX + 10) + "px");
})
.on("mouseout", function() {
tooltip.style("visibility", "hidden");
});
}
// filter trips by time
function filterTripsByTime() {
if (!trips.length) return; // Ensure data is loaded
//console.log("Current Time Filter:", timeFilter);
filteredTrips = timeFilter === -1
? trips
: trips.filter(trip => {
if (!trip.started_at || !trip.ended_at) return false; // Skip trips with missing timestamps
const startedMinutes = minutesSinceMidnight(trip.started_at);
const endedMinutes = minutesSinceMidnight(trip.ended_at);
return (
(startedMinutes !== null && Math.abs(startedMinutes - timeFilter) <= 60) ||
(endedMinutes !== null && Math.abs(endedMinutes - timeFilter) <= 60)
);
});
//console.log("Filtered Trips:", filteredTrips.length);
// Compute filtered arrivals & departures
filteredArrivals = new Map();
filteredDepartures = new Map();
for (let trip of filteredTrips) {
if (trip.end_station_id) {
filteredArrivals.set(trip.end_station_id, (filteredArrivals.get(trip.end_station_id) || 0) + 1);
}
if (trip.start_station_id) {
filteredDepartures.set(trip.start_station_id, (filteredDepartures.get(trip.start_station_id) || 0) + 1);
}
}
// Update station data
filteredStations = stations.map(station => {
let id = station.short_name;
let arrivals = filteredArrivals.get(id) ?? 0;
let departures = filteredDepartures.get(id) ?? 0;
let totalTraffic = arrivals + departures;
return {
...station,
arrivals: arrivals,
departures: departures,
totalTraffic: totalTraffic
};
});
//console.log("Updated Filtered Stations:", filteredStations.length, filteredStations);
//console.log("Final Filtered Stations Count:", filteredStations.length);
if (filteredStations.length === 0) {
console.warn("⚠️ No stations are being displayed! Check if the filtering logic is working.");
}
updateMapVisualization();
}
// Select the SVG inside #map
let svg = d3.select('#map').select('svg');
if (svg.empty()) {
svg = d3.select("#map").append("svg")
.attr("width", "100%")
.attr("height", "100%")
.style("position", "absolute")
.style("top", "0")
.style("left", "0");
}
let stations = []; // Initialize an empty array
// Step 3.1: Fetching and parsing the Bluebikes station data
const jsonurl = 'https://dsc106.com/labs/lab07/data/bluebikes-stations.json';
d3.json(jsonurl).then(jsonData => {
console.log('Loaded JSON Data:', jsonData);
// Assign station data
stations = jsonData.data.stations.map(station => ({
...station,
arrivals: 0,
departures: 0,
totalTraffic: 0
}));
// Step 4.1: Fetching the Bluebikes traffic data **AFTER** stations are loaded
const trafficUrl = 'https://dsc106.com/labs/lab07/data/bluebikes-traffic-2024-03.csv';
d3.csv(trafficUrl).then(trafficData => {
console.log('Loaded Traffic Data:', trafficData);
// Convert time strings to Date objects
trips = trafficData.map(trip => {
let startTime = new Date(trip.started_at);
let endTime = new Date(trip.ended_at);
//console.log(`🔍 Parsed Trip - Start: ${trip.started_at} -> ${startTime}, End: ${trip.ended_at} -> ${endTime}`);
return {
...trip,
started_at: startTime,
ended_at: endTime
};
});
//console.log("Converted Trip Data:", trips);
//console.log("Trips Loaded:", trips.length);
// APPLY INITIAL FILTER AFTER LOADING DATA
filterTripsByTime();
// Step 4.2: Calculate departures & arrivals at each station
const departures = d3.rollup(trips, v => v.length, d => d.start_station_id);
const arrivals = d3.rollup(trips, v => v.length, d => d.end_station_id);
// Update stations with traffic data
stations = stations.map(station => {
let id = station.short_name;
station.arrivals = arrivals.get(id) ?? 0;
station.departures = departures.get(id) ?? 0;
station.totalTraffic = station.arrivals + station.departures;
return station;
});
//console.log('Updated Stations with Traffic Data:', stations);
// Step 4.3: Define a square root scale for circle radius
const radiusScale = d3
.scaleSqrt()
.domain([0, d3.max(stations, d => d.totalTraffic)])
.range([0, 25]);
// Step 3.3: Append circles to the SVG for each station
const circles = svg.selectAll('circle')
.data(stations)
.enter()
.append('circle')
.attr('r', d => radiusScale(d.totalTraffic)) // Use scale to size markers
.attr('fill', 'steelblue')
.attr('stroke', 'white')
.attr('stroke-width', 1)
.on("mouseover", function(event, d) {
tooltip.style("visibility", "visible")
.html(`<strong>${d.totalTraffic} trips</strong> <br> (${d.departures} departures, ${d.arrivals} arrivals)`);
})
.on("mousemove", function(event) { // Move tooltip with mouse
tooltip.style("top", (event.pageY + 10) + "px")
.style("left", (event.pageX + 10) + "px");
})
.on("mouseout", function() { // Hide tooltip when not hovering
tooltip.style("visibility", "hidden");
});
// Function to update circle positions dynamically
function updatePositions() {
const circles = svg.selectAll('circle')
.data(filteredStations, d => d.short_name); // Ensure correct data binding
circles.attr('cx', d => getCoords(d).cx)
.attr('cy', d => getCoords(d).cy);
}
// Initial position update
updatePositions();
// Update positions when map moves/zooms
map.on('move', updatePositions);
map.on('zoom', updatePositions);
map.on('resize', updatePositions);
map.on('moveend', updatePositions);
}).catch(error => {
console.error('Error loading traffic data:', error);
});
}).catch(error => {
console.error('Error loading JSON:', error);
});
// Draws the map
map.on('load', () => {
map.addSource('boston_route', {
type: 'geojson',
data: 'https://bostonopendata-boston.opendata.arcgis.com/datasets/boston::existing-bike-network-2022.geojson?...'
});
map.addLayer({
id: 'bike-lanes',
type: 'line',
source: 'boston_route',
paint: bikeLaneStyle
});
map.addSource('cambridge_route', {
type: 'geojson',
data: 'https://raw.githubusercontent.com/cambridgegis/cambridgegis_data/main/Recreation/Bike_Facilities/RECREATION_BikeFacilities.geojson'
});
map.addLayer({
id: 'cambridge-bike-lanes',
type: 'line',
source: 'cambridge_route',
paint: bikeLaneStyle
});
});
// Function to convert minutes to HH:MM AM/PM format
function formatTime(minutes) {
if (minutes === -1) return "11:59 PM"; // Default display
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
const period = hours >= 12 ? "PM" : "AM";
const formattedHours = hours % 12 || 12;
return `${formattedHours}:${mins.toString().padStart(2, "0")} ${period}`;
}
// Helper function: Convert minutes to HH:MM AM/PM format
function formatTime(minutes) {
if (minutes === -1) return "11:59 PM"; // Default display
const date = new Date(0, 0, 0, 0, minutes); // Set hours & minutes
return date.toLocaleString('en-US', { timeStyle: 'short' }); // Format as HH:MM AM/PM
}
// Step 5.2: Function to update the time filter and display
function updateTimeDisplay() {
timeFilter = Number(timeSlider.value); // Get the selected time
//console.log("Time Filter Updated:", timeFilter);
selectedTime.textContent = timeFilter === -1 ? '' : formatTime(timeFilter);
anyTimeLabel.style.display = timeFilter === -1 ? 'block' : 'none';
filterTripsByTime(); // <---- Call filtering immediately
}
// Attach event listener to the slider
timeSlider.addEventListener('input', updateTimeDisplay);
// Set the initial display state
updateTimeDisplay();