forked from mroth/mta2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
221 lines (184 loc) · 7.22 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="stops.css">
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<div class="jumbotron">
<h1>MTA NYC Train Status</h1>
<p>Live map of current train positions throughout the New York City MTA Subway network, as reported by MTA's Real Time Data Feed. Trains are depicted as triangles, colored according to their lines using official MTA colors. Triangles pointing up are trains traveling roughly northbound; triangles pointing down are trains heading roughly southbound.</p>
<strong>Note:</strong> The feed includes information for the 1, 2, 3, 4, 5, 6, L, and Grand Central Shuttle only. No information on lettered lines or the 7 train is available at this time.
<p><strong>Legend:</strong><br />
<svg width="20" height="20">
<circle cx="10" cy="10" class="station" r="3"></circle>
</svg> Subway stop<br />
<svg width="20" height="20">
<path d="M0,-3.7224194364083982L4.298279727294168,3.7224194364083982 -4.298279727294168,3.7224194364083982Z" class="train line1 intransitto" transform="translate(10,10)"></path>
</svg> Train is in transit to the next station.<br />
<svg width="20" height="20">
<path d="M0,-3.7224194364083982L4.298279727294168,3.7224194364083982 -4.298279727294168,3.7224194364083982Z" class="train line1 incomingat" transform="translate(10,10)"></path>
</svg> Train is arriving at the station.<br />
<svg width="20" height="20">
<path d="M0,-3.7224194364083982L4.298279727294168,3.7224194364083982 -4.298279727294168,3.7224194364083982Z" class="train line1 stoppedat" transform="translate(10,10)"></path>
</svg> Train is stopped at the station.
</p>
</div>
<script>
var width = 960,
height = 1131; //aspect ratio of extents is approx 1:1.8
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "svgmap");
var defs = svg.append('defs');
var extents;
//begin refactoring here - calculate extents within the stops parsing and ignore Staten Island stops
d3.json("mapdata/pointextent.json", function(error, edges) {
extents = edges;
var centerlon = (extents.west-extents.east)/2;
var centerlat = (extents.north-extents.south)/2;
centerlon = centerlon + extents.east;
centerlat = centerlat + extents.south;
//set a background image where this map is located
var zoomlevel = 12;
bgurl = "http://api.tiles.mapbox.com/v4/mboggie.jdna5g96/"+(centerlon+0.002)+","+centerlat+","+ zoomlevel+"/"+width+"x"+height+".png64?access_token=pk.eyJ1IjoibWJvZ2dpZSIsImEiOiIyaFpVeU9rIn0.9XvdKyRmTIxfZz3wddfUtw";
defs
.append('svg:pattern')
.attr('id', 'bgmap')
.attr('patternUnits', 'userSpaceOnUse')
.attr('width', width)
.attr('height', height)
.append('svg:image')
.attr('xlink:href', bgurl)
.attr('x', 0)
.attr('y', 0)
.attr('width', width)
.attr('height', height);
svg.append('rect')
.attr("class", "svgmap")
.attr('x', 0)
.attr('y', 0)
.attr('width', width)
.attr('height', height);
d3.json("mapdata/stops.json", function(error, stops) {
var stop_positions = [];
var stoploc = [];
var projection = d3.geo.mercator()
.center([centerlon, centerlat])
.scale(171000)
.translate([(width) / 2, (height)/2]);
for (var i = stops.length - 1; i >= 0; i--) {
if (stops[i].location_type == 1) {
stoploc = projection([stops[i].stop_lon, stops[i].stop_lat]);
stop_positions.push({"x": stoploc[0], "y": stoploc[1], "name": stops[i].stop_name, "id": stops[i].stop_id});
}
};
//draw the initial map
svg.selectAll('stopcircle')
.data(stop_positions)
.enter().append('circle')
.attr('cx', function(d){return d.x;})
.attr('cy', function(d){return d.y;})
.attr('class', 'station')
.attr('data-id', function(d){return d.id;})
.attr('data-name', function(d){return d.name;})
.attr('r', 2);
});
});
var ticker = new WebSocket('ws://localhost:7070/ws/1');
ticker.onmessage = function(event){
//when the ticker ticks, clean up old trains
// 1 = recently active
// 0 = unsure
// -1 = no longer active
d3.selectAll('[data-active="-1"]').remove();
d3.selectAll('[data-active="0"]').attr('data-active', -1);
d3.selectAll('[data-active="1"]').attr('data-active', 0);
}
var socket = new WebSocket('ws://localhost:7070/ws/19');
socket.onmessage = function(event){
var train = JSON.parse(event.data);
if(train.stop_id !== null){
var target_stop_id = train.stop_id.substring(0,3);
var train_id = train.trip.nyct_trip_descriptor.train_id.replace(/ /g, '').replace(/\//g, '').replace(/\+/g, '');
var stopdot = d3.select('[data-id="' + target_stop_id + '"]');
var route = "line" + train.trip.route_id.substring(0,1).toLowerCase();
var status = train.current_status_text.replace(/ /g, "").toLowerCase();
// see if a train exists
var newtrain = false;
var movingtrain = d3.select('[data-id="t' + train_id + '"]');
if(movingtrain.empty()){
//make a train
newtrain = true;
svg.append("path")
.attr('d', d3.svg.symbol().type("triangle-up").size(32))
.attr('class', "train " + route + " " + status)
.attr('data-id', "t"+train_id);
movingtrain = d3.select('[data-id="t' + train_id + '"]');
}
else{
movingtrain.attr('class', "train " + route + " " + status);
}
//mark the train as active
movingtrain.attr('data-active', 1);
//move the train to its new location
var rotation = 0;
var offx = 0;
var offy = 0;
var offw = movingtrain.node().getBBox().width;
offw = offw/2;
switch (train.trip.nyct_trip_descriptor.direction) {
case 1:
rotation = 0;
offx = offw;
break;
case 2:
rotation = 90;
offy = 0-offw;
break;
case 3:
rotation = 180;
offx = 0-offw;
break;
case 4:
rotation = 270;
offy = offw;
break;
}
var movex, movey;
if(newtrain) {
movingtrain.attr("transform", "translate(" + parseInt(parseInt(stopdot.attr('cx'))+offx) + "," + parseInt(stopdot.attr('cy')+offy) + ")rotate(" + rotation + ")");
}
else
{
if(status == "intransitto"){
//TODO: Note that this algorithm creates a Zeno's Paradox effect. Not terrible, but a little weird.
//find where the train is now
var curx = d3.transform(movingtrain.attr("transform")).translate[0];
var cury = d3.transform(movingtrain.attr("transform")).translate[1];
// remove the offsets given to this train previously, so now its coordinates match that of its stop
curx -= offx;
cury -= offy;
// calculate half-way between where we are and where we're going
movex = (stopdot.attr('cx') - curx)/2;
movey = (stopdot.attr('cy') - cury)/2;
// add halfway to new stop
movex += curx;
movey += cury;
}
else
{
// move to the station
movex = stopdot.attr('cx');
movey = stopdot.attr('cy');
}
movingtrain.transition().duration(500).attr("transform", "translate(" + parseInt(movex+offx) + "," + parseInt(movey+offy) + ")rotate(" + rotation + ")");
}
}
}
</script>
</body>
</html>