-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.js
More file actions
154 lines (131 loc) · 5.21 KB
/
visualizer.js
File metadata and controls
154 lines (131 loc) · 5.21 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
let map;
let locations = {}; // { locationId: { lat, lng, name } }
let paths = []; // [ { from, to, time } ]
let toggledOffPathKeys = new Set(); // Stores "from-to" strings of disabled paths
function initializeMap() {
map = L.map('map').setView([29.64833, -82.34944], 15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}
/**
* Parses raw CSV text into an array of objects.
* @param {string} text The raw CSV string.
* @returns {Array<Object>} An array of objects representing the rows.
*/
function parseCSV(text) {
const lines = text.trim().split('\n');
if (lines.length < 2) return []; // Return empty if no data rows
const header = lines[0].split(',').map(h => h.trim());
const rows = lines.slice(1).map(line => {
const values = line.split(',').map(v => v.trim());
let obj = {};
header.forEach((col, index) => {
obj[col] = values[index];
});
return obj;
});
return rows;
}
async function loadMasterGraph() {
try {
const [locationsResponse, pathsResponse] = await Promise.all([
fetch('locations.csv'),
fetch('edges.csv')
]);
if (!locationsResponse.ok) throw new Error(`Failed to load locations.csv: ${locationsResponse.statusText}`);
if (!pathsResponse.ok) throw new Error(`Failed to load edges.csv: ${pathsResponse.statusText}`);
const locationsText = await locationsResponse.text();
const pathsText = await pathsResponse.text();
const locationsData = parseCSV(locationsText);
locations = {};
locationsData.forEach(row => {
if (row.LocationID && row.Latitude && row.Longitude) {
locations[row.LocationID] = {
name: row.Name || 'Unnamed Location',
lat: parseFloat(row.Latitude),
lng: parseFloat(row.Longitude)
};
}
});
const pathsData = parseCSV(pathsText);
paths = [];
pathsData.forEach(row => {
if (row.LocationID_1 && row.LocationID_2 && row.Time) {
paths.push({
from: row.LocationID_1,
to: row.LocationID_2,
time: parseInt(row.Time, 10)
});
}
});
toggledOffPathKeys.clear();
console.log("Master graph data loaded successfully from local files.");
drawMap();
} catch (error) {
console.error("Failed to load master graph:", error);
alert(`Failed to load master graph files (locations.csv, paths.csv): ${error.message}\n\nPlease ensure the files exist in the same directory as the HTML file.`);
}
}
function drawMap() {
// Clear previous layers (everything except the base map tiles)
map.eachLayer(layer => {
if (layer instanceof L.Path || layer instanceof L.Marker) {
map.removeLayer(layer);
}
});
if (Object.keys(locations).length === 0) return;
// Draw Location Markers (Nodes)
for (const id in locations) {
const loc = locations[id];
const icon = L.divIcon({
className: 'location-marker-icon',
html: `<div>${id}</div>`,
iconSize: [28, 28],
iconAnchor: [14, 14]
});
L.marker([loc.lat, loc.lng], { icon: icon }).addTo(map)
.bindPopup(`<b>${loc.name}</b><br>ID: ${id}`);
}
paths.forEach(path => {
const fromLoc = locations[path.from];
const toLoc = locations[path.to];
const pathKey = `${path.from}-${path.to}`;
const reversePathKey = `${path.to}-${path.from}`;
if (fromLoc && toLoc) {
const latlngs = [[fromLoc.lat, fromLoc.lng], [toLoc.lat, toLoc.lng]];
const isToggledOff = toggledOffPathKeys.has(pathKey);
let edgeColor = '#0000FF'; // Default blue
if (isToggledOff) {
edgeColor = '#FF0000'; // Red for disabled
}
const style = {
color: edgeColor,
weight: 4,
};
const polyline = L.polyline(latlngs, style).addTo(map);
// Add click handler to toggle the edge's "disabled" state
polyline.on('click', () => {
if (toggledOffPathKeys.has(pathKey)) {
toggledOffPathKeys.delete(pathKey);
toggledOffPathKeys.delete(reversePathKey);
} else {
toggledOffPathKeys.add(pathKey);
toggledOffPathKeys.add(reversePathKey);
}
drawMap(); // Redraw to reflect the change
});
if (!isToggledOff) {
const weightIcon = L.divIcon({
className: 'edge-weight-label',
html: `<span>${path.time}</span>`
});
L.marker(polyline.getCenter(), { icon: weightIcon }).addTo(map);
}
}
});
}
document.addEventListener('DOMContentLoaded', () => {
initializeMap();
loadMasterGraph();
});