-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
266 lines (195 loc) · 5.16 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
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #f5f6fa;
}
.airport {
fill: #000;
}
text {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 10px;
pointer-events: none;
}
svg path.land {
fill: #CCC;
}
.country {
fill: #edeffa;
stroke: #CCC;
}
.startingAirport {
fill: #00F;
pointRadius: 5;
}
.unconnectedAirport {
opacity: 0.00;
}
.src {
fill: #F00;
}
.country:hover {
}
.airportError {
fill: #00F;
}
.q0-9 { fill:#0F0; }
.q1-9 { fill:#0D0; }
.q2-9 { fill:#0B0; }
.q3-9 { fill:#090; }
.q4-9 { fill:#070; }
.q5-9 { fill:#060; }
.q6-9 { fill:#040; }
.q7-9 { fill:#020; }
.q8-9 { fill:#000; }
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script src="geojson/geojson-utils.js"></script>
<script src="underscore.js"></script>
<script>
var minConnections = 0;
var airSpeed = 250; //meters/second
var airportDelay = 7200 //two hours in seconds
var maxDomain = 50000000/airSpeed;
var width = 1200,
height = 800;
var projection = d3.geo.miller()
.scale(150)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection)
.pointRadius(2);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var quantize = d3.scale.quantize()
.domain([0, maxDomain])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
window.startingCity=null;
function planeTimeBetween(src,dest){
return ( gju.pointDistance(src,dest) / airSpeed ) + airportDelay;
}
d3.json("data/world.json", function(error, world) {
// draw countries
svg.selectAll(".country")
.data(topojson.object(world, world.objects.countries).geometries)
.enter().append('path')
.attr("d", path)
.attr("class", "country")
});
function initializeDijkstras(links)
{
for (var l in links)
{
links[l].prev=undefined;
links[l].cost=Infinity;
}
}
function dijkstras(d,links)
{
var Q = JSON.parse(JSON.stringify(links))
var id = d.airportID;
//Dijkstra's Algotithm
initializeDijkstras(Q)
//Set current node to cost 0
Q[id].cost=0;
//Place all nodes in queue
//Final set of nodes
var nodes={};
console.log("working, please hold. airportid: " + id)
//While queue is not empty
while ( Q != {})
{
//u is node with smallest distance
//If the Queue was sorted, you could just dequeue it here #TODO
var u=_.min(Q,function(q){
return q.cost
})
//If minimum cost is infinity, we have reached the end
if (u==Infinity || u.cost==Infinity)
break;
//remove u from Q
delete Q[u.id];
//Push u to final graph
nodes[u.id]=u;
//For each neighbour v of u
for (var i=0; i<u.cout.length;i++)
{
//v is one neighbour of u, v=[id,cost]
var v = u.cout[i]
//is v still in Q?
var vObj=Q[v[0]]
if ( vObj != undefined )
{
//How much would it cost to go from u to v?
var alt = u.cost + v[1]
//Is this new cost lower than v's currnet cost?
if ( alt < vObj.cost )
{
//Set v's cost to the lower cost
vObj.cost = alt;
//Set the previous to u
vObj.prev = u;
//If I were going to sort the Q, I would sort it here #TODO
}
}
}
}
return nodes;
}
d3.json("data/majorAirports.json", function(allAirports) {
d3.csv("data/routes.csv", function(allRoutes) {
airportById = {};
airportLinks = {};
for (var i=0;i<allAirports.features.length;i++){
id = allAirports.features[i].properties.airportID;
if(id != "\\N"){
airportById[id] = allAirports.features[i];
airportLinks[id] = {id:id,cin:[],cout:[],prev:undefined,cost:Infinity};
}
}
for (var i=0;i<allRoutes.length;i++)
{
var src = allRoutes[i].srcAirportID;
var dest = allRoutes[i].destAirportID;
if(src != "\\N" && dest != "\\N"){//Check maybe redundant now
var time = planeTimeBetween(airportById[src],airportById[dest]);
airportLinks[src].cout.push([dest,time]);
airportLinks[dest].cin.push([src,time]);
}
}
var airports = svg.selectAll(".airport")
.data(allAirports.features)
.enter().append('path')
.attr("d", path)
.attr("class", "airport");
//On click of an airport
airports.on("click",function(d){
graph = dijkstras(d.properties,airportLinks);
//Look through all airports
airports.attr("class",function(all){
//id of airport we are looking at
var id = all.properties.airportID;
//if airport does not exist in graph, then it is not connected
if ( graph[id] == undefined )
return "unconnectedAirport"
var cost = graph[id].cost;
//If airport is non-infinity, set to quantization level
return quantize(cost);
})
d3.select(this).attr("class","startingAirport");
});
});
});
</script>
<!--
<body>
<svg width=1200 height=800 class="numer of strings">
<path d=path() class="place" /> data={}
</svg>
</body>
-->