-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic.js
228 lines (197 loc) · 7.34 KB
/
traffic.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
const downloadFile = (filename, text) => {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// modify passenger flow line width
let firstChange = true;
let linesElements = [];
let originStrokeWidth = [];
const changeLineWidth = (scale) => {
if (firstChange === true) {
linesElements = document.querySelectorAll('#passenger-flow');
linesElements.forEach((line, index) => {
originStrokeWidth[index] = Number(line.style['stroke-width'].slice(0, -2));
line.style['stroke-width'] = `${originStrokeWidth[index] * scale}px`;
});
firstChange = false;
} else {
linesElements.forEach((line, index) => {
line.style['stroke-width'] = `${originStrokeWidth[index] * scale}px`;
});
}
}
const generateSvgPathString = (coordinatesArray) => {
let string = '';
coordinatesArray.forEach((coordinates, index) => {
if (index === 0) {
string += `M${coordinates} `
} else {
string += `L${coordinates} `;
}
});
return string;
}
const addStationToSvg = (stationInfo, svgElement, color) => {
const stationTrafficSize = (parseInt(stationInfo.officeBuilding) + parseInt(stationInfo.residential)) * 0.2 + 4;
// station position in svg
const stationPosition = stationInfo.p.split(" ");
svgElement.innerHTML += `<circle cx="${stationPosition[0]}" cy="${stationPosition[1]}" r="${stationTrafficSize}" stroke="#${color}" stroke-width="1" fill="white" />`
}
/*
* @param {string} startStation - start station name
* @param {string} endStation - end station name
* @param {object} maps - end station path mapping, generated by graphlib dijkstra function
*/
const generateShortestPathArray = (startStation, endStation, maps) => {
let nextStation = maps[startStation].predecessor;
let shortestPath = [startStation, nextStation];
while (nextStation !== endStation) {
nextStation = maps[nextStation].predecessor;
shortestPath.push(nextStation);
}
return shortestPath;
}
/*
* @param {string} coordinatesA - coordinates A e.g."123 456"
* @param {string} coordinatesB - coordinates B e.g."123 457"
* @param {number} [deviation=2] - deviation to match
* @return {boolean} if the coordinates matched within deviation
*/
const matchCoordinates = (coordinatesA, coordinatesB, deviation = 2) => {
let coordinatesAArray = coordinatesA.split(" ");
let ax = parseInt(coordinatesAArray[0]);
let ay = parseInt(coordinatesAArray[1]);
let coordinatesBArray = coordinatesB.split(" ");
let bx = parseInt(coordinatesBArray[0]);
let by = parseInt(coordinatesBArray[1]);
return (ax <= (bx + deviation)) && (ax >= (bx - deviation)) && (ay <= (by + deviation)) && (ay >= (by - deviation));
}
(async () => {
// create an undirected graph
const g = new graphlib.Graph({
directed: false,
});
const map = document.querySelector('#map');
const cityAdcode = location.search.substr(2);
const data = await (await fetch(`data/${cityAdcode}.json`)).json();
const lines = data.l;
let stationsData = {};
// store station interval (edges) information
let edges = [];
// draw metro lines and generate basic data
lines.forEach((line, lineIndex) => {
const coordinatesArray = line.c;
let string = generateSvgPathString(coordinatesArray);
map.innerHTML += `<path d="${string}" style="stroke: #${line.cl}; stroke-width: 5px; stroke-opacity: 0.3;"/>`;
const stations = line.st;
stations.forEach((station) => {
// add nodes(stations) to undirected graph
g.setNode(station.n, station);
// seperate stations data
stationsData[station.n] = {
officeBuilding: parseInt(station.officeBuilding),
residential: parseInt(station.residential),
}
});
for (let index = 1; index < stations.length; index++) {
// add edges(station interval) to undirected graph
g.setEdge(stations[index - 1].n, stations[index].n, `${stations[index-1].n} - ${stations[index].n}`);
// add data to variable edges
edges.push({
startStation: stations[index - 1].n,
endStation: stations[index].n,
startCoordinates: stations[index - 1].p,
endCoordinates: stations[index].p,
traffic: 0,
path: '',
lineIndex: lineIndex,
color: line.cl
})
}
});
// generate traffic data
lines.forEach(line => {
const stations = line.st;
stations.forEach((station) => {
if (station.officeBuilding > 0) {
let maps = graphlib.alg.dijkstra(g, station.n, undefined, (v) => {
return g.nodeEdges(v);
});
Object.keys(maps).forEach(stationName => {
if (maps[stationName].distance > 0 && stationsData[stationName].residential > 0) {
const shortestPath = generateShortestPathArray(stationName, station.n, maps);
let distance = shortestPath.length;
// add corresponding traffic to edges
for (let index = 0; index < distance; index++) {
let edgesIndex = edges.findIndex((edge) => {
return (edge.startStation === shortestPath[index] && edge.endStation === shortestPath[index + 1]) ||
(edge.endStation === shortestPath[index] && edge.startStation === shortestPath[index + 1]);
})
if (edgesIndex > -1) {
edges[edgesIndex].traffic += 1 / distance * station.officeBuilding * stationsData[stationName].residential;
}
}
}
});
}
})
});
// generate traffic svg path
lines.forEach((line, lineIndex2) => {
const coordinates = line.c;
edges.forEach((edge, index) => {
if (edge.lineIndex === lineIndex2) {
let startIndex = coordinates.findIndex((coordinate) => {
return matchCoordinates(edge.startCoordinates, coordinate, 2);
});
let endIndex = coordinates.findIndex((coordinate) => {
return matchCoordinates(edge.endCoordinates, coordinate, 2);
});
if (endIndex < startIndex) {
let tmp = startIndex;
startIndex = endIndex;
endIndex = tmp;
}
edges[index].path = coordinates.slice(startIndex, endIndex + 1);
}
});
});
// draw traffic map
edges.forEach(edge => {
let pathString = generateSvgPathString(edge.path);
map.innerHTML += `<path id="passenger-flow" d="${pathString}" style="stroke: #${edge.color}; stroke-width: ${edge.traffic * 0.002}px;"/>`;
});
// draw metro stations
lines.forEach(line => {
const stations = line.st;
stations.forEach((station) => {
addStationToSvg(station, map, line.cl);
})
});
// enable panzoom
panzoom(map, {
smoothScroll: false,
}).zoomAbs(
0, // initial x position
0, // initial y position
0.5 // initial zoom
);
// add export passenger flow data button and function
document.querySelector('#export-data').onclick = () => {
let exportData = [];
edges.forEach((edge) => {
exportData.push({
startStation: edge.startStation,
endStation: edge.endStation,
traffic: edge.traffic
});
})
const dataText = JSON.stringify(exportData);
downloadFile(`${cityAdcode}-passenger-flow-data.json`, dataText);
}
})();