-
Notifications
You must be signed in to change notification settings - Fork 50
/
airports.html
153 lines (126 loc) · 4 KB
/
airports.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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.csv.js"></script>
<script type="text/javascript" src="d3/d3.geo.js"></script>
<script type="text/javascript" src="d3/d3.geom.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">
#states path {
fill: #ccc;
stroke: #fff;
}
path.arc {
pointer-events: none;
fill: none;
stroke: #000;
display: none;
}
path.cell {
fill: none;
pointer-events: all;
}
circle {
fill: steelblue;
fill-opacity: .8;
stroke: #fff;
}
#cells.voronoi path.cell {
stroke: brown;
}
#cells g:hover path.arc {
display: inherit;
}
</style>
</head>
<body>
<div id="body">
<div id="footer">
<span>U.S. Commercial Flights</span>, 2008
<div class="hint">
mouseover for flights from each airport
</div>
<div style="font-size:12px;line-height:60px;"><input type="checkbox" id="voronoi"> <label for="voronoi">show Voronoi</label></div>
</div>
</div>
<script type="text/javascript">
var w = 1280,
h = 800;
var projection = d3.geo.azimuthal()
.mode("equidistant")
.origin([-98, 38])
.scale(1400)
.translate([640, 360]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var states = svg.append("svg:g")
.attr("id", "states");
var circles = svg.append("svg:g")
.attr("id", "circles");
var cells = svg.append("svg:g")
.attr("id", "cells");
d3.select("input[type=checkbox]").on("change", function() {
cells.classed("voronoi", this.checked);
});
d3.json("us-states.json", function(collection) {
states.selectAll("path")
.data(collection.features)
.enter().append("svg:path")
.attr("d", path);
});
d3.csv("flights-airport.csv", function(flights) {
var linksByOrigin = {},
countByAirport = {},
locationByAirport = {},
positions = [];
var arc = d3.geo.greatArc()
.source(function(d) { return locationByAirport[d.source]; })
.target(function(d) { return locationByAirport[d.target]; });
flights.forEach(function(flight) {
var origin = flight.origin,
destination = flight.destination,
links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
links.push({source: origin, target: destination});
countByAirport[origin] = (countByAirport[origin] || 0) + 1;
countByAirport[destination] = (countByAirport[destination] || 0) + 1;
});
d3.csv("airports.csv", function(airports) {
// Only consider airports with at least one flight.
airports = airports.filter(function(airport) {
if (countByAirport[airport.iata]) {
var location = [+airport.longitude, +airport.latitude];
locationByAirport[airport.iata] = location;
positions.push(projection(location));
return true;
}
});
// Compute the Voronoi diagram of airports' projected positions.
var polygons = d3.geom.voronoi(positions);
var g = cells.selectAll("g")
.data(airports)
.enter().append("svg:g");
g.append("svg:path")
.attr("class", "cell")
.attr("d", function(d, i) { return "M" + polygons[i].join("L") + "Z"; })
.on("mouseover", function(d, i) { d3.select("#footer span").text(d.name); });
g.selectAll("path.arc")
.data(function(d) { return linksByOrigin[d.iata] || []; })
.enter().append("svg:path")
.attr("class", "arc")
.attr("d", function(d) { return path(arc(d)); });
circles.selectAll("circle")
.data(airports)
.enter().append("svg:circle")
.attr("cx", function(d, i) { return positions[i][0]; })
.attr("cy", function(d, i) { return positions[i][1]; })
.attr("r", function(d, i) { return Math.sqrt(countByAirport[d.iata]); })
.sort(function(a, b) { return countByAirport[b.iata] - countByAirport[a.iata]; });
});
});
</script>
</body>
</html>